@boyingliu01/xp-gate 0.38.126 → 0.39.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (291) hide show
  1. package/README.md +93 -0
  2. package/adapters/gate-3.sh +13 -2
  3. package/adapters/python.sh +36 -38
  4. package/bin/xp-gate.js +11 -0
  5. package/build-integrity/gate-10-lines.ts +98 -0
  6. package/build-integrity/gate-10.ts +98 -109
  7. package/build-integrity/quote-masker.ts +17 -3
  8. package/gate-3.sh +13 -2
  9. package/gates/common-process.ts +116 -34
  10. package/gates/common.ts +51 -15
  11. package/gates/gate-3.ts +166 -61
  12. package/gates/gate-8.ts +76 -24
  13. package/gates/gate-9.ts +276 -82
  14. package/gates/pbt-detect.ts +70 -39
  15. package/gates/python-health.ts +236 -98
  16. package/gates/test-layers.ts +103 -30
  17. package/hooks/gate-3.sh +13 -2
  18. package/hooks/gate-entries/gate-1-entry.sh +10 -1
  19. package/hooks/gate-entries/gate-5-entry.sh +5 -1
  20. package/hooks/lib/validate-code-walkthrough.cjs +324 -99
  21. package/hooks/pre-commit +52 -0
  22. package/hooks/pre-push +54 -3
  23. package/lib/agent-runtime-journal.js +104 -37
  24. package/lib/arch.js +6 -0
  25. package/lib/baseline.js +329 -201
  26. package/lib/check.js +38 -2
  27. package/lib/doctor-constants.js +43 -0
  28. package/lib/doctor.js +2008 -1001
  29. package/lib/flaky.js +178 -79
  30. package/lib/gate-runner.js +110 -63
  31. package/lib/init.js +6 -0
  32. package/lib/language-capability-surfaces.js +5 -2
  33. package/lib/language-registry.js +608 -0
  34. package/lib/language-tools.js +487 -1018
  35. package/lib/phase-transition.js +646 -446
  36. package/lib/prepush-evidence.js +174 -0
  37. package/lib/sprint-audit.js +52 -22
  38. package/lib/sprint-discovery.js +66 -35
  39. package/lib/sprint-format.js +114 -0
  40. package/lib/sprint-init.js +372 -118
  41. package/lib/sprint-status.js +134 -90
  42. package/lib/star-notice.js +346 -0
  43. package/lib/tool-display.js +52 -0
  44. package/lib/tool-install.js +179 -0
  45. package/lib/uninstall.js +331 -188
  46. package/mock-policy/gate-m3.ts +74 -23
  47. package/mock-policy/mock-decision-engine.ts +21 -7
  48. package/mock-policy/scope-scanner.ts +128 -69
  49. package/mutation/detect-ai-test.ts +20 -12
  50. package/mutation/gate-m.ts +6 -2
  51. package/mutation/init-baseline.ts +49 -13
  52. package/mutation/runners/go-mutant-runner.ts +7 -49
  53. package/mutation/runners/gremlins-runner.ts +53 -18
  54. package/mutation/runners/mutmut-runner.ts +455 -163
  55. package/mutation/runners/pitest-runner.ts +85 -131
  56. package/mutation/runners/spawn-with-capture.ts +304 -0
  57. package/mutation/runners/stryker-runner.ts +14 -55
  58. package/mutation/update-baseline.ts +25 -18
  59. package/package.json +20 -8
  60. package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
  61. package/plugins/claude-code/bin/delphi-review-guard.sh +12 -61
  62. package/plugins/claude-code/skills/delphi-review/.delphi-config.json.example +6 -11
  63. package/plugins/claude-code/skills/delphi-review/AGENTS.md +1 -1
  64. package/plugins/claude-code/skills/sprint-flow/AGENTS.md +1 -1
  65. package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +1 -1
  66. package/plugins/dsh/package.json +1 -1
  67. package/plugins/dsh/scripts/prepack.cjs +43 -18
  68. package/plugins/dsh/scripts/verify-skill-provider.mjs +39 -14
  69. package/plugins/dsh/skills/delphi-review/.delphi-config.json.example +6 -11
  70. package/plugins/dsh/skills/delphi-review/AGENTS.md +1 -1
  71. package/plugins/dsh/skills/sprint-flow/AGENTS.md +1 -1
  72. package/plugins/dsh/skills/test-specification-alignment/AGENTS.md +1 -1
  73. package/plugins/opencode/index.ts +337 -123
  74. package/plugins/opencode/package.json +1 -1
  75. package/plugins/opencode/scripts/prepack.cjs +41 -16
  76. package/plugins/opencode/skills/delphi-review/.delphi-config.json.example +6 -11
  77. package/plugins/opencode/skills/delphi-review/AGENTS.md +1 -1
  78. package/plugins/opencode/skills/sprint-flow/AGENTS.md +1 -1
  79. package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +1 -1
  80. package/plugins/qoder/bin/sprint-flow-guard.sh +12 -60
  81. package/plugins/qoder/plugin.json +1 -1
  82. package/plugins/qoder/skills/delphi-review/.delphi-config.json.example +6 -11
  83. package/plugins/qoder/skills/delphi-review/AGENTS.md +1 -1
  84. package/plugins/qoder/skills/sprint-flow/AGENTS.md +1 -1
  85. package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +1 -1
  86. package/plugins/shared/README.md +3 -0
  87. package/plugins/shared/bin/delphi-review-guard.sh +68 -0
  88. package/principles/adapters/control-nesting.ts +481 -168
  89. package/principles/adapters/cpp.ts +61 -22
  90. package/principles/adapters/csharp.ts +183 -66
  91. package/principles/adapters/dart.ts +11 -1
  92. package/principles/adapters/duplication.ts +49 -16
  93. package/principles/adapters/import-clause.ts +62 -15
  94. package/principles/adapters/java.ts +199 -70
  95. package/principles/adapters/line-comment.ts +18 -0
  96. package/principles/adapters/member-scan.ts +50 -8
  97. package/principles/adapters/php.ts +232 -103
  98. package/principles/adapters/python.ts +25 -18
  99. package/principles/adapters/quote-state.ts +21 -8
  100. package/principles/adapters/typescript-mask.ts +276 -0
  101. package/principles/adapters/typescript.ts +206 -200
  102. package/principles/analyzer.ts +16 -6
  103. package/principles/baseline-prune.ts +18 -8
  104. package/principles/baseline.ts +28 -11
  105. package/principles/boy-scout-usage.ts +26 -0
  106. package/principles/boy-scout.ts +261 -180
  107. package/principles/config.ts +183 -57
  108. package/principles/direct-execution.ts +24 -12
  109. package/principles/lint-baseline.ts +235 -122
  110. package/principles/rules/arch/dependency.ts +34 -25
  111. package/principles/rules/arch/shared.ts +24 -20
  112. package/principles/rules/clean-code/deep-nesting.ts +23 -13
  113. package/principles/rules/clean-code/god-class.ts +67 -41
  114. package/principles/rules/clean-code/large-file.ts +30 -23
  115. package/principles/rules/clean-code/long-function.ts +51 -37
  116. package/principles/rules/clean-code/magic-numbers.ts +51 -36
  117. package/principles/rules/clean-code/many-exports.ts +46 -29
  118. package/principles/rules/clean-code/missing-error-handling.ts +38 -20
  119. package/principles/rules/clean-code/too-many-params.ts +41 -22
  120. package/principles/rules/function-source.ts +62 -0
  121. package/principles/rules/solid/dip.ts +10 -6
  122. package/principles/rules/solid/isp.ts +64 -24
  123. package/principles/rules/solid/lsp.ts +66 -18
  124. package/principles/rules/solid/ocp.ts +29 -13
  125. package/principles/rules/solid/srp.ts +34 -18
  126. package/principles/warnings-count.ts +43 -0
  127. package/scripts/__tests__/prepush-source-classification.test.js +129 -0
  128. package/scripts/delphi-config-read.cjs +245 -0
  129. package/scripts/delphi-external-review.cjs +410 -585
  130. package/scripts/delphi-json-extract.cjs +108 -0
  131. package/scripts/delphi-model-validation.cjs +297 -0
  132. package/scripts/delphi-prompts.cjs +55 -0
  133. package/scripts/delphi-repo-tools.cjs +273 -0
  134. package/scripts/delphi-seat-calibration.cjs +204 -0
  135. package/scripts/run-bats-ci.cjs +240 -97
  136. package/scripts/sync-package-content.js +18 -2
  137. package/skills/delphi-review/.delphi-config.json.example +6 -11
  138. package/skills/delphi-review/AGENTS.md +1 -1
  139. package/skills/sprint-flow/AGENTS.md +1 -1
  140. package/skills/test-specification-alignment/AGENTS.md +1 -1
  141. package/lib/__tests__/agent-runtime-journal.test.js +0 -326
  142. package/lib/__tests__/arch-config-template.test.js +0 -190
  143. package/lib/__tests__/arch.test.js +0 -430
  144. package/lib/__tests__/assertion-debt.test.js +0 -99
  145. package/lib/__tests__/audit-cli-contract.test.ts +0 -117
  146. package/lib/__tests__/audit-log.test.ts +0 -107
  147. package/lib/__tests__/audit-skip-reasons.test.ts +0 -143
  148. package/lib/__tests__/baseline-completeness.test.js +0 -264
  149. package/lib/__tests__/baseline-shell-scope.test.js +0 -60
  150. package/lib/__tests__/baseline-unevaluated.test.js +0 -142
  151. package/lib/__tests__/baseline.test.js +0 -212
  152. package/lib/__tests__/bootstrap.test.js +0 -60
  153. package/lib/__tests__/check-alignment-cli.test.mjs +0 -94
  154. package/lib/__tests__/check-alignment-reasons.test.mjs +0 -101
  155. package/lib/__tests__/check-all-skip-accounting.test.js +0 -171
  156. package/lib/__tests__/check-changed-brief.test.js +0 -63
  157. package/lib/__tests__/check-normalize-exit.test.js +0 -24
  158. package/lib/__tests__/check-single-file.test.js +0 -114
  159. package/lib/__tests__/check-tools-list.test.js +0 -155
  160. package/lib/__tests__/check-version.test.js +0 -463
  161. package/lib/__tests__/coverage-scope.test.js +0 -140
  162. package/lib/__tests__/coverage-threshold.test.js +0 -80
  163. package/lib/__tests__/crap-cli.test.js +0 -87
  164. package/lib/__tests__/crap.test.js +0 -494
  165. package/lib/__tests__/debt-classify.test.js +0 -78
  166. package/lib/__tests__/delphi-channel-doctor.test.js +0 -116
  167. package/lib/__tests__/delphi-channel-uninstall.test.js +0 -128
  168. package/lib/__tests__/delphi-channel.test.js +0 -109
  169. package/lib/__tests__/delphi-setup.test.js +0 -203
  170. package/lib/__tests__/deploy-manifest-check-wired.test.js +0 -138
  171. package/lib/__tests__/deploy-manifest-completeness.test.js +0 -151
  172. package/lib/__tests__/deploy-manifest-consumers-adapter-uninstall.test.js +0 -40
  173. package/lib/__tests__/deploy-manifest-consumers-no-private-lists.test.js +0 -74
  174. package/lib/__tests__/deploy-manifest-projection.test.js +0 -66
  175. package/lib/__tests__/deploy-manifest-shell-parity.test.js +0 -108
  176. package/lib/__tests__/deploy-manifest.test.js +0 -97
  177. package/lib/__tests__/detect-deps-dsh-profile.test.js +0 -323
  178. package/lib/__tests__/detect-deps-gate-identity.test.js +0 -135
  179. package/lib/__tests__/detect-deps.test.js +0 -655
  180. package/lib/__tests__/doctor-469-hook-env.test.js +0 -211
  181. package/lib/__tests__/doctor-arch-tools.test.js +0 -169
  182. package/lib/__tests__/doctor-audit-ledger.test.js +0 -61
  183. package/lib/__tests__/doctor-brief.test.js +0 -59
  184. package/lib/__tests__/doctor-delphi.test.js +0 -628
  185. package/lib/__tests__/doctor-drift-direction.test.js +0 -104
  186. package/lib/__tests__/doctor-drift-round2.test.js +0 -215
  187. package/lib/__tests__/doctor-exec-retry.test.js +0 -162
  188. package/lib/__tests__/doctor-executed-surface.test.js +0 -230
  189. package/lib/__tests__/doctor-helpers.test.js +0 -139
  190. package/lib/__tests__/doctor-hook-drift.test.js +0 -329
  191. package/lib/__tests__/doctor-hook-signatures.test.js +0 -65
  192. package/lib/__tests__/doctor-machine-mutation.test.js +0 -116
  193. package/lib/__tests__/doctor-module-drift.test.js +0 -212
  194. package/lib/__tests__/doctor-module-resolution-parity.test.js +0 -463
  195. package/lib/__tests__/doctor-path-equivalence.test.js +0 -70
  196. package/lib/__tests__/doctor-repair-boundary.test.js +0 -52
  197. package/lib/__tests__/doctor-signature-stability.test.js +0 -55
  198. package/lib/__tests__/doctor-spawn-capability.test.js +0 -116
  199. package/lib/__tests__/doctor-timeout-partial.test.js +0 -79
  200. package/lib/__tests__/doctor-tool-delegation.test.js +0 -156
  201. package/lib/__tests__/doctor-tui-skill-freshness.test.js +0 -385
  202. package/lib/__tests__/doctor.test.js +0 -1320
  203. package/lib/__tests__/find-bash-wsl.test.js +0 -121
  204. package/lib/__tests__/flaky.test.js +0 -128
  205. package/lib/__tests__/foreign-hooks-global.test.js +0 -125
  206. package/lib/__tests__/gate-audit-skip.test.ts +0 -81
  207. package/lib/__tests__/gate-audit.test.ts +0 -440
  208. package/lib/__tests__/gate-coverage-hook-floor.test.js +0 -94
  209. package/lib/__tests__/gate-coverage.test.js +0 -204
  210. package/lib/__tests__/gate-entry-provenance.test.js +0 -71
  211. package/lib/__tests__/gate-numbering.test.js +0 -79
  212. package/lib/__tests__/gate-rules.test.js +0 -58
  213. package/lib/__tests__/gate-runner-tsx-invocation.test.js +0 -149
  214. package/lib/__tests__/gate-runner-verdict.test.js +0 -105
  215. package/lib/__tests__/gate-runner.test.js +0 -371
  216. package/lib/__tests__/gate2-verdict.test.js +0 -183
  217. package/lib/__tests__/helpers/lib-sandbox.cjs +0 -55
  218. package/lib/__tests__/hook-health-path.test.js +0 -109
  219. package/lib/__tests__/hook-health.test.js +0 -85
  220. package/lib/__tests__/init-gitignore.test.js +0 -204
  221. package/lib/__tests__/init-gitleaks-notice.test.js +0 -57
  222. package/lib/__tests__/init.test.js +0 -530
  223. package/lib/__tests__/install-cmd.test.js +0 -6
  224. package/lib/__tests__/install-sh-deploy-manifest.test.js +0 -80
  225. package/lib/__tests__/install-skill-full-dir.test.js +0 -194
  226. package/lib/__tests__/install-skill.test.js +0 -416
  227. package/lib/__tests__/install-tools-repair.test.js +0 -263
  228. package/lib/__tests__/issue-519-phase-transition-guard.test.js +0 -233
  229. package/lib/__tests__/issue-566-sync-hooks-backup.test.js +0 -228
  230. package/lib/__tests__/issue-583-hook-deploy-log.test.js +0 -121
  231. package/lib/__tests__/issue-586-principles-cmdline.test.js +0 -218
  232. package/lib/__tests__/issue-589-check-all-gate3.test.js +0 -190
  233. package/lib/__tests__/issue-590-archlint-vacuous-pass.test.js +0 -138
  234. package/lib/__tests__/issue-618-gate-not-run.test.js +0 -100
  235. package/lib/__tests__/jscpd-entry-resolution.test.js +0 -143
  236. package/lib/__tests__/language-capability.test.js +0 -318
  237. package/lib/__tests__/language-tools-build-file-install.test.js +0 -82
  238. package/lib/__tests__/language-tools-detect.test.js +0 -301
  239. package/lib/__tests__/migrate.test.js +0 -232
  240. package/lib/__tests__/mirror-manifest.test.js +0 -64
  241. package/lib/__tests__/mirror-sync.test.js +0 -803
  242. package/lib/__tests__/next-sprint.test.js +0 -231
  243. package/lib/__tests__/npm-package.test.js +0 -140
  244. package/lib/__tests__/phase-transition.test.js +0 -1136
  245. package/lib/__tests__/principles-csharp-coverage.test.js +0 -227
  246. package/lib/__tests__/principles-directory.test.js +0 -87
  247. package/lib/__tests__/qoder-delphi-agents.test.js +0 -316
  248. package/lib/__tests__/retro-gate-m.test.js +0 -144
  249. package/lib/__tests__/retro.test.js +0 -131
  250. package/lib/__tests__/rework.test.js +0 -127
  251. package/lib/__tests__/rollback.test.js +0 -183
  252. package/lib/__tests__/runtime-journal.test.js +0 -164
  253. package/lib/__tests__/sastd-client.test.js +0 -514
  254. package/lib/__tests__/shared-paths-lazy-home.test.js +0 -67
  255. package/lib/__tests__/shared-paths.test.js +0 -130
  256. package/lib/__tests__/shared-utils-copyHooks-manifest.test.js +0 -69
  257. package/lib/__tests__/shared-utils.test.js +0 -180
  258. package/lib/__tests__/skill-flags.test.js +0 -48
  259. package/lib/__tests__/slo.test.js +0 -89
  260. package/lib/__tests__/sprint-audit.test.js +0 -207
  261. package/lib/__tests__/sprint-discovery.test.ts +0 -366
  262. package/lib/__tests__/sprint-state-manager.test.js +0 -310
  263. package/lib/__tests__/sprint-status-rework-704.test.js +0 -149
  264. package/lib/__tests__/sprint-status-rework.test.js +0 -329
  265. package/lib/__tests__/sprint-status.test.js +0 -193
  266. package/lib/__tests__/sprint-status.test.ts +0 -213
  267. package/lib/__tests__/test-alignment-java-702.test.ts +0 -177
  268. package/lib/__tests__/test-alignment-range-guard.test.ts +0 -82
  269. package/lib/__tests__/test-alignment.test.ts +0 -853
  270. package/lib/__tests__/tool-audit-collect.test.js +0 -184
  271. package/lib/__tests__/tool-audit-fixtures.js +0 -87
  272. package/lib/__tests__/tool-audit-latest.test.js +0 -84
  273. package/lib/__tests__/tool-audit.test.js +0 -428
  274. package/lib/__tests__/tsconfig.json +0 -8
  275. package/lib/__tests__/tsx-resolution.test.js +0 -89
  276. package/lib/__tests__/ui-detector.test.ts +0 -459
  277. package/lib/__tests__/ui-review.test.ts +0 -132
  278. package/lib/__tests__/uninstall-global-residue.test.js +0 -545
  279. package/lib/__tests__/uninstall-skill.test.js +0 -189
  280. package/lib/__tests__/uninstall.test.js +0 -717
  281. package/lib/__tests__/update-hooks.test.js +0 -737
  282. package/lib/__tests__/update-skill-flags.test.js +0 -78
  283. package/lib/__tests__/update-skill.test.js +0 -644
  284. package/lib/__tests__/upgrade-exec.test.js +0 -273
  285. package/lib/__tests__/upgrade-skill-sync.test.js +0 -65
  286. package/lib/__tests__/upgrade.test.js +0 -78
  287. package/lib/__tests__/windows-bash-probe.test.js +0 -123
  288. package/lib/check-alignment-runner.cjs +0 -97
  289. package/lib/coverage-total.sh +0 -92
  290. package/lib/layer-baseline.cjs +0 -94
  291. package/lib/mattpocock-license-verdict.json +0 -7
