qmd/test/collections-config.test.ts
Tobi Lutke 648779a04d
fix(test): reset currentIndexName between test files
collections-config.test.ts set currentIndexName to "myindex" in its
last test but only restored env vars in afterEach — not the module
variable. Under bun test (single process), this leaked into mcp.test.ts,
causing it to look for myindex.yml instead of index.yml.

Fix: reset setConfigIndexName("index") in afterEach, and add defensive
reset in mcp.test.ts beforeAll.
2026-02-18 15:53:58 -04:00

75 lines
2.4 KiB
TypeScript

/**
* Unit tests for collection config path resolution (PR #190).
*
* Tests that getConfigDir() respects XDG_CONFIG_HOME, QMD_CONFIG_DIR,
* and falls back to ~/.config/qmd.
*/
import { describe, test, expect, beforeEach, afterEach } from "vitest";
import { join } from "path";
import { homedir } from "os";
import { getConfigPath, setConfigIndexName } from "../src/collections.js";
// Save/restore env vars around each test
let savedEnv: Record<string, string | undefined>;
beforeEach(() => {
savedEnv = {
QMD_CONFIG_DIR: process.env.QMD_CONFIG_DIR,
XDG_CONFIG_HOME: process.env.XDG_CONFIG_HOME,
};
// Reset index name to default
setConfigIndexName("index");
});
afterEach(() => {
// Reset index name to default (prevents leaking into other test files under bun test)
setConfigIndexName("index");
for (const [key, val] of Object.entries(savedEnv)) {
if (val === undefined) {
delete process.env[key];
} else {
process.env[key] = val;
}
}
});
describe("getConfigDir via getConfigPath", () => {
test("defaults to ~/.config/qmd when no env vars are set", () => {
delete process.env.QMD_CONFIG_DIR;
delete process.env.XDG_CONFIG_HOME;
expect(getConfigPath()).toBe(join(homedir(), ".config", "qmd", "index.yml"));
});
test("QMD_CONFIG_DIR takes highest priority", () => {
process.env.QMD_CONFIG_DIR = "/custom/qmd-config";
process.env.XDG_CONFIG_HOME = "/xdg/config";
expect(getConfigPath()).toBe(join("/custom/qmd-config", "index.yml"));
});
test("XDG_CONFIG_HOME is used when QMD_CONFIG_DIR is not set", () => {
delete process.env.QMD_CONFIG_DIR;
process.env.XDG_CONFIG_HOME = "/xdg/config";
expect(getConfigPath()).toBe(join("/xdg/config", "qmd", "index.yml"));
});
test("XDG_CONFIG_HOME appends qmd subdirectory", () => {
delete process.env.QMD_CONFIG_DIR;
process.env.XDG_CONFIG_HOME = "/home/agent/.config";
expect(getConfigPath()).toBe(join("/home/agent/.config", "qmd", "index.yml"));
});
test("QMD_CONFIG_DIR overrides XDG_CONFIG_HOME", () => {
process.env.QMD_CONFIG_DIR = "/override";
process.env.XDG_CONFIG_HOME = "/should-not-use";
expect(getConfigPath()).toBe(join("/override", "index.yml"));
});
test("respects custom index name", () => {
delete process.env.QMD_CONFIG_DIR;
process.env.XDG_CONFIG_HOME = "/xdg/config";
setConfigIndexName("myindex");
expect(getConfigPath()).toBe(join("/xdg/config", "qmd", "myindex.yml"));
});
});