repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/canvas/__serial__/layout.test.ts
Rawimport { describe, expect, it } from "vitest";
import { createCanvasLayout } from "../src/layout.ts";
import { createDraw, emptyState } from "../src/store.ts";
const rectangle = (id: string) => ({
id,
type: "rectangle",
x: 10,
y: 20,
width: 30,
height: 40,
});
describe.runIf(process.env["PI_CANVAS_NATIVE_TESTS"] === "1")(
"canvas native layout",
() => {
it("uses Chromium for text, Mermaid, and relabel measurement while direct moves need no worker", async () => {
const packaged: typeof import("../server-test.ts") = await import(
new URL("../../../dist/canvas/server-test.ts", import.meta.url).href
);
const server = await packaged.startCanvasServer(
emptyState,
() => undefined,
);
const layout = createCanvasLayout(server.url);
try {
const state = {
...emptyState(),
elements: [
{
...rectangle("text"),
type: "text",
text: "old",
originalText: "old",
fontSize: 20,
fontFamily: 1,
textAlign: "center",
verticalAlign: "middle",
containerId: null,
autoResize: true,
baseline: 18,
},
],
};
const moved = createDraw(state, {
baseRevision: 0,
title: "move",
moves: [{ ids: ["text"], dx: 5, dy: -3 }],
});
expect((await layout.generate(state, moved))[0]).toMatchObject({
x: 15,
y: 17,
});
expect(layout.diagnostics().launches).toBe(0);
const relabel = createDraw(state, {
baseRevision: 0,
title: "relabel",
relabels: [{ id: "text", text: "centered text" }],
});
const relabeled = await layout.generate(state, relabel);
expect(relabeled[0]).toMatchObject({
id: "text",
text: "centered text",
});
// fontFamily 5 is Excalifont, the default canvas typeface.
const excalifont = createDraw(state, {
baseRevision: 0,
title: "Excalifont",
additions: [
{
...rectangle("excalifont"),
type: "text",
text: "stable Excalifont metrics",
originalText: "stable Excalifont metrics",
fontSize: 20,
fontFamily: 5,
textAlign: "left",
verticalAlign: "top",
},
],
});
const firstExcalifont = await layout.generate(emptyState(), excalifont);
const repeatedExcalifont = await layout.generate(
emptyState(),
excalifont,
);
const metrics = ({
x,
y,
width,
height,
baseline,
}: (typeof firstExcalifont)[number]) => ({
x,
y,
width,
height,
baseline,
});
expect(metrics(repeatedExcalifont[0]!)).toEqual(
metrics(firstExcalifont[0]!),
);
expect(layout.diagnostics().launches).toBe(1);
const text = createDraw(emptyState(), {
baseRevision: 0,
title: "text",
additions: [
{
...rectangle("added"),
type: "text",
text: "right aligned",
originalText: "right aligned",
fontSize: 20,
fontFamily: 1,
textAlign: "right",
verticalAlign: "top",
},
],
});
expect((await layout.generate(emptyState(), text))[0]).toMatchObject({
id: "added",
type: "text",
text: "right aligned",
});
const mermaid = createDraw(emptyState(), {
baseRevision: 0,
title: "diagram",
mermaid: { definition: "flowchart LR\nA --> B", x: 50, y: 60 },
});
expect(await layout.generate(emptyState(), mermaid)).not.toHaveLength(
0,
);
const evaluating = new AbortController();
const aborted = layout.generate(
emptyState(),
mermaid,
evaluating.signal,
);
evaluating.abort();
await expect(aborted).rejects.toThrow();
await layout.close();
// Compare independent runtimes sequentially to bound Chromium memory.
const independent = createCanvasLayout(server.url);
try {
expect(
metrics((await independent.generate(emptyState(), excalifont))[0]!),
).toEqual(metrics(firstExcalifont[0]!));
} finally {
await independent.close();
}
const closing = createCanvasLayout(server.url);
const starting = closing.generate(state, relabel);
await closing.close();
await expect(starting).rejects.toThrow();
expect(closing.diagnostics()).toMatchObject({
running: false,
closed: true,
});
} finally {
await layout.close();
await server.close();
}
}, 60000);
},
);