Files
openmotion/tests/sync.test.ts
Tom Huang ef36dc3b09 Use an official branded welcome template for initial projects (#389)
* Add branded welcome template for initial projects

Use the same editable OpenMotion welcome source for first-run initialization and template copies, while keeping new projects blank and preserving existing edits. Retain the required offline assets and licenses in application packages and add regression coverage.

* Update shipped template guides for the fourth builtin

Registering the OpenMotion welcome template changes the catalog to four
bundled editable templates and 895 entries. Align the remaining current
guides that still described three templates and 894 entries, and add the
welcome project to the editable-template table in the reverse-engineering
README.

Generated-By: looper 0.16.2 (runner=fixer, agent=pi)

* Align the last shipped guides with four bundled templates

Two current guides that ship in the npm distribution still described the
catalog as three official Studio templates: the editor-fork template
creation note and the Presenter Tech package README. Both now say four,
matching /api/templates, the gallery totals and the docs already updated
by the previous pass.

Generated-By: looper 0.16.2 (runner=fixer, agent=pi)

* Fix the last three-template claim in the shipped catalog guide

docs/template-catalog.md ships in the npm distribution through
scripts/distribution.ts, and its "页面与数据" section still told users the home
Motion gallery shows three editable templates. The gallery defaults to the
builtin category, template-library.ts registers openmotion-welcome as the
fourth builtin and tests/catalog.test.ts asserts a builtin total of 4, so the
sentence now says four and names the official welcome film, matching the other
sections of the same page and the guides aligned by 86a43946 and 2f425338.

Generated-By: looper 0.16.2 (runner=fixer, agent=pi)

* Clarify catalog documentation after welcome template review

---------

Co-authored-by: 罗高 <luogao@192.168.5.135>
2026-11-15 11:00:13 +08:00

110 lines
8.3 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';
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('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);
}));