Planners can now drop annotation cards on the Tech Tree canvas without inventing fake technologies. Notes live in their own tech-tree-notes collection (title, body, treePosition) and carry no prerequisites, costs, approval workflow or research state, so the layout engine, edge builder and research rules never see them. showOnFrontend defaults to false, making notes a backend authoring aid; ticking it publishes the amber card read-only to the member tech tree, which queries showOnFrontend = true. Canvas work: amber NoteNode card with no connection handles, NoteDialog for create/edit/delete, a New Note toolbar button (technologies:create gated), viewport-centre placement, and namespaced note-<id> React Flow ids so note and technology ids can never collide. Note drags, Save positions and Auto-arrange reuse the single-statement bulk /api/tech-tree/positions write by sending collection: "tech-tree-notes"; no PATCH-per-note path was added. Also fixes the note card body being clipped by the inherited line-height: the classification strip and footer now have fixed heights with leading-none, and the body takes the remaining flex space instead of line-clamp. Tests: tech-tree-notes.int.spec.ts (defaults, required title, publication filter, in-place edits, note layout and id namespacing) plus note coverage in tech-tree-positions.int.spec.ts. Migration 20261123_073019_add_tech_tree_notes must be run on prod after deploy.
182 lines
5 KiB
TypeScript
182 lines
5 KiB
TypeScript
import { getTestPayload } from "../helpers/get-test-payload";
|
|
import type { Payload } from "payload";
|
|
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
computeNoteLayout,
|
|
noteNodeId,
|
|
parseNoteNodeId,
|
|
} from "@/components/admin/tech-tree/notes";
|
|
import { COLUMN_STEP, ROW_STEP } from "@/components/admin/tech-tree/types";
|
|
|
|
const RUN = `ttnote-${Date.now().toString(36)}`;
|
|
const TIMEOUT = 30_000;
|
|
|
|
describe("tech tree note node layout (pure)", () => {
|
|
const note = (
|
|
id: number,
|
|
treePosition: { x: number; y: number } | null = null,
|
|
): Parameters<typeof computeNoteLayout>[0][number] => ({
|
|
id,
|
|
title: `note ${id}`,
|
|
body: "",
|
|
showOnFrontend: false,
|
|
treePosition,
|
|
updatedAt: "",
|
|
});
|
|
|
|
it("parks untouched notes in a column one step right of the tree", () => {
|
|
const layout = computeNoteLayout([note(1), note(2)], { columns: 3 });
|
|
expect(layout).toEqual([
|
|
{ noteId: 1, x: 3 * COLUMN_STEP, y: 0 },
|
|
{ noteId: 2, x: 3 * COLUMN_STEP, y: ROW_STEP },
|
|
]);
|
|
});
|
|
|
|
it("uses dragged positions without shifting the automatic stack", () => {
|
|
const layout = computeNoteLayout(
|
|
[note(1, { x: 40, y: 400 }), note(2), note(3)],
|
|
{ columns: 2 },
|
|
);
|
|
expect(layout).toEqual([
|
|
{ noteId: 1, x: 40, y: 400 },
|
|
{ noteId: 2, x: 2 * COLUMN_STEP, y: 0 },
|
|
{ noteId: 3, x: 2 * COLUMN_STEP, y: ROW_STEP },
|
|
]);
|
|
});
|
|
|
|
it("starts at column 0 when the tree has no technologies", () => {
|
|
expect(computeNoteLayout([note(9)], { columns: 0 })).toEqual([
|
|
{ noteId: 9, x: 0, y: 0 },
|
|
]);
|
|
});
|
|
|
|
it("note node ids can never collide with technology ids", () => {
|
|
expect(noteNodeId(4)).toBe("note-4");
|
|
expect(parseNoteNodeId("note-4")).toBe(4);
|
|
expect(parseNoteNodeId("4")).toBeNull();
|
|
expect(parseNoteNodeId("note-x")).toBeNull();
|
|
expect(parseNoteNodeId("note-0")).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("tech-tree-notes collection", () => {
|
|
let payload: Payload;
|
|
const ids: number[] = [];
|
|
|
|
beforeAll(async () => {
|
|
payload = await getTestPayload();
|
|
}, TIMEOUT);
|
|
|
|
afterAll(async () => {
|
|
for (const id of ids) {
|
|
await payload
|
|
.delete({ collection: "tech-tree-notes", id, overrideAccess: true })
|
|
.catch(() => undefined);
|
|
}
|
|
}, TIMEOUT);
|
|
|
|
it(
|
|
"defaults to a backend-only note with no canvas position",
|
|
async () => {
|
|
const created = await payload.create({
|
|
collection: "tech-tree-notes",
|
|
data: { title: `${RUN}-default` },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
ids.push(created.id);
|
|
|
|
expect(created.showOnFrontend).toBe(false);
|
|
expect(created.body ?? null).toBeNull();
|
|
expect(created.treePosition?.x ?? null).toBeNull();
|
|
expect(created.treePosition?.y ?? null).toBeNull();
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it(
|
|
"requires a title",
|
|
async () => {
|
|
await expect(
|
|
payload.create({
|
|
collection: "tech-tree-notes",
|
|
// Deliberately missing the required title.
|
|
data: {} as never,
|
|
overrideAccess: true,
|
|
}),
|
|
).rejects.toThrow();
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it(
|
|
"publishes only the notes flagged for the frontend",
|
|
async () => {
|
|
const hidden = await payload.create({
|
|
collection: "tech-tree-notes",
|
|
data: { title: `${RUN}-hidden`, showOnFrontend: false },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
const shown = await payload.create({
|
|
collection: "tech-tree-notes",
|
|
data: { title: `${RUN}-shown`, showOnFrontend: true },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
ids.push(hidden.id, shown.id);
|
|
|
|
const published = await payload.find({
|
|
collection: "tech-tree-notes",
|
|
where: {
|
|
and: [
|
|
{ title: { like: RUN } },
|
|
// The member-facing page's exact publication gate.
|
|
{ showOnFrontend: { equals: true } },
|
|
],
|
|
},
|
|
depth: 0,
|
|
limit: 0,
|
|
overrideAccess: true,
|
|
});
|
|
|
|
const publishedIds = published.docs.map((doc) => doc.id);
|
|
expect(publishedIds).toContain(shown.id);
|
|
expect(publishedIds).not.toContain(hidden.id);
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it(
|
|
"keeps body line breaks and updates in place",
|
|
async () => {
|
|
const created = await payload.create({
|
|
collection: "tech-tree-notes",
|
|
data: {
|
|
title: `${RUN}-edit`,
|
|
body: "line one\nline two",
|
|
showOnFrontend: true,
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
ids.push(created.id);
|
|
|
|
const updated = await payload.update({
|
|
collection: "tech-tree-notes",
|
|
id: created.id,
|
|
data: { showOnFrontend: false, title: `${RUN}-edit-renamed` },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
|
|
expect(updated.showOnFrontend).toBe(false);
|
|
expect(updated.title).toBe(`${RUN}-edit-renamed`);
|
|
// An unrelated field update must not drop the body.
|
|
expect(updated.body).toBe("line one\nline two");
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
});
|