workspaceHookDiscoveryDedup.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. buildWorkspaceHookBundleSnapshot,
  7. discoverWorkspaceHookConfigPaths,
  8. readWorkspaceHookProjectSources,
  9. } from "../src/workspace-hook-discovery.js";
  10. const roots: string[] = [];
  11. function makeRoot(): string {
  12. const root = mkdtempSync(join(tmpdir(), "zcode-hook-dedup-"));
  13. roots.push(root);
  14. return root;
  15. }
  16. function writeJson(path: string, value: unknown): void {
  17. mkdirSync(join(path, ".."), { recursive: true });
  18. writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`, "utf8");
  19. }
  20. afterEach(async () => {
  21. const { rm } = await import("node:fs/promises");
  22. await Promise.all(roots.splice(0).map((root) => rm(root, { force: true, recursive: true })));
  23. });
  24. describe("workspace hook config discovery 去重(#10)", () => {
  25. it("explicit path 与 auto-discovery 重叠时只保留一个条目", () => {
  26. const workspace = makeRoot();
  27. mkdirSync(join(workspace, ".git"));
  28. const configPath = join(workspace, ".zcode", "config.json");
  29. writeJson(configPath, {
  30. hooks: {
  31. enabled: true,
  32. events: {
  33. SessionStart: [{ hooks: [{ type: "command", command: "echo hello" }] }],
  34. },
  35. },
  36. });
  37. // 不传 explicit:auto-discovery 发现一个文件
  38. const withoutExplicit = discoverWorkspaceHookConfigPaths({
  39. workingDirectory: workspace,
  40. });
  41. expect(withoutExplicit).toHaveLength(1);
  42. expect(withoutExplicit[0]?.explicitProjectConfig).toBe(false);
  43. // 传 explicit 且指向同一文件:去重后仍只有一个条目
  44. const withExplicit = discoverWorkspaceHookConfigPaths({
  45. workingDirectory: workspace,
  46. explicitProjectConfigPath: configPath,
  47. });
  48. expect(withExplicit).toHaveLength(1);
  49. // 首次出现者(auto-discovered)保留,explicit 重复条目被丢弃
  50. expect(withExplicit[0]?.explicitProjectConfig).toBe(false);
  51. expect(withExplicit[0]?.path).toBe(configPath);
  52. });
  53. it("explicit path 指向 auto-discovery 未发现的文件时正常追加", () => {
  54. const workspace = makeRoot();
  55. mkdirSync(join(workspace, ".git"));
  56. writeJson(join(workspace, ".zcode", "config.json"), {
  57. hooks: {
  58. enabled: true,
  59. events: {
  60. SessionStart: [{ hooks: [{ type: "command", command: "echo hello" }] }],
  61. },
  62. },
  63. });
  64. // explicit 指向一个额外文件
  65. const extraPath = join(workspace, "custom-config.json");
  66. writeJson(extraPath, {
  67. hooks: {
  68. enabled: true,
  69. events: {
  70. Stop: [{ hooks: [{ type: "command", command: "echo stop" }] }],
  71. },
  72. },
  73. });
  74. const refs = discoverWorkspaceHookConfigPaths({
  75. workingDirectory: workspace,
  76. explicitProjectConfigPath: extraPath,
  77. });
  78. expect(refs).toHaveLength(2);
  79. expect(refs[0]?.explicitProjectConfig).toBe(false);
  80. expect(refs[1]?.explicitProjectConfig).toBe(true);
  81. expect(refs[1]?.path).toBe(extraPath);
  82. });
  83. it("无 explicit path 时 discovery 结果不变(digest 稳定性)", async () => {
  84. // 此前引入去重逻辑后,必须确认对无 explicit path 的 workspace 是 no-op,
  85. // 否则 auto-discovery 产生的 discoveryOrder 变化会使所有既有 trust 记录失效。
  86. const workspace = makeRoot();
  87. mkdirSync(join(workspace, ".git"));
  88. writeJson(join(workspace, ".zcode", "config.json"), {
  89. hooks: {
  90. enabled: true,
  91. events: {
  92. SessionStart: [{ hooks: [{ type: "command", command: "echo hello" }] }],
  93. },
  94. },
  95. });
  96. const { sources } = await readWorkspaceHookProjectSources({
  97. workingDirectory: workspace,
  98. });
  99. expect(sources).toHaveLength(1);
  100. expect(sources[0]?.discoveryOrder).toBe(0);
  101. expect(sources[0]?.explicitProjectConfig).toBe(false);
  102. // 构建 bundle snapshot 并确认 digest 可重复(同一输入两次构建结果一致)
  103. const snapshot1 = buildWorkspaceHookBundleSnapshot({
  104. workspaceIdentity: workspace,
  105. workspacePath: workspace,
  106. sources,
  107. runtimeRoot: { enabled: true, timeoutMs: 60_000, maxOutputBytes: 32_768 },
  108. });
  109. const snapshot2 = buildWorkspaceHookBundleSnapshot({
  110. workspaceIdentity: workspace,
  111. workspacePath: workspace,
  112. sources,
  113. runtimeRoot: { enabled: true, timeoutMs: 60_000, maxOutputBytes: 32_768 },
  114. });
  115. expect(snapshot1.bundleDigest).toBe(snapshot2.bundleDigest);
  116. expect(snapshot1.bundleDigest).toBeTruthy();
  117. });
  118. it("explicit 重叠时 async discovery 也只产生一个 source", async () => {
  119. const workspace = makeRoot();
  120. mkdirSync(join(workspace, ".git"));
  121. const configPath = join(workspace, ".zcode", "config.json");
  122. writeJson(configPath, {
  123. hooks: {
  124. enabled: true,
  125. events: {
  126. SessionStart: [{ hooks: [{ type: "command", command: "echo hello" }] }],
  127. },
  128. },
  129. });
  130. const { sources } = await readWorkspaceHookProjectSources({
  131. workingDirectory: workspace,
  132. explicitProjectConfigPath: configPath,
  133. });
  134. // 去重后只应有一个 source(auto-discovered),explicit 重复被丢弃
  135. expect(sources).toHaveLength(1);
  136. expect(sources[0]?.explicitProjectConfig).toBe(false);
  137. expect(sources[0]?.discoveryOrder).toBe(0);
  138. });
  139. });