@vibe-agent-toolkit/utils 0.2.0-rc.1 → 0.2.0-rc.10

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 (270) hide show
  1. package/README.md +69 -23
  2. package/dist/asset-reference.js +3 -11
  3. package/dist/asset-reference.js.map +1 -1
  4. package/dist/compare-code-units.d.ts +28 -0
  5. package/dist/compare-code-units.d.ts.map +1 -0
  6. package/dist/compare-code-units.js +33 -0
  7. package/dist/compare-code-units.js.map +1 -0
  8. package/dist/crawl-timing.d.ts +657 -0
  9. package/dist/crawl-timing.d.ts.map +1 -0
  10. package/dist/crawl-timing.js +765 -0
  11. package/dist/crawl-timing.js.map +1 -0
  12. package/dist/crawl.d.ts +2 -4
  13. package/dist/crawl.d.ts.map +1 -1
  14. package/dist/crawl.js +2 -4
  15. package/dist/crawl.js.map +1 -1
  16. package/dist/dirent-kind.d.ts +83 -0
  17. package/dist/dirent-kind.d.ts.map +1 -0
  18. package/dist/dirent-kind.js +129 -0
  19. package/dist/dirent-kind.js.map +1 -0
  20. package/dist/entrypoint.d.ts +54 -0
  21. package/dist/entrypoint.d.ts.map +1 -0
  22. package/dist/entrypoint.js +71 -0
  23. package/dist/entrypoint.js.map +1 -0
  24. package/dist/env-flag.d.ts +51 -0
  25. package/dist/env-flag.d.ts.map +1 -0
  26. package/dist/env-flag.js +62 -0
  27. package/dist/env-flag.js.map +1 -0
  28. package/dist/errors/errno.d.ts +51 -0
  29. package/dist/errors/errno.d.ts.map +1 -0
  30. package/dist/errors/errno.js +112 -0
  31. package/dist/errors/errno.js.map +1 -0
  32. package/dist/errors/vat-error.d.ts +60 -0
  33. package/dist/errors/vat-error.d.ts.map +1 -0
  34. package/dist/errors/vat-error.js +86 -0
  35. package/dist/errors/vat-error.js.map +1 -0
  36. package/dist/file-crawler.d.ts +60 -0
  37. package/dist/file-crawler.d.ts.map +1 -1
  38. package/dist/file-crawler.js +133 -29
  39. package/dist/file-crawler.js.map +1 -1
  40. package/dist/fs/file-hash.d.ts.map +1 -1
  41. package/dist/fs/file-hash.js +0 -1
  42. package/dist/fs/file-hash.js.map +1 -1
  43. package/dist/fs-utils.d.ts +422 -191
  44. package/dist/fs-utils.d.ts.map +1 -1
  45. package/dist/fs-utils.js +534 -204
  46. package/dist/fs-utils.js.map +1 -1
  47. package/dist/fs.d.ts +5 -2
  48. package/dist/fs.d.ts.map +1 -1
  49. package/dist/fs.js +9 -7
  50. package/dist/fs.js.map +1 -1
  51. package/dist/git-run.d.ts +167 -0
  52. package/dist/git-run.d.ts.map +1 -0
  53. package/dist/git-run.js +154 -0
  54. package/dist/git-run.js.map +1 -0
  55. package/dist/git-snapshot.d.ts +199 -0
  56. package/dist/git-snapshot.d.ts.map +1 -0
  57. package/dist/git-snapshot.js +250 -0
  58. package/dist/git-snapshot.js.map +1 -0
  59. package/dist/git-tracker.d.ts +166 -3
  60. package/dist/git-tracker.d.ts.map +1 -1
  61. package/dist/git-tracker.js +250 -22
  62. package/dist/git-tracker.js.map +1 -1
  63. package/dist/git-utils.d.ts +84 -1
  64. package/dist/git-utils.d.ts.map +1 -1
  65. package/dist/git-utils.js +208 -82
  66. package/dist/git-utils.js.map +1 -1
  67. package/dist/git.d.ts +13 -4
  68. package/dist/git.d.ts.map +1 -1
  69. package/dist/git.js +12 -4
  70. package/dist/git.js.map +1 -1
  71. package/dist/gitignore-checker.d.ts.map +1 -1
  72. package/dist/gitignore-checker.js +15 -6
  73. package/dist/gitignore-checker.js.map +1 -1
  74. package/dist/glob/glob-pattern.js +5 -5
  75. package/dist/glob/glob-pattern.js.map +1 -1
  76. package/dist/index.d.ts +32 -20
  77. package/dist/index.d.ts.map +1 -1
  78. package/dist/index.js +95 -50
  79. package/dist/index.js.map +1 -1
  80. package/dist/listing-refusal.d.ts +112 -0
  81. package/dist/listing-refusal.d.ts.map +1 -0
  82. package/dist/listing-refusal.js +105 -0
  83. package/dist/listing-refusal.js.map +1 -0
  84. package/dist/path-containment.d.ts +64 -0
  85. package/dist/path-containment.d.ts.map +1 -0
  86. package/dist/path-containment.js +111 -0
  87. package/dist/path-containment.js.map +1 -0
  88. package/dist/path-core.d.ts +113 -18
  89. package/dist/path-core.d.ts.map +1 -1
  90. package/dist/path-core.js +139 -23
  91. package/dist/path-core.js.map +1 -1
  92. package/dist/path-utils.d.ts.map +1 -1
  93. package/dist/path-utils.js +42 -47
  94. package/dist/path-utils.js.map +1 -1
  95. package/dist/path.d.ts +1 -1
  96. package/dist/path.d.ts.map +1 -1
  97. package/dist/path.js +1 -1
  98. package/dist/path.js.map +1 -1
  99. package/dist/process.d.ts +7 -0
  100. package/dist/process.d.ts.map +1 -1
  101. package/dist/process.js +7 -0
  102. package/dist/process.js.map +1 -1
  103. package/dist/project-utils.d.ts.map +1 -1
  104. package/dist/project-utils.js +15 -9
  105. package/dist/project-utils.js.map +1 -1
  106. package/dist/project.d.ts +7 -5
  107. package/dist/project.d.ts.map +1 -1
  108. package/dist/project.js +7 -5
  109. package/dist/project.js.map +1 -1
  110. package/dist/safe-exec.d.ts +40 -11
  111. package/dist/safe-exec.d.ts.map +1 -1
  112. package/dist/safe-exec.js +111 -25
  113. package/dist/safe-exec.js.map +1 -1
  114. package/dist/skill-targets.d.ts +2 -2
  115. package/dist/skill-targets.js +2 -2
  116. package/dist/skill-test/auth-resolver.d.ts +10 -2
  117. package/dist/skill-test/auth-resolver.d.ts.map +1 -1
  118. package/dist/skill-test/auth-resolver.js +26 -11
  119. package/dist/skill-test/auth-resolver.js.map +1 -1
  120. package/dist/skill-test/index.d.ts +1 -1
  121. package/dist/skill-test/index.d.ts.map +1 -1
  122. package/dist/skill-test/index.js +1 -1
  123. package/dist/skill-test/index.js.map +1 -1
  124. package/dist/skill-test/spawn-claude.d.ts +30 -1
  125. package/dist/skill-test/spawn-claude.d.ts.map +1 -1
  126. package/dist/skill-test/spawn-claude.js +71 -5
  127. package/dist/skill-test/spawn-claude.js.map +1 -1
  128. package/dist/skill-test/transcript.d.ts +41 -2
  129. package/dist/skill-test/transcript.d.ts.map +1 -1
  130. package/dist/skill-test/transcript.js +55 -10
  131. package/dist/skill-test/transcript.js.map +1 -1
  132. package/dist/spawn-hardened.js +0 -1
  133. package/dist/spawn-hardened.js.map +1 -1
  134. package/dist/stdio-blocking.js +13 -14
  135. package/dist/stdio-blocking.js.map +1 -1
  136. package/dist/test-helpers.d.ts +135 -100
  137. package/dist/test-helpers.d.ts.map +1 -1
  138. package/dist/test-helpers.js +224 -146
  139. package/dist/test-helpers.js.map +1 -1
  140. package/dist/testing/executables.d.ts +32 -0
  141. package/dist/testing/executables.d.ts.map +1 -0
  142. package/dist/testing/executables.js +70 -0
  143. package/dist/testing/executables.js.map +1 -0
  144. package/dist/testing/hostile-tree.d.ts +79 -0
  145. package/dist/testing/hostile-tree.d.ts.map +1 -0
  146. package/dist/testing/hostile-tree.js +164 -0
  147. package/dist/testing/hostile-tree.js.map +1 -0
  148. package/dist/testing/platform-gates.d.ts +31 -0
  149. package/dist/testing/platform-gates.d.ts.map +1 -0
  150. package/dist/testing/platform-gates.js +31 -0
  151. package/dist/testing/platform-gates.js.map +1 -0
  152. package/dist/testing/temp-dir.d.ts +255 -0
  153. package/dist/testing/temp-dir.d.ts.map +1 -0
  154. package/dist/testing/temp-dir.js +363 -0
  155. package/dist/testing/temp-dir.js.map +1 -0
  156. package/dist/testing.d.ts +94 -3
  157. package/dist/testing.d.ts.map +1 -1
  158. package/dist/testing.js +119 -3
  159. package/dist/testing.js.map +1 -1
  160. package/dist/text-content.d.ts +197 -0
  161. package/dist/text-content.d.ts.map +1 -0
  162. package/dist/text-content.js +354 -0
  163. package/dist/text-content.js.map +1 -0
  164. package/dist/text-file.d.ts +45 -0
  165. package/dist/text-file.d.ts.map +1 -0
  166. package/dist/text-file.js +51 -0
  167. package/dist/text-file.js.map +1 -0
  168. package/dist/text.d.ts +16 -0
  169. package/dist/text.d.ts.map +1 -0
  170. package/dist/text.js +16 -0
  171. package/dist/text.js.map +1 -0
  172. package/dist/timing-dump.d.ts +136 -0
  173. package/dist/timing-dump.d.ts.map +1 -0
  174. package/dist/timing-dump.js +229 -0
  175. package/dist/timing-dump.js.map +1 -0
  176. package/dist/windows-shell.d.ts +0 -1
  177. package/dist/windows-shell.d.ts.map +1 -1
  178. package/dist/windows-shell.js +0 -1
  179. package/dist/windows-shell.js.map +1 -1
  180. package/dist/zod-introspection.js +2 -2
  181. package/dist/zod-introspection.js.map +1 -1
  182. package/eslint/README.md +223 -49
  183. package/eslint/index.cjs +111 -97
  184. package/eslint/index.d.cts +16 -6
  185. package/eslint/rules/callee-name.cjs +23 -0
  186. package/eslint/rules/commands-import-boundary.cjs +148 -0
  187. package/eslint/rules/dead-import.cjs +1 -1
  188. package/eslint/rules/dirent-type-needs-symlink-check.cjs +282 -0
  189. package/eslint/rules/eslint-rule-factory.cjs +5 -2
  190. package/eslint/rules/exempt-path-matcher.cjs +2 -1
  191. package/eslint/rules/explicit-zod-strictness.cjs +120 -0
  192. package/eslint/rules/no-bare-dynamic-import-path.cjs +5 -1
  193. package/eslint/rules/no-bare-executable-spawn.cjs +70 -0
  194. package/eslint/rules/no-bare-symlink-in-tests.cjs +213 -0
  195. package/eslint/rules/no-blind-catch.cjs +171 -0
  196. package/eslint/rules/no-child-process-execSync.cjs +8 -0
  197. package/eslint/rules/no-decaying-referent.cjs +217 -0
  198. package/eslint/rules/no-dotdot-containment.cjs +160 -0
  199. package/eslint/rules/no-file-url-string-concat.cjs +4 -1
  200. package/eslint/rules/no-fragile-entrypoint-guard.cjs +228 -0
  201. package/eslint/rules/no-fs-mkdirSync.cjs +8 -0
  202. package/eslint/rules/no-fs-promises-cp.cjs +7 -0
  203. package/eslint/rules/no-fs-realpathSync.cjs +8 -0
  204. package/eslint/rules/no-hardcoded-path-split.cjs +29 -18
  205. package/eslint/rules/no-io-in-unit-tier.cjs +129 -0
  206. package/eslint/rules/no-literal-process-exit.cjs +129 -0
  207. package/eslint/rules/no-manual-path-normalize.cjs +136 -107
  208. package/eslint/rules/no-os-tmpdir.cjs +8 -0
  209. package/eslint/rules/no-path-operations-in-comparisons.cjs +7 -2
  210. package/eslint/rules/no-path-sep-in-strings.cjs +7 -2
  211. package/eslint/rules/no-path-startswith.cjs +7 -2
  212. package/eslint/rules/no-process-exit-in-phase.cjs +127 -0
  213. package/eslint/rules/no-raw-node-path.cjs +563 -0
  214. package/eslint/rules/no-raw-text-decode.cjs +305 -0
  215. package/eslint/rules/no-registry-count-pin.cjs +255 -0
  216. package/eslint/rules/no-self-package-import.cjs +168 -0
  217. package/eslint/rules/no-test-scoped-functions.cjs +10 -3
  218. package/eslint/rules/no-unix-shell-commands.cjs +6 -2
  219. package/eslint/rules/no-unsafe-root-join.cjs +23 -2
  220. package/eslint/rules/no-url-pathname-for-fs.cjs +5 -1
  221. package/eslint/rules/no-version-literal.cjs +151 -0
  222. package/eslint/rules/prefer-startswith-over-regex.cjs +9 -0
  223. package/eslint/rules/require-justified-skip.cjs +8 -2
  224. package/eslint/rules/safe-import.cjs +1 -1
  225. package/package.json +16 -16
  226. package/dist/link-auth/build-headers.d.ts +0 -34
  227. package/dist/link-auth/build-headers.d.ts.map +0 -1
  228. package/dist/link-auth/build-headers.js +0 -58
  229. package/dist/link-auth/build-headers.js.map +0 -1
  230. package/dist/link-auth/expand-macro.d.ts +0 -38
  231. package/dist/link-auth/expand-macro.d.ts.map +0 -1
  232. package/dist/link-auth/expand-macro.js +0 -133
  233. package/dist/link-auth/expand-macro.js.map +0 -1
  234. package/dist/link-auth/macros.yaml +0 -50
  235. package/dist/link-auth/resolve-token.d.ts +0 -83
  236. package/dist/link-auth/resolve-token.d.ts.map +0 -1
  237. package/dist/link-auth/resolve-token.js +0 -101
  238. package/dist/link-auth/resolve-token.js.map +0 -1
  239. package/dist/link-auth/resolve.d.ts +0 -102
  240. package/dist/link-auth/resolve.d.ts.map +0 -1
  241. package/dist/link-auth/resolve.js +0 -66
  242. package/dist/link-auth/resolve.js.map +0 -1
  243. package/dist/link-auth/rewrite.d.ts +0 -52
  244. package/dist/link-auth/rewrite.d.ts.map +0 -1
  245. package/dist/link-auth/rewrite.js +0 -102
  246. package/dist/link-auth/rewrite.js.map +0 -1
  247. package/dist/link-auth/select-provider.d.ts +0 -30
  248. package/dist/link-auth/select-provider.d.ts.map +0 -1
  249. package/dist/link-auth/select-provider.js +0 -55
  250. package/dist/link-auth/select-provider.js.map +0 -1
  251. package/dist/link-auth/template.d.ts +0 -40
  252. package/dist/link-auth/template.d.ts.map +0 -1
  253. package/dist/link-auth/template.js +0 -89
  254. package/dist/link-auth/template.js.map +0 -1
  255. package/dist/link-auth/transforms.d.ts +0 -46
  256. package/dist/link-auth/transforms.d.ts.map +0 -1
  257. package/dist/link-auth/transforms.js +0 -52
  258. package/dist/link-auth/transforms.js.map +0 -1
  259. package/dist/template-entry.d.ts +0 -10
  260. package/dist/template-entry.d.ts.map +0 -1
  261. package/dist/template-entry.js +0 -10
  262. package/dist/template-entry.js.map +0 -1
  263. package/dist/template.d.ts +0 -7
  264. package/dist/template.d.ts.map +0 -1
  265. package/dist/template.js +0 -18
  266. package/dist/template.js.map +0 -1
  267. package/eslint/rules/no-path-join.cjs +0 -14
  268. package/eslint/rules/no-path-relative.cjs +0 -14
  269. package/eslint/rules/no-path-resolve.cjs +0 -14
  270. package/eslint/rules/path-function-rule-factory.cjs +0 -496
