workspaceHookDiscoveryDedup.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import {
  6. discoverWorkspaceHookConfigPaths,
  7. readWorkspaceHookProjectSources,
  8. } from "../src/workspace-hook-discovery.js";
  9. const roots: string[] = [];
  10. function makeRoot(): string {
  11. const root = mkdtempSync(join(tmpdir(), "zcode-hook-dedup-"));
  12. roots.push(root);
  13. return root;
  14. }
  15. function writeJson(path: string, value: unknown): void {
  16. mkdirSync(join(path, ".."), { recursive: true });
  17. writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`, "utf8");
  18. }
  19. afterEach(async () => {
  20. const { rm } = await import("node:fs/promises");
  21. await Promise.all(roots.splice(0).map((root) => rm(root, { force: true, recursive: true })));
  22. });
  23. describe("workspace hook config discovery 去重(#10)", () => {
  24. it("explicit path 与 auto-discovery 重叠时只保留一个条目", () => {
  25. const workspace = makeRoot();
  26. mkdirSync(join(workspace, ".git"));
  27. const configPath = join(workspace, ".zcode", "config.json");
  28. writeJson(configPath, {
  29. hooks: {
  30. enabled: true,
  31. events: {
  32. SessionStart: [{ hooks: [{ type: "command", command: "echo hello" }] }],
  33. },
  34. },
  35. });
  36. // 不传 explicit:auto-discovery 发现一个文件
  37. const withoutExplicit = discoverWorkspaceHookConfigPaths({
  38. workingDirectory: workspace,
  39. });
  40. expect(withoutExplicit).toHaveLength(1);
  41. expect(withoutExplicit[0]?.explicitProjectConfig).toBe(false);
  42. // 传 explicit 且指向同一文件:去重后仍只有一个条目
  43. const withExplicit = discoverWorkspaceHookConfigPaths({
  44. workingDirectory: workspace,
  45. explicitProjectConfigPath: configPath,
  46. });
  47. expect(withExplicit).toHaveLength(1);
  48. // 首次出现者(auto-discovered)保留,explicit 重复条目被丢弃
  49. expect(withExplicit[0]?.explicitProjectConfig).toBe(false);
  50. expect(withExplicit[0]?.path).toBe(configPath);
  51. });
  52. it("explicit path 指向 auto-discovery 未发现的文件时正常追加", () => {
  53. const workspace = makeRoot();
  54. mkdirSync(join(workspace, ".git"));
  55. writeJson(join(workspace, ".zcode", "config.json"), {
  56. hooks: {
  57. enabled: true,
  58. events: {
  59. SessionStart: [{ hooks: [{ type: "command", command: "echo hello" }] }],
  60. },
  61. },
  62. });
  63. // explicit 指向一个额外文件
  64. const extraPath = join(workspace, "custom-config.json");
  65. writeJson(extraPath, {
  66. hooks: {
  67. enabled: true,
  68. events: {
  69. Stop: [{ hooks: [{ type: "command", command: "echo stop" }] }],
  70. },
  71. },
  72. });
  73. const refs = discoverWorkspaceHookConfigPaths({
  74. workingDirectory: workspace,
  75. explicitProjectConfigPath: extraPath,
  76. });
  77. expect(refs).toHaveLength(2);
  78. expect(refs[0]?.explicitProjectConfig).toBe(false);
  79. expect(refs[1]?.explicitProjectConfig).toBe(true);
  80. expect(refs[1]?.path).toBe(extraPath);
  81. });
  82. it("无 explicit path 时异步 discovery 从零编号且保留来源", async () => {
  83. const workspace = makeRoot();
  84. mkdirSync(join(workspace, ".git"));
  85. writeJson(join(workspace, ".zcode", "config.json"), {
  86. hooks: {
  87. enabled: true,
  88. events: {
  89. SessionStart: [{ hooks: [{ type: "command", command: "echo hello" }] }],
  90. },
  91. },
  92. });
  93. const { sources } = await readWorkspaceHookProjectSources({
  94. workingDirectory: workspace,
  95. });
  96. expect(sources).toHaveLength(1);
  97. expect(sources[0]?.discoveryOrder).toBe(0);
  98. expect(sources[0]?.explicitProjectConfig).toBe(false);
  99. });
  100. it("explicit 重叠时 async discovery 也只产生一个 source", async () => {
  101. const workspace = makeRoot();
  102. mkdirSync(join(workspace, ".git"));
  103. const configPath = join(workspace, ".zcode", "config.json");
  104. writeJson(configPath, {
  105. hooks: {
  106. enabled: true,
  107. events: {
  108. SessionStart: [{ hooks: [{ type: "command", command: "echo hello" }] }],
  109. },
  110. },
  111. });
  112. const { sources } = await readWorkspaceHookProjectSources({
  113. workingDirectory: workspace,
  114. explicitProjectConfigPath: configPath,
  115. });
  116. // 去重后只应有一个 source(auto-discovered),explicit 重复被丢弃
  117. expect(sources).toHaveLength(1);
  118. expect(sources[0]?.explicitProjectConfig).toBe(false);
  119. expect(sources[0]?.discoveryOrder).toBe(0);
  120. });
  121. });