Files
openmotion/tests/sync.test.ts
open-design-crew[bot] ff22a47ca6 fix(tracking): report actionable MCP and export failures (#613)
* fix(tracking): report actionable MCP and export failures

* test: fix polling and packaged onboarding regressions

* fix(tracking): deduplicate cancellation and classify nested validation

* fix(tracking): report a missing tool action before variant ambiguity

---------

Co-authored-by: 罗高 <luogao@users.noreply.github.com>
2026-11-18 22:26:59 +08:00

154 lines
12 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdir, readFile, rm, symlink, truncate, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { WorkspaceStore, Conflict } from '../workspace.ts';
import { callTool } from '../mcp.ts';
import { pluginRoot, inspectProject } from '../hyperframes.ts';
import { transport } from './http.ts';
import { fixture, legacyProject } from './fixtures.ts';
import { copyTemplate } from '../template-library.ts';
test('Studio source changes are visible to MCP; MCP source writes reach the same native files', () => fixture(async store => {
const first = await store.get();
const source = (await store.readFile('compositions/intro.html')).content;
await writeFile(path.join(store.projectDirectory, 'compositions/intro.html'), source.replace('Great work', 'Studio edit'));
const read = await callTool('openmotion_get_project', {}, new WorkspaceStore(store.directory));
assert.notEqual(read.revision, first.revision); assert.equal(read.project!.format, 'hyperframes');
assert.ok(!JSON.stringify(read).includes('base64'));
await assert.rejects(store.writeFiles(first.revision, [{ path: 'index.html', content: 'stale' }]), Conflict);
const next = await callTool('openmotion_write_files', { expectedRevision: read.revision, files: [{ path: 'compositions/intro.html', content: source.replace('Great work', 'Agent edit') }] }, store);
assert.match((await store.readFile('compositions/intro.html')).content, /Agent edit/);
assert.equal((await new WorkspaceStore(store.directory).get()).revision, next.revision);
const browser = await (await transport(store)('/api/project')).json(); assert.equal(browser.revision, next.revision);
}));
test('concurrent Agent writers serialize and reject a stale revision', () => fixture(async store => {
const first = await store.get(), other = new WorkspaceStore(store.directory);
const results = await Promise.allSettled([
store.writeFiles(first.revision, [{ path: 'notes.md', content: 'A' }]),
other.writeFiles(first.revision, [{ path: 'notes.md', content: 'B' }]),
]);
assert.equal(results.filter(r => r.status === 'fulfilled').length, 1);
assert.ok((results.find(r => r.status === 'rejected') as PromiseRejectedResult).reason instanceof Conflict);
}));
test('invalid batch paths and symbolic links cannot escape or partially overwrite a project', () => fixture(async store => {
const first = await store.get();
for (const invalid of ['../outside.html', '/tmp/outside.html', 'assets/../../outside.html', 'a\\b.js', '.env', 'node_modules/x.js']) {
await assert.rejects(store.writeFiles(first.revision, [{ path: 'index.html', content: 'bad' }, { path: invalid, content: 'bad' }]));
assert.equal((await store.get()).revision, first.revision);
}
await symlink(store.directory, path.join(store.projectDirectory, 'escape'));
await assert.rejects(store.readFile('escape/outside.html'), /符号链接/);
await assert.rejects(store.writeFiles(first.revision, [{ path: 'escape/outside.html', content: 'bad' }]), /符号链接/);
await rm(path.join(store.projectDirectory, 'escape'));
assert.equal((await store.get()).revision, first.revision);
}));
test('asset imports are local files and their content participates in revisions', () => fixture(async store => {
const source = path.join(store.directory, 'audio.wav'); await writeFile(source, Buffer.from([1, 2, 3, 4]));
const first = await store.get();
const next = await store.importAsset(first.revision, source, 'assets/audio.wav');
assert.notEqual(next.revision, first.revision);
assert.deepEqual(await readFile(path.join(store.projectDirectory, 'assets/audio.wav')), Buffer.from([1, 2, 3, 4]));
await assert.rejects(store.importAsset(next.revision, source, '../outside.wav'));
await truncate(source, 500_000_001);
await assert.rejects(store.importAsset(next.revision, source, 'assets/audio.wav'), /500 MB/);
assert.equal((await store.get()).revision, next.revision);
assert.deepEqual(await readFile(path.join(store.projectDirectory, 'assets/audio.wav')), Buffer.from([1, 2, 3, 4]));
await writeFile(path.join(store.projectDirectory, 'assets/audio.wav'), Buffer.from([4, 3, 2, 1]));
assert.notEqual((await store.get()).revision, next.revision);
}));
test('render snapshots include scene sources and assets and survive later Studio edits', () => fixture(async store => {
const initial = await store.get(); const job = await store.requestExport(initial.revision);
await store.writeFiles(initial.revision, [{ path: 'compositions/intro.html', content: '<template>next version</template>' }]);
assert.equal(store.revision(await store.files(store.snapshotDirectory(job.id))), initial.revision);
const claimed = await store.claimExport(); assert.equal(claimed!.id, job.id);
assert.equal(await new WorkspaceStore(store.directory).claimExport(), null);
assert.equal((await store.status()).exportJob!.token, undefined);
await assert.rejects(store.finishExport(job.id, 'wrong', 'bad'));
await store.finishExport(job.id, claimed!.token!, 'renderer failed');
assert.equal((await store.status()).exportJob!.state, 'failed');
}));
test('export snapshots freeze template vendor dist files while native revisions and runtime exclusions stay unchanged', () => fixture(async store => {
await copyTemplate('decision-tree', store.projectDirectory, pluginRoot);
const dependency = 'vendor/gsap-3.14.2/dist/gsap.min.js';
const original = await readFile(path.join(store.projectDirectory, dependency));
for (const name of ['dist/build.js', 'renders/old.mp4', 'output/export.mp4', 'vendor/node_modules/private.js', 'vendor/.cache/private.js']) {
await mkdir(path.dirname(path.join(store.projectDirectory, name)), { recursive: true });
await writeFile(path.join(store.projectDirectory, name), 'excluded');
}
const before = await store.get();
assert.ok(!before.project!.files.some(file => file.path === dependency), 'native history keeps its existing inventory policy');
assert.ok(!before.project!.files.some(file => file.path.includes('private') || /^(dist|renders|output)\//.test(file.path)));
for (const invalid of ['dist/build.js', 'renders/old.mp4', 'output/export.mp4', 'vendor/node_modules/private.js', 'vendor/.cache/private.js']) {
await assert.rejects(store.writeFiles(before.revision, [{ path: invalid, content: 'blocked' }]));
}
const job = await store.requestExport(before.revision);
assert.deepEqual(await readFile(path.join(store.snapshotDirectory(job.id), dependency)), original);
for (const excluded of ['dist/build.js', 'renders/old.mp4', 'output/export.mp4', 'vendor/node_modules/private.js', 'vendor/.cache/private.js']) {
await assert.rejects(readFile(path.join(store.snapshotDirectory(job.id), excluded)), /ENOENT/);
}
await writeFile(path.join(store.projectDirectory, dependency), original.toString() + '\n// later edit');
const changed = await store.writeFiles(before.revision, [{ path: 'notes.md', content: 'Later edit' }]);
assert.notEqual(changed.revision, before.revision);
assert.deepEqual(await readFile(path.join(store.snapshotDirectory(job.id), dependency)), original);
}, false));
test('a vendored dependency changed while freezing an export rejects the snapshot', () => fixture(async store => {
await copyTemplate('decision-tree', store.projectDirectory, pluginRoot);
const dependency = 'vendor/gsap-3.14.2/dist/gsap.min.js';
const source = path.join(store.projectDirectory, dependency), original = await readFile(source);
const before = await store.get(), fileInfo = store.fileInfo.bind(store);
let changed = false;
store.fileInfo = async filename => {
const info = await fileInfo(filename);
if (!changed && filename.startsWith(path.join(store.directory, 'jobs') + path.sep) && filename.endsWith(path.join(...dependency.split('/')))) {
changed = true;
await writeFile(source, original.toString() + '\n// concurrent dependency edit');
}
return info;
};
await assert.rejects(store.requestExport(before.revision), error => error instanceof Conflict && error.code === 'VERSION_CONFLICT');
assert.equal(changed, true, 'the dependency changed after it had been copied');
assert.equal((await store.get()).revision, before.revision, 'the complete render fingerprint catches changes outside native history');
assert.equal(await store.job(), null, 'the rejected snapshot never becomes an export job');
}, false));
test('native Studio motion manifests participate in revisions and snapshots; preview caches do not', () => fixture(async store => {
const before = await store.get();
await mkdir(path.join(store.projectDirectory, '.hyperframes/preview'), { recursive: true });
await writeFile(path.join(store.projectDirectory, '.hyperframes/preview/cached.html'), 'cache');
assert.equal((await store.get()).revision, before.revision);
for (const name of ['studio-motion.json', 'studio-manual-edits.json']) await writeFile(path.join(store.projectDirectory, '.hyperframes', name), '{"version":1}');
const after = await store.get(); assert.notEqual(after.revision, before.revision);
const job = await store.requestExport(after.revision);
for (const name of ['studio-motion.json', 'studio-manual-edits.json']) assert.equal(await readFile(path.join(store.snapshotDirectory(job.id), '.hyperframes', name), 'utf8'), '{"version":1}');
await assert.rejects(readFile(path.join(store.snapshotDirectory(job.id), '.hyperframes/preview/cached.html')), /ENOENT/);
}));
test('stale export requests, dead workers, missing output and expiry are reported honestly', () => fixture(async store => {
const initial = await store.get();
await assert.rejects(store.requestExport('0'.repeat(64)), Conflict);
const job = await store.requestExport(initial.revision);
await store.saveJob({ ...job, expiresAt: Date.now() - 1 });
assert.equal((await store.status()).exportJob!.state, 'failed'); assert.equal(await store.claimExport(), null);
const retry = await store.requestExport(initial.revision); const claimed = await store.claimExport();
await assert.rejects(store.finishExport(retry.id, claimed!.token!));
await store.saveJob({ ...claimed!, workerPid: 2147483647 });
assert.equal((await store.status()).exportJob!.state, 'failed');
await store.saveJob({ ...retry, state: 'completed', file: `exports/${retry.id}.mp4` });
const read = await callTool('openmotion_get_export', {}, store);
assert.equal(read.job!.state, 'failed'); assert.equal(read.path, undefined);
}));
test('new project and migrated legacy projects pass the actual Hyperframes linter', () => fixture(async store => {
const fresh = await inspectProject(pluginRoot, store.projectDirectory, 'lint');
assert.equal(fresh.exitCode, 0); assert.equal(fresh.result.warningCount, 0);
const legacy = legacyProject(); legacy.shots[0].caption = '<script>alert("text")</script>';
const old = JSON.stringify({ revision: 7, project: legacy, exportJob: null });
const other = new WorkspaceStore(path.join(store.directory, 'legacy')); await mkdir(other.directory);
await writeFile(path.join(other.directory, 'workspace.json'), old); await other.initialize(pluginRoot);
assert.equal(await readFile(path.join(other.directory, 'workspace.json'), 'utf8'), old);
assert.match((await other.readFile('compositions/shot-0.html')).content, /&lt;script&gt;/);
const lint = await inspectProject(pluginRoot, other.projectDirectory, 'lint'); assert.equal(lint.exitCode, 0, JSON.stringify(lint.result));
const timeline = await inspectProject(pluginRoot, other.projectDirectory, 'timeline');
assert.equal(timeline.result.timeline.duration, 9);
assert.equal(timeline.result.timeline.tracks[0].rows.length, 3);
const before = await other.get(); await other.initialize(pluginRoot); assert.equal((await other.get()).revision, before.revision);
}));