Files
obsidian-foundry-sync/tests/helpers.ts
2026-06-20 19:15:38 +00:00

71 lines
3.1 KiB
TypeScript

import { cpSync, existsSync, mkdirSync } from "node:fs";
import { homedir } from "node:os";
import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
export const FIXTURE_JOURNAL = join(__dirname, "fixtures", "journal");
export const FIXTURE_VAULT = join(__dirname, "fixtures", "vault");
export const FIXTURE_REFINED = join(__dirname, "fixtures", "refined");
export const FIXTURE_CC = join(__dirname, "fixtures", "cc");
const SOURCES = [
process.env.JOURNAL_PATH,
"/tmp/journal-copy",
join(homedir(), "hosting", "Lore examples", "journal"),
].filter(Boolean) as string[];
const LORE_ROOT = [
join(homedir(), "hosting", "Lore examples"),
"/tmp/lore-copy",
].filter(Boolean) as string[];
/** Ensure a hermetic copy of the journal LevelDB exists under tests/fixtures/journal. */
export function ensureJournalFixture(): string {
if (existsSync(join(FIXTURE_JOURNAL, "CURRENT"))) return FIXTURE_JOURNAL;
let src: string | undefined;
for (const s of SOURCES) {
if (existsSync(join(s, "CURRENT"))) { src = s; break; }
}
if (!src) throw new Error("journal fixture source not found; set JOURNAL_PATH or run `cp -r ~/hosting/'Lore examples'/journal /tmp/journal-copy`");
mkdirSync(dirname(FIXTURE_JOURNAL), { recursive: true });
cpSync(src, FIXTURE_JOURNAL, { recursive: true });
return FIXTURE_JOURNAL;
}
/** Copy the whole Refined vault subtree into the fixture dir, return its path. */
export function ensureRefinedFixture(): string {
if (existsSync(FIXTURE_REFINED)) return FIXTURE_REFINED;
const src = LORE_ROOT.find((r) => existsSync(join(r, "Obsidian vault", "Land of Mardonar", "Refined")));
if (!src) throw new Error("refined fixture source not found under ~/hosting/'Lore examples' or /tmp/lore-copy");
mkdirSync(dirname(FIXTURE_REFINED), { recursive: true });
cpSync(join(src, "Obsidian vault", "Land of Mardonar", "Refined"), FIXTURE_REFINED, { recursive: true });
return FIXTURE_REFINED;
}
/** Copy the whole Campaign Codex export subtree into the fixture dir, return its path. */
export function ensureCcFixture(): string {
if (existsSync(FIXTURE_CC)) return FIXTURE_CC;
const src = LORE_ROOT.find((r) => existsSync(join(r, "campaign codex")));
if (!src) throw new Error("cc fixture source not found under ~/hosting/'Lore examples' or /tmp/lore-copy");
mkdirSync(dirname(FIXTURE_CC), { recursive: true });
cpSync(join(src, "campaign codex"), FIXTURE_CC, { recursive: true });
return FIXTURE_CC;
}
/** Copy a single canonical vault note into the fixture vault dir, return its path. */
export function ensureVaultFixture(name: string): string {
const candidates = [
join(homedir(), "hosting", "Lore examples", name),
join("/tmp/lore-copy", name),
];
mkdirSync(FIXTURE_VAULT, { recursive: true });
const dest = join(FIXTURE_VAULT, name);
if (existsSync(dest)) return dest;
for (const c of candidates) {
if (existsSync(c)) { cpSync(c, dest); return dest; }
}
throw new Error(`vault fixture note not found: ${name}`);
}
export const ROLAND_ID = "flGsAYaK24eUZhQE";