Files
wp-asciiplayer/tests/js/preview-lifecycle.test.js
T
LamGC 6f8182db41
Build and WordPress integration / Build and publish pristine package (push) Canceled after 0s
Build and WordPress integration / PHP 8.3 syntax and regression tests (push) Canceled after 0s
Build and WordPress integration / PHP 7.4 syntax and regression tests (push) Canceled after 0s
Build and WordPress integration / WordPress 6.3 / PHP 7.4 (push) Canceled after 0s
Build and WordPress integration / WordPress latest / PHP 8.2 (push) Canceled after 0s
Build and WordPress integration / Plugin Check (advisory) (push) Canceled after 0s
fix: bound recording resources and add security regressions (#41)
- Bound recording bytes, JSON structure, events, markers, terminal dimensions
  and timestamp arithmetic before unsafe player allocation or scheduling.
- Share validated playback data and metadata; cancel stale, failed and timed-out loads.
- Add PHP 7.4-compatible predecode budgets and preserve supported v1/v2/v3 behavior.
- Add localized errors, documented limits and 110 official/seeded recording fixtures.

Verified 325 Node tests, native PHP 7.4/8.3 regression suites and 55 Chromium tests
on each WordPress CI matrix. Preserve plugin version, vendor files and isolated
package artifacts.
2026-10-15 11:42:07 +08:00

117 lines
4.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { transformSync } from '@babel/core';
import * as options from '../../src/options.js';
import * as errors from '../../src/recording-errors.js';
import * as loader from '../../src/recording-loader.js';
const compiled = transformSync(fs.readFileSync(new URL('../../src/preview.js', import.meta.url), 'utf8'), {
configFile: false, babelrc: false,
presets: [['@babel/preset-react', { pragma: 'element' }]],
plugins: ['@babel/plugin-transform-modules-commonjs'],
}).code;
const flush = () => new Promise(resolve => setImmediate(resolve));
function harness(attributes = {}) {
let effect;
let resolveAssets;
const assets = new Promise(resolve => { resolveAssets = resolve; });
const updates = [];
const sizes = [];
const calls = [];
const timers = new Map();
let nextTimer = 0;
const window = { AbortController,
setTimeout(callback) { timers.set(++nextTimer, callback); return nextTimer; },
clearTimeout(id) { timers.delete(id); }, setInterval: () => 0, clearInterval() {},
fetch: async () => new Response('{"version":2,"width":80,"height":24}\n[0,"o","ok"]\n'),
};
const container = { ownerDocument: { defaultView: window }, replaceChildren() {} };
const api = { create(source) {
const player = { addEventListener() {}, getDuration: () => undefined,
dispose() { this.disposed = true; } };
calls.push({ source, player });
return player;
} };
const modules = {
'@wordpress/components': {}, '@wordpress/compose': { useRefEffect(callback) { effect = callback; } },
'@wordpress/element': { useRef: value => ({ current: value }), useState: value => [value, update => updates.push(update)] },
'@wordpress/i18n': { __: value => value }, './options.js': options,
'./recording-errors.js': errors, './recording-loader.js': loader,
'./player-bridge.js': { playerAssets: () => ({}), playerForDocument: () => assets },
};
const module = { exports: {} };
new Function('require', 'module', 'exports', 'element', compiled)(name => {
assert.ok(Object.hasOwn(modules, name), `Unexpected module ${name}`);
return modules[name];
}, module, module.exports, () => null);
module.exports.default({ source: '/original.cast', attributes, attempt: 0, onRecordingSize: (...args) => sizes.push(args) });
const cleanup = effect(container);
return { cleanup, updates, sizes, calls, timers, window, resolveAssets: () => resolveAssets(api) };
}
test('unmount while player assets load never creates a player or stale state', async () => {
const h = harness();
h.cleanup();
const count = h.updates.length;
h.resolveAssets();
await flush();
assert.equal(h.calls.length, 0);
assert.equal(h.updates.length, count);
assert.equal(h.timers.size, 0);
});
test('asset startup deadline cannot create a late player after showing an error', async () => {
const h = harness();
[...h.timers.values()][0]();
assert.equal(h.updates.at(-1), 'error');
h.resolveAssets();
await flush();
assert.equal(h.calls.length, 0);
h.cleanup();
});
test('unmount during a request aborts it without stale metadata or status', async () => {
const h = harness();
let signal;
h.window.fetch = (_source, init) => { signal = init.signal; return new Promise(() => {}); };
h.resolveAssets();
await flush();
const pending = h.calls[0].source.data();
await flush();
h.cleanup();
const count = h.updates.length;
await assert.rejects(pending, { code: 'aborted' });
assert.equal(signal.aborted, true);
assert.equal(h.calls[0].player.disposed, true);
assert.deepEqual(h.sizes, [['/original.cast', null]]);
assert.equal(h.updates.length, count);
});
test('preview reports original dimensions only after validated data finishes', async () => {
const h = harness();
h.resolveAssets();
await flush();
const data = await h.calls[0].source.data();
assert.equal(data[0].width, 80);
assert.deepEqual(h.sizes, [['/original.cast', null], ['/original.cast', { cols: 80, rows: 24 }]]);
h.cleanup();
});
test('preview rejects unsafe initial options before create or recording fetch', async () => {
for (const attributes of [{ cols: 1000, rows: 1000 }, { rows: 1000 }]) {
const h = harness(attributes);
let requests = 0;
h.window.fetch = () => { requests++; };
h.resolveAssets();
await flush();
assert.equal(h.calls.length, 0);
assert.equal(requests, 0);
assert.equal(h.updates.at(-1), 'error');
assert.match(h.updates.at(-2), /terminal cells/);
h.cleanup();
}
});