@hop-top/kit 0.5.0-alpha.1 → 0.5.0-alpha.11

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 (293) hide show
  1. package/CHANGELOG.md +275 -0
  2. package/LICENSE +1 -1
  3. package/LICENSE-cobra.txt +174 -0
  4. package/NOTICE +15 -0
  5. package/README.md +55 -259
  6. package/dist/aim-DZHwwiIc.d.ts +139 -0
  7. package/dist/aim.d.ts +1 -116
  8. package/dist/aim.js +13 -414
  9. package/dist/alias-D9_qFlyP.d.ts +174 -0
  10. package/dist/alias.d.ts +2 -73
  11. package/dist/alias.js +27 -207
  12. package/dist/api-DAexsu2H.d.ts +77 -0
  13. package/dist/api.d.ts +1 -62
  14. package/dist/api.js +10 -164
  15. package/dist/auth-2-jfgiHn.d.ts +34 -0
  16. package/dist/auth.d.ts +1 -26
  17. package/dist/auth.js +6 -40
  18. package/dist/chunk-3FIKGFKI.js +54 -0
  19. package/dist/chunk-4FMALWYB.js +669 -0
  20. package/dist/chunk-4GPCWBBA.js +143 -0
  21. package/dist/chunk-4UE5TCMQ.js +104 -0
  22. package/dist/chunk-65VPWMPV.js +1134 -0
  23. package/dist/chunk-6HFAEZ52.js +29 -0
  24. package/dist/chunk-6SVOHNIC.js +17 -0
  25. package/dist/chunk-BDNNPRX6.js +41 -0
  26. package/dist/chunk-BRNEKPWX.js +37 -0
  27. package/dist/chunk-BXJ2J27B.js +219 -0
  28. package/dist/chunk-CSJWB6D3.js +151 -0
  29. package/dist/chunk-CXBZTONY.js +65 -0
  30. package/dist/chunk-EE4M2UOF.js +818 -0
  31. package/dist/chunk-GFRAYYRD.js +817 -0
  32. package/dist/chunk-IVQHLI56.js +1890 -0
  33. package/dist/chunk-IZ6Q4SE3.js +178 -0
  34. package/dist/chunk-JKMQSAJP.js +174 -0
  35. package/dist/chunk-K7E34Q3A.js +1445 -0
  36. package/dist/chunk-KVG7YCTS.js +115 -0
  37. package/dist/chunk-L23O2FFE.js +51 -0
  38. package/dist/chunk-MFVUENZR.js +1549 -0
  39. package/dist/chunk-ML7D5DWY.js +263 -0
  40. package/dist/chunk-MQXYDYRD.js +968 -0
  41. package/dist/chunk-POR5L2DG.js +247 -0
  42. package/dist/chunk-QJVSKKRQ.js +369 -0
  43. package/dist/chunk-QQQYCWSC.js +240 -0
  44. package/dist/chunk-R5U7XKVJ.js +16 -0
  45. package/dist/chunk-RYKLVOVK.js +309 -0
  46. package/dist/chunk-SNVGDYX7.js +187 -0
  47. package/dist/chunk-T6OZMPPV.js +68 -0
  48. package/dist/chunk-UKZIAQA7.js +26 -0
  49. package/dist/chunk-VGOA5Y7S.js +251 -0
  50. package/dist/chunk-VWGP4NVJ.js +513 -0
  51. package/dist/chunk-WBVO43HU.js +355 -0
  52. package/dist/chunk-WUOAQVOA.js +86 -0
  53. package/dist/chunk-XJTRY453.js +868 -0
  54. package/dist/chunk-YOBAUCVN.js +583 -0
  55. package/dist/cli-RSRk1M36.d.ts +707 -0
  56. package/dist/cli.d.ts +7 -309
  57. package/dist/cli.js +125 -945
  58. package/dist/completion-csVun6SP.d.ts +112 -0
  59. package/dist/completion-values-By-mnjNt.d.ts +142 -0
  60. package/dist/completion-values.d.ts +1 -0
  61. package/dist/completion-values.js +29 -0
  62. package/dist/completion.d.ts +3 -0
  63. package/dist/completion.js +34 -0
  64. package/dist/compliance-AxFf-uMy.d.ts +60 -0
  65. package/dist/compliance.d.ts +1 -0
  66. package/dist/compliance.js +17 -0
  67. package/dist/config-CaK60ByQ.d.ts +241 -0
  68. package/dist/config.d.ts +1 -97
  69. package/dist/config.js +8 -78
  70. package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
  71. package/dist/errcorrect.d.ts +1 -37
  72. package/dist/errcorrect.js +8 -68
  73. package/dist/error-Do8Qji43.d.ts +231 -0
  74. package/dist/id/index.d.ts +2 -83
  75. package/dist/id/index.js +14 -65
  76. package/dist/index-BKJZClJx.d.ts +1486 -0
  77. package/dist/index-C3Zw4wDJ.d.ts +359 -0
  78. package/dist/index-CGE5yLBj.d.ts +328 -0
  79. package/dist/index-VLDcI5NF.d.ts +95 -0
  80. package/dist/index.d.ts +37 -0
  81. package/dist/index.js +132 -0
  82. package/dist/llm-DYuR7_uX.d.ts +1069 -0
  83. package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
  84. package/dist/llm-anthropic.d.ts +2 -0
  85. package/dist/llm-anthropic.js +13 -0
  86. package/dist/llm-openai-DyFrucmV.d.ts +87 -0
  87. package/dist/llm-openai.d.ts +2 -0
  88. package/dist/llm-openai.js +19 -0
  89. package/dist/llm.d.ts +1 -188
  90. package/dist/llm.js +101 -356
  91. package/dist/log-CkaxMX3U.d.ts +115 -0
  92. package/dist/log.d.ts +2 -0
  93. package/dist/log.js +14 -0
  94. package/dist/mcp/index.d.ts +4 -0
  95. package/dist/mcp/index.js +185 -0
  96. package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
  97. package/dist/netpolicy.d.ts +1 -0
  98. package/dist/netpolicy.js +25 -0
  99. package/dist/output-CjZWmL8s.d.ts +420 -0
  100. package/dist/output.d.ts +3 -213
  101. package/dist/output.js +135 -447
  102. package/dist/parity-CvMuYcVI.d.ts +61 -0
  103. package/dist/progress-CAzyxeWl.d.ts +38 -0
  104. package/dist/progress.d.ts +1 -30
  105. package/dist/progress.js +6 -55
  106. package/dist/provenance-SMFHm5gx.d.ts +30 -0
  107. package/dist/provenance.d.ts +1 -24
  108. package/dist/provenance.js +6 -31
  109. package/dist/routellm-B0nv59mH.d.ts +111 -0
  110. package/dist/routellm.d.ts +2 -64
  111. package/dist/routellm.js +13 -255
  112. package/dist/rpc-D31Lg0Kc.d.ts +32 -0
  113. package/dist/rpc.d.ts +2 -25
  114. package/dist/rpc.js +4 -98
  115. package/dist/safety-CUaYEjZc.d.ts +30 -0
  116. package/dist/safety.d.ts +1 -24
  117. package/dist/safety.js +6 -43
  118. package/dist/scope-BrhYB4ij.d.ts +141 -0
  119. package/dist/scope.d.ts +1 -121
  120. package/dist/scope.js +20 -377
  121. package/dist/serve-D8yWMrTc.d.ts +646 -0
  122. package/dist/serve.d.ts +3 -0
  123. package/dist/serve.js +75 -0
  124. package/dist/sqlstore.js +6 -39
  125. package/dist/stream-CrjeCp0M.d.ts +44 -0
  126. package/dist/stream.d.ts +1 -35
  127. package/dist/stream.js +8 -51
  128. package/dist/telemetry/index.d.ts +1 -315
  129. package/dist/telemetry/index.js +26 -599
  130. package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
  131. package/dist/toolspec.d.ts +2 -0
  132. package/dist/toolspec.js +37 -0
  133. package/dist/tui/index.d.ts +6 -298
  134. package/dist/tui/index.js +17 -381
  135. package/dist/upgrade-C3RWGwRi.d.ts +125 -0
  136. package/dist/upgrade.d.ts +1 -95
  137. package/dist/upgrade.js +6 -114
  138. package/dist/xdg-DB7Shoib.d.ts +179 -0
  139. package/dist/xdg.d.ts +1 -169
  140. package/dist/xdg.js +14 -128
  141. package/package.json +192 -24
  142. package/src/README.md +15 -0
  143. package/src/alias.conformance.test.ts +250 -0
  144. package/src/alias.test.ts +206 -2
  145. package/src/alias.ts +196 -21
  146. package/src/cli.help-command.test.ts +105 -0
  147. package/src/cli.offline.test.ts +93 -0
  148. package/src/cli.parity.test.ts +192 -0
  149. package/src/cli.root.conformance.test.ts +166 -0
  150. package/src/cli.test.ts +95 -1
  151. package/src/cli.ts +234 -25
  152. package/src/completion-scripts.ts +16 -0
  153. package/src/completion-values.test.ts +76 -16
  154. package/src/completion-values.ts +170 -66
  155. package/src/completion.conformance.test.ts +298 -0
  156. package/src/completion.test.ts +268 -145
  157. package/src/completion.ts +772 -121
  158. package/src/compliance.test.ts +196 -2
  159. package/src/compliance.ts +440 -20
  160. package/src/config-merge.test.ts +425 -0
  161. package/src/config.test.ts +228 -1
  162. package/src/config.ts +429 -16
  163. package/src/delegation.test.ts +299 -0
  164. package/src/delegation.ts +807 -0
  165. package/src/gen/crud_pb.ts +7 -7
  166. package/src/id/README.md +38 -0
  167. package/src/id/contract.test.ts +1 -1
  168. package/src/id/index.test.ts +1 -1
  169. package/src/id/index.ts +3 -2
  170. package/src/index.test.ts +71 -0
  171. package/src/index.ts +46 -0
  172. package/src/llm-anthropic.conformance.test.ts +1135 -0
  173. package/src/llm-anthropic.test.ts +152 -0
  174. package/src/llm-anthropic.ts +940 -0
  175. package/src/llm-choice-hermetic.test.ts +335 -0
  176. package/src/llm-config.test.ts +232 -0
  177. package/src/llm-config.ts +106 -0
  178. package/src/llm-endpoint.ts +254 -0
  179. package/src/llm-error.ts +17 -0
  180. package/src/llm-errors.ts +152 -0
  181. package/src/llm-file.ts +126 -0
  182. package/src/llm-http.ts +317 -0
  183. package/src/llm-keys.ts +167 -0
  184. package/src/llm-media.conformance.test.ts +87 -0
  185. package/src/llm-media.test.ts +279 -0
  186. package/src/llm-media.ts +322 -0
  187. package/src/llm-openai.conformance.test.ts +407 -0
  188. package/src/llm-openai.test.ts +510 -0
  189. package/src/llm-openai.ts +708 -0
  190. package/src/llm-payload.ts +155 -0
  191. package/src/llm-quote.ts +46 -0
  192. package/src/llm-routellm.conformance.test.ts +44 -0
  193. package/src/llm-toolnames.conformance.test.ts +112 -0
  194. package/src/llm-toolnames.test.ts +132 -0
  195. package/src/llm-toolnames.ts +298 -0
  196. package/src/llm-uri.ts +99 -0
  197. package/src/llm.test.ts +43 -15
  198. package/src/llm.toolcall.test.ts +175 -0
  199. package/src/llm.ts +602 -190
  200. package/src/log.parity.test.ts +105 -0
  201. package/src/log.test.ts +270 -1
  202. package/src/log.ts +228 -49
  203. package/src/mcp/README.md +63 -0
  204. package/src/mcp/args-separator.test.ts +164 -0
  205. package/src/mcp/args.test.ts +223 -0
  206. package/src/mcp/args.ts +160 -0
  207. package/src/mcp/audit.test.ts +520 -0
  208. package/src/mcp/audit.ts +70 -0
  209. package/src/mcp/bridge.test.ts +250 -0
  210. package/src/mcp/bridge.ts +286 -0
  211. package/src/mcp/conformance.test.ts +357 -0
  212. package/src/mcp/dispatch.ts +359 -0
  213. package/src/mcp/identity.test.ts +166 -0
  214. package/src/mcp/identity.ts +72 -0
  215. package/src/mcp/index.ts +176 -0
  216. package/src/mcp/legacy-notify.test.ts +59 -0
  217. package/src/mcp/legacy.ts +367 -0
  218. package/src/mcp/mcp.test.ts +707 -0
  219. package/src/mcp/modern-confirm.test.ts +520 -0
  220. package/src/mcp/modern-confirm.ts +484 -0
  221. package/src/mcp/modern.ts +788 -0
  222. package/src/mcp/stdio.test.ts +313 -0
  223. package/src/mcp/stdio.ts +254 -0
  224. package/src/mcp/tasks.ts +82 -0
  225. package/src/mcp/testtree.ts +250 -0
  226. package/src/mcp/toolname.test.ts +147 -0
  227. package/src/mcp/types.ts +689 -0
  228. package/src/netpolicy.test.ts +280 -0
  229. package/src/netpolicy.ts +256 -0
  230. package/src/output/README.md +46 -0
  231. package/src/output/builtins.ts +5 -5
  232. package/src/output/dispatch.ts +10 -2
  233. package/src/output/error.ts +459 -0
  234. package/src/output/formatter.ts +29 -5
  235. package/src/output/formatters/README.md +50 -0
  236. package/src/output/formatters/csv.ts +48 -12
  237. package/src/output/formatters/json.ts +4 -25
  238. package/src/output/formatters/project.ts +34 -0
  239. package/src/output/formatters/table.ts +8 -6
  240. package/src/output/formatters/text.ts +7 -3
  241. package/src/output/formatters/yaml.ts +5 -22
  242. package/src/output/projection.test.ts +298 -0
  243. package/src/output/projection.ts +33 -59
  244. package/src/output/template.ts +11 -21
  245. package/src/output.surface.test.ts +41 -0
  246. package/src/output.ts +64 -3
  247. package/src/plan.ts +89 -0
  248. package/src/policy.ts +472 -0
  249. package/src/published-types.test.ts +133 -0
  250. package/src/routellm.test.ts +209 -28
  251. package/src/routellm.toolcall.test.ts +521 -0
  252. package/src/routellm.ts +213 -177
  253. package/src/router/README.md +44 -0
  254. package/src/router/server.test.ts +41 -0
  255. package/src/router/server.ts +11 -0
  256. package/src/safety.test.ts +19 -9
  257. package/src/scope-defaults.json +29 -29
  258. package/src/serve.test.ts +1257 -0
  259. package/src/serve.ts +1665 -0
  260. package/src/sideeffect.ts +105 -0
  261. package/src/telemetry/README.md +48 -0
  262. package/src/telemetry/client.test.ts +31 -0
  263. package/src/telemetry/client.ts +12 -6
  264. package/src/telemetry/offline.test.ts +64 -0
  265. package/src/toolspec-internal.ts +279 -0
  266. package/src/toolspec-manifest.test.ts +167 -0
  267. package/src/toolspec-manifest.ts +415 -0
  268. package/src/toolspec-spec-schemas.ts +12 -0
  269. package/src/toolspec-spec.test.ts +273 -0
  270. package/src/toolspec-spec.ts +458 -0
  271. package/src/toolspec.from-command.test.ts +350 -0
  272. package/src/toolspec.test.ts +2 -2
  273. package/src/toolspec.ts +222 -0
  274. package/src/triton/README.md +34 -0
  275. package/src/tui/README.md +42 -0
  276. package/src/tui/parity.test.ts +120 -31
  277. package/src/tui/parity.ts +58 -7
  278. package/src/upgrade.test.ts +291 -0
  279. package/src/upgrade.ts +116 -7
  280. package/src/wizard.test.ts +7 -7
  281. package/test/llm/conformance-helpers.ts +100 -0
  282. package/test/output/csv.test.ts +164 -0
  283. package/test/output/dispatch.test.ts +1 -2
  284. package/test/output/error.test.ts +277 -0
  285. package/test/output/smoke-capture.ts +2 -2
  286. package/test/output/smoke.test.ts +2 -2
  287. package/test/output/taxonomy-contract.test.ts +148 -0
  288. package/test/setup-env.ts +16 -0
  289. package/test/toolspec/fixture.ts +79 -0
  290. package/tsconfig.json +3 -1
  291. package/tsconfig.test.json +10 -0
  292. package/vitest.config.ts +9 -0
  293. package/src/tui/parity.json +0 -74
