histoire
Version:
A fast story book powered by Vite
72 lines (71 loc) • 3.83 kB
JavaScript
import { COLLECT_TESTS, RUN_TESTS, TEST_DEFINITIONS, TEST_RESULT } from '@histoire/shared';
import { CAPTURE_LIMITS } from './limits.js';
/** Self-contained function serialized into trusted host, with no module globals. */
export function installPreviewTestHost(frame, state, options) {
const pending = new Map();
const results = new Map();
const used = new Set();
/** Check both live document and original tuple before dispatch or settlement. */
function current(command) {
return state.active && state.ready && state.nonce === command.nonce && state.epoch === command.epoch
&& state.storyId === command.storyId && state.variantId === command.variantId
&& state.documentId === command.documentId && state.currentDocumentId() === command.documentId;
}
window.addEventListener('message', (event) => {
const data = event.data;
const kind = data?.type === options.types[2] ? 'collect' : data?.type === options.types[3] ? 'run' : undefined;
if (!kind || event.source !== frame.contentWindow || event.origin !== options.origin || data.__histoire !== true)
return;
const id = kind === 'collect' ? data.requestId : data.runId;
const command = pending.get(id);
if (!command || command.kind !== kind || !current(command) || data.documentId !== command.documentId
|| data.mcpNonce !== command.nonce || data.mcpEpoch !== command.epoch
|| data.storyId !== command.storyId || data.variantId !== command.variantId) {
return;
}
pending.delete(id);
const result = kind === 'collect' ? { count: Array.isArray(data.definitions) ? data.definitions.length : undefined, error: data.error } : { summary: data.summary };
try {
results.set(id, new TextEncoder().encode(JSON.stringify(result)).byteLength <= options.resultBytes ? result : { oversized: true });
}
catch {
results.set(id, { malformed: true });
}
});
const api = {
/** Dispatch once; response loss never repeats test side effects. */
request(command) {
if (!current(command) || pending.size || used.has(command.id))
return false;
used.add(command.id);
pending.set(command.id, command);
frame.contentWindow.postMessage({
__histoire: true,
type: options.types[command.kind === 'collect' ? 0 : 1],
...(command.kind === 'collect' ? { requestId: command.id } : { runId: command.id }),
variantKey: `${command.storyId}:${command.variantId}`,
storyId: command.storyId,
variantId: command.variantId,
documentId: command.documentId,
mcpNonce: command.nonce,
mcpEpoch: command.epoch,
}, options.origin);
return true;
},
/** Poll only completion or lost authority; Playwright owns deadline. */
settled(command) { return results.has(command.id) || !current(command); },
/** Consume one response and discard pending state on failed navigation. */
take(command) {
const result = current(command) ? results.get(command.id) : undefined;
results.delete(command.id);
pending.delete(command.id);
return result;
},
};
state.tests = api;
return api;
}
/** Fragment references only enclosing fixed bootstrap frame/state/options. */
export function renderPreviewTestScript() {
return `(${installPreviewTestHost.toString()})(frame, state, { origin: options.authority.origin, types: ${JSON.stringify([COLLECT_TESTS, RUN_TESTS, TEST_DEFINITIONS, TEST_RESULT])}, resultBytes: ${CAPTURE_LIMITS.artifactBytes} });`;
}