collections-config.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. for (const [key, val] of Object.entries(savedEnv)) {
  23. if (val === undefined) {
  24. delete process.env[key];
  25. } else {
  26. process.env[key] = val;
  27. }
  28. }
  29. });
  30. describe("getConfigDir via getConfigPath", () => {
  31. test("defaults to ~/.config/qmd when no env vars are set", () => {
  32. delete process.env.QMD_CONFIG_DIR;
  33. delete process.env.XDG_CONFIG_HOME;
  34. expect(getConfigPath()).toBe(join(homedir(), ".config", "qmd", "index.yml"));
  35. });
  36. test("QMD_CONFIG_DIR takes highest priority", () => {
  37. process.env.QMD_CONFIG_DIR = "/custom/qmd-config";
  38. process.env.XDG_CONFIG_HOME = "/xdg/config";
  39. expect(getConfigPath()).toBe(join("/custom/qmd-config", "index.yml"));
  40. });
  41. test("XDG_CONFIG_HOME is used when QMD_CONFIG_DIR is not set", () => {
  42. delete process.env.QMD_CONFIG_DIR;
  43. process.env.XDG_CONFIG_HOME = "/xdg/config";
  44. expect(getConfigPath()).toBe(join("/xdg/config", "qmd", "index.yml"));
  45. });
  46. test("XDG_CONFIG_HOME appends qmd subdirectory", () => {
  47. delete process.env.QMD_CONFIG_DIR;
  48. process.env.XDG_CONFIG_HOME = "/home/agent/.config";
  49. expect(getConfigPath()).toBe(join("/home/agent/.config", "qmd", "index.yml"));
  50. });
  51. test("QMD_CONFIG_DIR overrides XDG_CONFIG_HOME", () => {
  52. process.env.QMD_CONFIG_DIR = "/override";
  53. process.env.XDG_CONFIG_HOME = "/should-not-use";
  54. expect(getConfigPath()).toBe(join("/override", "index.yml"));
  55. });
  56. test("respects custom index name", () => {
  57. delete process.env.QMD_CONFIG_DIR;
  58. process.env.XDG_CONFIG_HOME = "/xdg/config";
  59. setConfigIndexName("myindex");
  60. expect(getConfigPath()).toBe(join("/xdg/config", "qmd", "myindex.yml"));
  61. });
  62. });