mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-08 10:56:15 -08:00
test(iframe): [PKG-IFRAME-02] reproduce request and native window failures
This commit is contained in:
1 parent
a52f79a0cc
commit
b3f22652c2
10 files changed
+8259
-12
No files matched your search
+3
-2
@@ -51,7 +51,7 @@
|
||||
"ci:build": "yarn build:types && yarn build all && yarn build:i18n && yarn build:ts && yarn build:docs && yarn test:imports",
|
||||
"test:imports": "node --test test/esm.test.js test/i18n.test.js test/ssr.test.js",
|
||||
"typecheck": "node scripts/typecheck.mjs",
|
||||
"test:unit": "node --test test/mediabunny.test.js test/mediabunny-shim.test.js test/mediabunny-coordination.test.js test/mediabunny-video.test.js test/mediabunny-audio.test.js test/mediabunny-hls.test.js test/mediabunny-entry.test.js test/mediabunny-capability.test.js test/mediabunny-load.test.js test/mediabunny-input.test.js test/dpip.test.js test/dpip-lifecycle.test.js test/canvas.test.js test/canvas-lifecycle.test.js test/ambilight.test.js test/ambilight-lifecycle.test.js test/ambilight-proxy.test.js test/vast.test.js test/vast-lifecycle.test.js test/ads.test.js test/ads-lifecycle.test.js test/playback.test.js test/dash-control.test.js test/dash-contract.test.js test/dash-lifecycle.test.js test/dash-events.test.js test/hls-control.test.js test/audio-track.test.js test/public-behavior.test.js test/helpers.test.js test/chapter.test.js test/utils.test.js test/resource-scope.test.js test/instance-lifecycle.test.js test/options.test.js test/media-hosts.test.js test/plugins.test.js test/source.test.js test/playback-properties.test.js test/media-events.test.js test/template-resources.test.js test/core-vendor.test.js test/component-resources.test.js test/setting-model.test.js test/setting-layout.test.js test/setting-resources.test.js test/subtitle.test.js test/display-native.test.js test/display-video-fullscreen.test.js test/display-pip.test.js test/display-mini.test.js test/display-sizing.test.js test/display-orientation.test.js test/hotkey.test.js test/listener-registry.test.js test/global-events.test.js test/pointer-events.test.js test/gesture.test.js test/event-scheduling.test.js test/notice.test.js test/fast-forward.test.js test/auto-playback.test.js test/builtin-layers.test.js test/prompt-components.test.js test/screenshot.test.js test/thumbnails.test.js test/progress.test.js test/environment.test.js test/storage.test.js test/facade-properties.test.js test/dom-boundaries.test.js test/initialization.test.js test/entry.test.js test/accessibility-button.test.js test/accessibility-focus.test.js test/accessibility-slider.test.js",
|
||||
"test:unit": "node --test test/iframe.test.js test/mediabunny.test.js test/mediabunny-shim.test.js test/mediabunny-coordination.test.js test/mediabunny-video.test.js test/mediabunny-audio.test.js test/mediabunny-hls.test.js test/mediabunny-entry.test.js test/mediabunny-capability.test.js test/mediabunny-load.test.js test/mediabunny-input.test.js test/dpip.test.js test/dpip-lifecycle.test.js test/canvas.test.js test/canvas-lifecycle.test.js test/ambilight.test.js test/ambilight-lifecycle.test.js test/ambilight-proxy.test.js test/vast.test.js test/vast-lifecycle.test.js test/ads.test.js test/ads-lifecycle.test.js test/playback.test.js test/dash-control.test.js test/dash-contract.test.js test/dash-lifecycle.test.js test/dash-events.test.js test/hls-control.test.js test/audio-track.test.js test/public-behavior.test.js test/helpers.test.js test/chapter.test.js test/utils.test.js test/resource-scope.test.js test/instance-lifecycle.test.js test/options.test.js test/media-hosts.test.js test/plugins.test.js test/source.test.js test/playback-properties.test.js test/media-events.test.js test/template-resources.test.js test/core-vendor.test.js test/component-resources.test.js test/setting-model.test.js test/setting-layout.test.js test/setting-resources.test.js test/subtitle.test.js test/display-native.test.js test/display-video-fullscreen.test.js test/display-pip.test.js test/display-mini.test.js test/display-sizing.test.js test/display-orientation.test.js test/hotkey.test.js test/listener-registry.test.js test/global-events.test.js test/pointer-events.test.js test/gesture.test.js test/event-scheduling.test.js test/notice.test.js test/fast-forward.test.js test/auto-playback.test.js test/builtin-layers.test.js test/prompt-components.test.js test/screenshot.test.js test/thumbnails.test.js test/progress.test.js test/environment.test.js test/storage.test.js test/facade-properties.test.js test/dom-boundaries.test.js test/initialization.test.js test/entry.test.js test/accessibility-button.test.js test/accessibility-focus.test.js test/accessibility-slider.test.js",
|
||||
"test:coverage": "node --test test/coverage.test.js && node scripts/coverage.mjs",
|
||||
"test": "yarn test:node && yarn test:baseline",
|
||||
"test:browser": "playwright test",
|
||||
@@ -79,7 +79,8 @@
|
||||
"test:mediabunny": "node --test test/mediabunny.test.js test/mediabunny-shim.test.js test/mediabunny-coordination.test.js test/mediabunny-video.test.js test/mediabunny-audio.test.js test/mediabunny-hls.test.js test/mediabunny-entry.test.js test/mediabunny-capability.test.js test/mediabunny-load.test.js test/mediabunny-input.test.js",
|
||||
"test:mediabunny-types-package": "node refactor/scripts/mb-package-types.mjs",
|
||||
"test:mediabunny-soak": "playwright test --config playwright.soak.config.js",
|
||||
"generate:media:hls": "node scripts/generate-hls-fixture.mjs"
|
||||
"generate:media:hls": "node scripts/generate-hls-fixture.mjs",
|
||||
"test:iframe": "node --test test/iframe.test.js refactor/scripts/iframe-contract.test.mjs"
|
||||
},
|
||||
"browserslist": "last 1 Chrome version",
|
||||
"devDependencies": {
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,59 @@
|
||||
# PKG-IFRAME-02 历史消息、请求与销毁行为
|
||||
|
||||
新增52项Node检查,使用冻结发布主main/legacy及工作区main/legacy,保留既有正常控制,
|
||||
并将生命周期风险从源码观察推进到可重跑复现。额外helper继续由IFRAME-01独立契约覆盖,
|
||||
本轮不能代表helper的完整迁移验收。
|
||||
|
||||
| 场景 | 四份主实现的实际结果 |
|
||||
| --- | --- |
|
||||
| 同毫秒两个请求 | id相同;第二回调覆盖第一,第一响应错误地结算第二请求,第一请求悬挂 |
|
||||
| 已发送后destroy | 监听被移除,但public promises记录和Promise保留 |
|
||||
| 等待inject时destroy | 200ms定时器仍保留;待旧timer执行才拒绝,不能称为立即取消 |
|
||||
| 销毁后新请求 | 正常拒绝既有错误消息,没有新timer;作为正向控制 |
|
||||
| 不同ID乱序/错误 | 正确对应resolve/reject与Error(data),删除记录 |
|
||||
| 同ID自定义非error类型 | 同样resolve;这是generic postMessage兼容协议,不能只允许response |
|
||||
| event.data=null | public receiver抛TypeError |
|
||||
| 即时发送失败 | Promise拒绝,但请求记录保留 |
|
||||
| 轮询后发送失败 | 异常逃离timer回调,外层Promise悬挂且记录保留 |
|
||||
| 销毁后的已保存receiver | 仍可结算旧请求并通知message回调 |
|
||||
| iframe导航 | injected沿用旧值,未等新inject便发送 |
|
||||
| 重复child inject | 重复握手,但浏览器式EventTarget对相同handler去重,不能声称重复注册监听 |
|
||||
| child执行错误/恢复 | 发error后异步拒绝,后续命令正常 |
|
||||
|
||||
## 真实浏览器
|
||||
|
||||
矩阵4份产物×同源/跨源×6场景×Chromium/Firefox/WebKit,共144项通过。这里的通过是
|
||||
旧行为/缺陷的复现断言通过,不表示这些缺陷已修复。所有场景都创建实际iframe,使用浏览器
|
||||
原生WindowProxy、postMessage、event.source与event.origin;没有伪造MessageEvent来源。
|
||||
跨源使用127.0.0.1与localhost两个实际不同origin,由本地route提供冻结库及可控页面。
|
||||
首轮5项和120项、最终144项均归档report.json和整个results。
|
||||
|
||||
六类场景为:同步/resolve异步/错误/恢复round-trip,同毫秒冲突,destroy后迟到回复,
|
||||
无关窗口消息,导航到未inject页面,以及原生结构化克隆失败。
|
||||
|
||||
无关窗口能让父实例误认inject、结算其请求,还能向另一个子iframe发送commit并让它执行
|
||||
本地测试的常量表达式。报告保存真实origin/source关系;测试只操作自己创建的本地页面,
|
||||
不能从这组证据推导其他站点的数据可访问性。
|
||||
|
||||
导航后,旧实例提前发出的commit被新页面真实接收,但因尚未inject而丢失;之后inject并
|
||||
发出新请求可恢复,旧请求仍悬挂。clone-failure使用原生不可克隆函数触发DataCloneError,
|
||||
分别验证timer中的未处理异常/悬挂与即时拒绝/留记录。只拦截测试刻意触发的DataCloneError
|
||||
以及固定child错误,其余未处理页面错误仍由通用fixture判失败。Date.now仅为同tick冲突和
|
||||
两个clone失败的不同ID做受控设置,不能将其报告为自然网络计时。
|
||||
|
||||
工具本身不依赖ArtPlayer实例;本轮未创建播放器,不计作旧/新核心媒体集成,也未访问线上demo。
|
||||
正常消息控制和故障断言保持分离,后续03/04须新增候选修复断言,同时保留历史负例。
|
||||
|
||||
## 后续实施约束
|
||||
|
||||
保留resove公开拼写、promises公开视图、绑定onMessage、message回调receiver/返回形状、
|
||||
任意非error的同ID响应、commit函数体/resolve约定及既有错误消息。请求身份、等待timer、
|
||||
发送异常、销毁和导航的资源归属可在内部修复,必须用相同场景验证旧失败/候选成功。
|
||||
source/origin与可执行commit的信任模型在IFRAME-03/05单独说明,保留同源/跨源正常使用,
|
||||
不能将所有历史消息静默改成另一套RPC。
|
||||
|
||||
本任务没有修改生产源码、公开类型、协议或版本。完整CI和最终证据核验完成后专用提交;
|
||||
IFRAME-LIFE-01、IFRAME-TRUST-01仍open,下一项IFRAME-03实施资源与消息边界。
|
||||
|
||||
完整CI退出0:1489项(1315单元、14工程、160基线)与44重复契约通过,324生产TS。
|
||||
最终源码和浏览器报告哈希已核验;本任务完成后立即单独本地提交,不推送或发布。
|
||||
+3
-2
@@ -4,7 +4,7 @@
|
||||
|
||||
基线:`40fcda6a37d0049d42e49c1e64e70d4fd9ba5f7f`。总任务 222 项,范围 22 个包及工作区/示例。
|
||||
|
||||
状态:todo 107 / doing 9 / blocked 0 / done 106 / deferred 0。风险 L/M/H 表示兼容风险,不表示工期。
|
||||
状态:todo 106 / doing 9 / blocked 0 / done 107 / deferred 0。风险 L/M/H 表示兼容风险,不表示工期。
|
||||
|
||||
前置依赖是启动条件;验收是完成条件。任务可以继续拆分,但不能复用或悄悄删除旧 ID。
|
||||
|
||||
@@ -345,7 +345,7 @@
|
||||
| ID | 范围 / 步骤 | 前置依赖 | 交付物 | 验收条件 | 风险 | 状态 |
|
||||
| --- | --- | --- | --- | --- | --- | --- |
|
||||
| PKG-IFRAME-01 | artplayer-tool-iframe<br>核对包契约与历史用法 | BASE-05 | constructor/commit/message/inject、postMessage 协议与历史公开拼写 | 源码/声明/README/demo/发布包差异已登记;公开形状和版本范围冻结 | H | done |
|
||||
| PKG-IFRAME-02 | artplayer-tool-iframe<br>建立特有行为与错误测试 | PKG-IFRAME-01, ENG-03, ENG-05 | 跨窗口消息、ID 匹配、请求失败、重复 inject、销毁中请求 | 旧版本行为可重跑,成功/失败/切源/销毁有必要断言 | H | todo |
|
||||
| PKG-IFRAME-02 | artplayer-tool-iframe<br>建立特有行为与错误测试 | PKG-IFRAME-01, ENG-03, ENG-05 | 跨窗口消息、ID 匹配、请求失败、重复 inject、销毁中请求 | 旧版本行为可重跑,成功/失败/切源/销毁有必要断言 | H | done |
|
||||
| PKG-IFRAME-03 | artplayer-tool-iframe<br>整理内部职责与资源 | PKG-IFRAME-02, CORE-02, BASE-07 | 请求注册/响应匹配/监听清理分离;origin/source 安全边界独立决策 | 结构变化和缺陷修复分开记录;原 API/事件/资源生命周期通过 | H | todo |
|
||||
| PKG-IFRAME-04 | artplayer-tool-iframe<br>迁移自有源码和公开类型 | PKG-IFRAME-03, ENG-04, ENG-06, CORE-07 | 消息联合类型、回调/Promise 推导、旧公开字段兼容;旧npm export=与实际namespace、额外helper协议分别核验 | 严格类型检查、旧消费样例通过;声明路径/导出和同步异步兼容 | H | todo |
|
||||
| PKG-IFRAME-05 | artplayer-tool-iframe<br>验证新旧核心和组合 | PKG-IFRAME-04, CORE-22 | 真实同源/跨源 iframe、既有 commit 协议;安全变化有独立结论 | 最终核心与原支持范围核心分别通过;设备/SDK 缺证据不能标完成 | H | todo |
|
||||
@@ -531,4 +531,5 @@
|
||||
- PKG-MB-08: [记录](baselines/mb-entry-source.json) [记录](changes/2026-09-12-PKG-MB-08-entry-checkpoint.md) [记录](baselines/mb-entry-checkpoint.json) [记录](baselines/mb-capability-source.json) [记录](changes/2026-09-12-PKG-MB-08-capability.md) [记录](baselines/mb-capability-validation.json)
|
||||
- PKG-MB-09: [记录](changes/2026-09-12-PKG-MB-09-native-pip-checkpoint.md) [记录](baselines/mb-native-pip-checkpoint.json) [记录](changes/2026-09-12-PKG-MB-09-sustained-playback-checkpoint.md) [记录](baselines/mb-sustained-validation.json) [记录](baselines/mb-sustained-media.json)
|
||||
- PKG-IFRAME-01: [记录](changes/2026-09-12-PKG-IFRAME-01-contract.md) [记录](baselines/iframe-release.json) [记录](baselines/iframe-contract.md) [记录](baselines/iframe-contract-validation.json)
|
||||
- PKG-IFRAME-02: [记录](changes/2026-09-12-PKG-IFRAME-02-behavior.md) [记录](baselines/iframe-behavior-validation.json)
|
||||
- PKG-FACTORY-01: [记录](baselines/factory-assignment-gaps.json) [记录](baselines/factory-compatibility-proposals.json) [记录](factory-compatibility-decision.md) [记录](changes/2026-09-12-PKG-FACTORY-01-decision.md)
|
||||
@@ -1,5 +1,14 @@
|
||||
# 进度与证据
|
||||
|
||||
## PKG-IFRAME-02 完成:历史生命周期与真实窗口缺陷复现
|
||||
|
||||
52项Node和144项Chromium/Firefox/WebKit真实窗口检查通过,覆盖4份历史产物、同源/跨源。
|
||||
确认同毫秒请求串线、销毁/导航悬挂、克隆失败泄漏,以及无关窗口冒充握手/响应/commit。
|
||||
这是历史缺陷断言通过,尚未代表修复;工具测试未创建播放器,不计作核心媒体集成。
|
||||
完整CI1489与44重复契约通过,324生产TS;源码和归档报告哈希一致。
|
||||
见[变更](changes/2026-09-12-PKG-IFRAME-02-behavior.md)与[验证](baselines/iframe-behavior-validation.json)。
|
||||
222项:107 done、9 doing、106 todo;立即专用本地提交。三项Iframe风险仍open,下一项03实施修复。
|
||||
|
||||
## PKG-IFRAME-01 完成:发布名、辅助类与消息契约
|
||||
|
||||
实际旧包plugin-iframe@1.0.0的8个成员和工作区11个输入已冻结;新tool名本次npm查询404,
|
||||
|
||||
@@ -174,6 +174,6 @@
|
||||
| MB-CAP-01 | open / 已复现 | Current Windows WebKit lacks WebCodecs and Web Audio constructors needed by historical MediaBunny proxies | PKG-MB-02, PKG-MB-09 |
|
||||
| MB-READY-01 | resolved / 已复现 | MediaBunny historical readiness can repeat for trackless input and occur after capability errors | PKG-MB-04, PKG-MB-09 |
|
||||
| DPIP-DISPLAY-01 | resolved / 已复现 | Web fullscreen body placement reclaims a player adopted into another document | CORE-22, PKG-DPIP-05 |
|
||||
| IFRAME-LIFE-01 | open / 源码/产物事实 | Iframe timestamp IDs and unowned pending requests/timers lack terminal cleanup | PKG-IFRAME-02, PKG-IFRAME-03 |
|
||||
| IFRAME-TRUST-01 | open / 源码/产物事实 | Iframe executable commit protocol accepts messages without source/origin validation | PKG-IFRAME-03, PKG-IFRAME-05 |
|
||||
| IFRAME-LIFE-01 | open / 已复现 | Iframe timestamp IDs and unowned pending requests/timers lack terminal cleanup | PKG-IFRAME-02, PKG-IFRAME-03 |
|
||||
| IFRAME-TRUST-01 | open / 已复现 | Iframe executable commit protocol accepts messages without source/origin validation | PKG-IFRAME-03, PKG-IFRAME-05 |
|
||||
| IFRAME-DIST-01 | open / 已复现 | Iframe tool name is unavailable at registry observation and old archive includes distinct helper exports | PKG-IFRAME-04, PKG-IFRAME-06 |
|
||||
+8
-4
@@ -3774,13 +3774,15 @@
|
||||
"PKG-IFRAME-03"
|
||||
],
|
||||
"status": "open",
|
||||
"confirmation": "source-observed",
|
||||
"confirmation": "reproduced",
|
||||
"compatibleResolution": "Reproduce same-tick request collision, pending injection, response errors and destroy behavior before changing internal ownership; preserve public promises[id].resove.",
|
||||
"closureCriteria": "Old behavior and candidate fixes have deterministic race/resource tests plus actual iframe integration.",
|
||||
"evidence": [
|
||||
"refactor/baselines/iframe-contract.md",
|
||||
"refactor/baselines/iframe-release.json",
|
||||
"refactor/changes/2026-09-12-PKG-IFRAME-01-contract.md"
|
||||
"refactor/changes/2026-09-12-PKG-IFRAME-01-contract.md",
|
||||
"refactor/baselines/iframe-behavior-validation.json",
|
||||
"refactor/changes/2026-09-12-PKG-IFRAME-02-behavior.md"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -3791,13 +3793,15 @@
|
||||
"PKG-IFRAME-05"
|
||||
],
|
||||
"status": "open",
|
||||
"confirmation": "source-observed",
|
||||
"confirmation": "reproduced",
|
||||
"compatibleResolution": "Treat message origin/source and executable commit as an explicit compatibility and trust-boundary decision; do not silently replace the protocol during TS conversion.",
|
||||
"closureCriteria": "Controlled and real same/cross-origin evidence plus an explicit documented compatibility decision and supported trust model.",
|
||||
"evidence": [
|
||||
"refactor/baselines/iframe-contract.md",
|
||||
"refactor/baselines/iframe-release.json",
|
||||
"refactor/changes/2026-09-12-PKG-IFRAME-01-contract.md"
|
||||
"refactor/changes/2026-09-12-PKG-IFRAME-01-contract.md",
|
||||
"refactor/baselines/iframe-behavior-validation.json",
|
||||
"refactor/changes/2026-09-12-PKG-IFRAME-02-behavior.md"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
+5
-2
@@ -3635,11 +3635,14 @@
|
||||
"ENG-03",
|
||||
"ENG-05"
|
||||
],
|
||||
"status": "todo",
|
||||
"status": "done",
|
||||
"risk": "H",
|
||||
"deliverable": "跨窗口消息、ID 匹配、请求失败、重复 inject、销毁中请求",
|
||||
"acceptance": "旧版本行为可重跑,成功/失败/切源/销毁有必要断言",
|
||||
"evidence": []
|
||||
"evidence": [
|
||||
"changes/2026-09-12-PKG-IFRAME-02-behavior.md",
|
||||
"baselines/iframe-behavior-validation.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "PKG-IFRAME-03",
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
import { hash } from '../../refactor/scripts/releases.mjs'
|
||||
import { iframeHistorical } from '../helpers/iframe.js'
|
||||
import { expect, test } from './fixtures.js'
|
||||
|
||||
const implementations = (await iframeHistorical()).filter(item => item.global !== 'ArtplayerHelperIframe')
|
||||
|
||||
for (const implementation of implementations) {
|
||||
for (const relation of ['same-origin', 'cross-origin']) {
|
||||
for (const scenario of ['round-trip', 'same-tick', 'destroy-pending', 'foreign-message', 'navigation', 'clone-failure']) {
|
||||
test(`Iframe ${implementation.name}/${relation}: ${scenario} historical behavior`, async ({ page }, testInfo) => {
|
||||
await page.goto('/test/player.html?core=published')
|
||||
const parentOrigin = new URL(page.url()).origin
|
||||
const alternate = parentOrigin.replace('127.0.0.1', 'localhost')
|
||||
const childOrigin = relation === 'same-origin' ? parentOrigin : alternate
|
||||
const foreignOrigin = relation === 'same-origin' ? alternate : parentOrigin
|
||||
await page.route('**/iframe-baseline-lib.js', route => route.fulfill({ contentType: 'text/javascript', body: implementation.code }))
|
||||
await page.route('**/iframe-baseline-child?*', route => route.fulfill({ contentType: 'text/html', body: `<!doctype html><script src="/iframe-baseline-lib.js"></script><script>
|
||||
window.Tool = window.${implementation.global}; window.received = []; window.expectedErrors = [];
|
||||
addEventListener('unhandledrejection', event => {
|
||||
if (event.reason?.message === 'iframe baseline expected error') {
|
||||
expectedErrors.push(event.reason.message); event.preventDefault();
|
||||
}
|
||||
});
|
||||
addEventListener('message', event => {
|
||||
received.push({ type: event.data?.type, id: event.data?.id, fromParent: event.source === parent, origin: event.origin });
|
||||
if (event.data?.type === 'hold') {
|
||||
window.held = event.data;
|
||||
parent.postMessage({ type: 'fixture-held', data: held.id }, '*');
|
||||
}
|
||||
});
|
||||
if (new URL(location.href).searchParams.get('auto') !== '0') Tool.inject();
|
||||
parent.postMessage({ type: 'fixture-ready', data: location.search }, '*');
|
||||
</script>` }))
|
||||
await page.route('**/iframe-baseline-foreign', route => route.fulfill({ contentType: 'text/html', body: '<!doctype html><script>parent.postMessage({type:"inject",data:"foreign"},"*")</script>' }))
|
||||
await page.addScriptTag({ content: `(() => { const module = { exports: {} }; const exports = module.exports; ${implementation.code}; window.IframeFactory = module.exports.default || module.exports; })();` })
|
||||
const manualInjection = scenario === 'foreign-message' || scenario === 'clone-failure'
|
||||
const url = `${childOrigin}/iframe-baseline-child?auto=${manualInjection ? 0 : 1}&epoch=1`
|
||||
await page.evaluate((url) => {
|
||||
window.packets = []
|
||||
window.states = {}
|
||||
const frame = document.createElement('iframe')
|
||||
frame.id = 'target-frame'
|
||||
document.body.append(frame)
|
||||
window.targetFrame = frame
|
||||
window.addEventListener('message', event => window.packets.push({ type: event.data?.type, data: event.data?.data, id: event.data?.id, origin: event.origin, fromChild: event.source === frame.contentWindow }))
|
||||
window.tool = new window.IframeFactory({ iframe: frame, url })
|
||||
window.track = (key, promise) => {
|
||||
window.states[key] = { status: 'pending' }
|
||||
promise.then(value => window.states[key] = { status: 'resolved', value }, error => window.states[key] = { status: 'rejected', error: error.message })
|
||||
}
|
||||
}, url)
|
||||
let result
|
||||
try {
|
||||
await expect.poll(() => page.evaluate(() => window.packets.some(packet => packet.type === 'fixture-ready'))).toBe(true)
|
||||
if (!manualInjection)
|
||||
await expect.poll(() => page.evaluate(() => window.tool.injected)).toBe(true)
|
||||
if (scenario === 'round-trip') {
|
||||
result = await page.evaluate(async () => {
|
||||
const sync = await window.tool.commit(() => {
|
||||
return 7
|
||||
})
|
||||
const async = await window.tool.commit((resolve) => {
|
||||
setTimeout(() => resolve(8), 10)
|
||||
})
|
||||
let error
|
||||
try {
|
||||
await window.tool.commit(() => {
|
||||
throw new Error('iframe baseline expected error')
|
||||
})
|
||||
}
|
||||
catch (failure) { error = failure.message }
|
||||
const recovery = await window.tool.commit(() => {
|
||||
return 9
|
||||
})
|
||||
return { sync, async, error, recovery, pending: Object.keys(window.tool.promises).length }
|
||||
})
|
||||
expect(result).toEqual({ sync: 7, async: 8, error: 'iframe baseline expected error', recovery: 9, pending: 0 })
|
||||
}
|
||||
else if (scenario === 'same-tick') {
|
||||
await page.evaluate(() => {
|
||||
const now = Date.now
|
||||
Date.now = () => 1234
|
||||
try {
|
||||
window.track('first', window.tool.commit(() => {
|
||||
return 1
|
||||
}))
|
||||
window.track('second', window.tool.commit(() => {
|
||||
return 2
|
||||
}))
|
||||
}
|
||||
finally { Date.now = now }
|
||||
})
|
||||
await expect.poll(() => page.evaluate(() => window.packets.filter(packet => packet.type === 'response' && packet.id === 1234).length)).toBe(2)
|
||||
result = await page.evaluate(() => ({ states: window.states, pending: Object.keys(window.tool.promises).length }))
|
||||
expect(result).toEqual({ states: { first: { status: 'pending' }, second: { status: 'resolved', value: 1 } }, pending: 0 })
|
||||
}
|
||||
else if (scenario === 'destroy-pending') {
|
||||
await page.evaluate(() => window.track('held', window.tool.postMessage({ type: 'hold' })))
|
||||
await expect.poll(() => page.evaluate(() => window.packets.some(packet => packet.type === 'fixture-held'))).toBe(true)
|
||||
await page.evaluate(() => window.tool.destroy())
|
||||
const child = page.frames().find(frame => frame.url() === url)
|
||||
await child.evaluate(() => window.Tool.postMessage({ type: 'response', data: 'late', id: window.held.id }))
|
||||
await expect.poll(() => page.evaluate(() => window.packets.some(packet => packet.type === 'response' && packet.data === 'late'))).toBe(true)
|
||||
result = await page.evaluate(() => ({ destroyed: window.tool.destroyed, state: window.states.held, pending: Object.keys(window.tool.promises).length }))
|
||||
expect(result).toEqual({ destroyed: true, state: { status: 'pending' }, pending: 1 })
|
||||
}
|
||||
else if (scenario === 'foreign-message') {
|
||||
expect(await page.evaluate(() => window.tool.injected)).toBe(false)
|
||||
await page.evaluate((url) => {
|
||||
const frame = document.createElement('iframe')
|
||||
frame.id = 'foreign-frame'
|
||||
frame.src = url
|
||||
document.body.append(frame)
|
||||
}, `${foreignOrigin}/iframe-baseline-foreign`)
|
||||
await expect.poll(() => page.evaluate(() => window.tool.injected)).toBe(true)
|
||||
const id = await page.evaluate(() => {
|
||||
window.track('foreign', window.tool.postMessage({ type: 'hold' }))
|
||||
return Number(Object.keys(window.tool.promises)[0])
|
||||
})
|
||||
const foreign = page.frames().find(frame => frame.url().endsWith('/iframe-baseline-foreign'))
|
||||
await foreign.evaluate(id => parent.postMessage({ type: 'response', data: 'foreign response', id }, '*'), id)
|
||||
await expect.poll(() => page.evaluate(() => window.states.foreign.status)).toBe('resolved')
|
||||
const child = page.frames().find(frame => frame.url() === url)
|
||||
await child.evaluate(() => window.Tool.inject())
|
||||
await foreign.evaluate(() => parent.frames[0].postMessage({ type: 'commit', data: 'return "foreign command"', id: 17 }, '*'))
|
||||
await expect.poll(() => page.evaluate(() => window.packets.some(packet => packet.type === 'response' && packet.data === 'foreign command'))).toBe(true)
|
||||
result = await page.evaluate(() => ({ state: window.states.foreign, foreignResponse: window.packets.find(packet => packet.data === 'foreign response') }))
|
||||
expect(result.state).toEqual({ status: 'resolved', value: 'foreign response' })
|
||||
expect(result.foreignResponse.fromChild).toBe(false)
|
||||
expect(result.foreignResponse.origin).toBe(foreignOrigin)
|
||||
expect(await child.evaluate(() => window.received.some(packet => packet.type === 'commit' && !packet.fromParent))).toBe(true)
|
||||
}
|
||||
else if (scenario === 'clone-failure') {
|
||||
await page.evaluate(() => {
|
||||
window.cloneErrors = []
|
||||
window.addEventListener('error', (event) => {
|
||||
if (event.error?.name === 'DataCloneError') {
|
||||
window.cloneErrors.push(event.error.name)
|
||||
event.preventDefault()
|
||||
}
|
||||
})
|
||||
window.track('deferredClone', window.tool.postMessage({ type: 'query', data: () => 'not cloneable' }))
|
||||
})
|
||||
const child = page.frames().find(frame => frame.url() === url)
|
||||
await child.evaluate(() => window.Tool.inject())
|
||||
await expect.poll(() => page.evaluate(() => window.cloneErrors.length)).toBe(1)
|
||||
result = await page.evaluate(async () => {
|
||||
const deferredId = Object.keys(window.tool.promises)[0]
|
||||
const now = Date.now
|
||||
Date.now = () => Number(deferredId) + 1
|
||||
let failure
|
||||
try {
|
||||
await window.tool.postMessage({ type: 'query', data: () => 'also not cloneable' })
|
||||
}
|
||||
catch (error) { failure = error.name }
|
||||
finally { Date.now = now }
|
||||
return { failure, deferred: window.states.deferredClone, pending: Object.keys(window.tool.promises).length, uncaught: window.cloneErrors }
|
||||
})
|
||||
expect(result).toEqual({ failure: 'DataCloneError', deferred: { status: 'pending' }, pending: 2, uncaught: ['DataCloneError'] })
|
||||
}
|
||||
else {
|
||||
const nextUrl = `${childOrigin}/iframe-baseline-child?auto=0&epoch=2`
|
||||
await page.evaluate(url => window.targetFrame.src = url, nextUrl)
|
||||
await expect.poll(() => page.evaluate(() => window.packets.some(packet => packet.type === 'fixture-ready' && packet.data.includes('epoch=2')))).toBe(true)
|
||||
await page.evaluate(() => window.track('navigation', window.tool.commit(() => {
|
||||
return 9
|
||||
})))
|
||||
const child = page.frames().find(frame => frame.url() === nextUrl)
|
||||
await expect.poll(() => child.evaluate(() => window.received.some(packet => packet.type === 'commit'))).toBe(true)
|
||||
await child.evaluate(() => window.Tool.inject())
|
||||
const recovery = await page.evaluate(() => window.tool.commit(() => {
|
||||
return 10
|
||||
}))
|
||||
result = await page.evaluate(() => ({ injected: window.tool.injected, state: window.states.navigation, pending: Object.keys(window.tool.promises).length }))
|
||||
expect(result).toEqual({ injected: true, state: { status: 'pending' }, pending: 1 })
|
||||
expect(recovery).toBe(10)
|
||||
}
|
||||
const packets = await page.evaluate(() => window.packets)
|
||||
expect(packets.filter(packet => packet.fromChild).every(packet => packet.origin === childOrigin)).toBe(true)
|
||||
await testInfo.attach('iframe-behavior', { contentType: 'application/json', body: JSON.stringify({ implementation: implementation.name, sha256: hash(implementation.code), relation, scenario, parentOrigin, childOrigin, foreignOrigin, result, packets, scope: 'Actual browser iframe WindowProxy/source/origin and postMessage. Local route fulfills immutable library bytes. Date.now is controlled for collision and distinct clone-failure IDs. Only the deliberate child error/DataCloneError is intercepted; known historical defects are asserted, not fixed or waived.' }) })
|
||||
}
|
||||
finally {
|
||||
await page.evaluate(() => {
|
||||
window.tool.destroy()
|
||||
document.querySelectorAll('iframe').forEach(frame => frame.remove())
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
import assert from 'node:assert/strict'
|
||||
// eslint-disable-next-line test/no-import-node-test -- Repository Node runner.
|
||||
import test from 'node:test'
|
||||
import { iframeEnvironment, iframeHistorical } from './helpers/iframe.js'
|
||||
|
||||
const implementations = (await iframeHistorical()).filter(item => item.global !== 'ArtplayerHelperIframe')
|
||||
const flush = () => new Promise(resolve => setImmediate(resolve))
|
||||
function observe(promise) {
|
||||
const state = { status: 'pending' }
|
||||
promise.then(value => Object.assign(state, { status: 'resolved', value }), error => Object.assign(state, { status: 'rejected', error: error.message }))
|
||||
return state
|
||||
}
|
||||
function setup(implementation, injected = true) {
|
||||
const env = iframeEnvironment(implementation)
|
||||
const instance = new env.Factory({ iframe: new env.Frame(), url: '/first' })
|
||||
if (injected)
|
||||
env.dispatch({ type: 'inject' })
|
||||
return { ...env, instance }
|
||||
}
|
||||
|
||||
for (const implementation of implementations) {
|
||||
test(`Iframe ${implementation.name}: same-millisecond requests overwrite a pending callback`, async () => {
|
||||
const env = setup(implementation)
|
||||
const first = observe(env.instance.postMessage({ type: 'one', data: 1 }))
|
||||
const second = observe(env.instance.postMessage({ type: 'two', data: 2 }))
|
||||
assert.deepEqual(env.sent.map(item => item.packet.id), [1234, 1234])
|
||||
assert.equal(Object.keys(env.instance.promises).length, 1)
|
||||
env.dispatch({ type: 'response', data: 'first response', id: 1234 })
|
||||
await flush()
|
||||
assert.deepEqual(first, { status: 'pending' })
|
||||
assert.deepEqual(second, { status: 'resolved', value: 'first response' })
|
||||
env.instance.destroy()
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: destroy retains already-sent pending requests`, async () => {
|
||||
const env = setup(implementation)
|
||||
const state = observe(env.instance.postMessage({ type: 'one', data: 1 }))
|
||||
env.instance.destroy()
|
||||
await flush()
|
||||
assert.equal(state.status, 'pending')
|
||||
assert.deepEqual(Object.keys(env.instance.promises), ['1234'])
|
||||
assert.equal(env.handlers.get('message').size, 0)
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: destroy leaves injection polling until the old timer runs`, async () => {
|
||||
const env = setup(implementation, false)
|
||||
const state = observe(env.instance.postMessage({ type: 'one', data: 1 }))
|
||||
assert.equal(env.timers.size, 1)
|
||||
env.instance.destroy()
|
||||
await flush()
|
||||
assert.equal(state.status, 'pending')
|
||||
const [id, timer] = [...env.timers][0]
|
||||
assert.equal(timer.delay, 200)
|
||||
env.timers.delete(id)
|
||||
timer.callback()
|
||||
await flush()
|
||||
assert.deepEqual(state, { status: 'rejected', error: 'The instance has been destroyed' })
|
||||
assert.equal(env.timers.size, 0)
|
||||
assert.equal(env.sent.length, 0)
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: requests started after destroy reject without a timer`, async () => {
|
||||
const env = setup(implementation)
|
||||
env.instance.destroy()
|
||||
await assert.rejects(env.instance.postMessage({ type: 'late' }), /The instance has been destroyed/)
|
||||
assert.equal(env.timers.size, 0)
|
||||
assert.equal(env.sent.length, 0)
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: distinct IDs settle out of order and errors retain their message`, async () => {
|
||||
const env = setup(implementation)
|
||||
const first = observe(env.instance.postMessage({ type: 'one' }))
|
||||
env.now(1235)
|
||||
const second = observe(env.instance.postMessage({ type: 'two' }))
|
||||
env.dispatch({ type: 'error', data: 'remote failure', id: 1235 })
|
||||
env.dispatch({ type: 'response', data: 7, id: 1234 })
|
||||
await flush()
|
||||
assert.deepEqual(first, { status: 'resolved', value: 7 })
|
||||
assert.deepEqual(second, { status: 'rejected', error: 'remote failure' })
|
||||
assert.equal(Object.keys(env.instance.promises).length, 0)
|
||||
env.instance.destroy()
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: custom non-error response types remain part of the generic protocol`, async () => {
|
||||
const env = setup(implementation)
|
||||
const result = env.instance.postMessage({ type: 'custom-query' })
|
||||
env.dispatch({ type: 'custom-result', data: { ok: true }, id: 1234 })
|
||||
assert.deepEqual(await result, { ok: true })
|
||||
env.instance.destroy()
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: malformed message data throws from the public receiver`, () => {
|
||||
const env = setup(implementation)
|
||||
assert.throws(() => env.dispatch(null), error => error.name === 'TypeError')
|
||||
env.instance.destroy()
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: immediate send failure rejects but leaves its public record`, async () => {
|
||||
const env = setup(implementation)
|
||||
env.instance.$iframe.contentWindow.postMessage = () => {
|
||||
throw new Error('clone failure')
|
||||
}
|
||||
await assert.rejects(env.instance.postMessage({ type: 'one' }), /clone failure/)
|
||||
assert.deepEqual(Object.keys(env.instance.promises), ['1234'])
|
||||
env.instance.destroy()
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: send failure after polling escapes while its promise stays pending`, async () => {
|
||||
const env = setup(implementation, false)
|
||||
const state = observe(env.instance.postMessage({ type: 'one' }))
|
||||
env.dispatch({ type: 'inject' })
|
||||
env.instance.$iframe.contentWindow.postMessage = () => {
|
||||
throw new Error('late clone failure')
|
||||
}
|
||||
const [id, timer] = [...env.timers][0]
|
||||
env.timers.delete(id)
|
||||
assert.throws(() => timer.callback(), /late clone failure/)
|
||||
await flush()
|
||||
assert.equal(state.status, 'pending')
|
||||
assert.deepEqual(Object.keys(env.instance.promises), ['1234'])
|
||||
env.instance.destroy()
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: saved message receiver can still settle and notify after destroy`, async () => {
|
||||
const env = setup(implementation)
|
||||
const state = observe(env.instance.postMessage({ type: 'one' }))
|
||||
const callback = env.instance.onMessage
|
||||
let calls = 0
|
||||
env.instance.message(() => calls++)
|
||||
env.instance.destroy()
|
||||
callback({ data: { type: 'response', data: 'late', id: 1234 } })
|
||||
await flush()
|
||||
assert.deepEqual(state, { status: 'resolved', value: 'late' })
|
||||
assert.equal(calls, 1)
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: navigation retains the previous injected state`, () => {
|
||||
const env = setup(implementation)
|
||||
env.instance.$iframe.src = '/second'
|
||||
observe(env.instance.postMessage({ type: 'before-second-inject' }))
|
||||
assert.equal(env.instance.injected, true)
|
||||
assert.equal(env.sent.length, 1)
|
||||
assert.equal(env.timers.size, 0)
|
||||
assert.equal(env.instance.url, '/first')
|
||||
env.instance.destroy()
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: repeated child inject repeats handshake but deduplicates its DOM listener`, () => {
|
||||
const env = iframeEnvironment(implementation, true)
|
||||
env.Factory.inject()
|
||||
env.Factory.inject()
|
||||
assert.deepEqual(env.sent.map(item => item.packet.type), ['inject', 'inject'])
|
||||
assert.equal(env.handlers.get('message').size, 1)
|
||||
})
|
||||
|
||||
test(`Iframe ${implementation.name}: child errors send a packet then reject and later commands still run`, async () => {
|
||||
const env = iframeEnvironment(implementation, true)
|
||||
await assert.rejects(env.Factory.onMessage({ data: { type: 'commit', data: 'throw new Error("child failure")', id: 1 } }), /child failure/)
|
||||
await env.Factory.onMessage({ data: { type: 'commit', data: 'return 9', id: 2 } })
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(env.sent)), [{ packet: { type: 'error', data: 'child failure', id: 1 }, origin: '*' }, { packet: { type: 'response', data: 9, id: 2 }, origin: '*' }])
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user