A self-organizing Obsidian vault that gives AI coding agents persistent memory.
TypeScript ai-agentsclaude-codecodex-cligemini-cliknowledge-managementobsidianobsidian-vaultpersistent-memorysecond-braintemplate
fix: keep the test suite from writing QMD stores and configs into the user's folders (#307)
Brenno Ferrari204931b572c5
diff --git a/.claude/scripts/lib/qmd-ignore.ts b/.claude/scripts/lib/qmd-ignore.tsindex ebd6b64..06080c7 100644--- a/.claude/scripts/lib/qmd-ignore.ts+++ b/.claude/scripts/lib/qmd-ignore.ts@@ -102,8 +102,24 @@ export function translateToGlob(pattern: string): string | null {return pattern;}-export function qmdConfigPath(indexName: string): string {- return join(homedir(), ".config", "qmd", `${indexName}.yml`);+/**+ * Where qmd keeps `<index>.yml`, resolved exactly as qmd's own+ * `getConfigDir()` does (`collections.js`): `QMD_CONFIG_DIR`, else+ * `XDG_CONFIG_HOME/qmd`, else `<home>/.config/qmd`, with home taken as qmd's+ * `qmdHomedir()` takes it (`HOME`, then `USERPROFILE`, then the OS). A+ * hard-coded `~/.config/qmd` wrote to a file qmd never reads whenever either+ * override was set — including a test that redirected qmd to a temp folder.+ */+export function qmdConfigPath(+ indexName: string,+ env: Readonly<Record<string, string | undefined>> = process.env,+): string {+ const dir = env["QMD_CONFIG_DIR"]+ ? env["QMD_CONFIG_DIR"]+ : env["XDG_CONFIG_HOME"]+ ? join(env["XDG_CONFIG_HOME"], "qmd")+ : join(env["HOME"] || env["USERPROFILE"] || homedir(), ".config", "qmd");+ return join(dir, `${indexName}.yml`);}/**diff --git a/.claude/scripts/package.json b/.claude/scripts/package.jsonindex 9d5782c..43c23d2 100644--- a/.claude/scripts/package.json+++ b/.claude/scripts/package.json@@ -3,7 +3,7 @@"type": "module","description": "Hook scripts for obsidian-mind. Runtime has zero dependencies — executed by `node --experimental-strip-types`. This file exists only to mark .ts files as ESM for Node's module resolution and for CI typecheck (devDependencies installed ad hoc via `npm install --no-save typescript @types/node`).","scripts": {- "test": "node --experimental-strip-types --test",+ "test": "node --experimental-strip-types tests/_run-tests.ts","typecheck": "tsc --noEmit"}}diff --git a/.claude/scripts/tests/_qmd-cache-guard.ts b/.claude/scripts/tests/_qmd-cache-guard.tsnew file mode 100644index 0000000..93a17fe--- /dev/null+++ b/.claude/scripts/tests/_qmd-cache-guard.ts@@ -0,0 +1,30 @@+/**+ * Per-process redirect of QMD's cache and config, for a direct+ * `node --import ./tests/_qmd-cache-guard.ts --test` run.+ *+ * `npm test` goes through `_run-tests.ts`, which sets the redirect once for+ * the whole run and checks the user's folders afterwards; this module then+ * sees `OM_QMD_ISOLATED` and does nothing. Node's test runner applies+ * `--import` only to the child process of each test FILE, never to the+ * runner, so without the wrapper each file gets its own temp folders here.+ *+ * See `_run-tests.ts` for why the redirect exists.+ */++import { mkdtempSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";++if (process.env["OM_QMD_ISOLATED"] === undefined) {+ const isolated = mkdtempSync(join(tmpdir(), "om-test-qmd-"));+ process.env["XDG_CACHE_HOME"] = join(isolated, "cache");+ process.env["QMD_CONFIG_DIR"] = join(isolated, "config", "qmd");+ process.env["OM_QMD_ISOLATED"] = isolated;+ process.on("exit", () => {+ try {+ rmSync(isolated, { recursive: true, force: true });+ } catch {+ /* a detached qmd may still hold a file; the OS cleans temp */+ }+ });+}diff --git a/.claude/scripts/tests/_run-tests.ts b/.claude/scripts/tests/_run-tests.tsnew file mode 100644index 0000000..08f69c4--- /dev/null+++ b/.claude/scripts/tests/_run-tests.ts@@ -0,0 +1,86 @@+/**+ * `npm test`: run the suite with QMD's cache and config redirected to a temp+ * folder, and fail if the run still wrote into the user's own.+ *+ * Any test that spawns a hook script can reach a real `qmd`. SessionStart+ * derives an index name from its temp vault's folder and runs `qmd status`,+ * `update` or the bootstrap against it, and qmd writes+ * `<cache>/qmd/<index>.sqlite` (and can write `<config>/qmd/<index>.yml`)+ * for every name it meets. Left alone, every run on a machine with qmd+ * installed added one store per temp vault to the user's cache.+ *+ * This wrapper:+ * 1. records the stores (`.sqlite`) and configs (`.yml`) already in the+ * user's qmd folders;+ * 2. sets `XDG_CACHE_HOME` and `QMD_CONFIG_DIR` to a fresh temp folder for+ * the runner, which every test file and every subprocess inherits. qmd+ * reads both (config: `QMD_CONFIG_DIR`, then `XDG_CONFIG_HOME/qmd`, then+ * `~/.config/qmd`), and so do OM's own path helpers. `XDG_CONFIG_HOME`+ * itself is left alone: it also moves git's global config, which the+ * tests' git calls must keep seeing;+ * 3. after the run, fails if a new store or config appeared in the user's+ * folders, naming each, then removes the temp folder.+ *+ * A test that builds a subprocess env from scratch, without `process.env`,+ * escapes the redirect; step 3 is what catches it. The wrapper exists+ * because Node's runner applies `--import` to each test file's process and+ * never to itself, so a preload cannot see the run as a whole.+ */++import { spawnSync } from "node:child_process";+import { mkdtempSync, readdirSync, rmSync } from "node:fs";+import { homedir, tmpdir } from "node:os";+import { join } from "node:path";++const env = process.env;+const watched: readonly { readonly dir: string; readonly ext: string }[] = [+ { dir: join(env["XDG_CACHE_HOME"] ?? join(homedir(), ".cache"), "qmd"), ext: ".sqlite" },+ {+ dir: env["QMD_CONFIG_DIR"] ?? join(env["XDG_CONFIG_HOME"] ?? join(homedir(), ".config"), "qmd"),+ ext: ".yml",+ },+];++function listing(dir: string, ext: string): Set<string> {+ try {+ return new Set(readdirSync(dir).filter((f) => f.endsWith(ext)));+ } catch {+ return new Set();+ }+}++const before = watched.map(({ dir, ext }) => listing(dir, ext));+const isolated = mkdtempSync(join(tmpdir(), "om-test-qmd-"));++const run = spawnSync(+ process.execPath,+ ["--experimental-strip-types", "--import", "./tests/_qmd-cache-guard.ts", "--test", ...process.argv.slice(2)],+ {+ stdio: "inherit",+ env: {+ ...env,+ XDG_CACHE_HOME: join(isolated, "cache"),+ QMD_CONFIG_DIR: join(isolated, "config", "qmd"),+ OM_QMD_ISOLATED: isolated,+ },+ },+);++const added = watched.flatMap(({ dir, ext }, i) =>+ [...listing(dir, ext)].filter((f) => !before[i]!.has(f)).sort().map((f) => join(dir, f)),+);+try {+ rmSync(isolated, { recursive: true, force: true });+} catch {+ /* a detached qmd may still hold a file; the OS cleans temp */+}++if (added.length > 0) {+ process.stderr.write(+ `\nqmd cache guard: the test run created ${added.length} file(s) in the user's qmd folders:\n` ++ added.map((f) => ` ${f}`).join("\n") ++ "\nA test reached qmd with the user's environment. Route its subprocess env through process.env.\n",+ );+ process.exit(1);+}+process.exit(run.status ?? 1);diff --git a/.claude/scripts/tests/qmd-ignore.test.ts b/.claude/scripts/tests/qmd-ignore.test.tsindex b79a2a7..b3007f4 100644--- a/.claude/scripts/tests/qmd-ignore.test.ts+++ b/.claude/scripts/tests/qmd-ignore.test.ts@@ -332,14 +332,36 @@ describe("writeQmdIgnore — file IO wrapper", () => {});});+/**+ * Mirrors qmd's own `getConfigDir()`: QMD_CONFIG_DIR, then XDG_CONFIG_HOME/qmd,+ * then <home>/.config/qmd. A hard-coded ~/.config/qmd wrote where qmd never+ * reads whenever an override was set.+ */describe("qmdConfigPath", () => {- test("composes path from homedir, .config/qmd, and index name", () => {- const p = qmdConfigPath("obsidian-mind");- // Normalize separators so the assertion works on both POSIX (`/`) and- // Windows (`\`). path.join picks the platform-native separator.- const normalized = p.replaceAll("\\", "/");- assert.ok(normalized.endsWith("/.config/qmd/obsidian-mind.yml"));- // Absolute path, not relative.+ const norm = (p: string) => p.replaceAll("\\", "/");++ test("QMD_CONFIG_DIR wins over everything", () => {+ const p = qmdConfigPath("v", { QMD_CONFIG_DIR: "/qcfg", XDG_CONFIG_HOME: "/xdg", HOME: "/h" });+ assert.equal(norm(p), "/qcfg/v.yml");+ });++ test("XDG_CONFIG_HOME/qmd when QMD_CONFIG_DIR is unset", () => {+ const p = qmdConfigPath("v", { XDG_CONFIG_HOME: "/xdg", HOME: "/h" });+ assert.equal(norm(p), "/xdg/qmd/v.yml");+ });++ test("<home>/.config/qmd otherwise, HOME before USERPROFILE", () => {+ assert.equal(norm(qmdConfigPath("v", { HOME: "/h", USERPROFILE: "/u" })), "/h/.config/qmd/v.yml");+ assert.equal(norm(qmdConfigPath("v", { USERPROFILE: "/u" })), "/u/.config/qmd/v.yml");+ });++ test("an empty override is treated as unset, as qmd's truthiness check does", () => {+ assert.equal(norm(qmdConfigPath("v", { QMD_CONFIG_DIR: "", XDG_CONFIG_HOME: "", HOME: "/h" })), "/h/.config/qmd/v.yml");+ });++ test("with no env at all, an absolute path under the OS home", () => {+ const p = qmdConfigPath("obsidian-mind", {});+ assert.ok(norm(p).endsWith("/.config/qmd/obsidian-mind.yml"));assert.ok(p.startsWith("/") || /^[A-Za-z]:[/\\]/.test(p));});});