mirror of
https://github.com/Matysh/houseplan-card
synced 2026-10-01 20:29:00 +00:00
The lazy-runtime contract (#353) says a non-terminal failure waits for the next explicit intent and that there are no background retries. But _renderBody calls ensure() on every repaint while a surface waits for the editor or onboarding runtime, and to the loader that call was indistinguishable from an intent. Surfaces the core opens without the runtime - the kiosk size dialog after a 3 s hold, the floor import wizard on an empty plan, a dialog a warm remount revives - therefore turned one failure into a loop: the loader's own state change, the toast and its expiry, every hass tick repainted, started a new cycle and showed a new toast every ~3.5 s. A wall tablet whose old hashed chunks answer 404 after an integration update sat in that loop forever. EditorRuntimeLoader.ensure takes an intent: the render calls it as 'reconcile'. A reconcile starts the first cycle a surface needs, but after a non-terminal failure it returns false without loading until an explicit ensure() - a tab, an opener, _requestMode, "Add space" - has started a new cycle. Explicit calls, the terminal fingerprint failure, ready and an in-flight cycle behave as before, for every loader instance. The card's render lines stay line-neutral. smoke_lazy_editor_chunk gains the three surfaces offline through their real paths (a 3 s touch hold on a kiosk card, an empty plan pushed by the server, General settings revived by a remount): one cycle, one notice and an idle loader over 8 s, then the Plan tab and "Add space" heal. On dev: 4 requests / 3 notices, 6 / 2 and 3 / 2. The loader unit test pins reconcile versus intent; the mutant render-reconcile-restarts-editor-runtime-cycle is guarded by the smoke. Issue: #757 User-Visible: yes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018qZfe7YS4rqEMKoVeS3GKd
314 lines
12 KiB
JavaScript
314 lines
12 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
import test from 'node:test';
|
|
import { dirname, join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { EditorRuntimeLoader, safeRuntimeDiagnostic } from '../test-build/editor-runtime-loader.js';
|
|
|
|
const repoRoot = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
|
|
const deferred = () => {
|
|
let resolve;
|
|
let reject;
|
|
const promise = new Promise((ok, fail) => { resolve = ok; reject = fail; });
|
|
return { promise, resolve, reject };
|
|
};
|
|
|
|
test('runtime diagnostics expose only allowlisted metadata', () => {
|
|
const secrets = [
|
|
'space.private_identifier|abc123',
|
|
'https://private.invalid/iso.js?token=secret-value',
|
|
new Error('sensor.private_identifier\nhttps://private.invalid/?auth=secret'),
|
|
];
|
|
for (const secret of secrets) {
|
|
const diagnostic = safeRuntimeDiagnostic('structural', secret, true);
|
|
assert.deepEqual(diagnostic, {
|
|
kind: 'structural', fingerprint: 'redacted', terminal: true,
|
|
});
|
|
assert.ok(Object.isFrozen(diagnostic));
|
|
assert.doesNotMatch(JSON.stringify(diagnostic), /private|sensor\.|https?:|token|auth|secret/i);
|
|
}
|
|
assert.deepEqual(safeRuntimeDiagnostic('runtime-load', 'abc123', false), {
|
|
kind: 'runtime-load', fingerprint: 'abc123', terminal: false,
|
|
});
|
|
});
|
|
|
|
test('editor runtime loader deduplicates concurrent intent and installs atomically', async () => {
|
|
const pending = deferred();
|
|
const states = [];
|
|
const installed = [];
|
|
let loads = 0;
|
|
const loader = new EditorRuntimeLoader({
|
|
expectedFingerprint: 'same',
|
|
load: async () => { loads++; return pending.promise; },
|
|
install: (runtime) => installed.push(runtime),
|
|
stateChanged: (state) => states.push(state),
|
|
});
|
|
|
|
const first = loader.ensure();
|
|
const second = loader.ensure();
|
|
assert.equal(loads, 1);
|
|
assert.equal(loader.state, 'loading');
|
|
assert.equal(installed.length, 0);
|
|
|
|
pending.resolve({ fingerprint: 'same', create: () => ({ ready: true }) });
|
|
assert.deepEqual(await Promise.all([first, second]), [true, true]);
|
|
assert.equal(loads, 1);
|
|
assert.deepEqual(installed, [{ ready: true }]);
|
|
assert.deepEqual(states, ['loading', 'ready']);
|
|
});
|
|
|
|
test('editor runtime loader retries the first failure exactly once', async () => {
|
|
const attempts = [];
|
|
const loader = new EditorRuntimeLoader({
|
|
expectedFingerprint: 'same',
|
|
load: async (attempt) => {
|
|
attempts.push(attempt);
|
|
if (attempt === 0) throw new Error('404');
|
|
return { fingerprint: 'same', create: () => 'runtime' };
|
|
},
|
|
install: () => {},
|
|
});
|
|
|
|
assert.equal(await loader.ensure(), true);
|
|
assert.deepEqual(attempts, [0, 1]);
|
|
assert.equal(await loader.ensure(), true);
|
|
assert.deepEqual(attempts, [0, 1], 'a ready loader never imports again');
|
|
});
|
|
|
|
test('fingerprint mismatch fails closed after one retry and remains terminal', async () => {
|
|
const attempts = [];
|
|
const installed = [];
|
|
const failures = [];
|
|
const loader = new EditorRuntimeLoader({
|
|
expectedFingerprint: 'entry',
|
|
load: async (attempt) => {
|
|
attempts.push(attempt);
|
|
return { fingerprint: 'other', create: () => 'mixed-runtime' };
|
|
},
|
|
install: (runtime) => installed.push(runtime),
|
|
failed: (error) => failures.push(String(error)),
|
|
});
|
|
|
|
assert.equal(await loader.ensure(), false);
|
|
assert.deepEqual(attempts, [0, 1]);
|
|
assert.deepEqual(installed, []);
|
|
assert.equal(loader.state, 'failed');
|
|
assert.match(failures[0], /fingerprint mismatch/);
|
|
|
|
assert.equal(await loader.ensure(), false);
|
|
assert.deepEqual(attempts, [0, 1], 'terminal failure requires a page refresh');
|
|
});
|
|
|
|
test('lazy runtime boundary keeps the eager entry independent and does not grow any usage', () => {
|
|
const card = readFileSync(join(repoRoot, 'src', 'houseplan-card.ts'), 'utf8');
|
|
assert.doesNotMatch(
|
|
card,
|
|
/^import(?!\s+type\b)[^;]*houseplan-editor-runtime[^;]*;/m,
|
|
'the eager card must not statically import the editor runtime',
|
|
);
|
|
const sources = [];
|
|
const visit = (directory) => {
|
|
for (const name of readdirSync(directory)) {
|
|
const path = join(directory, name);
|
|
if (statSync(path).isDirectory()) visit(path);
|
|
else if (/\.ts$/.test(name)) sources.push(readFileSync(path, 'utf8'));
|
|
}
|
|
};
|
|
visit(join(repoRoot, 'src'));
|
|
const anyCount = sources.reduce(
|
|
(total, source) => total + (source.match(/\bany\b/g)?.length || 0), 0,
|
|
);
|
|
assert.ok(anyCount <= 1123, `src any count grew from 1123 to ${anyCount}`);
|
|
});
|
|
|
|
test('source type references stay portable across build hosts', () => {
|
|
const sources = [];
|
|
const visit = (directory) => {
|
|
for (const name of readdirSync(directory)) {
|
|
const path = join(directory, name);
|
|
if (statSync(path).isDirectory()) visit(path);
|
|
else if (/\.ts$/.test(name)) sources.push(readFileSync(path, 'utf8'));
|
|
}
|
|
};
|
|
visit(join(repoRoot, 'src'));
|
|
assert.doesNotMatch(
|
|
sources.join('\n'),
|
|
/import\(["'][A-Za-z]:[\\/]/,
|
|
'type imports must not capture an absolute Windows workspace path',
|
|
);
|
|
});
|
|
|
|
test('lazy runtime event handlers keep the runtime receiver', () => {
|
|
const runtime = readFileSync(join(repoRoot, 'src', 'houseplan-editor-runtime.ts'), 'utf8');
|
|
const bareHandlers = [...runtime.matchAll(/@[a-zA-Z-]+=\$\{this\.(_[A-Za-z0-9]+)\}/g)]
|
|
.map((match) => match[1]);
|
|
assert.deepEqual(
|
|
bareHandlers,
|
|
[],
|
|
`Lit binds bare listeners to the host element, not the lazy runtime: ${bareHandlers.join(', ')}`,
|
|
);
|
|
});
|
|
|
|
test('documentation capture materializes the complete bundle tree', () => {
|
|
const capture = readFileSync(join(repoRoot, 'demo', 'docs', 'capture.mjs'), 'utf8');
|
|
assert.match(capture, /import ['"]\.\.\/\.\.\/scripts\/bundle-sync\.mjs['"]/);
|
|
assert.doesNotMatch(capture, /copyFileSync\(BUNDLE,\s*DEMO_BUNDLE\)/);
|
|
});
|
|
|
|
test('network failure re-arms the loader for the next explicit intent (#353 AC1)', async () => {
|
|
const attempts = [];
|
|
const failures = [];
|
|
let cycles = 0;
|
|
const loader = new EditorRuntimeLoader({
|
|
expectedFingerprint: 'same',
|
|
load: async (attempt) => {
|
|
if (attempt === 0) cycles++;
|
|
attempts.push(attempt);
|
|
if (cycles < 3) throw new Error('net::ERR_INTERNET_DISCONNECTED');
|
|
return { fingerprint: 'same', create: () => 'runtime' };
|
|
},
|
|
install: () => {},
|
|
failed: (error, info) => failures.push({ error: String(error), terminal: info.terminal }),
|
|
});
|
|
|
|
assert.equal(await loader.ensure(), false);
|
|
assert.equal(loader.state, 'idle', 'a network failure must not be terminal');
|
|
assert.deepEqual(attempts, [0, 1]);
|
|
|
|
assert.equal(await loader.ensure(), false, 'second press runs a fresh cycle');
|
|
assert.deepEqual(attempts, [0, 1, 0, 1]);
|
|
assert.equal(failures.length, 2, 'every failed cycle is reported');
|
|
assert.ok(failures.every((failure) => failure.terminal === false));
|
|
|
|
assert.equal(await loader.ensure(), true, 'third press succeeds');
|
|
assert.equal(loader.state, 'ready');
|
|
assert.equal(failures.length, 2);
|
|
});
|
|
|
|
test('a render reconcile never starts another cycle after a network failure (#757 AC3)', async () => {
|
|
let cycles = 0;
|
|
let online = false;
|
|
const failures = [];
|
|
const loader = new EditorRuntimeLoader({
|
|
expectedFingerprint: 'same',
|
|
load: async (attempt) => {
|
|
if (attempt === 0) cycles++;
|
|
if (!online) throw new Error('net::ERR_FAILED');
|
|
return { fingerprint: 'same', create: () => 'runtime' };
|
|
},
|
|
install: () => {},
|
|
failed: (error, info) => failures.push(info.terminal),
|
|
});
|
|
|
|
assert.equal(await loader.ensure('reconcile'), false, 'a surface waiting for the runtime starts the first cycle');
|
|
assert.equal(cycles, 1);
|
|
assert.equal(loader.state, 'idle', 'the failure stays non-terminal');
|
|
for (let render = 0; render < 5; render++) assert.equal(await loader.ensure('reconcile'), false);
|
|
online = true;
|
|
assert.equal(await loader.ensure('reconcile'), false, 'a render after the network is back is still not an intent');
|
|
assert.equal(cycles, 1, 'no render started a cycle of its own');
|
|
assert.deepEqual(failures, [false], 'one failure, one notice');
|
|
|
|
online = false;
|
|
assert.equal(await loader.ensure(), false, 'an explicit intent starts a fresh cycle');
|
|
assert.equal(cycles, 2);
|
|
assert.equal(await loader.ensure('reconcile'), false, 'and its failure waits for the next intent again');
|
|
assert.equal(cycles, 2);
|
|
|
|
online = true;
|
|
const explicit = loader.ensure();
|
|
assert.equal(loader.ensure('reconcile'), explicit, 'a render joins the explicit cycle in flight');
|
|
assert.equal(await explicit, true);
|
|
assert.equal(cycles, 3);
|
|
assert.equal(await loader.ensure('reconcile'), true, 'ready answers a render too');
|
|
});
|
|
|
|
test('a render reconcile after a terminal failure stays terminal (#757 AC3)', async () => {
|
|
let loads = 0;
|
|
const loader = new EditorRuntimeLoader({
|
|
expectedFingerprint: 'entry',
|
|
load: async () => { loads++; return { fingerprint: 'other', create: () => 'foreign' }; },
|
|
install: () => {},
|
|
});
|
|
assert.equal(await loader.ensure('reconcile'), false);
|
|
assert.equal(loader.state, 'failed');
|
|
assert.equal(await loader.ensure('reconcile'), false);
|
|
assert.equal(await loader.ensure(), false);
|
|
assert.equal(loads, 2, 'one cycle of two attempts, never another import');
|
|
});
|
|
|
|
test('fingerprint mismatch on either attempt is terminal (#353 AC2)', async () => {
|
|
const attempts = [];
|
|
const failures = [];
|
|
const loader = new EditorRuntimeLoader({
|
|
expectedFingerprint: 'entry',
|
|
load: async (attempt) => {
|
|
attempts.push(attempt);
|
|
if (attempt === 0) return { fingerprint: 'other', create: () => 'foreign' };
|
|
throw new Error('net::ERR_FAILED');
|
|
},
|
|
install: () => {},
|
|
failed: (error, info) => failures.push(info.terminal),
|
|
});
|
|
|
|
assert.equal(await loader.ensure(), false);
|
|
assert.equal(loader.state, 'failed', 'a foreign build was observed — only a refresh helps');
|
|
assert.deepEqual(failures, [true]);
|
|
assert.equal(await loader.ensure(), false);
|
|
assert.deepEqual(attempts, [0, 1], 'terminal failure never imports again');
|
|
});
|
|
|
|
test('lazy failure toast wording follows terminality (#353 AC5)', async () => {
|
|
const { lazyLoadFailureMessage } = await import('../test-build/editor-runtime-loader.js');
|
|
const t = (key) => `<${key}>`;
|
|
assert.equal(
|
|
lazyLoadFailureMessage(t, { terminal: true }),
|
|
'<editor.load_failed> <editor.refresh_advice>',
|
|
);
|
|
assert.equal(
|
|
lazyLoadFailureMessage(t, { terminal: false }),
|
|
'<editor.load_failed> <editor.retry_advice>',
|
|
);
|
|
});
|
|
|
|
test('user-facing card loaders route their toast through lazyLoadFailureMessage (#353 AC5)', async () => {
|
|
const ts = (await import('typescript')).default;
|
|
const source = readFileSync(join(repoRoot, 'src', 'houseplan-card.ts'), 'utf8');
|
|
const file = ts.createSourceFile(
|
|
'houseplan-card.ts', source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS,
|
|
);
|
|
const callbacks = [];
|
|
const visit = (node) => {
|
|
if (ts.isPropertyAssignment(node)
|
|
&& node.name.getText(file) === 'failed'
|
|
&& ts.isArrowFunction(node.initializer)) {
|
|
callbacks.push(node.initializer);
|
|
}
|
|
ts.forEachChild(node, visit);
|
|
};
|
|
visit(file);
|
|
assert.equal(callbacks.length, 4,
|
|
'editor, onboarding, PDF and hidden isometric runtimes all declare failed callbacks');
|
|
let forwardedCallbacks = 0;
|
|
for (const callback of callbacks) {
|
|
assert.equal(callback.parameters.length, 2, 'the failure info parameter must be declared');
|
|
const infoName = callback.parameters[1].name.getText(file);
|
|
let forwards = false;
|
|
const inspect = (node) => {
|
|
if (ts.isCallExpression(node)
|
|
&& node.expression.getText(file) === 'lazyLoadFailureMessage'
|
|
&& node.arguments.length === 2
|
|
&& node.arguments[1].getText(file) === infoName) {
|
|
forwards = true;
|
|
}
|
|
ts.forEachChild(node, inspect);
|
|
};
|
|
inspect(callback.body);
|
|
if (forwards) forwardedCallbacks++;
|
|
}
|
|
assert.equal(forwardedCallbacks, 3,
|
|
'editor, onboarding and PDF are user-facing; all must forward terminality to the toast');
|
|
});
|