Files
openmotion/tests/templates.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

70 lines
5.5 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { startServer } from '../server.ts';
import { callTool } from '../mcp.ts';
import { WorkspaceStore } from '../workspace.ts';
import { inspectProject, pluginRoot } from '../hyperframes.ts';
import { fixture } from './fixtures.ts';
test('official templates open in native Studio, preserve edits and switch the project shared by MCP processes', { timeout: 180_000 }, () => fixture(async store => {
const original = await store.get();
const originalDirectory = store.projectDirectory;
await store.writeFiles(original.revision, [{ path: 'notes.md', content: 'Keep the original project' }]);
const running = await startServer(store, 0);
// Studio adds stable data-hf-id attributes on its first start.
const initialized = await store.get();
const address = running.server.address(); assert.ok(address && typeof address !== 'string');
const base = `http://127.0.0.1:${address.port}`;
await writeFile(path.join(store.directory, 'preview.json'), JSON.stringify({ port: address.port }));
const post = (url: string, expectedRevision: string) => fetch(base + url, { method: 'POST', headers: { Origin: base, 'Content-Type': 'application/json' }, body: JSON.stringify({ expectedRevision }) });
try {
const catalog = await (await fetch(base + '/api/templates')).json();
assert.deepEqual(catalog.templates.map(item => item.id), ['product-promo', 'nyt-graph', 'decision-tree', 'openmotion-welcome']);
const opened: { id: string; directory: string }[] = [];
for (const template of catalog.templates) {
const before = await store.get(), previousStudio = running.studio;
const poster = await fetch(base + template.poster); assert.equal(poster.status, 200); assert.equal(poster.headers.get('content-type'), 'image/jpeg');
assert.ok((await poster.arrayBuffer()).byteLength > 5000);
const response = await post(`/api/templates/${template.id}/open`, before.revision);
assert.equal(response.status, 200, await response.clone().text());
const openedProject = await response.json();
assert.notEqual(openedProject.projectId, 'project'); assert.equal(previousStudio.alive(), true);
assert.equal(running.studio.url, previousStudio.url);
const agent = await callTool('openmotion_get_project', {}, new WorkspaceStore(store.directory));
assert.equal(agent.project!.directory, store.projectDirectory); assert.match(agent.project!.html!, new RegExp(template.title));
assert.equal(agent.project!.files.some(file => file.path === 'assets/gsap.min.js'), true);
const lint = await inspectProject(pluginRoot, store.projectDirectory, 'lint'); assert.equal(lint.exitCode, 0, JSON.stringify(lint.result));
const nativeBase = new URL(openedProject.studioUrl).origin;
const identity = await (await fetch(nativeBase + '/__hyperframes_config?projectId=' + openedProject.projectId)).json(); assert.equal(identity.projectDir, store.projectDirectory);
const fileUrl = `${nativeBase}/api/projects/${openedProject.projectId}/files/index.html`;
const file = await (await fetch(fileUrl)).json();
assert.equal(typeof file.content, 'string');
const edited = await fetch(fileUrl, { method: 'PUT', headers: { 'Content-Type': 'text/html', 'If-Match': file.version, Origin: nativeBase }, body: file.content.replace('<title>', '<title>Edited ') });
assert.equal(edited.status, 200, await edited.text());
assert.match((await callTool('openmotion_get_project', {}, new WorkspaceStore(store.directory))).project!.html!, /<title>Edited /);
opened.push({ id: openedProject.projectId, directory: store.projectDirectory });
assert.equal((await post(`/api/templates/${template.id}/open`, before.revision)).status, 409);
}
const saved = await (await fetch(base + '/motion-api/projects')).json(); assert.equal(saved.projects.length, 5);
assert.equal((await post('/api/templates/unknown/open', (await store.get()).revision)).status, 400);
assert.equal(store.projectDirectory, opened.at(-1)!.directory);
assert.equal((await post('/motion-api/projects/project/open', (await store.get()).revision)).status, 200);
assert.equal(store.projectDirectory, originalDirectory);
assert.equal((await store.readFile('notes.md')).content, 'Keep the original project');
assert.equal((await store.get()).project!.html, initialized.project!.html);
for (const project of opened) assert.match(await readFile(path.join(project.directory, 'index.html'), 'utf8'), /<title>Edited /);
const reopened = await post(`/motion-api/projects/${opened[0].id}/open`, (await store.get()).revision);
assert.equal(reopened.status, 200, await reopened.clone().text());
assert.equal(store.projectDirectory, opened[0].directory);
assert.match((await store.get()).project!.html!, /<title>Edited Product Promo/);
const beforeAgentTemplate = await store.get();
const applied = await callTool('openmotion_apply_template', { expectedRevision: beforeAgentTemplate.revision, templateId: 'nyt-graph' }, new WorkspaceStore(store.directory));
assert.match(applied.project!.html!, /NYT Graph/);
assert.notEqual(applied.project!.directory, opened[0].directory);
assert.match(await readFile(path.join(opened[0].directory, 'index.html'), 'utf8'), /<title>Edited Product Promo/);
await assert.rejects(() => callTool('openmotion_apply_template', { expectedRevision: beforeAgentTemplate.revision, templateId: 'nyt-graph' }, store));
} finally { await running.close(); }
}));