@@ -0,0 +1,192 @@
1
+ /**
2
+ * Load-bearing coverage for the verbosity/streams wiring in `cli.ts`.
3
+ *
4
+ * These feed a *constructed* ParityData (never the shared parity.json) whose
5
+ * values differ from the shipped contract, and assert the flag names, label
6
+ * and destination follow it. A port that re-hardcodes `-V`, `--stream`,
7
+ * `[name] ` or `process.stderr` fails here.
8
+ *
9
+ * Mirrors `go/console/cli/verbose_parity_wiring_test.go` and
10
+ * `go/console/cli/streams_parity_wiring_test.go`.
11
+ */
12
+
13
+ import { Command } from 'commander';
14
+ import { describe, expect, it } from 'vitest';
15
+
16
+ import {
17
+ channel,
18
+ createCLI,
19
+ registerStream,
20
+ streamFlagName,
21
+ streamLabel,
22
+ streamOutput,
23
+ verbosityFlagSpec,
24
+ verbosityShorthand,
25
+ } from './cli';
26
+ import { parity, type ParityData } from './tui/parity';
27
+
28
+ /** A ParityData carrying only the given streams block. */
29
+ function streamsData(flag: string, labelFormat: string, output: string): ParityData {
30
+ return { streams: { flag, label_format: labelFormat, output } } as unknown as ParityData;
31
+ }
32
+
33
+ /** A ParityData carrying only the given verbosity flag. */
34
+ function verbosityData(flag: string): ParityData {
35
+ return { verbosity: { flag, levels: {}, quiet_override: 'warn' } } as unknown as ParityData;
36
+ }
37
+
38
+ /**
39
+ * Name a resolved destination. `process.stdout` and `process.stderr` render
40
+ * identically in vitest diffs, so assert on the name instead — a failure
41
+ * then reads "expected 'stderr' to be 'stdout'".
42
+ */
43
+ function destName(w: NodeJS.WritableStream): string {
44
+ if (w === process.stdout) return 'stdout';
45
+ if (w === process.stderr) return 'stderr';
46
+ return 'other';
47
+ }
48
+
49
+ /** Capture what a body writes to stdout and stderr. */
50
+ function captureStreams(fn: () => void): { out: string; err: string } {
51
+ const outChunks: string[] = [];
52
+ const errChunks: string[] = [];
53
+ const oo = process.stdout.write;
54
+ const oe = process.stderr.write;
55
+ process.stdout.write = ((c: string) => { outChunks.push(c); return true; }) as never;
56
+ process.stderr.write = ((c: string) => { errChunks.push(c); return true; }) as never;
57
+ try {
58
+ fn();
59
+ } finally {
60
+ process.stdout.write = oo;
61
+ process.stderr.write = oe;
62
+ }
63
+ return { out: outChunks.join(''), err: errChunks.join('') };
64
+ }
65
+
66
+ // ─── Load-bearing: streams ───────────────────────────────────────────────────
67
+
68
+ /**
69
+ * Swaps the label template, destination and flag name away from the shipped
70
+ * `[{name}]` / stderr / `--stream`. Hardcoded literals fail here.
71
+ */
72
+ describe('streams follow contract not literals', () => {
73
+ const d = streamsData('--channel', '<<{name}>>', 'stdout');
74
+
75
+ it('renders the contract label_format', () => {
76
+ expect(streamLabel(d, 'trace'), 'label must render label_format, not a hardcoded [name]')
77
+ .toBe('<<trace>> ');
78
+ });
79
+
80
+ it('resolves the contract output destination', () => {
81
+ expect(destName(streamOutput(d)),
82
+ 'destination must follow streams.output, not a hardcoded stderr').toBe('stdout');
83
+ });
84
+
85
+ it('resolves the contract flag name', () => {
86
+ expect(streamFlagName(d), 'flag name must follow streams.flag, not a hardcoded "stream"')
87
+ .toBe('channel');
88
+ });
89
+ });
90
+
91
+ // ─── Load-bearing: verbosity flag ────────────────────────────────────────────
92
+
93
+ /**
94
+ * Swaps the shorthand away from the shipped `-V`. A hardcoded `'-V, --verbose'`
95
+ * option spec fails here.
96
+ */
97
+ describe('verbosity flag follows contract not literals', () => {
98
+ it('takes the shorthand from the contract', () => {
99
+ expect(verbosityShorthand(verbosityData('-d')),
100
+ 'shorthand must follow verbosity.flag, not a hardcoded V').toBe('d');
101
+ expect(verbosityFlagSpec(verbosityData('-d')),
102
+ 'option spec must be built from the contract shorthand').toBe('-d, --verbose');
103
+ });
104
+
105
+ it('registers a diverging shorthand on a real commander program', () => {
106
+ // Commander takes the option spec as a runtime string, so the shorthand
107
+ // need not be a compile-time literal. This proves the wiring would work
108
+ // if the contract changed.
109
+ const p = new Command('t');
110
+ p.option(verbosityFlagSpec(verbosityData('-d')), 'Increase verbosity',
111
+ (_: string, prev: number) => prev + 1, 0);
112
+ p.command('run').action(() => {});
113
+ p.parse(['node', 't', 'run', '-dd']);
114
+ expect(p.opts().verbose).toBe(2);
115
+ });
116
+ });
117
+
118
+ // ─── Requirement pins: observable behavior unchanged ─────────────────────────
119
+
120
+ /**
121
+ * Pins the wiring to the values actually declared in parity.json. NOT
122
+ * load-bearing: it keeps passing if a literal is restored, by design.
123
+ */
124
+ describe('streams match shipped contract', () => {
125
+ it('pins the contract values', () => {
126
+ expect(parity.streams.label_format).toBe('[{name}]');
127
+ expect(parity.streams.output).toBe('stderr');
128
+ expect(parity.streams.flag).toBe('--stream');
129
+ });
130
+
131
+ it('renders the historical [name] prefix to stderr', () => {
132
+ expect(streamLabel(parity, 'trace')).toBe('[trace] ');
133
+ expect(destName(streamOutput(parity))).toBe('stderr');
134
+ expect(streamFlagName(parity)).toBe('stream');
135
+ });
136
+
137
+ it('registers --stream and prefixes enabled channels end-to-end', () => {
138
+ const { program } = createCLI({ name: 't', version: '1.0.0', description: 'd' });
139
+ const sub = program.command('run');
140
+ registerStream(sub, 'sql', 'SQL queries');
141
+ expect(sub.options.map((o) => o.flags)).toContain('--stream <names>');
142
+
143
+ program.parse(['node', 't', 'run', '--stream', 'sql']);
144
+ const { out, err } = captureStreams(() => {
145
+ channel(sub, 'sql').write('SELECT 1\n');
146
+ channel(sub, 'off').write('hidden\n');
147
+ });
148
+ expect(err).toBe('[sql] SELECT 1\n');
149
+ expect(out).toBe('');
150
+ });
151
+ });
152
+
153
+ describe('verbosity flag matches shipped contract', () => {
154
+ it('pins the contract flag', () => {
155
+ expect(parity.verbosity.flag).toBe('-V');
156
+ expect(verbosityShorthand(parity)).toBe('V');
157
+ expect(verbosityFlagSpec(parity)).toBe('-V, --verbose');
158
+ });
159
+
160
+ it('registers -V as a stacking count on the real CLI', () => {
161
+ const { program } = createCLI({ name: 't', version: '1.0.0', description: 'd' });
162
+ program.command('run').action(() => {});
163
+ program.parse(['node', 't', 'run', '-VVV']);
164
+ expect(program.opts().verbose).toBe(3);
165
+ });
166
+ });
167
+
168
+ // ─── Degradation ─────────────────────────────────────────────────────────────
169
+
170
+ describe('streams degrade safely', () => {
171
+ it('falls back on an empty contract', () => {
172
+ const empty = streamsData('', '', '');
173
+ expect(streamLabel(empty, 'trace'), 'absent label_format falls back to [name]')
174
+ .toBe('[trace] ');
175
+ expect(destName(streamOutput(empty)), 'absent output falls back to stderr').toBe('stderr');
176
+ expect(streamFlagName(empty), 'absent flag falls back to "stream"').toBe('stream');
177
+ });
178
+
179
+ it('renders a label_format without {name} verbatim', () => {
180
+ const noPlaceholder = streamsData('--s', 'LOG:', 'stderr');
181
+ expect(streamLabel(noPlaceholder, 'trace')).toBe('LOG: ');
182
+ expect(streamFlagName(noPlaceholder)).toBe('s');
183
+ });
184
+ });
185
+
186
+ describe('verbosity flag degrades safely', () => {
187
+ it('falls back when the contract flag is absent or not a shorthand', () => {
188
+ expect(verbosityShorthand(verbosityData('')), 'absent flag falls back to V').toBe('V');
189
+ expect(verbosityShorthand(verbosityData('--verbose')),
190
+ 'a multi-character flag is not a commander shorthand; fall back to V').toBe('V');
191
+ });
192
+ });
@@ -0,0 +1,166 @@
1
+ // Runs the delegation slice of the shared corpus in contracts/cli-root-v1.
2
+ // The vectors are the source of truth: a failure means this port drifted
3
+ // from the contract (Go is canonical), not that a vector should be edited.
4
+ //
5
+ // This port implements the delegation gates (--confirm, --confirm-token,
6
+ // --max-ops, --policy, --dry-run) and the side-effect inference. Every case
7
+ // is either replayed or listed in a skip table with a reason; a case that
8
+ // is neither fails the run, so nothing is skipped silently.
9
+
10
+ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
11
+ import { tmpdir } from 'node:os';
12
+ import { dirname, join } from 'node:path';
13
+ import { Command, CommanderError } from 'commander';
14
+ import { describe, expect, it } from 'vitest';
15
+
16
+ import { createCLI } from './cli';
17
+ import { dryRunApplied, noPrompt, scriptedPrompt } from './delegation';
18
+ import { defaultPolicyLoader } from './policy';
19
+
20
+ const CORPUS = join(__dirname, '..', '..', '..', 'contracts', 'cli-root-v1', 'vectors.json');
21
+
22
+ interface Flag { long: string; help?: string; type?: string }
23
+ interface Cmd {
24
+ name: string;
25
+ about: string;
26
+ side_effect?: string;
27
+ annotations?: Record<string, string>;
28
+ dry_run?: string;
29
+ kind?: string;
30
+ flags?: Flag[];
31
+ commands?: Cmd[];
32
+ }
33
+ interface Tree {
34
+ program: string;
35
+ commands: Cmd[];
36
+ fixtures?: Record<string, string>;
37
+ policy?: boolean;
38
+ }
39
+ interface Case {
40
+ name: string;
41
+ tree: string;
42
+ args: string[];
43
+ exit: number;
44
+ stdout?: string | null;
45
+ stdout_contains?: string[];
46
+ stderr_contains?: string[];
47
+ stderr_json?: Record<string, unknown>;
48
+ prompt_answer?: string;
49
+ }
50
+
51
+ const file = JSON.parse(readFileSync(CORPUS, 'utf8')) as { version: string; trees: Record<string, Tree>; cases: Case[] };
52
+
53
+ /** Trees the delegation slice replays; every other tree is skipped whole. */
54
+ const REPLAYED = new Set(['ops', 'infer']);
55
+
56
+ const TREE_SKIP =
57
+ 'outside the delegation slice: needs root-flag, alias, validate, signature or config features this port lacks';
58
+
59
+ /** Cases of a replayed tree that need a feature the TS CLI lacks. */
60
+ const CASE_SKIP: Array<[RegExp, string]> = [
61
+ [/^(chdir|no chdir)/, '-C/--chdir resolver not ported'],
62
+ [/^config /, '-c/--config layering and the cfg command not ported'],
63
+ [/^flag validator/, 'flag validators not ported'],
64
+ [
65
+ /^a malformed annotation is not inferred over$/,
66
+ 'TS validates side-effect tiers at run time with no disable_validate; Go runs this tree with validation off',
67
+ ],
68
+ ];
69
+
70
+ function skipReason(c: Case): string | undefined {
71
+ if (!REPLAYED.has(c.tree)) return TREE_SKIP;
72
+ return CASE_SKIP.find(([re]) => re.test(c.name))?.[1];
73
+ }
74
+
75
+ function addCmd(parent: Command, c: Cmd, prefix: string, ran: { value: boolean }): void {
76
+ const path = `${prefix} ${c.name}`.trim();
77
+ const cmd = parent.command(c.name).description(c.about);
78
+ cmd.exitOverride();
79
+ for (const f of c.flags ?? []) cmd.option(f.type === 'bool' ? `--${f.long}` : `--${f.long} <v>`, f.help ?? '');
80
+ const ann: Record<string, string> = { ...(c.annotations ?? {}) };
81
+ if (c.side_effect) ann['kit/side-effect'] = c.side_effect;
82
+ if (c.dry_run === 'opted-out') ann['kit/dry-run'] = 'opted-out';
83
+ (cmd as unknown as { annotations: Record<string, string> }).annotations = ann;
84
+ if ((c.commands ?? []).length > 0) {
85
+ for (const s of c.commands ?? []) addCmd(cmd, s, path, ran);
86
+ return;
87
+ }
88
+ cmd.argument('[args...]');
89
+ cmd.action(() => {
90
+ ran.value = true;
91
+ cmd.configureOutput().writeOut?.(`${JSON.stringify({ dry_run: dryRunApplied(cmd), path })}\n`);
92
+ });
93
+ }
94
+
95
+ function run(c: Case): { code: number; out: string; err: string } {
96
+ const t = file.trees[c.tree]!;
97
+ const tmp = mkdtempSync(join(tmpdir(), 'cli-root-'));
98
+ const sub = (s: string): string => s.split('{tmp}').join(tmp);
99
+ const saved = process.env['XDG_CONFIG_HOME'];
100
+ try {
101
+ for (const [name, body] of Object.entries(t.fixtures ?? {})) {
102
+ const p = join(tmp, name);
103
+ if (name.endsWith('/')) mkdirSync(p, { recursive: true });
104
+ else {
105
+ mkdirSync(dirname(p), { recursive: true });
106
+ writeFileSync(p, sub(body));
107
+ }
108
+ }
109
+ process.env['XDG_CONFIG_HOME'] = join(tmp, 'xdg');
110
+ const { program } = createCLI({
111
+ name: t.program,
112
+ version: '1.2.3',
113
+ description: 'Kit app',
114
+ promptSource: c.prompt_answer !== undefined ? scriptedPrompt([c.prompt_answer]) : noPrompt,
115
+ policy: t.policy ? defaultPolicyLoader(t.program) : undefined,
116
+ });
117
+ let out = '';
118
+ let err = '';
119
+ program.exitOverride();
120
+ program.configureOutput({ writeOut: (s) => { out += s; }, writeErr: (s) => { err += s; } });
121
+ const ran = { value: false };
122
+ for (const cc of t.commands) addCmd(program, cc, '', ran);
123
+ let code = 0;
124
+ try {
125
+ program.parse(['node', t.program, ...c.args.map(sub)]);
126
+ } catch (e) {
127
+ if (!(e instanceof CommanderError)) throw e;
128
+ code = e.exitCode;
129
+ }
130
+ return { code, out, err };
131
+ } finally {
132
+ if (saved === undefined) delete process.env['XDG_CONFIG_HOME'];
133
+ else process.env['XDG_CONFIG_HOME'] = saved;
134
+ rmSync(tmp, { recursive: true, force: true });
135
+ }
136
+ }
137
+
138
+ describe('cli-root-v1 corpus (delegation slice)', () => {
139
+ it('is version v1', () => expect(file.version).toBe('v1'));
140
+
141
+ const skipped: Array<[string, string]> = [];
142
+ for (const c of file.cases) {
143
+ const reason = skipReason(c);
144
+ if (reason) {
145
+ skipped.push([c.name, reason]);
146
+ it.skip(`${c.tree}: ${c.name} [skip: ${reason}]`, () => {});
147
+ continue;
148
+ }
149
+ it(`${c.tree}: ${c.name}`, () => {
150
+ const r = run(c);
151
+ expect(r.code, `exit (stderr ${r.err})`).toBe(c.exit);
152
+ if (typeof c.stdout === 'string') expect(r.out).toBe(c.stdout);
153
+ if (c.stdout === null) expect(r.out).toBe('');
154
+ for (const s of c.stdout_contains ?? []) expect(r.out).toContain(s);
155
+ for (const s of c.stderr_contains ?? []) expect(r.err).toContain(s.replace('{tmp}', ''));
156
+ if (c.stderr_json) {
157
+ const j = JSON.parse(r.err) as Record<string, unknown>;
158
+ for (const [k, v] of Object.entries(c.stderr_json)) expect(j[k]).toEqual(v);
159
+ }
160
+ });
161
+ }
162
+
163
+ it('every skip carries a reason', () => {
164
+ for (const [, reason] of skipped) expect(reason.length).toBeGreaterThan(0);
165
+ });
166
+ });
package/src/cli.test.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { describe, it, expect, afterEach } from 'vitest';
2
2
  import { createCLI, buildTheme, applyHelpTheme, Neon, Dark,
3
- setCommandGroup, verboseCount, registerStream,
3
+ setCommandGroup, verboseCount, isQuiet, registerStream,
4
4
  channel, getStreamDefs } from './cli';