@@ -0,0 +1,363 @@
1
+ /**
2
+ * The ONE temp-directory helper family.
3
+ *
4
+ * Every shape a suite needs to mint scratch space lives here: a per-call
5
+ * `createTempDir`, a tracker that removes what it minted, and the per-suite
6
+ * sync/async suite helpers with their bounded teardown. The audit counted
7
+ * eleven definitions of "give me a temp dir that cleans itself up" across
8
+ * eight modules, six of them named differently and none of them visible to
9
+ * the duplication gate; this module is where they were collapsed to.
10
+ *
11
+ * ⛔ Framework-free, like everything under `testing/`: nothing here imports
12
+ * `vitest`, so the `./testing` subpath keeps the empty third-party set its
13
+ * purity pin asserts. Each suite owns its own hooks and calls these from
14
+ * inside them.
15
+ */
16
+ import { randomBytes } from 'node:crypto';
17
+ import { mkdtempSync, rmSync } from 'node:fs';
18
+ import fs, { mkdtemp } from 'node:fs/promises';
19
+ import { isUnderRoot } from '../path-containment.js';
20
+ import { mkdirSyncReal, normalizedTmpdir, safePath } from '../path-utils.js';
21
+ /**
22
+ * Mint a fresh temp directory under the host's (short-name-resolved) tmpdir.
23
+ *
24
+ * The one-liner that used to be spelled in three packages as
25
+ * `createTestTempDir`. Pair with {@link removeTempDir}, or let
26
+ * {@link tempDirTracker} pair them for you.
27
+ *
28
+ * @param prefix - `mkdtemp` prefix, so a leaked directory names its own suite
29
+ * @returns Absolute, forward-slashed path of the new directory
30
+ */
31
+ export function createTempDir(prefix) {
32
+ return safePath.resolve(mkdtempSync(safePath.join(normalizedTmpdir(), prefix)));
33
+ }
34
+ /** The async counterpart of {@link createTempDir}, for suites already on `fs/promises`. */
35
+ export async function createTempDirAsync(prefix) {
36
+ return safePath.resolve(await mkdtemp(safePath.join(normalizedTmpdir(), prefix)));
37
+ }
38
+ /**
39
+ * Remove a directory {@link createTempDir} minted — and ONLY such a directory.
40
+ *
41
+ * Refuses, by name, to remove anything that is not strictly under the host
42
+ * tmpdir as the filesystem sees it. A teardown is the one place test code
43
+ * runs `rm -rf` on a variable, and a variable that was never assigned (a
44
+ * `beforeAll` that threw), or was assigned a fixture INSIDE the repo, must
45
+ * not become `rm -rf ''` or `rm -rf packages/`. The `dev-tools` copy of this
46
+ * helper carried that guard as a lexical `startsWith`; the guard here asks
47
+ * the filesystem, so a symlinked tmpdir (macOS) still passes.
48
+ *
49
+ * `force: true` tolerates a directory already gone; the retries are Node's
50
+ * own remedy for the transient `EBUSY` / `EPERM` a just-closed handle
51
+ * produces on Windows. Anything left after that is a real teardown failure
52
+ * and stays loud — a teardown that swallows it hides a leaking fixture.
53
+ *
54
+ * @param dir - The directory to remove
55
+ * @throws When `dir` is not strictly under the host tmpdir
56
+ */
57
+ export function removeTempDir(dir) {
58
+ if (isUnderRoot(normalizedTmpdir(), dir) === 'outside') {
59
+ throw new Error(`removeTempDir: refusing to remove ${dir} — not inside the host tmpdir ${normalizedTmpdir()}`);
60
+ }
61
+ rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
62
+ }
63
+ /**
64
+ * A factory that remembers every directory it minted, so one `afterEach`
65
+ * removes them all.
66
+ *
67
+ * @param prefix - `mkdtemp` prefix for every directory this tracker mints
68
+ * @returns `create` to mint one more, `cleanupAll` to remove every one so far
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const scratch = tempDirTracker('my-suite-');
73
+ * afterEach(() => scratch.cleanupAll());
74
+ * it('…', () => { const dir = scratch.create(); … });
75
+ * ```
76
+ */
77
+ export function tempDirTracker(prefix) {
78
+ const minted = [];
79
+ return {
80
+ create: () => {
81
+ const dir = createTempDir(prefix);
82
+ minted.push(dir);
83
+ return dir;
84
+ },
85
+ cleanupAll: () => {
86
+ for (const dir of minted)
87
+ removeTempDir(dir);
88
+ minted.length = 0;
89
+ },
90
+ };
91
+ }
92
+ /**
93
+ * How long a scratch-dir teardown may run before it gives up and warns.
94
+ *
95
+ * The value only has to be comfortably *under* the hook timeout it runs in —
96
+ * that is the whole design. Sizing a teardown budget to beat contention is
97
+ * unprovable (see {@link removeScratchDir}); sizing it below a known constant is
98
+ * arithmetic.
99
+ *
100
+ * ⚠️ **The known constant is the UNIT tier's**, which is the only tier that
101
+ * takes vitest's 10s default (`vitest.shared.ts` declines to override it there
102
+ * on purpose) and the tier where the flake was actually observed. The other two
103
+ * tiers set their own, far larger: integration gets `platformTestTimeout`
104
+ * (60s on Unix, 900s on Windows) and system gets 300s. A suite in those tiers
105
+ * inherits this 4s default and therefore gives up 15x–225x earlier than its hook
106
+ * would have allowed — for a heavy fixture tree that is a leaked directory and a
107
+ * warning bought for nothing, since an abandoned removal does not stop (see
108
+ * {@link removeScratchDir}). Such a suite should pass its own `budgetMs`, which
109
+ * both suite helpers forward.
110
+ */
111
+ const SCRATCH_REMOVAL_BUDGET_MS = 4000;
112
+ /**
113
+ * Delete a scratch directory as *best effort* — never failing the suite that
114
+ * created it, and never taking longer than its own budget to say so.
115
+ *
116
+ * ## Why this is not just `await rm(dir, { recursive: true, force: true })`
117
+ *
118
+ * A teardown hook that can redden a suite whose every assertion passed is a
119
+ * defect in the harness, not a flake. `packages/lab/test/instrument.test.ts`
120
+ * timed out here on two consecutive Windows runs with all 655 assertions
121
+ * green — only the cleanup lost.
122
+ *
123
+ * The measurement is what rules out the obvious fixes: that scratch dir holds
124
+ * 490 files / 378 KiB across 14 fixture git repos, and deletes in **59 ms**
125
+ * idle. Against vitest's 10,000 ms unit-hook budget that is 170x of headroom,
126
+ * and Windows blew through it anyway. No quantity of real work explains that,
127
+ * so the cause is scheduling — contention from a fully parallel `validate`,
128
+ * plus per-unlink antivirus on Windows — which is unbounded by nature. Hence:
129
+ *
130
+ * - **Raising `hookTimeout` cannot be argued.** You would be picking a number
131
+ * to beat an unbounded quantity, when 10s of 170x headroom already lost. It
132
+ * also punches a hole in the deliberate policy in `vitest.shared.ts` ("no
133
+ * hookTimeout override here on purpose") for every unit hook, to fix one.
134
+ * - **`try`/`catch` around the `rm` cannot work.** A vitest hook timeout is a
135
+ * race decided on the *timer* side; the hook's own catch never sees it. It
136
+ * addresses a failure mode we did not observe and leaves the one we did.
137
+ * - **`maxRetries` alone makes it worse.** Retries target transient
138
+ * `EPERM`/`EBUSY`, which fail *fast*; our failure was *slow*, and retry
139
+ * backoff only adds to it. Kept below as a cheap inner win, not as the fix.
140
+ *
141
+ * So the deadline is taken away from vitest: the removal races a timer of our
142
+ * own, well inside the hook budget, and expiry is a warning rather than a
143
+ * failure. The hook therefore always resolves in time, which makes it
144
+ * *structurally* incapable of reddening a green suite on any machine at any
145
+ * load — rather than merely unlikely to.
146
+ *
147
+ * The cost, stated plainly: under pathological contention the directory
148
+ * survives in the OS temp dir, which the OS reclaims, and the warning names
149
+ * the path. It can never surface as an unhandled rejection, because the only
150
+ * rejection handler is installed before the race.
151
+ *
152
+ * ⚠️ **Abandoning the removal does not stop it, and does not free the worker.**
153
+ * A pending libuv `fs` request is an active handle, so the `rm` runs to
154
+ * completion regardless — measured at 2,407 ms on an 8,000-file tree after the
155
+ * race was decided at 5 ms — and the process cannot exit until it does.
156
+ * `timer.unref()` below unrefs the *timer*, not the removal. So what this buys
157
+ * is bounded: the **hook** always resolves in time, which is what stops a green
158
+ * suite going red. It does **not** shed the work, and under the contention it
159
+ * targets the abandoned removal competes for disk with whatever runs next in
160
+ * the same worker. That is the trade, and it is why the budget wants to be as
161
+ * large as the tier's hook allows rather than as small as possible.
162
+ *
163
+ * @param dir - Directory to remove. An empty string is a no-op, so a suite
164
+ * whose `beforeAll` never ran can call this unconditionally.
165
+ * @param options - Deadline and warning sink
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * afterAll(async () => {
170
+ * await removeScratchDir(scratch);
171
+ * });
172
+ * ```
173
+ */
174
+ export async function removeScratchDir(dir, options = {}) {
175
+ if (dir === '')
176
+ return;
177
+ const budgetMs = options.budgetMs ?? SCRATCH_REMOVAL_BUDGET_MS;
178
+ const onWarn = options.onWarn ??
179
+ ((message) => {
180
+ console.warn(message);
181
+ });
182
+ // Latches on the first outcome so a removal that finishes (or fails) after
183
+ // the budget expired cannot log a second time into an already-finished suite.
184
+ let settled = false;
185
+ const giveUp = (reason) => {
186
+ if (settled)
187
+ return;
188
+ settled = true;
189
+ onWarn(`scratch dir left behind at ${dir}: ${reason}`);
190
+ };
191
+ const remove = options.remove ??
192
+ ((target) => fs.rm(target, { recursive: true, force: true, maxRetries: 2, retryDelay: 50 }));
193
+ const removal = remove(dir)
194
+ .then(() => {
195
+ settled = true;
196
+ })
197
+ .catch((error) => {
198
+ giveUp(error instanceof Error ? error.message : String(error));
199
+ });
200
+ let timer;
201
+ const deadline = new Promise((resolve) => {
202
+ timer = setTimeout(() => {
203
+ giveUp(`removal did not finish within ${budgetMs}ms`);
204
+ resolve();
205
+ }, budgetMs);
206
+ // Never hold the process open for a teardown nobody is waiting on.
207
+ timer.unref();
208
+ });
209
+ await Promise.race([removal, deadline]);
210
+ clearTimeout(timer);
211
+ }
212
+ /**
213
+ * Get isolated test output directory for current test run
214
+ *
215
+ * Creates a unique directory under `packages/{packageName}/.test-output/{testType}/{runId}`
216
+ * where runId is `{timestamp}-{randomId}` to ensure isolation across parallel test runs.
217
+ *
218
+ * @param packageName - Name of package (e.g., 'rag-lancedb')
219
+ * @param testType - Type of test ('unit', 'integration', 'system')
220
+ * @param subdirs - Optional subdirectories to create within the test output directory
221
+ * @returns Absolute path to the created directory
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * // Create isolated database directory for system tests
226
+ * const dbPath = getTestOutputDir('rag-lancedb', 'system', 'databases', 'test-db');
227
+ * // Result: packages/rag-lancedb/.test-output/system/20260105-143022-abc123/databases/test-db
228
+ *
229
+ * // Create temporary file directory for integration tests
230
+ * const tempDir = getTestOutputDir('agent-skills', 'integration', 'temp-files');
231
+ * // Result: packages/agent-skills/.test-output/integration/20260105-143022-def456/temp-files
232
+ * ```
233
+ */
234
+ export function getTestOutputDir(packageName, testType, ...subdirs) {
235
+ // Generate unique run ID: timestamp + random hex
236
+ const timestamp = new Date().toISOString().replaceAll(/[:.]/g, '-').slice(0, 19);
237
+ const randomId = randomBytes(4).toString('hex');
238
+ const runId = `${timestamp}-${randomId}`;
239
+ // Find project root (assuming we're always in packages/*/test/*)
240
+ const projectRoot = safePath.resolve(process.cwd());
241
+ // Build path: packages/{packageName}/.test-output/{testType}/{runId}/{...subdirs}
242
+ const testOutputDir = safePath.join(projectRoot, 'packages', packageName, '.test-output', testType, runId, ...subdirs);
243
+ // Create directory structure and return normalized path
244
+ return mkdirSyncReal(testOutputDir, { recursive: true });
245
+ }
246
+ /**
247
+ * Get the base test output directory for a package
248
+ * Useful for cleanup operations that need to remove all test output
249
+ *
250
+ * @param packageName - Name of package (e.g., 'rag-lancedb')
251
+ * @returns Absolute path to packages/{packageName}/.test-output
252
+ *
253
+ * @example
254
+ * ```typescript
255
+ * const baseDir = getTestOutputBase('rag-lancedb');
256
+ * // Result: packages/rag-lancedb/.test-output
257
+ * ```
258
+ */
259
+ export function getTestOutputBase(packageName) {
260
+ const projectRoot = safePath.resolve(process.cwd());
261
+ return safePath.join(projectRoot, 'packages', packageName, '.test-output');
262
+ }
263
+ /**
264
+ * Per-suite temp directory pattern (async version)
265
+ * Creates a single temp directory for the entire test suite,
266
+ * with subdirectories for each test. This is 3-5x faster on Windows
267
+ * than creating a new mkdtemp for each test.
268
+ *
269
+ * @param prefix - Prefix for the suite temp directory name
270
+ * @param teardown - Forwarded to {@link removeScratchDir}. Raise `budgetMs` for a
271
+ * suite whose fixture tree is heavy or whose tier allows a longer hook than the
272
+ * unit tier this default is sized against — see {@link SCRATCH_REMOVAL_BUDGET_MS}.
273
+ * @returns Suite helper with beforeAll, afterAll, beforeEach, afterEach, and getTempDir
274
+ *
275
+ * @example
276
+ * ```typescript
277
+ * const suite = setupAsyncTempDirSuite('my-test');
278
+ *
279
+ * describe('my tests', () => {
280
+ * beforeAll(suite.beforeAll);
281
+ * afterAll(suite.afterAll);
282
+ * beforeEach(suite.beforeEach);
283
+ *
284
+ * it('test 1', async () => {
285
+ * const tempDir = suite.getTempDir();
286
+ * // Use tempDir...
287
+ * });
288
+ * });
289
+ * ```
290
+ */
291
+ export function setupAsyncTempDirSuite(prefix, teardown = {}) {
292
+ let suiteDir = '';
293
+ let tempDir = '';
294
+ let testCounter = 0;
295
+ return {
296
+ beforeAll: async () => {
297
+ suiteDir = await fs.mkdtemp(safePath.join(normalizedTmpdir(), `${prefix}-suite-`));
298
+ },
299
+ afterAll: async () => {
300
+ await removeScratchDir(suiteDir, teardown);
301
+ },
302
+ beforeEach: async () => {
303
+ testCounter++;
304
+ tempDir = safePath.join(suiteDir, `test-${testCounter}`);
305
+ await fs.mkdir(tempDir, { recursive: true });
306
+ },
307
+ afterEach: async () => {
308
+ // Per-test cleanup handled by suite cleanup
309
+ },
310
+ getTempDir: () => tempDir,
311
+ };
312
+ }
313
+ /**
314
+ * Per-suite temp directory pattern (sync version)
315
+ * Creates a single temp directory for the entire test suite,
316
+ * with subdirectories for each test. This is 3-5x faster on Windows
317
+ * than creating a new mkdtemp for each test.
318
+ *
319
+ * @param prefix - Prefix for the suite temp directory name
320
+ * @param teardown - Forwarded to {@link removeScratchDir}. Raise `budgetMs` for a
321
+ * suite whose fixture tree is heavy or whose tier allows a longer hook than the
322
+ * unit tier this default is sized against — see {@link SCRATCH_REMOVAL_BUDGET_MS}.
323
+ * @returns Suite helper with beforeAll, afterAll, beforeEach, afterEach, and getTempDir
324
+ *
325
+ * @example
326
+ * ```typescript
327
+ * const suite = setupSyncTempDirSuite('my-test');
328
+ *
329
+ * describe('my tests', () => {
330
+ * beforeAll(suite.beforeAll);
331
+ * afterAll(suite.afterAll);
332
+ * beforeEach(suite.beforeEach);
333
+ *
334
+ * it('test 1', () => {
335
+ * const tempDir = suite.getTempDir();
336
+ * // Use tempDir...
337
+ * });
338
+ * });
339
+ * ```
340
+ */
341
+ export function setupSyncTempDirSuite(prefix, teardown = {}) {
342
+ let suiteDir = '';
343
+ let tempDir = '';
344
+ let testCounter = 0;
345
+ return {
346
+ beforeAll: () => {
347
+ suiteDir = mkdtempSync(safePath.join(normalizedTmpdir(), `${prefix}-suite-`));
348
+ },
349
+ afterAll: async () => {
350
+ await removeScratchDir(suiteDir, teardown);
351
+ },
352
+ beforeEach: () => {
353
+ testCounter++;
354
+ tempDir = safePath.join(suiteDir, `test-${testCounter}`);
355
+ mkdirSyncReal(tempDir);
356
+ },
357
+ afterEach: () => {
358
+ // Per-test cleanup handled by suite cleanup
359
+ },
360
+ getTempDir: () => tempDir,
361
+ };
362
+ }
363
+ //# sourceMappingURL=temp-dir.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"temp-dir.js","sourceRoot":"","sources":["../../src/testing/temp-dir.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO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CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,WAAW,EAAE,CAAC,CAAC;YACzD,MAAM,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC/C,CAAC;QACD,SAAS,EAAE,KAAK,IAAI,EAAE;YACpB,4CAA4C;QAC9C,CAAC;QACD,UAAU,EAAE,GAAG,EAAE,CAAC,OAAO;KAC1B,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAc,EAAE,WAAoC,EAAE;IAU1F,IAAI,QAAQ,GAAG,EAAE,CAAC;IAClB,IAAI,OAAO,GAAG,EAAE,CAAC;IACjB,IAAI,WAAW,GAAG,CAAC,CAAC;IAEpB,OAAO;QACL,SAAS,EAAE,GAAG,EAAE;YACd,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,gBAAgB,EAAE,EAAE,GAAG,MAAM,SAAS,CAAC,CAAC,CAAC;QAChF,CAAC;QACD,QAAQ,EAAE,KAAK,IAAI,EAAE;YACnB,MAAM,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAC7C,CAAC;QACD,UAAU,EAAE,GAAG,EAAE;YACf,WAAW,EAAE,CAAC;YACd,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,WAAW,EAAE,CAAC,CAAC;YACzD,aAAa,CAAC,OAAO,CAAC,CAAC;QACzB,CAAC;QACD,SAAS,EAAE,GAAG,EAAE;YACd,4CAA4C;QAC9C,CAAC;QACD,UAAU,EAAE,GAAG,EAAE,CAAC,OAAO;KAC1B,CAAC;AACJ,CAAC"}
package/dist/testing.d.ts CHANGED
@@ -1,8 +1,99 @@
1
1
  /**
2
- * @vibe-agent-toolkit/utils/testing
2
+ * Fixture primitives shared by suites in more than one package.
3
3
  *
4
- * Isolated temp-directory helpers for test suites. Node-only. Intended for
5
- * a consumer's test code — not for their shipped runtime.
4
+ * Deliberately framework-free: nothing here imports `vitest`, so this module
5
+ * stays a plain function library rather than something that registers hooks as a
6
+ * side effect of being imported. Each suite owns its own `beforeEach`/
7
+ * `afterEach` and calls these from inside them — two lines, which is below
8
+ * anything worth sharing, while the part that is genuinely identical (mkdtemp,
9
+ * write the literal, remove the tree) lives here once.
10
+ *
11
+ * It earned a home in `utils` the ordinary way: two packages needed it, not one
12
+ * package speculating that a second might.
13
+ *
14
+ * ⛔ **Do not import `vitest` here to share the hook wrapper itself.**
15
+ * `subpath-purity.test.ts` pins this entry at an EMPTY third-party set, and that
16
+ * pin is load-bearing: `./testing` is a published subpath, so a test framework
17
+ * reached from it becomes a runtime requirement for every adopter who imports
18
+ * it. {@link replantableCorpus} exists precisely so the per-test wrapper each
19
+ * suite still writes is three lines rather than twelve.
6
20
  */
