import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { notePath, writeNote } from "@gk/schema"; import { ContentService } from "../src/service.ts"; import { Store } from "../src/store.ts"; import { Vault } from "../src/vault.ts"; export interface Rig { dir: string; site: string; store: Store; vault: Vault; service: ContentService; clock: { t: number; advance(minutes: number): void }; /** Write a note into the vault the way Obsidian would, returning its path. */ put(data: Record, body?: string): string; putText(path: string, text: string): void; done(): void; } export function rig(): Rig { const dir = mkdtempSync(join(tmpdir(), "gk-content-")); const site = join(dir, "Site"); mkdirSync(site); const store = new Store(join(dir, "content.db")); const vault = new Vault(site); const clock = { t: Date.parse("2026-10-11T12:00:00Z"), advance(minutes: number) { this.t += minutes * 60_000; }, }; const service = new ContentService({ store, vault, now: () => new Date(clock.t) }); const putText = (path: string, text: string) => { mkdirSync(dirname(join(site, path)), { recursive: true }); writeFileSync(join(site, path), text); }; return { dir, site, store, vault, service, clock, put(data, body = "") { const path = notePath(data); putText(path, writeNote(data, body)); return path; }, putText, done() { store.close(); rmSync(dir, { recursive: true, force: true }); }, }; } export const book = (title: string, extra: Record = {}) => ({ type: "library-item", title, medium: "book", state: "finished", publish: true, ...extra, }); export const post = (title: string, extra: Record = {}) => ({ type: "post", title, date: "2026-10-01", topic: "Making", summary: "A summary.", publish: true, ...extra, }); export const long = (n = 600) => "Paragraph of real writing. ".repeat(Math.ceil(n / 27)).slice(0, n);