import test from 'node:test'; import assert from 'node:assert/strict'; import { readFile, writeFile, access } from 'node:fs/promises'; import path from 'node:path'; import { fixture } from './fixtures.ts'; import { renderNext, startServer } from '../server.ts'; import { rpc } from '../mcp.ts'; import { delay, pluginRoot } from '../hyperframes.ts'; const film = (duration = 1) => `
`; test('phase1 renders the snapshot at its authored rational frame rate', { timeout: 120_000 }, () => fixture(async store => { for (const numerator of [24_000, 30_000]) { const saved = await store.writeFiles((await store.get()).revision, [{ path: 'index.html', content: film().replace('data-fps="10"', `data-fps="${numerator}/1001"`), }]); const job = await store.requestExport(saved.revision, { phase1: true, audio: 'silent' }); // A later edit must not alter the frame rate in the immutable export snapshot. await store.writeFiles(saved.revision, [{ path: 'index.html', content: film() }]); await renderNext(store, pluginRoot); const completed = (await store.job(job.id))!; assert.equal(completed.state, 'completed', JSON.stringify(completed)); assert.equal(completed.expected!.fps, numerator / 1001); assert.equal((completed.verification as any).status, 'passed'); } })); test('real phase1 MCP, active Studio history, confirmed running cancellation and automatically verified MP4', { timeout: 180_000 }, () => fixture(async store => { const running = await startServer(store, 0, { editor: false }); const address = running.server.address(); assert.ok(address && typeof address !== 'string'); await writeFile(path.join(store.directory, 'preview.json'), JSON.stringify({ port: address.port })); const call = async (name: string, args: any = {}) => { const response: any = await rpc({ jsonrpc: '2.0', id: crypto.randomUUID(), method: 'tools/call', params: { name, arguments: args } }, store); assert.ok(!response.result.isError, JSON.stringify(response)); return response.result.structuredContent; }; const waitFor = async (pid: string, jobId: string, predicate: (job: any) => boolean) => { const end = Date.now() + 100_000; for (;;) { const value = await call('openmotion_get_project', { projectId: pid, exportJobId: jobId }); assert.notEqual(value.exportJob.state, 'failed', JSON.stringify(value.exportJob)); if (predicate(value.exportJob)) return value.exportJob; assert.ok(Date.now() < end, JSON.stringify(value.exportJob)); await delay(250); } }; try { let p = await call('openmotion_create_project', { requestId: 'create', projectType: 'natural_language', title: 'Runtime smoke', requirements: '静音小方块原生动画' }); let saved = await call('openmotion_edit_project', { projectId: p.projectId, requestId: 'long', expectedRevision: p.revision, expectedVersionId: p.version.versionId, files: [{ path: 'index.html', content: film(90) }] }); p = await call('openmotion_open_preview', { projectId: p.projectId, expectedRevision: saved.revision, expectedVersionId: saved.version.versionId }); assert.notEqual(p.revision, saved.revision, 'first preview returns the stamped source version without a retry'); assert.equal(p.previewHandoff.versionId, p.currentVersionId); assert.equal((await fetch(p.previewUrl)).status, 200); assert.equal((await call('openmotion_open_preview')).projectId, p.projectId, 'same MCP client binds only its own session'); const activeSave = await call('openmotion_edit_project', { projectId: p.projectId, requestId: 'active-edit', expectedRevision: p.revision, expectedVersionId: p.version.versionId, files: [{ path: 'note.txt', content: 'Studio owns history' }] }); assert.equal(activeSave.previewSync, 'file_watcher_notified'); p = await call('openmotion_get_project', { projectId: p.projectId }); const long = await call('openmotion_export_request', { projectId: p.projectId, requestId: 'long-export', expectedRevision: p.revision, versionId: p.version.versionId, audio: 'silent' }); await waitFor(p.projectId, long.job.id, job => job.stage === 'rendering'); const cancelled = await call('openmotion_export_cancel', { projectId: p.projectId, requestId: 'cancel', jobId: long.job.id }); assert.ok(['cancelling', 'cancelled'].includes(cancelled.job.state)); const stopped = await waitFor(p.projectId, long.job.id, job => job.state === 'cancelled'); assert.equal(stopped.state, 'cancelled'); await assert.rejects(access(store.exportPath(long.job))); saved = await call('openmotion_edit_project', { projectId: p.projectId, requestId: 'short', expectedRevision: p.revision, expectedVersionId: p.version.versionId, files: [{ path: 'index.html', content: film() }] }); saved = await call('openmotion_open_preview', { projectId: p.projectId, expectedRevision: saved.revision, expectedVersionId: saved.version.versionId }); const exportArgs = { projectId: p.projectId, requestId: 'short-export', expectedRevision: saved.revision, versionId: saved.version.versionId, audio: 'silent' }; const requested = await call('openmotion_export_request', exportArgs); const legacyReplay = await call('motion_export_request', exportArgs); assert.equal(legacyReplay.replayed, true); assert.equal(legacyReplay.job.id, requested.job.id, 'a legacy retry must not queue another render'); const completed = await waitFor(p.projectId, requested.job.id, job => job.state === 'completed'); assert.equal(completed.verification.status, 'passed', JSON.stringify(completed.verification)); assert.equal(completed.expected.width, 320); assert.equal(completed.expected.fps, 10); assert.equal((await fetch(completed.downloadUrl)).status, 200); assert.ok((await readFile(completed.path)).length > 1000); const legacyRead = await call('motion_get_project', { projectId: p.projectId, exportJobId: requested.job.id }); assert.equal(legacyRead.exportJob.path, completed.path, 'both names expose the same verified MP4'); assert.equal((await call('openmotion_get_project', { projectId: p.projectId, exportJobId: long.job.id })).exportJob.state, 'cancelled'); assert.equal((await call('openmotion_export_cancel', { projectId: p.projectId, requestId: 'cancel-completed', jobId: completed.id })).job.state, 'completed'); const review = await call('openmotion_export_request', { action: 'record_review', projectId: p.projectId, jobId: completed.id, requestId: 'review', review: { objectId: completed.id, versionId: completed.versionId, source: 'agent', conclusion: 'pending', evidence: 'Technical checks passed; actual playback not yet reviewed' } }); assert.equal(review.review.verifiedUserAdoption, false); assert.equal(review.changed, false); } finally { await running.close(); } }, false));