21
+ import type { UnreadablePolicy } from './listing-refusal.js';
7
22
  export * from './test-helpers.js';
23
+ export { __readCrawlTimingSnapshot, __setCrawlTimingForTest, __writeCrawlTimingDumpForTest, } from './crawl-timing.js';
24
+ export * from './testing/executables.js';
25
+ export * from './testing/hostile-tree.js';
26
+ export * from './testing/platform-gates.js';
27
+ export * from './testing/temp-dir.js';
28
+ /** A planted fixture tree and the means to remove it. */
29
+ export interface TempCorpus {
30
+ /** Absolute path to the tree's root. */
31
+ root: string;
32
+ /** Remove the tree. Safe to call when it is already gone. */
33
+ cleanup: () => void;
34
+ }
35
+ /**
36
+ * Write a literal corpus into a fresh temp directory.
37
+ *
38
+ * 🪤 Fixture files must NOT be byte-identical to each other when the suite is
39
+ * about content-addressed behaviour: VAT's blobs are keyed on their bytes plus
40
+ * the parser kind, so two files with the same content collapse into ONE blob and
41
+ * assertions then describe whichever path sorted first. Give each fixture a
42
+ * distinguishing marker line.
43
+ *
44
+ * ⚠️ The root is minted per call, so a suite that plants per test must call this
45
+ * per test — a root captured once and reused across tests survives its own
46
+ * `cleanup`.
47
+ *
48
+ * @param prefix - `mkdtemp` prefix, so a leaked directory names its own suite
49
+ * @param corpus - Fixture name to file content; written verbatim as UTF-8.
50
+ * Names may include forward-slash subpaths only if their parents already exist
51
+ * @returns The tree's root and its teardown
52
+ */
53
+ export declare function createTempCorpus(prefix: string, corpus: Readonly<Record<string, string>>): TempCorpus;
54
+ /** A corpus that can be planted and cleared repeatedly — one tree per test. */
55
+ export interface ReplantableCorpus {
56
+ /** Plant a fresh tree. Drive from the suite's `beforeEach`. */
57
+ plant: () => void;
58
+ /** Remove the current tree, if any. Drive from the suite's `afterEach`. */
59
+ clear: () => void;
60
+ /** The root minted by the most recent {@link plant}. */
61
+ root: () => string;
62
+ }
63
+ /**
64
+ * Hold a per-test corpus root, so a suite's hook wrapper is three lines.
65
+ *
66
+ * ⚠️ **The root comes back through a GETTER, never as a value.** A per-test
67
+ * fixture is reminted for every test, so a root captured at registration time is
68
+ * `undefined` in the first test and stale in every one after it. That mistake is
69
+ * the reason this holder exists rather than each suite keeping its own `let`.
70
+ *
71
+ * ⭐ Framework-free on purpose: it takes no hooks and registers none, so the
72
+ * suite still owns its own `beforeEach`/`afterEach` and this module keeps the
73
+ * empty third-party set its purity pin asserts. See the module docstring.
74
+ *
75
+ * 🪤 `root()` before the first `plant()` THROWS by name. The shape it replaced
76
+ * read an uninitialised `let` and raised `Cannot read properties of undefined`
77
+ * from inside the fixture, which names neither the suite nor the missing hook.
78
+ *
79
+ * @param prefix - `mkdtemp` prefix, so a leaked directory names its own suite
80
+ * @param corpus - Fixture name to file content, as {@link createTempCorpus} takes it
81
+ * @returns Plant/clear/root, to be driven from the caller's own hooks
82
+ */
83
+ export declare function replantableCorpus(prefix: string, corpus: Readonly<Record<string, string>>): ReplantableCorpus;
84
+ /**
85
+ * The `unreadable` policy a FIXTURE crawl states: refuse, against the fixture
86
+ * root, with a remedy that names the fixture rather than an adopter knob.
87
+ *
88
+ * A suite that is not ABOUT refusals still has to say what one would mean —
89
+ * the policy is required, with no default, and test files are not typechecked
90
+ * — and the honest answer for a planted tree is that a directory it cannot
91
+ * list is a broken fixture, not a case to degrade around. A suite that IS
92
+ * about refusals spells its own policy inline, because the policy is the
93
+ * subject.
94
+ *
95
+ * @param root - The fixture root the refused directory is expressed against
96
+ * @returns A `refuse` policy for `crawlDirectory` and the git listings
97
+ */
98
+ export declare function refuseUnreadableFixture(root: string): UnreadablePolicy;
8
99
  //# sourceMappingURL=testing.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,cAAc,mBAAmB,CAAC"}
