collections-config.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Unit tests for collection config path resolution (PR #190).
  3. *
  4. * Tests that getConfigDir() respects XDG_CONFIG_HOME, QMD_CONFIG_DIR,
  5. * and falls back to ~/.config/qmd.
  6. */
  7. import { describe, test, expect, beforeEach, afterEach } from "vitest";
  8. import { join } from "path";
  9. import { homedir } from "os";
  10. import { getConfigPath, setConfigIndexName } from "../src/collections.js";
  11. // Save/restore env vars around each test
  12. let savedEnv: Record<string, string | undefined>;
  13. beforeEach(() => {
  14. savedEnv = {
  15. QMD_CONFIG_DIR: process.env.QMD_CONFIG_DIR,
  16. XDG_CONFIG_HOME: process.env.XDG_CONFIG_HOME,
  17. };
  18. // Reset index name to default
  19. setConfigIndexName("index");
  20. });
  21. afterEach(() => {
  22. // Reset index name to default (prevents leaking into other test files under bun test)
  23. setConfigIndexName("index");
  24. for (const [key, val] of Object.entries(savedEnv)) {
  25. if (val === undefined) {
  26. delete process.env[key];
  27. } else {
  28. process.env[key] = val;
  29. }
  30. }
  31. });
  32. describe("getConfigDir via getConfigPath", () => {
  33. test("defaults to ~/.config/qmd when no env vars are set", () => {
  34. delete process.env.QMD_CONFIG_DIR;
  35. delete process.env.XDG_CONFIG_HOME;
  36. expect(getConfigPath()).toBe(join(homedir(), ".config", "qmd", "index.yml"));
  37. });
  38. test("QMD_CONFIG_DIR takes highest priority", () => {
  39. process.env.QMD_CONFIG_DIR = "/custom/qmd-config";
  40. process.env.XDG_CONFIG_HOME = "/xdg/config";
  41. expect(getConfigPath()).toBe(join("/custom/qmd-config", "index.yml"));
  42. });
  43. test("XDG_CONFIG_HOME is used when QMD_CONFIG_DIR is not set", () => {
  44. delete process.env.QMD_CONFIG_DIR;
  45. process.env.XDG_CONFIG_HOME = "/xdg/config";
  46. expect(getConfigPath()).toBe(join("/xdg/config", "qmd", "index.yml"));
  47. });
  48. test("XDG_CONFIG_HOME appends qmd subdirectory", () => {
  49. delete process.env.QMD_CONFIG_DIR;
  50. process.env.XDG_CONFIG_HOME = "/home/agent/.config";
  51. expect(getConfigPath()).toBe(join("/home/agent/.config", "qmd", "index.yml"));
  52. });
  53. test("QMD_CONFIG_DIR overrides XDG_CONFIG_HOME", () => {
  54. process.env.QMD_CONFIG_DIR = "/override";
  55. process.env.XDG_CONFIG_HOME = "/should-not-use";
  56. expect(getConfigPath()).toBe(join("/override", "index.yml"));
  57. });
  58. test("respects custom index name", () => {
  59. delete process.env.QMD_CONFIG_DIR;
  60. process.env.XDG_CONFIG_HOME = "/xdg/config";
  61. setConfigIndexName("myindex");
  62. expect(getConfigPath()).toBe(join("/xdg/config", "qmd", "myindex.yml"));
  63. });
  64. });