68 lines
2.7 KiB
JavaScript
68 lines
2.7 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import test from 'node:test';
|
|
import { parse } from '@babel/parser';
|
|
|
|
const index = readFileSync(new URL('../../index.html', import.meta.url), 'utf8');
|
|
const main = readFileSync(new URL('../../src/web/main.tsx', import.meta.url), 'utf8');
|
|
const app = readFileSync(new URL('../../src/web/App.tsx', import.meta.url), 'utf8');
|
|
|
|
test('typed main is the sole browser bootstrap owner', () => {
|
|
assert.match(index, /src="\/src\/web\/main\.tsx"/);
|
|
assert.doesNotMatch(index, /src="\/src\/web\/App\.tsx"/);
|
|
assert.match(main, /import \{ App \} from '\.\/App\.js'/);
|
|
assert.match(main, /import '\.\/styles\/index\.css'/);
|
|
assert.match(main, /document\.getElementById\('root'\)/);
|
|
assert.match(main, /throw new Error\('Harbor root element not found'\)/);
|
|
assert.equal((main.match(/createRoot\(/g) || []).length, 1);
|
|
assert.match(main, /createRoot\(root\)\.render\(<App \/>\)/);
|
|
});
|
|
|
|
test('App remains the exported composition component without bootstrap side effects', () => {
|
|
assert.match(app, /export function App\(\)/);
|
|
assert.doesNotMatch(app, /createRoot|react-dom\/client|styles(?:\/index)?\.css|getElementById\('root'\)/);
|
|
assert.match(app, /<ClientOverviewPage/);
|
|
assert.match(app, /<StaleBanner/);
|
|
});
|
|
|
|
test('state polling waits for completion and stops after cleanup', async (t) => {
|
|
const compiled = readFileSync(new URL('../../.test-dist/src/web/App.js', import.meta.url), 'utf8');
|
|
const component = parse(compiled, { sourceType: 'module' }).program.body
|
|
.find((node) => node.declaration?.id?.name === 'App').declaration;
|
|
const effect = component.body.body.map((node) => node.expression)
|
|
.filter((node) => node?.callee?.name === 'useEffect')
|
|
.map((node) => node.arguments[0])
|
|
.find((node) => compiled.slice(node.start, node.end).includes('loadState('));
|
|
// Execute the shipped effect with a deferred request; no DOM or copied polling loop.
|
|
const setup = new Function('loadState', `return (${compiled.slice(effect.start, effect.end)})`);
|
|
t.mock.timers.enable({ apis: ['setTimeout', 'setInterval'] });
|
|
let calls = 0;
|
|
let finish;
|
|
const start = setup(() => {
|
|
calls++;
|
|
return new Promise((resolve) => { finish = resolve; });
|
|
});
|
|
const cleanup = start();
|
|
assert.equal(calls, 1);
|
|
t.mock.timers.tick(40_000);
|
|
assert.equal(calls, 1);
|
|
finish();
|
|
await Promise.resolve();
|
|
t.mock.timers.tick(4_999);
|
|
assert.equal(calls, 1);
|
|
t.mock.timers.tick(1);
|
|
assert.equal(calls, 2);
|
|
cleanup();
|
|
finish();
|
|
await Promise.resolve();
|
|
t.mock.timers.tick(40_000);
|
|
assert.equal(calls, 2);
|
|
|
|
const cleanupScheduled = start();
|
|
finish();
|
|
await Promise.resolve();
|
|
cleanupScheduled();
|
|
t.mock.timers.tick(40_000);
|
|
assert.equal(calls, 3);
|
|
});
|