mirror of
https://github.com/nexu-io/openmotion.git
synced 2026-12-06 01:40:23 +08:00
* fix(fonts): require an imported licence beside its font Asset import accepts a font sourcePath and an optional licensePath. The licence reader required only an absolute path and valid UTF-8 text under 128 KiB, then wrote the bytes into the project as assets/fonts/<id>.license.txt. Project reads serve text extensions back to the caller, so the same install could read any readable text file through a two-step detour: import it as a licence, then read it as project source. The argument is genuinely useful for the normal case, where a font download ships with OFL.txt next to it, but it was not limited to that case. A licence now has to sit in the same directory as the font the user selected. The phase-one call site passes the original font path so the importer has something to compare against, and a mismatch fails with FONT_LICENSE_NOT_BESIDE_FONT. Nothing else about font import changes: the parsed family, weight and style, the relative @font-face CSS, the version transaction and the optional licence text all behave as before. Review follow-up: the constraint is now documented where callers read it, because the new rejection changes the accepted input of a public tool. The licensePath schema description in shared/phase1-tools.ts names the same-directory requirement and the error code, and the MCP contract document, the native authoring reference and the font preparation reference say the same thing next to their existing licence instructions. Validation: tests/font-import.test.ts passes with a negative case that copies the fixture licence into another directory and expects FONT_LICENSE_NOT_BESIDE_FONT, while the existing Orbitron plus OFL positive case still passes. tests/mcp-onboarding.test.ts passes 9/9 after the description change, and npm run check passes. * fix(fonts): require the source path for license validation --------- Co-authored-by: lefarcen <935902669@qq.com>
43 lines
3.7 KiB
TypeScript
43 lines
3.7 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
import { fileURLToPath } from 'node:url';
|
|
import path from 'node:path';
|
|
import { fixture } from './fixtures.ts';
|
|
import { Phase1Service } from '../phase1.ts';
|
|
import { pluginRoot } from '../hyperframes.ts';
|
|
|
|
// Uses the actual phase-one version/history boundary, not a mock of the import implementation.
|
|
test('font import retains the actual face and license, rejects corrupt fonts, deduplicates and restores resources', () => fixture(async store => {
|
|
const service = new Phase1Service(store, pluginRoot, { origin: () => 'http://127.0.0.1:4173', nativeUrl: () => undefined,
|
|
openProject: async () => {}, mutation: async task => task(), cancel: async () => ({}) });
|
|
const original = await service.call('openmotion_create_project', { requestId: 'font-project', projectType: 'natural_language', title: 'Font regression', requirements: 'Editable text' });
|
|
const sourcePath = fileURLToPath(new URL('./fixtures/fonts/orbitron.ttf', import.meta.url));
|
|
const licensePath = fileURLToPath(new URL('./fixtures/fonts/OFL.txt', import.meta.url));
|
|
const bad = path.join(store.directory, 'bad.ttf'); await writeFile(bad, 'not a font');
|
|
const args = { projectId: original.projectId, requestId: 'fonts', items: [{ itemId: 'orbitron', sourcePath, licensePath }, { itemId: 'bad', sourcePath: bad }] };
|
|
const imported = await service.call('openmotion_import_assets', args);
|
|
assert.equal(imported.status, 'partial');
|
|
const font = imported.items[0]; assert.equal(font.status, 'completed');
|
|
assert.equal(font.font.family, 'Orbitron'); assert.equal(font.font.weight, '400 900');
|
|
assert.match(font.fontFaceCss, /font-weight: 400 900/); assert.equal(imported.items[1].status, 'failed');
|
|
const directory = store.projectDirectoryFor(original.projectId);
|
|
assert.deepEqual(await readFile(path.join(directory, font.path)), await readFile(sourcePath));
|
|
assert.equal(await readFile(path.join(directory, font.license.path), 'utf8'), await readFile(licensePath, 'utf8'));
|
|
// A licence from outside the font's directory would make the argument a bounded file copy.
|
|
const strayLicense = path.join(store.directory, 'OFL.txt');
|
|
await writeFile(strayLicense, await readFile(licensePath));
|
|
const stray = await service.call('openmotion_import_assets', { projectId: original.projectId, requestId: 'stray-license',
|
|
items: [{ itemId: 'orbitron-stray', sourcePath, licensePath: strayLicense }] });
|
|
assert.equal(stray.items[0].status, 'failed');
|
|
assert.match(stray.items[0].error, /FONT_LICENSE_NOT_BESIDE_FONT/);
|
|
const dedupe = await service.call('openmotion_import_assets', { ...args, requestId: 'font-again', items: [args.items[0]] });
|
|
assert.equal(dedupe.items[0].assetId, font.assetId); assert.equal(dedupe.items[0].reused, true);
|
|
const before = await service.call('openmotion_get_project', { projectId: original.projectId });
|
|
const removed = await service.call('openmotion_restore_version', { projectId: original.projectId, requestId: 'before-font', versionId: original.version.versionId, expectedRevision: before.revision, expectedVersionId: before.version.versionId });
|
|
await assert.rejects(readFile(path.join(directory, font.path)), { code: 'ENOENT' });
|
|
await service.call('openmotion_restore_version', { projectId: original.projectId, requestId: 'after-font', versionId: imported.version.versionId, expectedRevision: removed.revision, expectedVersionId: removed.version.versionId });
|
|
assert.deepEqual(await readFile(path.join(directory, font.path)), await readFile(sourcePath));
|
|
assert.equal(await readFile(path.join(directory, font.license.path), 'utf8'), await readFile(licensePath, 'utf8'));
|
|
}, false));
|