import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { pathToFileURL } from "node:url"; import test from "node:test"; import ts from "typescript"; const repoRoot = path.resolve(import.meta.dirname, "../../.."); const sourcePath = path.join(repoRoot, "web/protocol/grease-pencil-marquee.ts"); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "grease-pencil-marquee-unit-")); const transpiled = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: sourcePath, reportDiagnostics: true, }); assert.deepEqual(transpiled.diagnostics, []); const modulePath = path.join(temporary, "grease-pencil-marquee.mjs"); fs.writeFileSync(modulePath, transpiled.outputText); const marquee = await import(pathToFileURL(modulePath)); const drawing = { dataId: "grease-pencil:Data", layerId: "grease-pencil-layer:Data:Lines", frame: 12, drawingId: "grease-pencil-drawing:Data:4", }; function point(strokeIndex, pointIndex, viewportPosition) { return { ...drawing, strokeId: `grease-pencil-stroke:Data:4:${strokeIndex}`, pointId: `grease-pencil-point:Data:4:${strokeIndex}:${pointIndex}`, strokeIndex, pointIndex, viewportPosition, }; } function request(candidates) { return { schemaVersion: 1, baseRevision: 27, baseSelectionRevision: 4, drawing, box: { left: 0.2, top: 0.2, right: 0.8, bottom: 0.8 }, candidates, }; } test("M9-06 selects stable point and stroke IDs only inside the current drawing marquee", () => { const result = marquee.selectGreasePencilMarquee(request([ point(1, 1, [0.5, 0.5]), point(0, 2, [0.8, 0.2]), point(0, 0, [0.1, 0.5]), ]), 27); assert.deepEqual(result.drawing, drawing); assert.equal(result.baseSelectionRevision, 4); assert.deepEqual(result.selectedStrokeIds, [ "grease-pencil-stroke:Data:4:0", "grease-pencil-stroke:Data:4:1", ]); assert.deepEqual(result.selectedPoints.map(({ pointId, strokeId, strokeIndex, pointIndex }) => ({ pointId, strokeId, strokeIndex, pointIndex })), [ { pointId: "grease-pencil-point:Data:4:0:2", strokeId: "grease-pencil-stroke:Data:4:0", strokeIndex: 0, pointIndex: 2 }, { pointId: "grease-pencil-point:Data:4:1:1", strokeId: "grease-pencil-stroke:Data:4:1", strokeIndex: 1, pointIndex: 1 }, ]); }); test("M9-06 rejects stale, foreign-drawing, duplicate and forged marquee candidates", () => { assert.throws(() => marquee.selectGreasePencilMarquee(request([point(0, 0, [0.5, 0.5])]), 28), (error) => error.code === "REVISION_CONFLICT"); assert.throws(() => marquee.selectGreasePencilMarquee(request([{ ...point(0, 0, [0.5, 0.5]), drawingId: "grease-pencil-drawing:Data:5" }]), 27), (error) => error.code === "GREASE_PENCIL_SELECTION_SCOPE_INVALID"); const duplicate = point(0, 0, [0.5, 0.5]); assert.throws(() => marquee.selectGreasePencilMarquee(request([duplicate, { ...duplicate }]), 27), (error) => error.code === "GREASE_PENCIL_SELECTION_INVALID"); assert.throws(() => marquee.selectGreasePencilMarquee(request([{ ...point(0, 0, [0.5, 0.5]), pointId: "point:0" }]), 27), (error) => error.code === "GREASE_PENCIL_SELECTION_INVALID"); assert.throws(() => marquee.selectGreasePencilMarquee({ ...request([]), box: { left: 0.8, top: 0.2, right: 0.2, bottom: 0.8 } }, 27), (error) => error.code === "GREASE_PENCIL_SELECTION_INVALID"); }); test("M9-06 rejects a marquee candidate list over the bounded drawing point budget", () => { const candidates = new Array(marquee.GREASE_PENCIL_MARQUEE_BUDGET.maxCandidates + 1).fill(null); assert.throws(() => marquee.selectGreasePencilMarquee(request(candidates), 27), (error) => error.code === "GREASE_PENCIL_BUDGET_EXCEEDED"); });