1
+ {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAIH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAO7D,cAAc,mBAAmB,CAAC;AAIlC,OAAO,EACL,yBAAyB,EACzB,uBAAuB,EACvB,6BAA6B,GAC9B,MAAM,mBAAmB,CAAC;AAG3B,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,uBAAuB,CAAC;AAEtC,yDAAyD;AACzD,MAAM,WAAW,UAAU;IACzB,wCAAwC;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,6DAA6D;IAC7D,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GACvC,UAAU,CAMZ;AAED,+EAA+E;AAC/E,MAAM,WAAW,iBAAiB;IAChC,+DAA+D;IAC/D,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,2EAA2E;IAC3E,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,wDAAwD;IACxD,IAAI,EAAE,MAAM,MAAM,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GACvC,iBAAiB,CAyBnB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,CAEtE"}
package/dist/testing.js CHANGED
@@ -1,8 +1,124 @@
1
1
  /**
2
- * @vibe-agent-toolkit/utils/testing
2
+ * Fixture primitives shared by suites in more than one package.
3
3
  *
4
- * Isolated temp-directory helpers for test suites. Node-only. Intended for
5
- * a consumer's test code — not for their shipped runtime.
4
+ * Deliberately framework-free: nothing here imports `vitest`, so this module
5
+ * stays a plain function library rather than something that registers hooks as a
6
+ * side effect of being imported. Each suite owns its own `beforeEach`/
7
+ * `afterEach` and calls these from inside them — two lines, which is below
8
+ * anything worth sharing, while the part that is genuinely identical (mkdtemp,
9
+ * write the literal, remove the tree) lives here once.
10
+ *
11
+ * It earned a home in `utils` the ordinary way: two packages needed it, not one
12
+ * package speculating that a second might.
13
+ *
14
+ * ⛔ **Do not import `vitest` here to share the hook wrapper itself.**
15
+ * `subpath-purity.test.ts` pins this entry at an EMPTY third-party set, and that
16
+ * pin is load-bearing: `./testing` is a published subpath, so a test framework
17
+ * reached from it becomes a runtime requirement for every adopter who imports
18
+ * it. {@link replantableCorpus} exists precisely so the per-test wrapper each
19
+ * suite still writes is three lines rather than twelve.
6
20
  */
21
+ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
22
+ import { normalizedTmpdir, safePath } from './path-utils.js';
23
+ // The rest of the testing surface. ⚠️ This re-export is the ENTIRETY of what
24
+ // `./testing` used to be, and dropping it silently removed `detachGitEnv`,
25
+ // `setupSyncTempDirSuite` and every sibling from a published subpath. A module
26
+ // that is both a definition site and a barrel loses the barrel first.
7
27
  export * from './test-helpers.js';
28
+ // The crawl-timing recorder's test seams: read the snapshot, point it at a
29
+ // directory, flush a dump. Test scaffolding, so they live on this subpath and
30
+ // not on the runtime barrel that publishes the recorder itself.
31
+ export { __readCrawlTimingSnapshot, __setCrawlTimingForTest, __writeCrawlTimingDumpForTest, } from './crawl-timing.js';
32
+ // The three fixture modules the sinks' suites share: the ONE temp-dir family,
33
+ // the host gates, and the hostile tree every sink is tested against.
34
+ export * from './testing/executables.js';
35
+ export * from './testing/hostile-tree.js';
36
+ export * from './testing/platform-gates.js';
37
+ export * from './testing/temp-dir.js';
38
+ /**
39
+ * Write a literal corpus into a fresh temp directory.
40
+ *
41
+ * 🪤 Fixture files must NOT be byte-identical to each other when the suite is
42
+ * about content-addressed behaviour: VAT's blobs are keyed on their bytes plus
43
+ * the parser kind, so two files with the same content collapse into ONE blob and
44
+ * assertions then describe whichever path sorted first. Give each fixture a
45
+ * distinguishing marker line.
46
+ *
47
+ * ⚠️ The root is minted per call, so a suite that plants per test must call this
48
+ * per test — a root captured once and reused across tests survives its own
49
+ * `cleanup`.
50
+ *
51
+ * @param prefix - `mkdtemp` prefix, so a leaked directory names its own suite
52
+ * @param corpus - Fixture name to file content; written verbatim as UTF-8.
53
+ * Names may include forward-slash subpaths only if their parents already exist
54
+ * @returns The tree's root and its teardown
55
+ */
56
+ export function createTempCorpus(prefix, corpus) {
57
+ const root = mkdtempSync(safePath.join(normalizedTmpdir(), prefix));
58
+ for (const [name, content] of Object.entries(corpus)) {
59
+ writeFileSync(safePath.join(root, name), content, 'utf8');
60
+ }
61
+ return { root, cleanup: () => rmSync(root, { recursive: true, force: true }) };
62
+ }
63
+ /**
64
+ * Hold a per-test corpus root, so a suite's hook wrapper is three lines.
65
+ *
66
+ * ⚠️ **The root comes back through a GETTER, never as a value.** A per-test
67
+ * fixture is reminted for every test, so a root captured at registration time is
68
+ * `undefined` in the first test and stale in every one after it. That mistake is
69
+ * the reason this holder exists rather than each suite keeping its own `let`.
70
+ *
71
+ * ⭐ Framework-free on purpose: it takes no hooks and registers none, so the
72
+ * suite still owns its own `beforeEach`/`afterEach` and this module keeps the
73
+ * empty third-party set its purity pin asserts. See the module docstring.
74
+ *
75
+ * 🪤 `root()` before the first `plant()` THROWS by name. The shape it replaced
76
+ * read an uninitialised `let` and raised `Cannot read properties of undefined`
77
+ * from inside the fixture, which names neither the suite nor the missing hook.
78
+ *
79
+ * @param prefix - `mkdtemp` prefix, so a leaked directory names its own suite
80
+ * @param corpus - Fixture name to file content, as {@link createTempCorpus} takes it
81
+ * @returns Plant/clear/root, to be driven from the caller's own hooks
82
+ */
83
+ export function replantableCorpus(prefix, corpus) {
84
+ let planted;
85
+ return {
86
+ plant: () => {
87
+ // 🪤 Removes any tree still standing FIRST. Nested `describe` blocks each
88
+ // get their own `beforeEach` and vitest runs outer-then-inner before a
89
+ // single `afterEach`, so a double plant is ordinary rather than exotic.
90
+ // Overwriting the handle without this would leak the first tree for the
91
+ // process's lifetime, silently and only on the suites that nest.
92
+ planted?.cleanup();
93
+ planted = createTempCorpus(prefix, corpus);
94
+ },
95
+ clear: () => {
96
+ planted?.cleanup();
97
+ planted = undefined;
98
+ },
99
+ root: () => {
100
+ if (planted === undefined) {
101
+ throw new Error(`replantableCorpus('${prefix}'): root() before plant() — the suite is missing its beforeEach`);
102
+ }
103
+ return planted.root;
104
+ },
105
+ };
106
+ }
107
+ /**
108
+ * The `unreadable` policy a FIXTURE crawl states: refuse, against the fixture
109
+ * root, with a remedy that names the fixture rather than an adopter knob.
110
+ *
111
+ * A suite that is not ABOUT refusals still has to say what one would mean —
112
+ * the policy is required, with no default, and test files are not typechecked
113
+ * — and the honest answer for a planted tree is that a directory it cannot
114
+ * list is a broken fixture, not a case to degrade around. A suite that IS
115
+ * about refusals spells its own policy inline, because the policy is the
116
+ * subject.
117
+ *
118
+ * @param root - The fixture root the refused directory is expressed against
119
+ * @returns A `refuse` policy for `crawlDirectory` and the git listings
120
+ */
121
+ export function refuseUnreadableFixture(root) {
122
+ return { refuse: { root, remedy: 'A test fixture must be listable in full; fix the fixture, not the walk.' } };
123
+ }
8
124
  //# sourceMappingURL=testing.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"testing.js","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,cAAc,mBAAmB,CAAC"}
1
+ {"version":3,"file":"testing.js","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAG7D,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE7D,6EAA6E;AAC7E,2EAA2E;AAC3E,+EAA+E;AAC/E,sEAAsE;AACtE,cAAc,mBAAmB,CAAC;AAClC,2EAA2E;AAC3E,8EAA8E;AAC9E,gEAAgE;AAChE,OAAO,EACL,yBAAyB,EACzB,uBAAuB,EACvB,6BAA6B,GAC9B,MAAM,mBAAmB,CAAC;AAC3B,8EAA8E;AAC9E,qEAAqE;AACrE,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,uBAAuB,CAAC;AAUtC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,gBAAgB,CAC9B,MAAc,EACd,MAAwC;IAExC,MAAM,IAAI,GAAG,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,gBAAgB,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC;IACpE,KAAK,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACrD,aAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAC5D,CA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