Files
feuerwerk/tests/core/fonts.test.ts
T
adminandClaude Opus 5.5 6b49a0bea7 Give each account its own folder of uploaded files to delete (#344)
Uploaded images, fonts and sounds belonged to nobody. Every account
saw every file in the pickers, and only an administrator could
delete one, through the API alone. Now every account uploads into a
folder of its own, sees its own files and nobody's, and can delete a
file of its own that no project uses.

- Files are stored as data/<kind>/<folder>/<sha256>.<ext>, and the
  folder is part of asset.file. A name without a folder belongs to
  nobody.
- The folder is a random 12-character name kept in users.json and
  given with the first upload. Renaming an account moves no file, and
  a released game's URLs carry no account name.
- The list shows the caller's own files and nobody's. An
  administrator sees all, each with owner and deletable.
- DELETE takes the name with its folder. It answers 403 unless the
  file is the caller's or the caller is an administrator, and 409
  while a project, snapshot or release of any account names it
  (FileUsage). The 409 names the caller's own projects only.
- The pickers have a Delete button. The first press asks, the second
  deletes, and a refusal shows the server's reason.
- A deleted account's folder stays and only administrators see it.
- tools/migrate-file-owners.php moved every used file into the folder
  of the account using it (a copy each for two accounts) and rewrote
  projects, snapshots and releases. A project's revision goes up, so
  an open editor gets a conflict instead of saving old names back.
- Font families are named after the hash alone, so a folder never
  ends up in a CSS family name.

Run on stein2 on 2026-10-07, backup in
/root/config-backup-20261007/data-before-344: 25 files used,
27 copies, 8 projects, 53 snapshots and 1 release rewritten, 30
unused files stay nobody's. Afterwards all 600 references resolve to
a file in their owner's folder.

Found while building: the Delete button cut its new label to "Del…"
because a button does not measure itself again on a new text. The
picker now lays its button row out again.

Tests:
- PHP files.test.php (new): 46 checks, counter-check 3 red
- Unit core + editor: 220 files, 3403 tests
- e2e 34 (melektaus-atx): ALL PASSED - 67 checks (2 new)
- Gate (melektaus-atx): CHECK EXIT 0, 256 files, 3977 tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 08:25:13 +02:00

104 lines
4.3 KiB
TypeScript

/**
* A font is an asset holding an uploaded typeface, worn by a text object through its `font`
* property the way a sprite wears a texture. Empty, or dangling, means the editor's own typeface.
*/
import { describe, expect, it } from 'vitest';
import {
buildAsset,
CreateAssetCommand,
CreateFontCommand,
SetAssetPropertyCommand,
} from '../../src/core/commands/assetCommands';
import type { FontAsset } from '../../src/core/model/Asset';
import { isPlaceableType } from '../../src/core/model/AssetTypes';
import { fontFamilyOf, fontFileOf, isFontFile } from '../../src/core/model/font';
import { assetRefChoices } from '../../src/core/properties/assetRef';
import { FONT_KEY, fontOf } from '../../src/core/properties/fonts';
import { schemaFor } from '../../src/core/properties/PropertySchema';
import { deserializeProject } from '../../src/core/store/serialize';
import { makeProject, roundTrip } from '../helpers';
const FILE = `${'a1'.repeat(32)}.ttf`;
function makeFont(name = 'Serif', file = FILE) {
const fixture = makeProject();
const command = new CreateFontCommand(name, file);
fixture.stack.execute(command);
return { ...fixture, font: command.asset };
}
describe('font files', () => {
it('accepts only a content-addressed name in a supported format', () => {
expect(isFontFile(FILE)).toBe(true);
expect(isFontFile(`${'b3'.repeat(32)}.woff2`)).toBe(true);
expect(isFontFile(`${'a1'.repeat(32)}.png`)).toBe(false);
expect(isFontFile(`${'a1'.repeat(32)}.TTF`)).toBe(false);
expect(isFontFile('DejaVuSerif.ttf')).toBe(false);
expect(isFontFile('')).toBe(false);
});
it('names one family per file, and never two for the same bytes', () => {
expect(fontFamilyOf(FILE)).toBe(fontFamilyOf(FILE));
expect(fontFamilyOf(FILE)).not.toBe(fontFamilyOf(`${'b3'.repeat(32)}.ttf`));
// The same bytes in two accounts' folders (#344) are one face, and a slash is no family name.
expect(fontFamilyOf(`0123456789ab/${FILE}`)).toBe(fontFamilyOf(FILE));
// A CSS family, so it has to survive being written into a font shorthand unquoted.
expect(fontFamilyOf(FILE)).toMatch(/^[A-Za-z][\w-]*$/);
});
});
describe('the font asset', () => {
it('is not placeable and starts without a file', () => {
expect(isPlaceableType('font')).toBe(false);
expect(buildAsset('font', 'Font')).toMatchObject({ type: 'font', file: '' });
});
it('refuses a file name the server could not have produced', () => {
const fixture = makeProject();
// The stack lets the error through; the editor turns it into the notice the importer reports.
expect(() => fixture.stack.execute(new CreateFontCommand('Bad', 'DejaVuSerif.ttf'))).toThrow(
'is not a stored font file',
);
expect(fixture.project.assets).toHaveLength(0);
});
it('survives a round trip and drops a file name it cannot trust', () => {
const f = makeFont();
expect(roundTrip(f.project)).toEqual(f.project);
const repaired = deserializeProject({
name: 'P',
assets: [
{ id: 'f1', type: 'font', name: 'Local', file: '../../etc/passwd' },
{ id: 'f2', type: 'font', name: 'Gone' },
],
});
expect(repaired.assets.map((a) => (a as FontAsset).file)).toEqual(['', '']);
});
});
describe('the Font row on a text', () => {
it('offers font assets and nothing else', () => {
const f = makeFont();
f.stack.execute(new CreateAssetCommand('text', 'Title'));
const row = schemaFor('text').find((def) => def.key === FONT_KEY);
expect(row).toMatchObject({ kind: 'asset', default: '', assetTypes: ['font'] });
expect(assetRefChoices(f.project, ['font']).map((a) => a.id)).toEqual([f.font.id]);
});
it('resolves to a file, and to none when the id is unset or dangling', () => {
const f = makeFont();
const text = new CreateAssetCommand('text', 'Title');
f.stack.execute(text);
expect(fontOf(text.asset.props)).toBe('');
expect(fontFileOf(f.project, '')).toBe('');
f.stack.execute(new SetAssetPropertyCommand(text.asset.id, FONT_KEY, f.font.id));
expect(fontFileOf(f.project, fontOf(text.asset.props))).toBe(FILE);
expect(fontFileOf(f.project, 'no-such-asset')).toBe('');
// A text asset is not a font asset: an id pointing at the wrong type wears nothing.
expect(fontFileOf(f.project, text.asset.id)).toBe('');
});
});