// An in-memory workspace for the tests of the native Project Management feature. It records each read. import { defaultBoard } from "../../../../src/lib/features/pm/storage/board.ts"; import { catalogPath } from "../../../../src/lib/features/pm/storage/catalog.ts"; import { cardPath } from "../../../../src/lib/features/pm/storage/cards.ts"; import { newTask } from "../../../../src/lib/features/pm/storage/notes.ts"; import { cardsMarker, privateRulesPath } from "../../../../src/lib/features/pm/storage/persistence.ts"; import type { TaskFields, Workspace } from "../../../../src/lib/features/pm/model/model.ts"; export interface MemoryWorkspace extends Workspace { files: Map; /** Each path that a read asked for, in order. A `readMany` row counts as one read. */ reads: string[]; /** Each folder that a listing asked for. */ lists: string[]; /** The number of Markdown listings. */ markdownLists: number; } export function memoryWorkspace(initial: Record = {}): MemoryWorkspace { const files = new Map(Object.entries(initial)); const reads: string[] = [], lists: string[] = []; const parent = (path: string) => path.includes("/") ? path.slice(0, path.lastIndexOf("/")) : ""; const workspace: MemoryWorkspace = { files, reads, lists, markdownLists: 0, async list(path = "") { lists.push(path); const names = new Map(); for (const file of files.keys()) { if (path && !file.startsWith(path + "/")) continue; const rest = path ? file.slice(path.length + 1) : file; const name = rest.split("/")[0]; names.set(name, rest.includes("/") ? "folder" : "file"); } return [...names].map(([name, kind]) => ({ name, rel_path: path ? `${path}/${name}` : name, kind })); }, async listMarkdown() { workspace.markdownLists++; return [...files.keys()].filter(path => /\.(md|markdown|mdown)$/i.test(path) && !path.startsWith(".")).sort(); }, async read(path) { reads.push(path); if (!files.has(path)) throw new Error(`Missing file: ${path}`); return files.get(path)!; }, async readMany(paths) { reads.push(...paths); return paths.map(path => files.has(path) ? { path, source: files.get(path) } : { path, error: "Missing file" }); }, async readOptional(path) { reads.push(path); return files.get(path) ?? null; }, async write(path, contents, expected) { if (files.get(path) !== expected) throw new Error(`The file changed: ${path}`); files.set(path, contents); }, async create(path, contents) { if (files.has(path)) throw new Error(`The file exists: ${path}`); void parent(path); files.set(path, contents); }, }; return workspace; } export const fields = (extra: Partial = {}): TaskFields => ({ status: "todo", tags: [], sessions: [], archived: false, ...extra }); /** A workspace with hidden storage: one board `board` and the given cards. */ export function hiddenWorkspace(cards: { id: string; title: string; fields?: Partial }[] = [], extra: Record = {}) { const board = defaultBoard("board"); const files: Record = { [privateRulesPath]: "*\n", [cardsMarker]: "*\n", [catalogPath]: JSON.stringify({ schema: 1, boards: [{ name: "Board", value: board }] }, null, 2) + "\n", ...extra, }; for (const card of cards) files[cardPath(card.id)] = newTask(card.title, fields({ title: card.title, placements: { board: { listId: "todo" } }, ...card.fields }), "Body", card.id).source; return memoryWorkspace(files); }