Recurring cleanup round over the code since round 7 (bc688f3). The
issue now also asks to fix bugs and small inconsistencies found on the
way, so unlike the rounds before, this one changes behaviour in a few
places. Duplicated code in api went from 23 clones with 228 lines to
none (jscpd, at least 50 tokens). In src it stays at 22 clones with
271 lines, the same before and after.
Bugs:
- nginx served project and snapshot files as they were, so anyone who
knew a project id could read it without signing in. Every file the
API writes now goes through AtomicFile (temp file, mode, rename).
Projects, snapshots, releases, accounts and sessions are 0640, and
nginx answers 403 for them.
- Any signed-in account could delete a shared image, font or sound,
which projects of other accounts may wear. Deleting one is now for
administrators only. The editor never deleted a file, so the
client's delete is gone.
- Two saves of one project could run at once. The lock file was
removed after each save, so a waiting save and the next one locked
different files. The lock file now stays, and deleting a project
takes the lock as well.
- Bytes that are no picture got a 500 instead of a 422, because the
error handler also threw for warnings silenced with @.
- A session file was rewritten in place. A request reading it at that
moment found it empty and was signed out. It is replaced atomically.
- Delete did nothing for a shape picked in the vector art list. Delete
now goes through removeSelection, one case per kind of selection,
and a kind without a case fails the type check.
Inconsistencies:
- Escape on a question does what its Cancel does. Cancel on "Delete
project?" and "Restore snapshot?" leads back to the picker or the
list like the other questions did, and so does deleting a project.
- Editor texts use American spelling throughout, like the port names:
48 British spellings in 45 lines (colour, centre, initialised).
- objectName and groupName replace eleven copies of "own name, else
the asset's" and three of "Group". One copy fell back to the object
id instead of "?".
Duplicates, dead code, comments:
- routeFiles serves images, fonts and sounds instead of three copies.
HashedFileStore holds what the three stores shared: keeping a file
under its hash with its name, listing and deleting. The stores keep
only which bytes they take and what a record says.
- AtomicFile replaces nine copies of temp file plus rename.
- removeShapes is the one way the canvas, the inspector button and
Delete take shapes out.
- GraphInterpreter.fire was called by tests only. The 54 calls use a
helper in tests/helpers.ts now.
- FileStore.delete, HttpFileStore.delete and the PluginApi type were
unused. A doc comment in actions.ts sat above the wrong function.
Checked and left as is:
- solidCells is used by tests only. It is the per-cell view before the
merge into rectangles, the one place the largest-scale rule shows.
- An undo checker ran all 124 commands of the unit tests and compared
the project before and after undo. The only differences are empty
ports, groups and notes lists where there were none, which the file
format drops anyway.
Docs:
- README: the check sum.
- Wiki, own commit: API (admin-only deletes, 422, file modes, lock
file), Home (the "Not yet" list named three things that exist),
Concepts, Keyboard, Vector art, Architecture and Development, and
the 38 characters Gitea flagged as ambiguous on eight pages.
- .gitignore: the lock files, and pasted-*.png in any folder, which
keeps the screenshot archive out.
Tests:
- ./check.sh: 195 files, 2817 tests, exit 0 (+2 for the name helpers).
- PHP: accounts 52 in the web container and 53 in the CI image with
the race test (+2), snapshots 15 (+1), releases 20.
- Full e2e run: ALL PASSED, 1586 checks (+6 in 12, 47, 49 and 50).
- Smokes jumprun, tetris, actor: 24, 19 and 21 checks. The example
project files were restored byte for byte afterwards.
- The file stores of HEAD and of this commit give byte-identical
output for the live folders and for store, list and delete in a
throwaway folder.
- Counter-checks, all fail as expected: a 500 for bad bytes fails 12,
no admin check fails 49, removing the lock file again loses saves
in 3 of 3 runs (151 to 170 of 321), Delete ignoring shapes fails 47,
Escape that only closes fails 50, and dropping the shape case fails
the type check (TS2366).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
228 lines
9.9 KiB
TypeScript
228 lines
9.9 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import { CreateAssetCommand } from '../../src/core/commands/assetCommands';
|
||
import { CreateContainerCommand } from '../../src/core/commands/containerCommands';
|
||
import {
|
||
GroupObjectsCommand,
|
||
RenameGroupCommand,
|
||
SetObjectGroupCommand,
|
||
UngroupCommand,
|
||
} from '../../src/core/commands/groupCommands';
|
||
import { PasteObjectsCommand } from '../../src/core/commands/pasteCommands';
|
||
import { PlaceAssetCommand, SetParentCommand } from '../../src/core/commands/sceneObjectCommands';
|
||
import type { ContainerAsset } from '../../src/core/model/Asset';
|
||
import { copyObjects } from '../../src/core/model/clipboard';
|
||
import {
|
||
canGroup,
|
||
findObject,
|
||
groupMemberIds,
|
||
groupRowId,
|
||
hierarchyRows,
|
||
treeOrder,
|
||
} from '../../src/core/model/hierarchy';
|
||
import { instanceChildId, resolvedHolder } from '../../src/core/model/instance';
|
||
import { findAsset, findScene } from '../../src/core/model/Project';
|
||
import { groupName, objectName } from '../../src/core/model/SceneObject';
|
||
import { serializeProject } from '../../src/core/store/serialize';
|
||
import { makeSpriteScene, place, roundTrip } from '../helpers';
|
||
|
||
/** Five top-level boxes a–e in that stored order; `c` has a child `k`. */
|
||
function fiveBoxes() {
|
||
const f = makeSpriteScene();
|
||
const [a, b, c, d, e] = [0, 1, 2, 3, 4].map((i) => place(f, f.spriteId, { x: i * 10, y: 0 }));
|
||
const k = place(f, f.spriteId, { x: 20, y: 10 }, c);
|
||
return { ...f, a: a!, b: b!, c: c!, d: d!, e: e!, k };
|
||
}
|
||
|
||
const ids = (entries: { object: { id: string } }[]) => entries.map((entry) => entry.object.id);
|
||
|
||
describe('hierarchy groups (#241)', () => {
|
||
it('lists a group as one block where its first member stands, and draws equal z in that order', () => {
|
||
const { stack, sceneId, sceneOf, a, b, c, d, e, k } = fiveBoxes();
|
||
stack.execute(new GroupObjectsCommand(sceneId, [b, d], 'Crates'));
|
||
// d moves up behind b; the stored list is not touched.
|
||
expect(ids(treeOrder(sceneOf()))).toEqual([a, b, d, c, k, e]);
|
||
expect(sceneOf().objects.map((o) => o.id)).toEqual([a, b, c, d, e, k]);
|
||
});
|
||
|
||
it('shows a row over the members, indents them and folds them away', () => {
|
||
const { stack, sceneId, sceneOf, a, b, c, d, e, k } = fiveBoxes();
|
||
const group = new GroupObjectsCommand(sceneId, [c, e], 'Crates');
|
||
stack.execute(group);
|
||
const row = groupRowId(group.groupId);
|
||
const rows = (expanded: string[]) =>
|
||
hierarchyRows(sceneOf(), new Set(expanded)).map((r) =>
|
||
r.kind === 'group' ? [r.group.name, r.memberIds.length, r.depth] : [r.object.id, r.depth],
|
||
);
|
||
// Folded, the way a freshly opened scene starts.
|
||
expect(rows([])).toEqual([
|
||
[a, 0],
|
||
[b, 0],
|
||
['Crates', 2, 0],
|
||
[d, 0],
|
||
]);
|
||
// Open: members one step in, the child of a member one more.
|
||
expect(rows([row, c])).toEqual([
|
||
[a, 0],
|
||
[b, 0],
|
||
['Crates', 2, 0],
|
||
[c, 1],
|
||
[k, 2],
|
||
[e, 1],
|
||
[d, 0],
|
||
]);
|
||
expect(rows([row])).toEqual([
|
||
[a, 0],
|
||
[b, 0],
|
||
['Crates', 2, 0],
|
||
[c, 1],
|
||
[e, 1],
|
||
[d, 0],
|
||
]);
|
||
});
|
||
|
||
it('only groups top-level objects', () => {
|
||
const { stack, sceneId, sceneOf, a, k } = fiveBoxes();
|
||
expect(canGroup(sceneOf(), [a])).toBe(true);
|
||
expect(canGroup(sceneOf(), [a, k])).toBe(false);
|
||
expect(() => stack.execute(new GroupObjectsCommand(sceneId, [a, k], 'G'))).toThrow(/top-level/);
|
||
expect(sceneOf().groups).toBeUndefined();
|
||
expect(findObject(sceneOf(), a)!.groupId).toBeUndefined();
|
||
});
|
||
|
||
it('takes an object out of its group when it goes under a parent, and back on undo', () => {
|
||
const { stack, sceneId, sceneOf, a, b, c } = fiveBoxes();
|
||
const group = new GroupObjectsCommand(sceneId, [b, c], 'G');
|
||
stack.execute(group);
|
||
stack.execute(new SetParentCommand(sceneId, b, a));
|
||
expect(findObject(sceneOf(), b)!.groupId).toBeUndefined();
|
||
expect(groupMemberIds(sceneOf(), group.groupId)).toEqual([c]);
|
||
stack.undo();
|
||
expect(groupMemberIds(sceneOf(), group.groupId)).toEqual([b, c]);
|
||
});
|
||
|
||
it('renames, ungroups and moves members in and out, each one undo step', () => {
|
||
const { stack, sceneId, sceneOf, a, b, c } = fiveBoxes();
|
||
const group = new GroupObjectsCommand(sceneId, [a, b], 'Group');
|
||
stack.execute(group);
|
||
stack.execute(new RenameGroupCommand(sceneId, group.groupId, 'Crates'));
|
||
stack.execute(new SetObjectGroupCommand(sceneId, [c], group.groupId));
|
||
stack.execute(new SetObjectGroupCommand(sceneId, [a], ''));
|
||
expect(sceneOf().groups).toEqual([{ id: group.groupId, name: 'Crates' }]);
|
||
expect(groupMemberIds(sceneOf(), group.groupId)).toEqual([b, c]);
|
||
|
||
stack.execute(new UngroupCommand(sceneId, group.groupId));
|
||
expect(sceneOf().groups).toBeUndefined();
|
||
expect(sceneOf().objects.some((o) => o.groupId)).toBe(false);
|
||
|
||
stack.undo();
|
||
expect(groupMemberIds(sceneOf(), group.groupId)).toEqual([b, c]);
|
||
stack.undo();
|
||
stack.undo();
|
||
expect(groupMemberIds(sceneOf(), group.groupId)).toEqual([a, b]);
|
||
stack.undo();
|
||
expect(sceneOf().groups).toEqual([{ id: group.groupId, name: 'Group' }]);
|
||
stack.undo();
|
||
expect(sceneOf().groups).toBeUndefined();
|
||
expect(sceneOf().objects.some((o) => o.groupId !== undefined)).toBe(false);
|
||
});
|
||
|
||
it('keeps groups across saving and drops what no longer holds together', () => {
|
||
const f = fiveBoxes();
|
||
const { project, stack, sceneId, a, b, c, k } = f;
|
||
const kept = new GroupObjectsCommand(sceneId, [a, c], 'Crates');
|
||
const emptied = new GroupObjectsCommand(sceneId, [b], 'Gone');
|
||
stack.execute(kept);
|
||
stack.execute(emptied);
|
||
stack.execute(new SetObjectGroupCommand(sceneId, [b], ''));
|
||
// A group id on a child and one naming no group, as a hand-edited file could have them.
|
||
const raw = serializeProject(project) as {
|
||
assets: { id: string; objects?: { id: string; groupId?: string }[] }[];
|
||
};
|
||
const scene = raw.assets.find((asset) => asset.id === sceneId)!;
|
||
scene.objects!.find((o) => o.id === k)!.groupId = kept.groupId;
|
||
scene.objects!.find((o) => o.id === b)!.groupId = 'nowhere';
|
||
|
||
const loaded = findScene(roundTrip(raw as never), sceneId)!;
|
||
expect(loaded.groups).toEqual([{ id: kept.groupId, name: 'Crates' }]);
|
||
expect(groupMemberIds(loaded, kept.groupId)).toEqual([a, c]);
|
||
expect(findObject(loaded, k)!.groupId).toBeUndefined();
|
||
expect(findObject(loaded, b)!.groupId).toBeUndefined();
|
||
});
|
||
|
||
it('loads a project from before groups unchanged', () => {
|
||
const { project, sceneId } = fiveBoxes();
|
||
const loaded = roundTrip(project);
|
||
expect(JSON.stringify(serializeProject(loaded))).not.toMatch(/group/i);
|
||
expect(findScene(loaded, sceneId)!.groups).toBeUndefined();
|
||
expect(treeOrder(findScene(loaded, sceneId)!).map((e) => e.object.id)).toEqual(
|
||
treeOrder(findScene(project, sceneId)!).map((e) => e.object.id),
|
||
);
|
||
});
|
||
|
||
it('pastes a copy into the group only at the top level of a holder that has the group', () => {
|
||
const { project, stack, sceneId, sceneOf, a, b } = fiveBoxes();
|
||
const group = new GroupObjectsCommand(sceneId, [a], 'Crates');
|
||
stack.execute(group);
|
||
const clip = copyObjects(project, sceneOf(), [a])!;
|
||
|
||
const beside = new PasteObjectsCommand(sceneId, clip, {}, { x: 5, y: 0 });
|
||
stack.execute(beside);
|
||
expect(groupMemberIds(sceneOf(), group.groupId)).toEqual([a, beside.rootIds[0]]);
|
||
|
||
const under = new PasteObjectsCommand(sceneId, clip, { [a]: b }, { x: 5, y: 0 });
|
||
stack.execute(under);
|
||
expect(findObject(sceneOf(), under.rootIds[0]!)!.groupId).toBeUndefined();
|
||
|
||
const other = new CreateAssetCommand('scene', 'Other');
|
||
stack.execute(other);
|
||
const elsewhere = new PasteObjectsCommand(other.asset.id, clip, {}, { x: 0, y: 0 });
|
||
stack.execute(elsewhere);
|
||
expect(findObject(findScene(project, other.asset.id)!, elsewhere.rootIds[0]!)!.groupId).toBeUndefined();
|
||
});
|
||
|
||
it('puts a container made of one group in its place and leaves the template without groups', () => {
|
||
const { project, stack, sceneId, sceneOf, a, b, c } = fiveBoxes();
|
||
const group = new GroupObjectsCommand(sceneId, [a, b, c], 'Crates');
|
||
stack.execute(group);
|
||
const create = new CreateContainerCommand(sceneId, [a, b]);
|
||
stack.execute(create);
|
||
expect(groupMemberIds(sceneOf(), group.groupId)).toEqual([c, create.objectId]);
|
||
const template = findAsset(project, create.assetId) as ContainerAsset;
|
||
expect(template.objects.some((o) => o.groupId !== undefined)).toBe(false);
|
||
});
|
||
|
||
it('orders the content of a placed container the way its template lists it', () => {
|
||
const f = makeSpriteScene();
|
||
const container = new CreateAssetCommand('container', 'Crates');
|
||
f.stack.execute(container);
|
||
const holderId = container.asset.id;
|
||
const [x, y, z] = [0, 1, 2].map((i) => {
|
||
const put = new PlaceAssetCommand(holderId, f.spriteId, { x: i * 10, y: 0 });
|
||
f.stack.execute(put);
|
||
return put.objectId;
|
||
});
|
||
f.stack.execute(new GroupObjectsCommand(holderId, [x!, z!], 'Pair'));
|
||
const template = findAsset(f.project, holderId) as ContainerAsset;
|
||
expect(ids(treeOrder(template))).toEqual([x, z, y]);
|
||
|
||
const instance = place(f, holderId, { x: 0, y: 0 });
|
||
const content = treeOrder(resolvedHolder(f.project, f.sceneOf()))
|
||
.filter((entry) => entry.object.parentId === instance)
|
||
.map((entry) => entry.object.id);
|
||
expect(content).toEqual([x, z, y].map((id) => instanceChildId(instance, id!)));
|
||
});
|
||
});
|
||
|
||
describe('the names the lists show', () => {
|
||
it('call an object by its own name, else by its asset, and "?" once the asset is gone', () => {
|
||
expect(objectName({ name: 'Hero' }, { name: 'Sprite' })).toBe('Hero');
|
||
expect(objectName({ name: '' }, { name: 'Sprite' })).toBe('Sprite');
|
||
expect(objectName({ name: '' }, undefined)).toBe('?');
|
||
});
|
||
|
||
it('call a group that was never named a "Group"', () => {
|
||
expect(groupName({ id: 'g', name: '' })).toBe('Group');
|
||
expect(groupName({ id: 'g', name: 'Crates' })).toBe('Crates');
|
||
});
|
||
});
|