@@ -1,212 +0,0 @@
1
- /**
2
- * @test REQ-495 xp-gate doctor sees the gate modules that actually execute
3
- * @intent doctor 对全局目录只做存在性检查(checkAdapters),drift 检查只看
4
- * pre-commit/pre-push/post-merge + lib/;而 pre-commit 把 GATE_DIR 解析到
5
- * ~/.config/xp-gate/adapters(`if [ -f "$ADAPTER_DIR/gate-3.sh" ]`),
6
- * 门禁模块全部在那里执行。于是本仓库里 gate-4/gate-8/typescript.sh 与机器
7
- * 副本三份互异,doctor 却报全绿,#493 那类仓库侧修复在本地根本不会被执行
8
- * (#495)。更糟的是任何单向同步都会销毁另一侧的机器独有加固,所以判定
9
- * 必须给出"双向发散"这一态并拒绝静默覆盖。
10
- * @covers AC-495-01, AC-495-02, AC-495-03, AC-495-04, AC-495-05, AC-495-06
11
- */
12
- import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, utimesSync, existsSync } from 'node:fs';
13
- import { tmpdir } from 'node:os';
14
- import { join } from 'node:path';
15
- import { describe, expect, it } from 'vitest';
16
-
17
- /**
18
- * Build a repo + global-install pair. `spec` names each module's repo copy and
19
- * installed copy; null means "not present on that side".
20
- */
21
- function fixture(spec, options = {}) {
22
- const root = mkdtempSync(join(tmpdir(), 'xp-gate-drift-'));
23
- const repoHooks = join(root, 'githooks');
24
- const installedHooks = join(root, 'hooks');
25
- const installedAdapters = join(root, 'adapters');
26
- mkdirSync(join(repoHooks, 'adapters'), { recursive: true });
27
- mkdirSync(installedHooks, { recursive: true });
28
- mkdirSync(installedAdapters, { recursive: true });
29
-
30
- const write = (file, text) => {
31
- mkdirSync(join(file, '..'), { recursive: true });
32
- writeFileSync(file, text);
33
- };
34
-
35
- for (const [name, sides] of Object.entries(spec)) {
36
- if (sides.repo !== null) {
37
- // gate-*/adapter-common.sh live at the githooks root; language adapters
38
- // live under githooks/adapters/ -- the same split the installer uses.
39
- const rootLevel = name.startsWith('gate-') || name === 'adapter-common.sh' || name.startsWith('lib/');
40
- write(join(repoHooks, rootLevel ? name : join('adapters', name)), sides.repo);
41
- }
42
- if (sides.installed !== null) {
43
- write(join(installedAdapters, sides.nested ? join('adapters', name) : name), sides.installed);
44
- }
45
- if (sides.repoMtime) utimesSync(join(repoHooks, name), sides.repoMtime, sides.repoMtime);
46
- }
47
-
48
- // A canonical hook source is what makes the drift surface apply at all.
49
- write(join(repoHooks, 'pre-commit'), '#!/bin/bash\necho repo\n');
50
- write(join(installedHooks, 'pre-commit'), '#!/bin/bash\necho repo\n');
51
-
52
- // Resolution anchors. Which tier OWNS the anchor decides which tier executes:
53
- // precedence is project-first (AC-495-23), so writing adapter-common.sh into
54
- // the repo tree would make the repo tier run and these global-vs-repo drift
55
- // comparisons would have no executed counterpart to describe.
56
- //
57
- // The default here is therefore the GLOBAL-anchored world: a repo that carries
58
- // faithful copies in githooks/ but has not taken the tier over. Consumers that
59
- // never run `xp-gate init` are in exactly this shape, and it is what the drift
60
- // surface is for. Tests that want the project tier to win pass
61
- // { projectTakesOver: true }.
62
- const ANCHOR = '#!/bin/bash\nanchor\n';
63
- const projectOwnsAnchor = options.projectTakesOver === true;
64
- for (const anchor of ['adapter-common.sh', 'gate-3.sh']) {
65
- if (spec[anchor]) continue;
66
- if (projectOwnsAnchor) write(join(repoHooks, anchor), ANCHOR);
67
- write(join(installedAdapters, anchor), ANCHOR);
68
- }
69
-
70
- return { repoHooks, installedHooks, installedAdapters, root, write };
71
- }
72
-
73
- function drift(ctx) {
74
- const { diagnoseModuleDrift } = require('../doctor');
75
- expect(typeof diagnoseModuleDrift).toBe('function');
76
- return diagnoseModuleDrift(ctx);
77
- }
78
-
79
- /**
80
- * Name every tier the hook can resolve into, so a test can never end up
81
- * comparing against this machine's real `~/.config/xp-gate` (#495 Round 3).
82
- */
83
- function ctxFor(f, extra = {}) {
84
- return {
85
- repoRoot: f.root,
86
- repoHooks: f.repoHooks,
87
- installedAdapters: f.installedAdapters,
88
- scriptDir: f.installedHooks,
89
- ...extra,
90
- };
91
- }
92
-
93
- function checkOf(result, needle) {
94
- const hit = result.checks.find((c) => c.name.includes(needle));
95
- expect(hit, `no check named like ${needle} in ${JSON.stringify(result.checks)}`).toBeDefined();
96
- return hit;
97
- }
98
-
99
- describe('doctor module drift (#495)', () => {
100
- it('AC-495-01: a stale executed gate module is reported, not waved through on presence', () => {
101
- const f = fixture({
102
- 'gate-8.sh': { repo: '#!/bin/bash\n# repo version\n', installed: '#!/bin/bash\n# installed version\n' },
103
- });
104
- const result = drift(ctxFor(f));
105
- expect(result.issues).toBeGreaterThanOrEqual(1);
106
- expect(checkOf(result, 'gate-8.sh').status).toBe('FAIL');
107
- });
108
-
109
- it('AC-495-02: language adapters are compared through the hook\'s own lookup order', () => {
110
- // Flat installed copy wins at runtime, so the repo's nested source is the
111
- // right counterpart for it.
112
- const flat = fixture({
113
- 'typescript.sh': { repo: 'run_tests() { echo repo; }\n', installed: 'run_tests() { echo stale; }\n' },
114
- });
115
- const flatResult = drift(ctxFor(flat));
116
- expect(checkOf(flatResult, 'typescript.sh').status).toBe('FAIL');
117
-
118
- // When the flat copy is absent the hook falls back to adapters/<name>.sh,
119
- // and that nested file is the one that executes -- it must be checked too.
120
- const nested = fixture({
121
- 'python.sh': { repo: 'run_tests() { echo repo; }\n', installed: 'run_tests() { echo stale; }\n', nested: true },
122
- });
123
- expect(checkOf(drift(ctxFor(nested)), 'python.sh').status).toBe('FAIL');
124
- });
125
-
126
- it('AC-495-03: equal content passes and a module missing on either side stays silent', () => {
127
- const same = 'run_tests() { echo same; }\n';
128
- const f = fixture({
129
- 'python.sh': { repo: same, installed: same },
130
- 'gate-3.sh': { repo: '#!/bin/bash\nv1\n', installed: '#!/bin/bash\nv1\n' },
131
- // Repo-only and install-only modules have no counterpart to compare with.
132
- 'go.sh': { repo: 'echo repo only\n', installed: null },
133
- 'swift.sh': { repo: null, installed: 'echo installed only\n' },
134
- });
135
- const result = drift(ctxFor(f));
136
- expect(result.issues).toBe(0);
137
- expect(checkOf(result, 'python.sh').status).toBe('PASS');
138
- expect(result.checks.filter((c) => c.name.includes('go.sh'))).toHaveLength(0);
139
- expect(result.checks.filter((c) => c.name.includes('swift.sh'))).toHaveLength(0);
140
-
141
- // A consumer project has no githooks/ at all -- the whole surface must stay
142
- // completely quiet there, exactly like the hook drift check does.
143
- const absent = drift(ctxFor(f, { repoHooks: join(f.root, 'nope') }));
144
- expect(absent.checks).toHaveLength(0);
145
- expect(absent.issues).toBe(0);
146
- });
147
-
148
- it('AC-495-04: bidirectional divergence is a distinct verdict from one-way staleness', () => {
149
- // Installed carries lines the repo lacks (the machine-only detect-secrets
150
- // hardening) AND the repo carries lines the install lacks (#449 verdict).
151
- const f = fixture({
152
- 'gate-8.sh': {
153
- repo: '#!/bin/bash\nrepo_has_report_artifact_verdict\n',
154
- installed: '#!/bin/bash\ninstalled_has_detect_secrets_fallback\n',
155
- },
156
- 'gate-4.sh': { repo: '#!/bin/bash\nline_a\nline_b\n', installed: '#!/bin/bash\nline_a\n' },
157
- });
158
- const result = drift(ctxFor(f));
159
-
160
- const diverged = checkOf(result, 'gate-8.sh');
161
- expect(diverged.status).toBe('FAIL');
162
- expect(diverged.detail).toMatch(/both sides|diverged/i);
163
-
164
- // One-way staleness must NOT be described as divergence, or the guard fires
165
- // on every routine upgrade and the real case stops standing out.
166
- const stale = checkOf(result, 'gate-4.sh');
167
- expect(stale.status).toBe('FAIL');
168
- expect(stale.detail).not.toMatch(/both sides|diverged/i);
169
- });
170
-
171
- it('AC-495-05: sync copies stale modules and refuses to overwrite diverged ones', () => {
172
- const { syncModulesFromRepo } = require('../doctor');
173
- expect(typeof syncModulesFromRepo).toBe('function');
174
-
175
- const f = fixture({
176
- 'gate-8.sh': {
177
- repo: '#!/bin/bash\nrepo_has_report_artifact_verdict\n',
178
- installed: '#!/bin/bash\ninstalled_has_detect_secrets_fallback\n',
179
- },
180
- 'gate-4.sh': { repo: '#!/bin/bash\nline_a\nline_b\n', installed: '#!/bin/bash\nline_a\n' },
181
- });
182
-
183
- const first = syncModulesFromRepo(ctxFor(f));
184
- expect(first.synced).toContain('gate-4.sh');
185
- expect(readFileSync(join(f.installedAdapters, 'gate-4.sh'), 'utf8')).toContain('line_b');
186
- // The guard is the point: the machine-only hardening survives untouched.
187
- expect(first.refused.map((r) => r.name)).toContain('gate-8.sh');
188
- expect(readFileSync(join(f.installedAdapters, 'gate-8.sh'), 'utf8')).toContain('detect_secrets');
189
-
190
- // An explicit decision still wins, because the developer may have judged the
191
- // repo copy to be the correct one.
192
- const forced = syncModulesFromRepo(ctxFor(f, { force: true }));
193
- expect(forced.synced).toContain('gate-8.sh');
194
- expect(readFileSync(join(f.installedAdapters, 'gate-8.sh'), 'utf8')).toContain('report_artifact_verdict');
195
- expect(existsSync(join(f.installedAdapters, 'gate-8.sh'))).toBe(true);
196
- });
197
-
198
- it('AC-495-06: anti-vacuity -- the surface is wired into the report and the repair', () => {
199
- const source = readFileSync(join(__dirname, '..', 'doctor.js'), 'utf8');
200
-
201
- // Detection must run in the real diagnosis, not only in the exported helper.
202
- expect(/diagnoseModuleDrift\(/.test(source)).toBe(true);
203
- expect(/diagnoseModuleDrift\(\)/.test(source)).toBe(true);
204
- // Repair must be reachable from the doctor command surface.
205
- expect(/syncModulesFromRepo\(/.test(source)).toBe(true);
206
- // The developer has to be able to see WHICH directory executes (#495's
207
- // "print the resolved GATE_DIR/ADAPTER_DIR" requirement).
208
- expect(source).toMatch(/printEffectiveModules|Effective (hooks|modules)/);
209
- // The presence-only verdict that hid this is gone for gate scripts.
210
- expect(source).not.toMatch(/status: 'PASS', detail: `\$\{EXPECTED_GATE_SCRIPTS\.length\} gate script\(s\)`/);
211
- });
212
- });
@@ -1,463 +0,0 @@
1
- /**
2
- * @test REQ-495 the JS mirror of the hook's module lookup must be reconciled against the bash original
3
- * @intent #495 的修复把 pre-commit 的 flat-优先-nested 解析顺序用 JavaScript 重写了一份
4
- * (doctor.js listExecutedModules)。本仓库在本批次已经示范过正确范式:
5
- * src/mutation/__tests__/test-filter-parity.test.ts(AC-480-04)把 bash 与 TS
6
- * 两份"非生产代码定义"钉成同一文本。跨语言的解析顺序同样不能靠注释宣称一致:
7
- * hook 侧一旦演化,doctor 就会镜像旧秩序,"doctor 全绿、执行面另起炉灶"重现。
8
- * 因此这里直接抽取 pre-commit 的 resolve_adapter_path 函数体,交给 bash 执行,
9
- * 与 doctor 的 listExecutedModules 在同一棵合成目录树上逐模块对账。第三轮把它
10
- * 扩到四层全填充的场景树:ADAPTER_DIR 链、GATE_DIR 链、resolve_adapter_path 三段
11
- * 全部从 pre-commit 原文抽取后交给 bash 执行,与 doctor 的
12
- * resolveExecutedModuleDirs()+executedModulePath() 对同一棵树逐一比对;hook 侧
13
- * 一旦改动解析顺序,这里立刻失配,而不是让 doctor 继续镜像旧秩序。
14
- * @covers AC-495-11, AC-495-12, AC-495-21, AC-495-22
15
- */
16
- import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync } from 'node:fs';
17
- import { tmpdir } from 'node:os';
18
- import { join, resolve } from 'node:path';
19
- import { spawnSync, execFileSync } from 'node:child_process';
20
- import { describe, expect, it } from 'vitest';
21
-
22
- const PRE_COMMIT = readFileSync(resolve(__dirname, '..', '..', '..', '..', 'githooks', 'pre-commit'), 'utf8');
23
- // #505 replaced the two hand-written shell chains with one data file generated
24
- // from lib/hook-module-resolution.js — the same module doctor.js requires. The
25
- // parity test therefore verifies the NEW contract: the generated data is current,
26
- // and the hook + doctor both honour it. The scenarios below still drive the hook's
27
- // real text through bash, so agreement is measured, not asserted.
28
- const RESOLUTION_DATA = readFileSync(
29
- resolve(__dirname, '..', '..', '..', '..', 'githooks', 'hook-module-resolution.sh'),
30
- 'utf8',
31
- );
32
- const { RESOLUTION_ORDER, GATE_RESOLUTION_ORDER, ADAPTER_DIR_ORDER } = require('../hook-module-resolution.js');
33
-
34
- /**
35
- * Extract one bash function's full text, brace-balanced from the declaration.
36
- */
37
- function extractFunction(source, signature) {
38
- const start = source.indexOf(signature);
39
- expect(start, `pre-commit no longer declares ${signature}`).toBeGreaterThan(-1);
40
- let depth = 0;
41
- let index = source.indexOf('{', start);
42
- const bodyStart = index;
43
- for (; index < source.length; index++) {
44
- const ch = source[index];
45
- if (ch === '{') depth++;
46
- else if (ch === '}') {
47
- depth--;
48
- if (depth === 0) return source.slice(start, index + 1);
49
- }
50
- }
51
- throw new Error(`unbalanced braces in ${signature}`);
52
- }
53
-
54
- /**
55
- * Grab one `if`-chain verbatim from the hook, up to and including its `fi`.
56
- */
57
- function matchBlock(source, pattern, label) {
58
- const hit = source.match(pattern);
59
- expect(hit, `pre-commit no longer carries ${label}`).not.toBeNull();
60
- return hit[0];
61
- }
62
-
63
- function bashBin() {
64
- if (process.env.XP_GATE_BASH_BIN) {
65
- const probe = spawnSync(process.env.XP_GATE_BASH_BIN, ['-c', 'echo ok'], { encoding: 'utf8' });
66
- if (probe.status !== 0 || probe.error) {
67
- throw new Error(`bash runner unavailable (${process.env.XP_GATE_BASH_BIN}): ${probe.error ? probe.error.message : `exit ${probe.status}`}`);
68
- }
69
- return process.env.XP_GATE_BASH_BIN;
70
- }
71
- // Resolve the authoritative bash the hooks actually get. On Windows a bare
72
- // PATH 'bash' may be the WSL System32 stub, whose `-c` scripts receive an
73
- // empty $1 and resolve nothing — a vacuous resolver run, not a passing one.
74
- const { checkBash } = require('../detect-deps.js');
75
- const b = checkBash();
76
- if (!b.ok) {
77
- throw new Error(`bash runner unavailable: ${b.message || 'checkBash failed'}`);
78
- }
79
- return b.path;
80
- }
81
-
82
- /**
83
- * Compare a path the bash resolver printed with one Node built. Bash emits the
84
- * separators it was handed and mixes them (`$ADAPTER_DIR/adapters/x.sh`), so the
85
- * comparison is on a single separator view, not on either side's spelling.
86
- */
87
- function samePath(a, b) {
88
- const normalize = (p) => p.replace(/\\/g, '/').replace(/\/+/g, '/');
89
- return normalize(a) === normalize(b);
90
- }
91
-
92
- /**
93
- * Build an ADAPTER_DIR holding the requested flat and nested modules.
94
- */
95
- function adapterTree(spec) {
96
- const root = mkdtempSync(join(tmpdir(), 'xp-gate-parity-'));
97
- const adapterDir = join(root, 'adapters');
98
- mkdirSync(join(adapterDir, 'adapters'), { recursive: true });
99
- // Empty PROJECT_GITHOOKS / SCRIPT_DIR tiers so only tiers 1-2 can answer.
100
- mkdirSync(join(root, 'project'), { recursive: true });
101
- mkdirSync(join(root, 'script'), { recursive: true });
102
- for (const [name, sides] of Object.entries(spec)) {
103
- if (sides.flat) writeFileSync(join(adapterDir, name), 'flat\n');
104
- if (sides.nested) writeFileSync(join(adapterDir, 'adapters', name), 'nested\n');
105
- }
106
- return { root, adapterDir };
107
- }
108
-
109
- /**
110
- * Where a module copy can sit across all four tiers the hook looks at. Each key
111
- * is a location name, so a scenario reads as a statement about the tree.
112
- */
113
- const LAYOUTS = {
114
- globalFlat: (t, name) => join(t.globalAdapters, name),
115
- globalNested: (t, name) => join(t.globalAdapters, 'adapters', name),
116
- projectFlat: (t, name) => join(t.projectGithooks, name),
117
- projectNested: (t, name) => join(t.projectGithooks, 'adapters', name),
118
- scriptFlat: (t, name) => join(t.scriptDir, name),
119
- scriptNested: (t, name) => join(t.scriptDir, 'adapters', name),
120
- };
121
-
122
- /**
123
- * A tree with all tiers present, including their resolution anchors, so tier
124
- * precedence is decided by the hook's own text rather than by the fixture's
125
- * assumptions (AC-495-13's project-tier pair depends on this too).
126
- */
127
- function fourTierTree(layouts) {
128
- const root = mkdtempSync(join(tmpdir(), 'xp-gate-parity4-'));
129
- const t = {
130
- root,
131
- globalAdapters: join(root, 'global', 'adapters'),
132
- projectGithooks: join(root, 'repo', 'githooks'),
133
- scriptDir: join(root, 'installed', 'hooks'),
134
- };
135
- for (const dir of [t.globalAdapters, t.projectGithooks, t.scriptDir]) {
136
- mkdirSync(join(dir, 'adapters'), { recursive: true });
137
- }
138
- for (const [name, places] of Object.entries(layouts)) {
139
- for (const place of places) {
140
- const file = LAYOUTS[place](t, name);
141
- mkdirSync(join(file, '..'), { recursive: true });
142
- writeFileSync(file, `${place}\n`);
143
- }
144
- }
145
- return t;
146
- }
147
-
148
- /**
149
- * Run the hook's own ADAPTER_DIR chain, GATE_DIR chain and resolve_adapter_path
150
- * over one tree, and report all three answers.
151
- */
152
- function hookResolvesAll(blocks, resolverText, t, lang, gateModule) {
153
- // Bash assignment uses single quotes below; a temp path containing one would
154
- // change the tree the two sides are looking at, so refuse rather than lie.
155
- for (const dir of [t.globalAdapters, t.projectGithooks, t.scriptDir]) {
156
- expect(dir, `fixture path contains a quote: ${dir}`).not.toMatch(/'/);
157
- }
158
- const script = [
159
- `GLOBAL_ADAPTER_DIR='${t.globalAdapters}'`,
160
- `PROJECT_GITHOOKS='${t.projectGithooks}'`,
161
- `SCRIPT_DIR='${t.scriptDir}'`,
162
- `ADAPTER_DIR=''`,
163
- resolverText,
164
- blocks.gateDir,
165
- // The real chains, driven by the same data the hook loads.
166
- '_hook_adapter_dir() {',
167
- ' local entry base suffix base_dir',
168
- ' eval "set -- \\"\\${HOOK_ADAPTER_DIR_TIERS[@]}\\""',
169
- ' for entry in "$@"; do',
170
- ' entry="${entry#*|}"; base="${entry%%|*}"; suffix="${entry#*|}"',
171
- ' base_dir="$(_hook_tier_base "$base")"',
172
- ' [ -n "$base_dir" ] || continue',
173
- ' if [ -f "$base_dir/$suffix" ]; then printf "%s" "$base_dir"; return 0; fi',
174
- ' done',
175
- ' return 1',
176
- '}',
177
- 'ADAPTER_DIR="$(_hook_adapter_dir)" || ADAPTER_DIR="$SCRIPT_DIR"',
178
- 'GATE_DIR="$(_hook_resolve_dir "$HOOK_GATE_PROBE" HOOK_GATE_TIERS)" || GATE_DIR=""',
179
- 'if [ -z "$GATE_DIR" ]; then GATE_DIR="$ADAPTER_DIR"; fi',
180
- 'echo "ADAPTER=$ADAPTER_DIR"',
181
- 'echo "GATE=$GATE_DIR"',
182
- `if [ -f "$ADAPTER_DIR/adapter-common.sh" ]; then echo "COMMON=$ADAPTER_DIR/adapter-common.sh"; else echo "COMMON="; fi`,
183
- `if [ -f "$GATE_DIR/${gateModule}" ]; then echo "MODULE=$GATE_DIR/${gateModule}"; else echo "MODULE="; fi`,
184
- // Captured by substitution rather than read off the last stdout line: the
185
- // script ends with a newline, so "last line" is always the empty one.
186
- `echo "LANG=$(resolve_adapter_path ${lang} || true)"`,
187
- ].join('\n');
188
- const run = spawnSync(bashBin(), ['-c', script], { encoding: 'utf8' });
189
- expect(run.status, `resolver run failed: ${run.stderr}`).toBe(0);
190
- const lines = run.stdout.split(/\r?\n/);
191
- const named = (prefix) => {
192
- const hit = lines.find((l) => l.startsWith(`${prefix}=`));
193
- expect(hit, `bash printed no ${prefix}= line:\n${run.stdout}`).toBeDefined();
194
- return hit ? hit.slice(prefix.length + 1) : '';
195
- };
196
- return {
197
- adapterDir: named('ADAPTER'),
198
- gateDir: named('GATE'),
199
- common: named('COMMON'),
200
- gateModule: named('MODULE'),
201
- langPath: named('LANG'),
202
- };
203
- }
204
-
205
- /**
206
- * Ask the hook's own resolver which file executes for one language.
207
- */
208
- function hookResolves(fnText, tree, lang) {
209
- const script = [
210
- `ADAPTER_DIR='${tree.adapterDir}'`,
211
- `PROJECT_GITHOOKS='${join(tree.root, 'project')}'`,
212
- `SCRIPT_DIR='${join(tree.root, 'script')}'`,
213
- fnText,
214
- `resolve_adapter_path ${lang}`,
215
- ].join('\n');
216
- const run = spawnSync(bashBin(), ['-c', script], { encoding: 'utf8' });
217
- expect(run.status, `resolver run failed: ${run.stderr}`).toBe(0);
218
- return run.stdout.trim();
219
- }
220
-
221
- describe('doctor module resolution parity with pre-commit (#495)', () => {
222
- // #505: the resolver helpers come from the hook, the tier DATA from the
223
- // generated file. Concatenating them is what the hook itself does, so the
224
- // bash side here is the production code path, not a paraphrase of it.
225
- const helpers = [
226
- extractFunction(PRE_COMMIT, '_hook_tier_base() {'),
227
- extractFunction(PRE_COMMIT, '_hook_resolve() {'),
228
- extractFunction(PRE_COMMIT, '_hook_resolve_dir() {'),
229
- extractFunction(PRE_COMMIT, 'resolve_adapter_path() {'),
230
- ].join('\n');
231
- // `blocks` keeps the names the scenarios use: the data file supplies the
232
- // ADAPTER_DIR/GATE_DIR chains that used to be matched out of the hook text.
233
- const blocks = {
234
- adapterDir: '',
235
- gateDir: '',
236
- };
237
- const resolverText = `${RESOLUTION_DATA}\n${helpers}`;
238
-
239
- it('AC-505-01: the generated shell data is current with its JS source', () => {
240
- // Stale data would silently run a different order than doctor reports — the
241
- // exact failure mode #505 exists to make impossible.
242
- expect(() => execFileSync(
243
- process.execPath,
244
- [resolve(__dirname, '..', '..', '..', '..', 'scripts', 'generate-hook-resolution.cjs'), '--check'],
245
- { stdio: 'pipe' },
246
- )).not.toThrow();
247
- });
248
-
249
- it('AC-505-02: the hook loads the generated data instead of carrying its own order', () => {
250
- // Anti-vacuity for the whole feature: if the hook re-grew a literal cascade,
251
- // the single-source-of-truth claim would be false even while scenarios pass.
252
- expect(PRE_COMMIT).toContain('hook-module-resolution.sh');
253
- // MENTIONING the array is not enough -- this assertion used to stop at
254
- // `toContain('HOOK_ADAPTER_TIERS')`, which the array's own declaration
255
- // satisfies. The hook sourced the generated file and then resolved
256
- // ADAPTER_DIR with a hand-written global-first cascade that never read the
257
- // array, so the data was dead weight and the spec change had no effect at
258
- // runtime while this test stayed green. Require an actual consumption.
259
- //
260
- // The readers expand the table indirectly (`eval "set -- ${tiers_var}[@]"`,
261
- // because the same helper serves both tables), so look for the call sites
262
- // that name each table rather than for a literal `${NAME[@]}`.
263
- expect(PRE_COMMIT, 'ADAPTER_DIR is never resolved from HOOK_ADAPTER_DIR_TIERS').toMatch(
264
- /_hook_resolve(?:_dir)?\s+['"][^'"]+['"]\s+HOOK_ADAPTER_DIR_TIERS/,
265
- );
266
- expect(PRE_COMMIT, 'GATE_DIR is never resolved from HOOK_GATE_TIERS').toMatch(
267
- /_hook_resolve(?:_dir)?\s+['"][^'"]+['"]\s+HOOK_GATE_TIERS/,
268
- );
269
- // The old hand-written chains must be gone.
270
- expect(PRE_COMMIT).not.toContain('$ADAPTER_DIR/${lang}.sh');
271
- expect(PRE_COMMIT).not.toMatch(/if \[ -f "\$ADAPTER_DIR\/gate-3\.sh" \]; then/);
272
- // And the ADAPTER_DIR cascade specifically must not be re-typed: a literal
273
- // "global first, then project" if/elif is exactly the order this data
274
- // replaces.
275
- expect(PRE_COMMIT, 'ADAPTER_DIR still has a hand-written global-first cascade').not.toMatch(
276
- /if \[ -f "\$GLOBAL_ADAPTER_DIR\/adapter-common\.sh" \][\s\S]{0,120}ADAPTER_DIR="\$GLOBAL_ADAPTER_DIR"/,
277
- );
278
- // And every tier the JS declares must appear in the emitted shell data.
279
- for (const t of RESOLUTION_ORDER) {
280
- expect(RESOLUTION_DATA).toContain(`${t.tier_id}|${t.base}|${t.template}`);
281
- }
282
- for (const t of GATE_RESOLUTION_ORDER) {
283
- expect(RESOLUTION_DATA).toContain(`'${t.tier_id}|${t.base}|'`);
284
- }
285
- for (const t of ADAPTER_DIR_ORDER) {
286
- expect(RESOLUTION_DATA).toContain(`${t.tier_id}|${t.base}|${t.probe}`);
287
- }
288
- });
289
-
290
- it('AC-495-11: flat and nested present -> both languages pick the flat copy', () => {
291
- const { listExecutedModules } = require('../doctor');
292
- const tree = adapterTree({ 'typescript.sh': { flat: true, nested: true } });
293
-
294
- const hookChoice = hookResolves(resolverText, tree, 'typescript');
295
- expect(samePath(hookChoice, join(tree.adapterDir, 'typescript.sh'))).toBe(true);
296
-
297
- const doctorChoice = listExecutedModules(tree.adapterDir).find((m) => m.name === 'typescript.sh');
298
- expect(doctorChoice).toBeDefined();
299
- expect(samePath(doctorChoice.file, hookChoice)).toBe(true);
300
- });
301
-
302
- it('AC-495-12: every module the hook can resolve in the executed dir is in doctor\'s surface, and agrees on each path', () => {
303
- const { listExecutedModules } = require('../doctor');
304
- const tree = adapterTree({
305
- 'typescript.sh': { nested: true },
306
- 'python.sh': { flat: true, nested: true },
307
- 'gate-4.sh': { flat: true },
308
- 'go.sh': { nested: true },
309
- });
310
-
311
- const doctorModules = listExecutedModules(tree.adapterDir);
312
- const doctorByName = new Map(doctorModules.map((m) => [m.name, m.file]));
313
-
314
- // Same set of module names, no extras, no omissions.
315
- expect([...doctorByName.keys()].sort()).toEqual(['gate-4.sh', 'go.sh', 'python.sh', 'typescript.sh']);
316
-
317
- for (const lang of ['typescript', 'python', 'go']) {
318
- const hookChoice = hookResolves(resolverText, tree, lang);
319
- expect(existsSync(hookChoice), `hook resolved a missing file: ${hookChoice}`).toBe(true);
320
- expect(doctorByName.has(`${lang}.sh`), `doctor does not see ${lang}.sh at all`).toBe(true);
321
- expect(
322
- samePath(doctorByName.get(`${lang}.sh`), hookChoice),
323
- `doctor disagrees with the hook for ${lang}.sh`,
324
- ).toBe(true);
325
- }
326
-
327
- // Anti-vacuity: the guard executed the hook's real text, not a paraphrase.
328
- // #505 moved the literal paths out of the hook, so assert that the tiers it
329
- // now reads still describe the two layouts those paths used to spell out.
330
- expect(resolverText).toContain('HOOK_ADAPTER_TIERS');
331
- const adapterTemplates = RESOLUTION_ORDER.map((t) => t.template);
332
- expect(adapterTemplates).toContain('{name}.sh');
333
- expect(adapterTemplates).toContain('adapters/{name}.sh');
334
- });
335
-
336
- // The scenarios below put a copy of the same module in EVERY tier the hook can
337
- // reach, so agreement can only come from following the real precedence. Round 2
338
- // had a hand-written two-tier list in doctor and called whatever it found
339
- // "executed" -- which both mislabelled files nothing runs and left a FAIL no
340
- // repair could clear (#495 Round 3).
341
- const SCENARIOS = [
342
- {
343
- name: 'global install present: the global flat copy wins over every other tier',
344
- layouts: {
345
- 'adapter-common.sh': ['globalFlat', 'projectFlat', 'scriptFlat'],
346
- 'gate-3.sh': ['globalFlat', 'projectFlat', 'scriptFlat'],
347
- 'gate-4.sh': ['globalFlat', 'projectFlat', 'scriptFlat'],
348
- 'typescript.sh': ['globalFlat', 'globalNested', 'projectFlat', 'projectNested', 'scriptNested'],
349
- },
350
- },
351
- {
352
- name: 'no global install: ADAPTER_DIR resolves to the project tier and its flat copy runs',
353
- layouts: {
354
- 'adapter-common.sh': ['projectFlat', 'scriptFlat'],
355
- 'gate-3.sh': ['projectFlat', 'scriptFlat'],
356
- 'gate-4.sh': ['projectFlat', 'scriptFlat'],
357
- 'typescript.sh': ['projectFlat', 'projectNested', 'scriptNested'],
358
- },
359
- },
360
- {
361
- name: 'project tier carries no anchor: GATE_DIR falls through to the installed hooks dir',
362
- layouts: {
363
- 'adapter-common.sh': ['globalFlat'],
364
- 'gate-3.sh': ['scriptFlat'],
365
- 'gate-4.sh': ['globalFlat', 'projectFlat', 'scriptFlat'],
366
- 'typescript.sh': ['globalNested', 'projectNested', 'scriptNested'],
367
- },
368
- },
369
- ];
370
-
371
- it('AC-495-21: the JS resolution picks the same file as the hook for every tier scenario', () => {
372
- const { resolveExecutedModuleDirs, executedModulePath } = require('../doctor');
373
- for (const scenario of SCENARIOS) {
374
- const t = fourTierTree(scenario.layouts);
375
- const hook = hookResolvesAll(blocks, resolverText, t, 'typescript', 'gate-4.sh');
376
- const dirs = resolveExecutedModuleDirs({
377
- globalAdapterDir: t.globalAdapters,
378
- projectGithooks: t.projectGithooks,
379
- scriptDir: t.scriptDir,
380
- });
381
-
382
- expect(samePath(dirs.adapterDir, hook.adapterDir), `${scenario.name} (ADAPTER_DIR)`).toBe(true);
383
- expect(samePath(dirs.gateDir, hook.gateDir), `${scenario.name} (GATE_DIR)`).toBe(true);
384
-
385
- const jsLang = executedModulePath('typescript.sh', dirs);
386
- expect(jsLang, `${scenario.name}: doctor finds no executed copy the hook resolves`).not.toBeNull();
387
- expect(samePath(jsLang, hook.langPath), `${scenario.name} (typescript.sh)`).toBe(true);
388
-
389
- // Gate modules come from GATE_DIR, language adapters from the resolver chain.
390
- expect(samePath(executedModulePath('gate-4.sh', dirs), hook.gateModule), `${scenario.name} (gate-4.sh)`).toBe(true);
391
- expect(samePath(executedModulePath('adapter-common.sh', dirs), hook.common), `${scenario.name} (adapter-common.sh)`).toBe(true);
392
- }
393
- });
394
-
395
- it('AC-495-23: a project install OVERRIDES the global install (project is the higher tier)', () => {
396
- // The install modes exist so the global copy is a DEFAULT that a project can
397
- // override: "no per-project customisation -> use the global gate suite; this
398
- // project has special needs -> its own copy wins". The cascade used to try
399
- // GLOBAL first, which made a project's githooks/ unreachable whenever the
400
- // machine had a global install -- so editing the repo's own gate scripts
401
- // changed nothing, silently.
402
- const idxGlobal = ADAPTER_DIR_ORDER.findIndex((t) => t.tier_id === 'global');
403
- const idxProject = ADAPTER_DIR_ORDER.findIndex((t) => t.tier_id === 'project');
404
- expect(idxProject, 'no project tier in ADAPTER_DIR_ORDER').toBeGreaterThanOrEqual(0);
405
- expect(idxGlobal, 'no global tier in ADAPTER_DIR_ORDER').toBeGreaterThanOrEqual(0);
406
- expect(
407
- idxProject,
408
- 'project must be consulted BEFORE global, or a per-project gate suite can never take effect',
409
- ).toBeLessThan(idxGlobal);
410
-
411
- // And the hook, driven by the generated data, must actually pick the project
412
- // copy when both trees carry the module.
413
- const { resolveExecutedModuleDirs, executedModulePath } = require('../doctor');
414
- const t = fourTierTree({
415
- 'adapter-common.sh': ['globalFlat', 'projectFlat'],
416
- 'typescript.sh': ['globalFlat', 'projectNested'],
417
- 'gate-3.sh': ['globalFlat', 'projectFlat'],
418
- });
419
- const dirs = resolveExecutedModuleDirs({
420
- globalAdapterDir: t.globalAdapters,
421
- projectGithooks: t.projectGithooks,
422
- scriptDir: t.scriptDir,
423
- });
424
- const hook = hookResolvesAll(blocks, resolverText, t, 'typescript', 'gate-3.sh');
425
-
426
- expect(
427
- samePath(hook.adapterDir, t.projectGithooks),
428
- `ADAPTER_DIR resolved to ${hook.adapterDir}; the project tier must win over global`,
429
- ).toBe(true);
430
- expect(samePath(hook.langPath, join(t.projectGithooks, 'adapters', 'typescript.sh'))).toBe(true);
431
- expect(samePath(executedModulePath('typescript.sh', dirs), hook.langPath)).toBe(true);
432
- });
433
-
434
- it('AC-495-24: a project with no githooks/ still uses the global install', () => {
435
- // The override must not become "the global install stops working": a repo
436
- // that never ran `xp-gate init` has to keep resolving to the global tree.
437
- const t = fourTierTree({
438
- 'adapter-common.sh': ['globalFlat'],
439
- 'typescript.sh': ['globalFlat'],
440
- 'gate-3.sh': ['globalFlat'],
441
- });
442
- const hook = hookResolvesAll(blocks, resolverText, t, 'typescript', 'gate-3.sh');
443
- expect(samePath(hook.adapterDir, t.globalAdapters)).toBe(true);
444
- expect(samePath(hook.langPath, join(t.globalAdapters, 'typescript.sh'))).toBe(true);
445
- });
446
-
447
- it('AC-495-22: a module no tier installs resolves to nothing on both sides', () => {
448
- const { resolveExecutedModuleDirs, executedModulePath } = require('../doctor');
449
- const t = fourTierTree({
450
- 'adapter-common.sh': ['globalFlat'],
451
- 'gate-3.sh': ['globalFlat'],
452
- });
453
- const dirs = resolveExecutedModuleDirs({
454
- globalAdapterDir: t.globalAdapters,
455
- projectGithooks: t.projectGithooks,
456
- scriptDir: t.scriptDir,
457
- });
458
- const hook = hookResolvesAll(blocks, resolverText, t, 'rust', 'gate-9.sh');
459
- expect(hook.langPath).toBe('');
460
- expect(executedModulePath('rust.sh', dirs)).toBeNull();
461
- expect(executedModulePath('gate-9.sh', dirs)).toBeNull();
462
- });
463
- });