UNPKG

histoire

Version:

A fast story book powered by Vite

53 lines (52 loc) • 3.15 kB
import { HistoireSdkError } from '@histoire/protocol'; import { getRuntimeCatalogForContext } from '../runtime/catalog/attachment.js'; import { getEmbedSourceId } from '../virtual/embed/identity.js'; import { collectGenerationTests } from './collection-task.js'; import { createTestContextSnapshot } from './context-snapshot.js'; /** Capture detached metadata before queueing; acquire runner only at lane head. */ export function createHistoireTestTask(ctx, options) { return createTestOperationTask(ctx, options, 'run'); } /** Definition collection shares captured source identity and server execution lane. */ export function createHistoireTestCollectionTask(ctx, options) { return createTestOperationTask(ctx, options, 'collect'); } /** Both operation kinds validate source ownership before acquisition and completion. */ function createTestOperationTask(ctx, options, operation) { const snapshot = createTestContextSnapshot(ctx); const catalog = getRuntimeCatalogForContext(ctx)?.catalog; const source = catalog?.current; if (operation === 'collect' && source) { const order = new Map(source.stories.map((story, index) => [story.id, index])); snapshot.storyFiles.sort((left, right) => (order.get(left.story?.id) ?? Infinity) - (order.get(right.story?.id) ?? Infinity)); } /** Captured source must still own both lane-head admission and completion. */ function assertSourceOwner() { if (catalog && (!source || catalog.current !== source || catalog.updating)) { throw new HistoireSdkError('RUNTIME_CHANGED', 'Test source owner changed'); } } return { validate: assertSourceOwner, async run(signal) { assertSourceOwner(); if (operation === 'collect') return collectGenerationTests(ctx, snapshot, source, options, assertSourceOwner, signal); const { runHistoireTests } = await import('./index.js'); const result = await runHistoireTests(snapshot, { storyId: options.storyId, variantId: options.variantId, onRunProgress: options.onRunProgress, skipStoryScan: true, signal, strictCleanup: true, isolate: true, maxRetries: 0 }); assertSourceOwner(); // UI/SDK consumers admit work under the portable book identity, not the // private runtime-controller project ID. Revision remains admission-owned. return source && result.execution ? { ...result, execution: { ...result.execution, sourceId: getEmbedSourceId(snapshot.root), epoch: source.epoch, revision: source.revision } } : result; }, }; } /** Common UI/MCP submission adapter; queue retains runner ownership through confirmed teardown. */ export function enqueueHistoireTestRun(execution, ctx, options, isActive = () => true, operation = 'run') { const task = operation === 'collect' ? createHistoireTestCollectionTask(ctx, options) : createHistoireTestTask(ctx, options); return execution.enqueue({ ...task, validate() { task.validate?.(); if (!isActive()) throw new Error('Project runtime closed'); } }); }