// Shared setup of the Project Management smoke tests (tests/smoke/features/pm*.smoke.mjs): a workspace with two boards // and five cards in hidden storage, a mock that keeps the workspace files through a page reload, and small helpers. import { mockTauri, smokeUrl } from "./index.mjs"; export const roadmap = "6f4c2a3e-6a0b-4d57-9a3e-1c2b3d4e5f60"; export const bugs = "0c18bcdc-f41e-4a31-87f4-87c369f398c1"; export const catalogPath = ".runestone/pm/workspace.json"; export const deletedPath = ".runestone/pm/deleted-cards.json"; const hex = id => Buffer.from(id).toString("hex"); export const cardPath = id => `.runestone/pm/cards/${hex(id)}.md`; export const draftPath = id => `.runestone/pm/drafts/${hex(id)}.json`; /** The local date `offset` days from today, as YYYY-MM-DD. */ export function localDay(offset = 0) { const date = new Date(); date.setDate(date.getDate() + offset); return [date.getFullYear(), date.getMonth() + 1, date.getDate()].map(part => String(part).padStart(2, "0")).join("-"); } /** A board with the three default lists and one review list. `review` gives the ID, the name, and the limit of that list. */ export function board(id, review = { id: "review", name: "Review" }) { return { schema: 1, kind: "board", id, lists: [ { id: "todo", name: "To do", status: "todo", collapsed: false }, { id: "doing", name: "In progress", status: "doing", collapsed: false }, { id: "done", name: "Done", status: "done", collapsed: false }, { collapsed: false, status: null, ...review }, ], defaults: { todo: "todo", doing: "doing", done: "done" }, }; } /** A card file. `placements` maps a board ID to a list ID. `extra` adds lines to the front matter. */ export function card(id, title, { placements = {}, status = "todo", order = id.slice(-1), extra = [] } = {}) { const lines = ["---", "runestone_pm:", " schema: 1", ` id: ${id}`, " kind: task", ` title: ${title}`, ' createdAt: "2026-10-01T08:30:00.000Z"', ` order: "${order}"`, ` status: ${status}`, " tags: []", " sessions: []", " archived: false", ...extra]; if (Object.keys(placements).length) { lines.push(" placements:"); for (const [boardId, listId] of Object.entries(placements)) lines.push(` ${boardId}:`, ` listId: ${listId}`, ` order: "${order}"`); } return [cardPath(id), [...lines, "---", `Body of ${title}.`, ""].join("\n")]; } /** * The workspace files. Roadmap: Card A and Card B in To do, Shared card in To do, and a limit of 2 on Review. * Bugs: Card C in its "review" list, Shared card in To do. Loose card has no board. Card B has planned dates and a deadline. */ export function pmFiles({ boards = true, defaultBoardId } = {}) { const catalog = { schema: 1, boards: [ { name: "Roadmap", value: board(roadmap, { id: "review", name: "Review", maxCards: 2 }) }, { name: "Bugs", value: board(bugs, { id: "qa", name: "review" }) }, ], ...(defaultBoardId ? { defaultBoardId, defaultBoardPath: `pm-board:${defaultBoardId}` } : {}) }; return Object.fromEntries([ ["note.md", "# Note\n"], [".runestone/pm/.gitignore", "# RuneStone Project Management local data\n*\n"], [".runestone/pm/cards/.gitignore", "*\n"], ...boards ? [[catalogPath, JSON.stringify(catalog, null, 2) + "\n"]] : [], card("card-a", "Card A", { placements: { [roadmap]: "todo" }, order: "a" }), card("card-b", "Card B", { placements: { [roadmap]: "todo" }, order: "b", extra: [ " due:", ` date: "${localDay(2)}"`, " plan:", ` start: "${localDay(0)}"`, ` end: "${localDay(4)}"`] }), card("card-c", "Card C", { placements: { [bugs]: "qa" }, order: "c" }), card("card-d", "Shared card", { placements: { [roadmap]: "todo", [bugs]: "todo" }, order: "d" }), card("card-e", "Loose card", { order: "e" }), ]); } // This function runs in the page. Keep it self-contained. The files stay in localStorage, so a reload keeps them. function pmCommands(test) { const KEY = "runestone.smoke.pm-files"; let saved = null; try { saved = JSON.parse(localStorage.getItem(KEY)); } catch { saved = null; } const workspace = test.workspace({ root: "C:\\pm-smoke", files: new Map(saved ?? Object.entries(test.state.files)) }); test.files = workspace.files; const keep = () => localStorage.setItem(KEY, JSON.stringify([...workspace.files])); const kept = handler => args => { const result = handler(args); keep(); return result; }; return { ...workspace, workspace_read: args => { if (!workspace.files.has(args.rel) && args.missingOk) return null; return workspace.workspace_read(args); }, workspace_write: kept(workspace.workspace_write), workspace_delete: kept(workspace.workspace_delete), workspace_create: kept(({ rel, kind, contents }) => { if (workspace.files.has(rel)) throw new Error("Destination exists"); if (kind === "file") workspace.files.set(rel, contents ?? ""); else workspace.folders.add(rel); return null; }), // The download of a copied web image: a 1 by 1 GIF. An address with "offline" fails. workspace_download_image: ({ url }) => { if (url.includes("offline")) throw new Error("Cannot reach the image server"); return "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"; }, plugins_snapshot: () => ({ root: "C:/plugins", plugins: [] }), spelling_menu_install: () => null, "plugin:window|is_maximized": () => false, }; } /** Mock the host with the files of `pmFiles()`, load the app, and wait for the file tree. */ export async function loadWorkspace(page, files = pmFiles()) { await mockTauri(page, { state: { files }, commands: pmCommands }); await page.goto(smokeUrl); await page.locator('[data-tree-rel="note.md"]').waitFor(); } /** The Project Management ribbon button. */ export const ribbon = page => page.locator(".plugin-ribbon").getByRole("button", { name: "Project Management", exact: true }); /** The visible Project Management view. */ export const pane = page => page.locator(".plugin-editor-pane:visible .rs-pm"); /** A Kanban column by its accessible name. */ export const column = (page, name) => pane(page).locator(`section.pm-column[aria-label="${name}"]`); /** Open All tasks with the ribbon button, then open a board through the board scope menu when `name` is given. */ export async function openBoard(page, name) { if (!await pane(page).count()) { await ribbon(page).click(); await pane(page).locator(".pm-board").waitFor(); } if (!name) return; await pane(page).locator(".pm-scope").click(); await page.getByRole("menuitemradio", { name, exact: true }).click(); await page.getByRole("tab", { name, exact: true }).waitFor(); await pane(page).locator(".pm-scope-label", { hasText: new RegExp(`^${name}$`) }).waitFor(); await pane(page).locator(".pm-column").first().waitFor(); } /** Use the dark or the light mode of the default theme. */ export async function setTheme(page, mode) { await page.evaluate(async value => { const { saveAppearance } = await import("/src/lib/theme/appearance.ts"); saveAppearance({ theme: "default", mode: value }); }, mode); await page.waitForFunction(value => document.documentElement.dataset.theme === value, mode); } /** The text of a workspace file, or undefined. */ export const fileText = (page, path) => page.evaluate(rel => window.__TEST__.files.get(rel), path); /** Wait until a workspace file matches `pattern`. */ export const waitForFile = (page, path, pattern) => page.waitForFunction(([rel, source, flags]) => new RegExp(source, flags).test(window.__TEST__.files.get(rel) ?? ""), [path, pattern.source, pattern.flags]); /** The saved boards of the catalog. */ export const savedBoards = async page => JSON.parse(await fileText(page, catalogPath)).boards; /** The element that has focus, as its accessible name or text. */ export const focusedName = page => page.evaluate(() => document.activeElement?.getAttribute("aria-label") ?? document.activeElement?.textContent?.trim());