Something went wrong. Try again.
Local-first knowledge base made for Typst and the Atmosphere. typbase.at
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018import type { NuxtPage } from "@nuxt/test-utils/e2e";
import { createPage, setup, url } from "@nuxt/test-utils/e2e";import { fileURLToPath } from "node:url";import { describe, expect, it } from "vitest";
/** * Plugin e2e: installs bundled plugins through the dev-only handle and exercises * their surfaces without touching the UI. Verifies the v2 wrapper compiles, * that one instance shares its data doc across surfaces, that windows mount * through the host, and that a broken local source lands in the error state. */
describe("plugin system", async () => { await setup({ rootDir: fileURLToPath(new URL("../", import.meta.url)), dev: true, browser: true, browserOptions: { type: "chromium" }, nuxtConfig: { buildDir: ".nuxt-e2e" }, env: { NUXT_IGNORE_LOCK: "1" }, setupTimeout: 300_000, });
async function openApp(page: NuxtPage, path = "/"): Promise<void> { await page.addInitScript(() => localStorage.setItem("typbase:storageMode", "opfs")); await page.goto(url(path), { waitUntil: "load" }); await page.waitForSelector(".sidebar", { timeout: 180_000 }); await page.waitForFunction(() => document.fonts.status === "loaded", null, { timeout: 60_000, }); }
it("shares one data doc between the calendar widget and pane", async () => { const page = await createPage(); await openApp(page);
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:calendar")); expect(instanceId).toBeTruthy();
await page.waitForFunction( (id) => window.__typbase.pluginStatus(id, "widget") === "ok", instanceId, { timeout: 120_000 }, );
const today = new Date().toISOString().slice(0, 10); await page.evaluate( ({ id, date }) => window.__typbase.pluginAction( id, "pane", "event.create", { date }, { title: "Dentist", date, time: "10:00" }, ), { id: instanceId, date: today }, );
// The event lands in the instance doc and both surfaces read it. const state = await page.evaluate((id) => window.__typbase.pluginState(id), instanceId); expect(state.events).toHaveLength(1); expect((state.events[0] as { title: string }).title).toBe("Dentist");
await page.waitForFunction( ({ id, title }) => window.__typbase.pluginHtml(id, "widget").includes(title), { id: instanceId, title: "Dentist" }, { timeout: 60_000 }, );
// Opening the pane subscribes its surface. It compiles the patched state. await page.evaluate((id) => window.__typbase.openPlugin(id), instanceId); await page.waitForFunction( ({ id, title }) => window.__typbase.pluginStatus(id, "pane") === "ok" && window.__typbase.pluginHtml(id, "pane").includes(title), { id: instanceId, title: "Dentist" }, { timeout: 60_000 }, );
// A view-only action (selecting a day) must re-render on its own. Before, // the outline only moved after some other action forced a render. Pick // another day in the current month: the grid only marks cells in the // month it shows, so a next-month date would leave it unmarked. const now = new Date(); const day = now.getUTCDate(); const otherDay = new Date( Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), day === 1 ? 2 : day - 1), ) .toISOString() .slice(0, 10); await page.evaluate( ({ id, date }) => window.__typbase.pluginAction(id, "pane", "calendar.select", { date }), { id: instanceId, date: otherDay }, ); await page.waitForFunction( ({ id, date }) => { const html = window.__typbase.pluginHtml(id, "pane"); const selected = html.match(/<button[^>]*cal-day--selected[^>]*>/)?.[0] ?? "";
return selected.includes(date); }, { id: instanceId, date: otherDay }, { timeout: 30_000 }, );
await page.close(); });
it("repaints the brush when a swatch is clicked", async () => { const page = await createPage(); await openApp(page);
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:drawing")); await page.evaluate((id) => window.__typbase.openPlugin(id), instanceId); await page.waitForFunction((id) => window.__typbase.pluginWindowOpen(id), instanceId, { timeout: 30_000, });
// Use the swatch's own theme color: the palette depends on the workspace // theme, so a hardcoded seed would only pass on the default theme. await page.waitForFunction( () => { const host = document.querySelector(".plugin-window .plugin-surface");
return (host?.shadowRoot?.querySelectorAll("button.tb-swatch").length ?? 0) > 1; }, null, { timeout: 30_000 }, ); const color = await page.evaluate(() => { const host = document.querySelector(".plugin-window .plugin-surface"); const swatch = host?.shadowRoot?.querySelectorAll("button.tb-swatch")[1];
return swatch?.getAttribute("style")?.split(":")[1] ?? ""; }); expect(color).toMatch(/^#/);
await page.evaluate( ({ id, swatchColor }) => window.__typbase.pluginAction(id, "window", "draw.color", { color: swatchColor }), { id: instanceId, swatchColor: color }, ); await page.waitForFunction( ({ id, swatchColor }) => { const html = window.__typbase.pluginHtml(id, "window"); const tag = html.match(new RegExp(`<button[^>]*${swatchColor}[^>]*>`))?.[0] ?? "";
return tag.includes("tb-button--selected"); }, { id: instanceId, swatchColor: color }, { timeout: 30_000 }, );
await page.close(); });
it("opens the drawing plugin in a host window and stores strokes", async () => { const page = await createPage(); await openApp(page);
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:drawing")); await page.evaluate( (id) => window.__typbase.pluginAction(id, "window", "stroke.add", { stroke: { color: "#b42828", width: 3, points: [ [4, 4], [12, 18], ], }, }), instanceId, );
const state = await page.evaluate((id) => window.__typbase.pluginState(id), instanceId); expect(state.strokes).toHaveLength(1);
await page.evaluate((id) => window.__typbase.openPlugin(id), instanceId); await page.waitForFunction((id) => window.__typbase.pluginWindowOpen(id), instanceId, { timeout: 30_000, }); // The window mounts its surface asynchronously. The canvas host appends // the real canvas on mount. await page.waitForSelector(".plugin-window canvas.tb-canvas", { timeout: 30_000 });
// Top-level blocks get host spacing: the drawing returns two panels and // the second one has the surface gap. const gap = await page.evaluate(() => { const host = document.querySelector(".plugin-window .plugin-surface"); const surface = host?.shadowRoot?.querySelector("#tb-root .tb-surface"); const panels = surface ? [...surface.children] : [];
return panels.length >= 2 ? getComputedStyle(panels[1]).marginTop : ""; }); expect(gap).toBe("12px");
// The insert button chains page-append and window-close, and toasts. const targetId = await page.evaluate(async () => { const meta = await window.__typbase.store.createPage({ title: "Insert target", content: "" });
return meta.id; }); await page.evaluate((id) => window.__typbase.openPage(id), targetId); await page.locator(".plugin-window button", { hasText: "Insert in the open note" }).click(); await page.waitForSelector(".ui-toast", { timeout: 15_000 }); await page.waitForFunction((id) => !window.__typbase.pluginWindowOpen(id), instanceId, { timeout: 30_000, });
const text = await page.evaluate((id) => window.__typbase.store.loadPageText(id), targetId); // The snippet names the module rather than importing `embed` bare, so the // note gets one namespaced call instead of a loose binding. expect(text).toContain('#import "/typbase/plugin/drawing/main.typ" as drawing'); expect(text).toContain("#drawing.embed(");
// The open editor adopts the appended text without a reload, and the // imported module resolves. await page.waitForFunction( () => (window.__typbase.view?.state.doc.toString() ?? "").includes("as drawing"), null, { timeout: 30_000 }, );
await page.close(); });
it("undoes, redoes, and erases strokes", async () => { const page = await createPage(); await openApp(page);
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:drawing"));
const action = (name: string, args: Record<string, unknown> = {}) => page.evaluate( ({ id, name: actionName, args: actionArgs }) => window.__typbase.pluginAction(id, "window", actionName, actionArgs), { id: instanceId, name, args }, ); const records = () => page.evaluate(async (id) => { const state = await window.__typbase.pluginState(id);
return (state.strokes ?? []) as Array<Record<string, unknown>>; }, instanceId); const stroke = (points: number[][]) => ({ stroke: { color: "#b42828", width: 3, points }, });
await action( "stroke.add", stroke([ [4, 4], [12, 18], ]), ); await action( "stroke.add", stroke([ [20, 20], [30, 30], ]), );
await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(2); // `seq` is the draw order. The collection itself comes back in map order, // which is why the canvas and the embed sort by it. expect((await records()).map((record) => Number(record.seq)).sort((a, b) => a - b)).toEqual([ 1, 2, ]);
await action("history.undo"); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(1);
await action("history.undo"); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(0);
// Undo past the start is a no-op. await action("history.undo"); expect((await records()).length).toBe(0);
await action("history.redo"); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(1); await action("history.redo"); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(2); await action("history.redo"); expect((await records()).length).toBe(2);
// A new edit drops the redo branch. await action("history.undo"); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(1); await action( "stroke.add", stroke([ [40, 40], [48, 48], ]), ); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(2); await action("history.redo"); expect((await records()).length).toBe(2);
// The eraser removes records and undo puts the same ones back. const ids = (await records()).map((record) => String(record.id)); await action("stroke.erase", { ids: [ids[0]] }); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(1); await action("history.undo"); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(2); await action("history.redo"); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(1);
// Clear is one entry, so one undo restores the whole board. await action("stroke.clear"); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(0); await action("history.undo"); await expect.poll(async () => (await records()).length, { timeout: 30_000 }).toBe(1);
await page.close(); });
it("draws and erases on the canvas with a pointer", async () => { const page = await createPage(); await openApp(page);
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:drawing")); await page.evaluate((id) => window.__typbase.openPlugin(id), instanceId); await page.waitForFunction((id) => window.__typbase.pluginWindowOpen(id), instanceId, { timeout: 30_000, }); await page.waitForSelector(".plugin-window canvas.tb-canvas", { timeout: 30_000 });
const count = () => page.evaluate(async (id) => { const state = await window.__typbase.pluginState(id);
return (state.strokes ?? []).length; }, instanceId); const canvasBox = () => page.evaluate(() => { const host = document.querySelector(".plugin-window .plugin-surface"); const canvas = host?.shadowRoot?.querySelector("canvas.tb-canvas"); canvas?.scrollIntoView({ block: "center" }); const rect = canvas?.getBoundingClientRect();
return rect ? { x: rect.x, y: rect.y, width: rect.width, height: rect.height } : null; }); const clickButton = (label: string) => page.evaluate((buttonLabel) => { const host = document.querySelector(".plugin-window .plugin-surface"); const button = [...(host?.shadowRoot?.querySelectorAll("button") ?? [])].find( (candidate) => candidate.textContent?.trim() === buttonLabel, ); (button as HTMLButtonElement | undefined)?.click(); }, label);
const first = await canvasBox(); expect(first).toBeTruthy(); if (!first) return;
await page.mouse.move(first.x + 20, first.y + 60); await page.mouse.down(); await page.mouse.move(first.x + Math.min(160, first.width - 20), first.y + 60, { steps: 8 }); await page.mouse.up();
await expect.poll(count, { timeout: 30_000 }).toBe(1);
await clickButton("Eraser"); await page.waitForFunction( () => { const host = document.querySelector(".plugin-window .plugin-surface"); const element = host?.shadowRoot?.querySelector('[data-tb-component="canvas"]'); const raw = element?.getAttribute("data-tb-props"); if (!raw) return false; try { return (JSON.parse(raw) as { tool?: string }).tool === "erase"; } catch { return false; } }, null, { timeout: 30_000 }, );
const second = await canvasBox(); expect(second).toBeTruthy(); if (!second) return;
await page.mouse.move(second.x + 80, second.y + 20); await page.mouse.down(); await page.mouse.move(second.x + 80, second.y + 110, { steps: 8 }); await page.mouse.up();
await expect.poll(count, { timeout: 30_000 }).toBe(0);
// The eraser patch is in the history, so the toolbar brings it back. await clickButton("Undo"); await expect.poll(count, { timeout: 30_000 }).toBe(1);
await page.close(); });
it("opens the plugin when an embed's link is clicked", async () => { const page = await createPage(); await openApp(page);
// The drawing wraps its board in a `typbase://plugin/<id>` link, so clicking // the drawing in a note is the way back to the window it belongs to. The // embed is a view of the instance's data, and the data is edited in the // plugin. const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:drawing")); await page.evaluate( (id) => window.__typbase.pluginAction(id, "window", "stroke.add", { stroke: { color: "#b42828", width: 3, points: [ [4, 4], [12, 18], ], }, }), instanceId, );
const noteId = await page.evaluate(async (id) => { const store = window.__typbase.store; const meta = await store.createPage({ title: "Embeds a drawing", content: `#import "/typbase/plugin/drawing/main.typ" as drawing\n#drawing.embed("${id}")\n`, });
return meta.id; }, instanceId); await page.evaluate((id) => window.__typbase.openPage(id), noteId);
// The embed is a real anchor in the rendered frame, not decoration. await page.waitForSelector('.typst-render a[href^="typbase://plugin/"]', { timeout: 60_000 });
expect(await page.evaluate((id) => window.__typbase.pluginWindowOpen(id), instanceId)).toBe( false, );
await page.locator('.typst-render a[href^="typbase://plugin/"]').first().click();
await page.waitForFunction((id) => window.__typbase.pluginWindowOpen(id), instanceId, { timeout: 30_000, });
await page.close(); });
it("includes plugin sources in an exported bundle", async () => { const page = await createPage(); await openApp(page);
// A note that has the embed the insert action writes. The note is never // opened, so nothing in the app has compiled it and no request has asked for // the plugin module: the bundle includes it without being told to. const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:drawing")); await page.evaluate( (id) => window.__typbase.pluginAction(id, "window", "stroke.add", { stroke: { color: "#b42828", width: 3, points: [ [4, 4], [12, 18], ], }, }), instanceId, ); const noteId = await page.evaluate(async (id) => { const store = window.__typbase.store; const meta = await store.createPage({ title: "Carries a drawing", content: `#import "/typbase/plugin/drawing/main.typ" as drawing\n#drawing.embed("${id}")\n`, });
return meta.id; }, instanceId);
// The export bundle has to stand on its own: compiled against its own files // and nothing else, because the app's request channel is not there to answer. const bundle = await page.evaluate((id) => window.__typbase.exportPage(id, {}), noteId); const names = bundle.files.map((file) => file.name); expect(names, "bundle is missing the plugin module").toContain( "typbase/plugin/drawing/main.typ", ); // The embed reads the instance's records, which live in a doc a compiler // cannot open, so the bundle has to include them as query JSON too. expect(names, "bundle is missing the plugin data").toContain( `typbase/query/plugin-data/${instanceId}.json`, );
// The exported body text is scaled for a page, not for the editor pane. // `settings.textSize` is a screen metric, so reusing it verbatim on A4 // renders larger than the editor ever showed. const entry = bundle.files.find((file) => file.name === "carries-a-drawing.typ"); expect(entry, "the bundle has no entry source").toBeTruthy(); // expect(entry?.text).toContain(`size:${textSize * 0.5}pt`);
const check = await page.evaluate( ({ files }) => window.__typbase.compileBundle(files, "carries-a-drawing.typ"), { files: bundle.files }, ); expect(check.foundEntry).toBe(true); expect(check.missing, "the compile asked for files the bundle lacks").toEqual([]); expect(check.errors).toEqual([]);
await page.close(); });
it("notices that an installed plugin's sources moved on", async () => { const page = await createPage(); await openApp(page);
await page.evaluate(async () => { await window.__typbase.writeWorkspaceFile( "plugins/versioned/plugin.json", JSON.stringify({ id: "local:versioned", name: "Versioned", version: "1.0.0", api: "typbase.host.v2", entry: "main.typ", capabilities: ["plugin.data"], collections: {}, surfaces: [{ kind: "widget", fn: "widget", title: "Versioned" }], }), ); await window.__typbase.writeWorkspaceFile( "plugins/versioned/main.typ", ['#import "/typbase/ui.typ": *', "", "#let widget(ctx) = surface([build 1.0.0])"].join( "\n", ), ); await window.__typbase.refreshPlugins(); });
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:versioned")); await page.waitForFunction( ({ id, want }) => window.__typbase.pluginHtml(id, "widget").includes(want), { id: instanceId, want: "build 1.0.0" }, { timeout: 60_000 }, );
// The install record starts at the version that was installed. await expect .poll(() => page.evaluate(() => window.__typbase.store.getPluginInstall("local:versioned")?.version), ) .toBe("1.0.0");
// Rewrite the plugin on disk, the way editing it in the studio or replacing // its folder would. The refresh is explicit because OPFS has no storage // watcher. The studio's save path calls exactly this after a write. await page.evaluate(async () => { await window.__typbase.writeWorkspaceFile( "plugins/versioned/plugin.json", JSON.stringify({ id: "local:versioned", name: "Versioned", version: "2.0.0", api: "typbase.host.v2", entry: "main.typ", capabilities: ["plugin.data"], collections: {}, surfaces: [{ kind: "widget", fn: "widget", title: "Versioned" }], }), ); await window.__typbase.writeWorkspaceFile( "plugins/versioned/main.typ", ['#import "/typbase/ui.typ": *', "", "#let widget(ctx) = surface([build 2.0.0])"].join( "\n", ), ); await window.__typbase.refreshPlugins(); });
// The mounted surface shows the new build. That part already worked, because a // catalog refresh re-registers the sources. await expect .poll(() => page.evaluate((id) => window.__typbase.pluginHtml(id, "widget"), instanceId), { timeout: 30_000, }) .toContain("build 2.0.0");
// The install record is what was stale: it is what the sidebar and the studio // read to show a version, a surface list, and a capability set. await expect .poll(() => page.evaluate(() => window.__typbase.store.getPluginInstall("local:versioned")?.version), ) .toBe("2.0.0");
await page.close(); });
it("renames an instance from the plugin manager", async () => { const page = await createPage(); await openApp(page);
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:drawing")); await page.locator(".plugin-sidebar__header button").click(); await page.locator('[aria-label="Rename instance"]').first().click(); await page.locator(".plugin-manager__rename input").fill("Sketchbook"); await page.getByRole("button", { name: "Save" }).click();
await page.waitForFunction( (id) => window.__typbase.store.getPluginInstance(id)?.title === "Sketchbook", instanceId, { timeout: 30_000 }, );
await page.close(); });
it("refuses state writes from a plugin without plugin.data", async () => { const page = await createPage(); await openApp(page);
// `capabilities` is empty, so the plugin never asked for `plugin.data`. Its // surface still patches, because nothing in the manifest stops it from // trying: the capability is what has to stop it. await page.evaluate(async () => { await window.__typbase.writeWorkspaceFile( "plugins/nodata/plugin.json", JSON.stringify({ id: "local:nodata", name: "No Data", version: "0.1.0", api: "typbase.host.v2", entry: "main.typ", capabilities: [], collections: { notes: { fields: { title: { type: "string" } } }, }, surfaces: [{ kind: "widget", fn: "pane", title: "No Data" }], }), ); await window.__typbase.writeWorkspaceFile( "plugins/nodata/main.typ", [ '#import "/typbase/ui.typ": *', "", "#let pane(ctx) = {", " // Patch only after an action, the way a well-behaved plugin does.", " let patch = if ctx.action != none {", ' ops-state((op-append("notes", (id: "n1", title: "x")),))', " } else { (:) }", " surface(", ' [writes #ctx.state.at("notes", default: ()).len() records],', " ..patch,", " )", "}", ].join("\n"), ); await window.__typbase.refreshPlugins(); });
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:nodata")); await page.evaluate((id) => window.__typbase.pluginAction(id, "widget", "noop"), instanceId); await page.waitForFunction( (id) => window.__typbase.pluginStatus(id, "widget") === "ok", instanceId, { timeout: 60_000 }, );
// The read is empty too, so the surface cannot render a record it may not write. expect( await page.evaluate((id) => window.__typbase.pluginHtml(id, "widget"), instanceId), ).toContain("writes 0 records");
// And nothing landed in the doc. expect(await page.evaluate((id) => window.__typbase.pluginState(id), instanceId)).toEqual({});
// The refusal is reported, so the author sees why the button did nothing. expect(await page.evaluate(() => window.__typbase.pluginLogs())).toContainEqual( expect.stringContaining('missing capability "plugin.data"'), );
await page.close(); });
it("shows a denied action on the surface rather than only in the log", async () => { const page = await createPage(); await openApp(page);
// A pane with no capabilities at all, whose only button asks the host to // create a page. The click cannot work, and the surface has to say so. await page.evaluate(async () => { await window.__typbase.writeWorkspaceFile( "plugins/denied/plugin.json", JSON.stringify({ id: "local:denied", name: "Denied", version: "0.1.0", api: "typbase.host.v2", entry: "main.typ", capabilities: [], collections: {}, surfaces: [{ kind: "pane", fn: "pane", title: "Denied" }], }), ); await window.__typbase.writeWorkspaceFile( "plugins/denied/main.typ", [ '#import "/typbase/ui.typ": *', "", "#let pane(ctx) = surface([", ' #button("Make a page", action: "app.create-page", args: (title: "x")),', "])", ].join("\n"), ); await window.__typbase.refreshPlugins(); });
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:denied")); await page.evaluate((id) => window.__typbase.openPlugin(id), instanceId); await page.waitForFunction( (id) => window.__typbase.pluginStatus(id, "pane") === "ok", instanceId, { timeout: 60_000 }, );
await page.evaluate( (id) => window.__typbase.pluginAction(id, "pane", "app.create-page", { title: "Nope" }), instanceId, );
// The surface shows the reason, so the button is not dead. await page.waitForFunction( (id) => window.__typbase.pluginStatus(id, "pane") === "error", instanceId, { timeout: 30_000, }, ); await expect .poll(() => page.locator(".plugin-surface__error-text").first().textContent(), { timeout: 30_000, }) .toContain("pages.create");
await page.close(); });
it("surfaces a broken local plugin as an error state", async () => { const page = await createPage(); await openApp(page);
await page.evaluate(async () => { await window.__typbase.writeWorkspaceFile( "plugins/broken/plugin.json", JSON.stringify({ id: "local:broken", name: "Broken", version: "0.1.0", api: "typbase.host.v2", entry: "main.typ", capabilities: [], collections: {}, surfaces: [{ kind: "pane", fn: "pane", title: "Broken" }], }), ); await window.__typbase.writeWorkspaceFile( "plugins/broken/main.typ", "#let pane(ctx) = surface(ui: [this is not (valid", ); await window.__typbase.refreshPlugins(); });
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:broken")); await page.evaluate((id) => window.__typbase.pluginAction(id, "pane", "noop"), instanceId);
await page.waitForFunction( (id) => window.__typbase.pluginStatus(id, "pane") === "error", instanceId, { timeout: 60_000, }, );
await page.close(); });
it("builds node and edge diagrams for HTML and paged output", async () => { const page = await createPage(); await openApp(page);
const instanceId = await page.evaluate(() => window.__typbase.installPlugin("local:diagrams")); await page.waitForFunction( () => document.querySelectorAll(".plugin-sidebar__row").length === 1, null, { timeout: 30_000 }, ); await page.evaluate((id) => window.__typbase.openPlugin(id), instanceId); await page.waitForFunction( (id) => window.__typbase.pluginStatus(id, "pane") === "ok", instanceId, { timeout: 120_000 }, );
const action = ( name: string, args: Record<string, unknown> = {}, fields: Record<string, unknown> = {}, ) => page.evaluate( ({ id, name: actionName, args: actionArgs, fields: actionFields }) => window.__typbase.pluginAction(id, "pane", actionName, actionArgs, actionFields), { id: instanceId, name, args, fields }, );
await action("node.add"); await action("node.add"); let state = await page.evaluate((id) => window.__typbase.pluginState(id), instanceId); const [first, second] = state.nodes as Array<{ id: string; fill: string }>; expect(first?.id).toBeTruthy(); expect(second?.id).toBeTruthy(); // Defaults are theme tokens, so changing the workspace theme updates the // diagram instead of leaving a baked-in light or dark color behind. expect(state.nodes.every((node) => (node as { fill: string }).fill === "@surface2")).toBe(true);
// Exercise the pointer path too: the movable wrapper captures the pointer, // so the click has to be answered by the wrapper's select action. const surface = page.locator(".plugin-surface"); const boardTop = () => page.evaluate( () => document .querySelector(".plugin-surface") ?.shadowRoot?.querySelector(".dg-board") ?.getBoundingClientRect().top ?? 0, ); const topBeforeConnect = await boardTop(); await surface.getByRole("button", { name: "Connect" }).click(); await page.waitForTimeout(150); expect(await boardTop()).toBe(topBeforeConnect); const nodeLocator = surface.locator(".dg-node"); await nodeLocator.nth(0).click(); await page.waitForFunction( () => [ ...(document .querySelector(".plugin-surface") ?.shadowRoot?.querySelectorAll(".tb-muted") ?? []), ] .map((element) => element.textContent) .includes("Click the target node to connect."), null, { timeout: 30_000 }, ); await nodeLocator.nth(1).click(); await expect .poll( async () => { const next = await page.evaluate((id) => window.__typbase.pluginState(id), instanceId);
return next.edges?.length ?? 0; }, { timeout: 30_000 }, ) .toBe(1); state = await page.evaluate((id) => window.__typbase.pluginState(id), instanceId); expect(state.edges).toHaveLength(1);
// The editor renders the graph as HTML/SVG, not as a blank Typst frame. await page.waitForFunction( (id) => { const html = window.__typbase.pluginHtml(id, "pane");
return html.includes("dg-edges") && html.includes("dg-node"); }, instanceId, { timeout: 30_000 }, );
// A node selects on click, and the grip replaces the old overflow icon. expect(await page.locator(".plugin-surface .dg-node-handle").count()).toBe(2); expect(await page.locator(".plugin-surface").textContent()).not.toContain("⋯"); await page.locator(".plugin-surface .dg-node").first().click(); await expect .poll(() => page.evaluate(() => { const root = document.querySelector(".plugin-surface")?.shadowRoot;
return Boolean(root?.querySelector(".dg-node--selected")); }), ) .toBe(true);
const pageId = await page.evaluate(async (id) => { const meta = await window.__typbase.store.createPage({ title: "Diagram export", content: `#import "/typbase/plugin/diagrams/main.typ" as diagrams\n\n#diagrams.embed("${id}", theme: theme)\n`, });
return meta.id; }, instanceId);
const html = await page.evaluate( (id) => window.__typbase.exportPage(id, { html: true }), pageId, ); const htmlFile = html.files.find((file) => file.name.endsWith(".html")); expect(htmlFile?.text).toContain("<svg"); expect(htmlFile?.text).toContain("Node 1"); expect(htmlFile?.text).toContain("Node 2");
// Paged output takes the Fletcher branch. It has to produce a PDF, not // the empty document the HTML renderer produces for CeTZ layouts. const pdf = await page.evaluate( (id) => window.__typbase.exportPage(id, { html: false, pdf: true }), pageId, ); // The call throws when renderPdf produces no bytes. The text bundle does // not include the PDF itself, so the project entry is the readable artifact. expect(pdf.files.some((file) => file.name.endsWith(".typ"))).toBe(true);
// A public page may render the diagram because the plugin declares // `site.publish`. The build otherwise strips every plugin-data query. const site = await page.evaluate( async ({ pageId: id }) => { const settings = window.__typbase.store.getSiteSettings(); settings.pages[id] = { ...(settings.pages[id] ?? {}), visibility: "public" }; window.__typbase.store.updateSettings({ site: settings });
return window.__typbase.siteBuild({ baseUrl: "https://example.test" }); }, { pageId }, ); const sitePage = site.files.find((file) => file.name === "diagram-export/index.html"); expect(sitePage?.text).toContain("<svg"); expect(sitePage?.text).toContain("Node 1"); expect(sitePage?.text).toContain( 'property="og:image" content="https://example.test/assets/og/', ); expect(sitePage?.text).toContain('property="og:site_name" content="My workspace"'); expect(sitePage?.text).not.toContain('name="twitter:'); // A page without an explicit description must not publish its opening // source paragraph as meta description. expect(sitePage?.text).not.toContain('name="description"'); expect( site.files.some((file) => file.name.startsWith("assets/og/") && file.name.endsWith(".png")), ).toBe(true);
// The phone layout keeps the canvas usable by scrolling it sideways and // stacks the two inspectors into one column. await page.setViewportSize({ width: 390, height: 844 }); await page.evaluate((id) => window.__typbase.openPlugin(id), instanceId); await page.waitForFunction( (id) => window.__typbase.pluginStatus(id, "pane") === "ok", instanceId, { timeout: 30_000 }, ); await page.waitForTimeout(300); const phone = await page.evaluate(() => { const root = document.querySelector(".plugin-surface")?.shadowRoot; const scroll = root?.querySelector(".dg-board-scroll"); const inspectors = root?.querySelector(".dg-inspectors");
return { clientWidth: scroll?.clientWidth ?? 0, scrollWidth: scroll?.scrollWidth ?? 0, inspectorColumns: inspectors ? getComputedStyle(inspectors).gridTemplateColumns.split(" ").length : 0, }; }); expect(phone.scrollWidth).toBeGreaterThan(phone.clientWidth); expect(phone.inspectorColumns).toBe(1);
await page.close(); });
it("renders the studio with an editor for a local plugin", async () => { const page = await createPage(); const errors: string[] = []; page.on("pageerror", (error) => errors.push(`pageerror: ${String(error)}`)); page.on("console", (message) => { if (message.type() === "error") errors.push(`console: ${message.text()}`); });
// The studio is its own page: no sidebar and no editor to wait for. await page.addInitScript(() => localStorage.setItem("typbase:storageMode", "opfs")); await page.goto(url("/plugins"), { waitUntil: "load" });
await page.waitForSelector(".studio", { timeout: 180_000 }); await page.waitForSelector(".studio__plugin", { timeout: 180_000 });
// A click can land before the client-only page finishes mounting, which // would be dropped. Retry until the selection sticks. let tabs = 0; for (let attempt = 0; attempt < 10 && tabs === 0; attempt++) { await page .locator(".studio__plugin") .first() .click({ timeout: 2000 }) .catch(() => {}); await page.waitForTimeout(500); tabs = await page.locator(".studio__tab").count(); } expect(tabs).toBeGreaterThan(0); expect(errors).toEqual([]);
await page.locator(".studio__tab", { hasText: "source" }).click(); await page.waitForSelector(".studio-editor .cm-editor", { timeout: 60_000 });
await page.close(); });});