"use strict"; // Unit tests for utils/planner-reorder.js — the pure decision cores of the // planner board's two concurrency fixes: // // 1. computeRelativeReorder: the relative-move protocol that replaced the // absolute id-list reorder. The server order is the source of truth; the // dragged item is spliced between the anchors the client names, so items // a stale client never saw keep their position instead of being silently // demoted to last priority. // 2. plannerUpdatedAtMatches: strict epoch-ms equality backing the Compose // form's optimistic-concurrency precondition (409 on concurrent edit). // // Run: node --test utils/planner-reorder.test.js const test = require("node:test"); const assert = require("node:assert"); const { computeRelativeReorder, plannerUpdatedAtMatches, } = require("./planner-reorder"); // ─── computeRelativeReorder ────────────────────────────────────────────────── test("moves a middle item to the top (aboveId null, belowId = current first)", () => { const result = computeRelativeReorder([1, 2, 3, 4], 3, null, 1); assert.deepStrictEqual(result, { ok: true, order: [3, 1, 2, 4] }); }); test("moves a middle item to the bottom (aboveId = current last, belowId null)", () => { const result = computeRelativeReorder([1, 2, 3, 4], 2, 4, null); assert.deepStrictEqual(result, { ok: true, order: [1, 3, 4, 2] }); }); test("moves the top item into the middle (after aboveId, before belowId)", () => { const result = computeRelativeReorder([1, 2, 3, 4], 1, 3, 4); assert.deepStrictEqual(result, { ok: true, order: [2, 3, 1, 4] }); }); test("moves the bottom item to the top", () => { const result = computeRelativeReorder([1, 2, 3], 3, null, 1); assert.deepStrictEqual(result, { ok: true, order: [3, 1, 2] }); }); test("single-item board with both anchors null is a no-op success", () => { const result = computeRelativeReorder([7], 7, null, null); assert.deepStrictEqual(result, { ok: true, order: [7] }); }); test("both anchors null with more than one active item is invalid_neighbors", () => { const result = computeRelativeReorder([1, 2, 3], 2, null, null); assert.deepStrictEqual(result, { ok: false, code: "invalid_neighbors" }); }); test("itemId absent from the current order is missing_item", () => { assert.deepStrictEqual(computeRelativeReorder([1, 2, 3], 9, 1, null), { ok: false, code: "missing_item", }); assert.deepStrictEqual(computeRelativeReorder([], 1, null, null), { ok: false, code: "missing_item", }); }); test("itemId equal to an anchor is invalid_neighbors", () => { assert.deepStrictEqual(computeRelativeReorder([1, 2, 3], 2, 2, null), { ok: false, code: "invalid_neighbors", }); assert.deepStrictEqual(computeRelativeReorder([1, 2, 3], 2, null, 2), { ok: false, code: "invalid_neighbors", }); }); test("anchor id no longer in the current order is anchor_missing", () => { // A teammate completed/deleted the anchor between render and drop: 409, the // client reloads instead of guessing. assert.deepStrictEqual(computeRelativeReorder([1, 2, 3], 3, 99, null), { ok: false, code: "anchor_missing", }); assert.deepStrictEqual(computeRelativeReorder([1, 2, 3], 1, null, 99), { ok: false, code: "anchor_missing", }); }); test("when both anchors are provided, aboveId wins and belowId is informational", () => { // Insert after 1 would be before 2, contradicting belowId=2 — aboveId wins. const result = computeRelativeReorder([1, 2, 3, 4], 4, 1, 2); assert.deepStrictEqual(result, { ok: true, order: [1, 4, 2, 3] }); }); test("inputs are never mutated", () => { const currentIds = [1, 2, 3, 4]; const snapshot = [...currentIds]; computeRelativeReorder(currentIds, 2, 4, null); assert.deepStrictEqual(currentIds, snapshot); }); test("a non-finite itemId is missing_item, not a crash", () => { assert.deepStrictEqual(computeRelativeReorder([1, 2, 3], NaN, 1, null), { ok: false, code: "missing_item", }); }); test("the result order is always a permutation of the input with length preserved", () => { const currentIds = [5, 10, 15, 20, 25]; for (const itemId of currentIds) { for (const aboveId of [null, ...currentIds]) { for (const belowId of [null, ...currentIds]) { const result = computeRelativeReorder(currentIds, itemId, aboveId, belowId); if (!result.ok) continue; assert.strictEqual(result.order.length, currentIds.length, `length for ${itemId}/${aboveId}/${belowId}`); assert.deepStrictEqual([...result.order].sort((a, b) => a - b), [...currentIds].sort((a, b) => a - b), `permutation for ${itemId}/${aboveId}/${belowId}`); } } } }); // ─── plannerUpdatedAtMatches ───────────────────────────────────────────────── test("a mysql2 Date row value matches the ISO string the client echoed", () => { const rowValue = new Date("2026-05-04T10:00:00Z"); assert.strictEqual(plannerUpdatedAtMatches(rowValue, "2026-05-04T10:00:00.000Z"), true); }); test("an ISO string row value matches an equivalent client string", () => { // ISO form on both sides: parsing a space-separated "YYYY-MM-DD HH:MM:SS" // string is implementation-defined (local time), so it must not be compared // against a UTC ISO string without pinning TZ. assert.strictEqual(plannerUpdatedAtMatches("2026-05-04T10:00:00.000Z", "2026-05-04T10:00:00.000Z"), true); }); test("equality is strict epoch-ms: a 1ms mismatch is a mismatch", () => { const rowValue = new Date("2026-05-04T10:00:00.000Z"); assert.strictEqual(plannerUpdatedAtMatches(rowValue, "2026-05-04T10:00:00.001Z"), false); }); test("null, undefined, NaN and garbage are mismatches on either side", () => { const good = new Date("2026-05-04T10:00:00Z"); const bad = [null, undefined, NaN, "not-a-date", "", {}, "2026-13-45T99:99:99Z"]; for (const value of bad) { assert.strictEqual(plannerUpdatedAtMatches(value, good), false, `row=${String(value)}`); assert.strictEqual(plannerUpdatedAtMatches(good, value), false, `expected=${String(value)}`); assert.strictEqual(plannerUpdatedAtMatches(value, value), false, `both=${String(value)}`); } }); test("a genuinely stale row value does not match (the precondition rejects)", () => { const rowNow = new Date("2026-05-04T11:30:00Z"); // teammate saved later const clientSaw = "2026-05-04T11:00:00.000Z"; // what the form loaded assert.strictEqual(plannerUpdatedAtMatches(rowNow, clientSaw), false); });