1
0
Fork 0
polaris-task-force/tests/int/tech-tree-positions.int.spec.ts
Z8MB1E 48556c3688 feat(tech-tree): add note nodes with optional frontend visibility
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.
2026-11-23 03:11:36 -05:00

253 lines
8.2 KiB
TypeScript

import { getTestPayload } from "../helpers/get-test-payload";
import { Payload } from "payload";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import {
MAX_POSITION_UPDATES,
parsePositionUpdates,
writeTreePositions,
} from "@/lib/tech-tree/positions";
const RUN = `ttpos-${Date.now().toString(36)}`;
const TIMEOUT = 30_000;
describe("tech tree position bulk-write validation", () => {
it("accepts pins and clears", () => {
const result = parsePositionUpdates({
positions: [
{ id: 1, x: 10.4, y: 20.6 },
{ id: 2, x: null, y: null },
],
});
expect(result).toEqual({
ok: true,
collection: "technologies",
updates: [
{ id: 1, x: 10, y: 21, expectedUpdatedAt: null },
{ id: 2, x: null, y: null, expectedUpdatedAt: null },
],
});
});
it("keeps the last write for a repeated id", () => {
const result = parsePositionUpdates({
positions: [
{ id: 3, x: 1, y: 1 },
{ id: 3, x: null, y: null },
],
});
expect(result).toEqual({
ok: true,
collection: "technologies",
updates: [{ id: 3, x: null, y: null, expectedUpdatedAt: null }],
});
});
it("carries and validates the optimistic concurrency token", () => {
const accepted = parsePositionUpdates({
positions: [{ id: 1, x: 5, y: 5, ifUnmodifiedAt: "2026-10-15T12:00:00.000Z" }],
});
expect(accepted).toEqual({
ok: true,
collection: "technologies",
updates: [{ id: 1, x: 5, y: 5, expectedUpdatedAt: "2026-10-15T12:00:00.000Z" }],
});
// Null means unconditional; malformed values are rejected.
expect(
parsePositionUpdates({ positions: [{ id: 1, x: 5, y: 5, ifUnmodifiedAt: null }] }),
).toEqual({
ok: true,
collection: "technologies",
updates: [{ id: 1, x: 5, y: 5, expectedUpdatedAt: null }],
});
expect(
parsePositionUpdates({ positions: [{ id: 1, x: 5, y: 5, ifUnmodifiedAt: "soon" }] }).ok,
).toBe(false);
expect(
parsePositionUpdates({ positions: [{ id: 1, x: 5, y: 5, ifUnmodifiedAt: 123 }] }).ok,
).toBe(false);
});
it("rejects malformed bodies", () => {
expect(parsePositionUpdates(null).ok).toBe(false);
expect(parsePositionUpdates({ positions: "nope" }).ok).toBe(false);
expect(parsePositionUpdates({ positions: [{ id: 0, x: 1, y: 1 }] }).ok).toBe(false);
expect(parsePositionUpdates({ positions: [{ id: 1.5, x: 1, y: 1 }] }).ok).toBe(false);
// Half-cleared and non-finite coordinates are both invalid.
expect(parsePositionUpdates({ positions: [{ id: 1, x: 5, y: null }] }).ok).toBe(false);
expect(parsePositionUpdates({ positions: [{ id: 1, x: Number.NaN, y: 1 }] }).ok).toBe(false);
});
it("routes the batch to the requested canvas collection", () => {
// Omitted collection keeps every existing caller on technologies.
expect(parsePositionUpdates({ positions: [{ id: 1, x: 1, y: 1 }] })).toMatchObject({
ok: true,
collection: "technologies",
});
expect(
parsePositionUpdates({ collection: "tech-tree-notes", positions: [{ id: 1, x: 1, y: 1 }] }),
).toMatchObject({ ok: true, collection: "tech-tree-notes" });
// Anything outside the whitelist is a validation error, not a fallback.
expect(
parsePositionUpdates({ collection: "users", positions: [{ id: 1, x: 1, y: 1 }] }).ok,
).toBe(false);
expect(parsePositionUpdates({ collection: 7, positions: [{ id: 1, x: 1, y: 1 }] }).ok).toBe(
false,
);
});
it("caps the batch size", () => {
const positions = Array.from({ length: MAX_POSITION_UPDATES + 1 }, (_, i) => ({
id: i + 1,
x: 0,
y: 0,
}));
expect(parsePositionUpdates({ positions }).ok).toBe(false);
});
});
describe("tech tree position bulk write (database)", () => {
let payload: Payload;
const ids: number[] = [];
beforeAll(async () => {
payload = await getTestPayload();
for (const label of ["a", "b", "c"]) {
const doc = await payload.create({
collection: "technologies",
data: {
name: `${RUN}-${label}`,
summary: "Bulk position write fixture.",
type: "upgrade",
approvalStatus: "in_progress",
researchCosts: { minimumResearchDuration: 1 },
},
overrideAccess: true,
depth: 0,
});
ids.push(doc.id);
}
}, TIMEOUT);
afterAll(async () => {
for (const id of ids) {
await payload.delete({ collection: "technologies", id, overrideAccess: true });
}
}, TIMEOUT);
const positionOf = async (id: number) => {
const doc = await payload.findByID({
collection: "technologies",
id,
overrideAccess: true,
depth: 0,
});
return doc.treePosition ?? null;
};
it(
"pins many technologies in one write and clears them again",
async () => {
const pinned = await writeTreePositions(payload, [
{ id: ids[0], x: 46, y: 92, expectedUpdatedAt: null },
{ id: ids[1], x: 368, y: 184, expectedUpdatedAt: null },
{ id: ids[2], x: 0, y: 0, expectedUpdatedAt: null },
]);
expect(pinned.updated).toBe(3);
expect(pinned.conflicts).toEqual([]);
expect(await positionOf(ids[0])).toMatchObject({ x: 46, y: 92 });
expect(await positionOf(ids[1])).toMatchObject({ x: 368, y: 184 });
expect(await positionOf(ids[2])).toMatchObject({ x: 0, y: 0 });
// Auto-arrange path: clear every pin in a single statement.
const cleared = await writeTreePositions(
payload,
ids.map((id) => ({ id, x: null, y: null, expectedUpdatedAt: null })),
);
expect(cleared.updated).toBe(3);
for (const id of ids) {
const position = await positionOf(id);
expect(position?.x ?? null).toBeNull();
expect(position?.y ?? null).toBeNull();
}
},
TIMEOUT,
);
it(
"writes note-node positions through the same bulk path",
async () => {
const note = await payload.create({
collection: "tech-tree-notes",
data: { title: `${RUN}-note` },
overrideAccess: true,
depth: 0,
});
try {
// A fresh note starts unpinned and unpublished.
expect(note.treePosition?.x ?? null).toBeNull();
expect(note.showOnFrontend).toBe(false);
const pinned = await writeTreePositions(
payload,
[{ id: note.id, x: 46, y: 92, expectedUpdatedAt: null }],
"tech-tree-notes",
);
expect(pinned.updated).toBe(1);
expect(pinned.conflicts).toEqual([]);
const afterPin = await payload.findByID({
collection: "tech-tree-notes",
id: note.id,
overrideAccess: true,
depth: 0,
});
expect(afterPin.treePosition).toMatchObject({ x: 46, y: 92 });
// A stale token from before the pin conflicts instead of overwriting.
const stale = await writeTreePositions(
payload,
[{ id: note.id, x: 0, y: 0, expectedUpdatedAt: note.updatedAt }],
"tech-tree-notes",
);
expect(stale.updated).toBe(0);
expect(stale.conflicts).toContainEqual({
id: note.id,
currentUpdatedAt: expect.any(String),
});
// Auto-arrange clears note pins exactly like technology pins.
const cleared = await writeTreePositions(
payload,
[{ id: note.id, x: null, y: null, expectedUpdatedAt: null }],
"tech-tree-notes",
);
expect(cleared.updated).toBe(1);
const afterClear = await payload.findByID({
collection: "tech-tree-notes",
id: note.id,
overrideAccess: true,
depth: 0,
});
expect(afterClear.treePosition?.x ?? null).toBeNull();
expect(afterClear.treePosition?.y ?? null).toBeNull();
} finally {
await payload.delete({ collection: "tech-tree-notes", id: note.id, overrideAccess: true });
}
},
TIMEOUT,
);
it(
"ignores ids that do not exist",
async () => {
const touched = await writeTreePositions(payload, [
{ id: 999_999_999, x: 1, y: 1, expectedUpdatedAt: null },
]);
expect(touched.updated).toBe(0);
expect(touched.conflicts).toEqual([]);
},
TIMEOUT,
);
});