5
5
 
6
6
  describe('createCLI', () => {
@@ -247,6 +247,45 @@ describe('help structure — line-by-line parity', () => {
247
247
  });
248
248
  });
249
249
 
250
+ describe('help ARGUMENTS section', () => {
251
+ /** Plain help lines for a `gen` command with the given arguments. */
252
+ function genHelpLines(withDescriptions: boolean): string[] {
253
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
254
+ const gen = program.command('gen').description('Generate');
255
+ if (withDescriptions) {
256
+ gen.argument('<shell>', 'shell to generate for')
257
+ .argument('[out]', 'output path');
258
+ } else {
259
+ gen.argument('<shell>').argument('[out]');
260
+ }
261
+ gen.option('--force', 'overwrite');
262
+ return stripANSI(gen.helpInformation()).split('\n');
263
+ }
264
+
265
+ it('lists described arguments with their descriptions', () => {
266
+ const lines = genHelpLines(true);
267
+ const argIdx = lines.findIndex(l => l.trim() === 'ARGUMENTS:');
268
+ expect(argIdx).toBeGreaterThanOrEqual(0);
269
+ expect(lines[argIdx + 1]).toMatch(/^\s+shell\s+shell to generate for$/);
270
+ expect(lines[argIdx + 2]).toMatch(/^\s+out\s+output path$/);
271
+ });
272
+
273
+ it('ARGUMENTS: appears after the description and before FLAGS:', () => {
274
+ const lines = genHelpLines(true);
275
+ const descIdx = lines.findIndex(l => l.trim() === 'Generate');
276
+ const argIdx = lines.findIndex(l => l.trim() === 'ARGUMENTS:');
277
+ const flagIdx = lines.findIndex(l => l.trim() === 'FLAGS:');
278
+ expect(descIdx).toBeLessThan(argIdx);
279
+ expect(argIdx).toBeLessThan(flagIdx);
280
+ });
281
+
282
+ it('omits ARGUMENTS: when no argument has a description', () => {
283
+ const lines = genHelpLines(false);
284
+ expect(lines.some(l => l.trim() === 'ARGUMENTS:')).toBe(false);
285
+ expect(lines.some(l => l.trim() === 'Arguments:')).toBe(false);
286
+ });
287
+ });
288
+
250
289
  describe('CLIConfig disable', () => {
251
290
  it('disable.format=true → no format key in opts after parse', () => {
252
291
  const { program } = createCLI({
@@ -608,6 +647,61 @@ describe('verbose flag', () => {
608
647
  });
609
648
  });
610
649
 
650
+ // ---------------------------------------------------------------------------
651
+ // Quiet accessor
652
+ // ---------------------------------------------------------------------------
653
+
654
+ describe('isQuiet', () => {
655
+ async function quietOf(argv: string[], disableQuiet = false): Promise<boolean | undefined> {
656
+ const { program } = createCLI({
657
+ name: 't', version: '0.1.0', description: 't',
658
+ disable: { quiet: disableQuiet },
659
+ });
660
+ let quiet: boolean | undefined;
661
+ program.command('run').action(function (this: any) {
662
+ quiet = isQuiet(this);
663
+ });
664
+ await program.parseAsync(['node', 't', ...argv]);
665
+ return quiet;
666
+ }
667
+
668
+ it('is false before parsing', () => {
669
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
670
+ const run = program.command('run');
671
+ expect(isQuiet(program)).toBe(false);
672
+ expect(isQuiet(run)).toBe(false);
673
+ });
674
+
675
+ it('is false when --quiet is absent', async () => {
676
+ expect(await quietOf(['run'])).toBe(false);
677
+ });
678
+
679
+ it('reads root --quiet from a subcommand', async () => {
680
+ expect(await quietOf(['--quiet', 'run'])).toBe(true);
681
+ });
682
+
683
+ it('reads root --quiet given after the subcommand', async () => {
684
+ expect(await quietOf(['run', '--quiet'])).toBe(true);
685
+ });
686
+
687
+ it('is false when the flag is disabled', async () => {
688
+ expect(await quietOf(['run'], true)).toBe(false);
689
+ });
690
+
691
+ it('wins over -V: quiet reported, verbose count 0', async () => {
692
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
693
+ let quiet: boolean | undefined;
694
+ let count = -1;
695
+ program.command('run').action(function (this: any) {
696
+ quiet = isQuiet(this);
697
+ count = verboseCount(this);
698
+ });
699
+ await program.parseAsync(['node', 't', '-VV', '--quiet', 'run']);
700
+ expect(quiet).toBe(true);
701
+ expect(count).toBe(0);
702
+ });
703
+ });
704
+
611
705
  // ---------------------------------------------------------------------------
612
706
  // Named streams
613
707
  // ---------------------------------------------------------------------------