@hop-top/kit 0.4.0-alpha.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 (174) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/LICENSE +25 -0
  3. package/README.md +189 -0
  4. package/dist/aim.d.ts +116 -0
  5. package/dist/aim.js +417 -0
  6. package/dist/alias.d.ts +73 -0
  7. package/dist/alias.js +207 -0
  8. package/dist/api.d.ts +62 -0
  9. package/dist/api.js +165 -0
  10. package/dist/auth.d.ts +26 -0
  11. package/dist/auth.js +41 -0
  12. package/dist/cli.d.ts +309 -0
  13. package/dist/cli.js +885 -0
  14. package/dist/config.d.ts +97 -0
  15. package/dist/config.js +79 -0
  16. package/dist/errcorrect.d.ts +37 -0
  17. package/dist/errcorrect.js +69 -0
  18. package/dist/llm.d.ts +188 -0
  19. package/dist/llm.js +356 -0
  20. package/dist/output.d.ts +213 -0
  21. package/dist/output.js +447 -0
  22. package/dist/progress.d.ts +30 -0
  23. package/dist/progress.js +56 -0
  24. package/dist/provenance.d.ts +24 -0
  25. package/dist/provenance.js +32 -0
  26. package/dist/routellm.d.ts +64 -0
  27. package/dist/routellm.js +258 -0
  28. package/dist/rpc.d.ts +25 -0
  29. package/dist/rpc.js +101 -0
  30. package/dist/safety.d.ts +24 -0
  31. package/dist/safety.js +44 -0
  32. package/dist/scope.d.ts +121 -0
  33. package/dist/scope.js +380 -0
  34. package/dist/sqlstore.d.ts +84 -0
  35. package/dist/sqlstore.js +85 -0
  36. package/dist/stream.d.ts +35 -0
  37. package/dist/stream.js +52 -0
  38. package/dist/tui/index.d.ts +300 -0
  39. package/dist/tui/index.js +332 -0
  40. package/dist/upgrade.d.ts +95 -0
  41. package/dist/upgrade.js +115 -0
  42. package/dist/xdg.d.ts +169 -0
  43. package/dist/xdg.js +129 -0
  44. package/eslint.config.mjs +17 -0
  45. package/package.json +133 -0
  46. package/src/README.md +3 -0
  47. package/src/aim.test.ts +331 -0
  48. package/src/aim.ts +388 -0
  49. package/src/alias.test.ts +423 -0
  50. package/src/alias.ts +256 -0
  51. package/src/api.test.ts +190 -0
  52. package/src/api.ts +214 -0
  53. package/src/auth.test.ts +24 -0
  54. package/src/auth.ts +37 -0
  55. package/src/bus.test.ts +178 -0
  56. package/src/bus.ts +114 -0
  57. package/src/cli.test.ts +690 -0
  58. package/src/cli.ts +741 -0
  59. package/src/completion-values.test.ts +202 -0
  60. package/src/completion-values.ts +173 -0
  61. package/src/completion.test.ts +212 -0
  62. package/src/completion.ts +158 -0
  63. package/src/compliance.test.ts +134 -0
  64. package/src/compliance.ts +669 -0
  65. package/src/config.test.ts +110 -0
  66. package/src/config.ts +164 -0
  67. package/src/errcorrect.test.ts +134 -0
  68. package/src/errcorrect.ts +84 -0
  69. package/src/flagregister.test.ts +103 -0
  70. package/src/flagregister.ts +78 -0
  71. package/src/gen/crud_connect.ts +76 -0
  72. package/src/gen/crud_pb.ts +269 -0
  73. package/src/hint.test.ts +315 -0
  74. package/src/hint.ts +178 -0
  75. package/src/llm.test.ts +612 -0
  76. package/src/llm.ts +577 -0
  77. package/src/log.test.ts +145 -0
  78. package/src/log.ts +155 -0
  79. package/src/output/builtins.ts +34 -0
  80. package/src/output/dispatch.ts +195 -0
  81. package/src/output/flags.ts +151 -0
  82. package/src/output/format_help.ts +80 -0
  83. package/src/output/formatter.ts +167 -0
  84. package/src/output/formatters/csv.ts +72 -0
  85. package/src/output/formatters/json.ts +44 -0
  86. package/src/output/formatters/table.ts +44 -0
  87. package/src/output/formatters/text.ts +109 -0
  88. package/src/output/formatters/yaml.ts +47 -0
  89. package/src/output/projection.ts +126 -0
  90. package/src/output/registry.ts +88 -0
  91. package/src/output/template.ts +63 -0
  92. package/src/output.test.ts +117 -0
  93. package/src/output.ts +90 -0
  94. package/src/progress.test.ts +93 -0
  95. package/src/progress.ts +58 -0
  96. package/src/provenance.test.ts +59 -0
  97. package/src/provenance.ts +26 -0
  98. package/src/routellm.test.ts +179 -0
  99. package/src/routellm.ts +365 -0
  100. package/src/router/bert.test.ts +180 -0
  101. package/src/router/bert.ts +145 -0
  102. package/src/router/controller.test.ts +183 -0
  103. package/src/router/controller.ts +165 -0
  104. package/src/router/integration.test.ts +309 -0
  105. package/src/router/intent.test.ts +187 -0
  106. package/src/router/intent.ts +208 -0
  107. package/src/router/mf.test.ts +66 -0
  108. package/src/router/mf.ts +52 -0
  109. package/src/router/random.test.ts +23 -0
  110. package/src/router/random.ts +13 -0
  111. package/src/router/router.test.ts +31 -0
  112. package/src/router/router.ts +62 -0
  113. package/src/router/server.test.ts +193 -0
  114. package/src/router/server.ts +134 -0
  115. package/src/router/sw.test.ts +199 -0
  116. package/src/router/sw.ts +232 -0
  117. package/src/rpc.test.ts +47 -0
  118. package/src/rpc.ts +91 -0
  119. package/src/safety.test.ts +53 -0
  120. package/src/safety.ts +42 -0
  121. package/src/scope-defaults.json +72 -0
  122. package/src/scope.test.ts +177 -0
  123. package/src/scope.ts +382 -0
  124. package/src/setflag.test.ts +110 -0
  125. package/src/setflag.ts +102 -0
  126. package/src/sqlstore.test.ts +110 -0
  127. package/src/sqlstore.ts +144 -0
  128. package/src/stream.test.ts +69 -0
  129. package/src/stream.ts +47 -0
  130. package/src/textflag.test.ts +105 -0
  131. package/src/textflag.ts +68 -0
  132. package/src/toolspec.test.ts +187 -0
  133. package/src/toolspec.ts +279 -0
  134. package/src/triton/client.test.ts +205 -0
  135. package/src/triton/client.ts +261 -0
  136. package/src/tui/anim.test.ts +146 -0
  137. package/src/tui/anim.ts +134 -0
  138. package/src/tui/badge.test.ts +41 -0
  139. package/src/tui/badge.ts +55 -0
  140. package/src/tui/confirm.test.ts +68 -0
  141. package/src/tui/confirm.ts +49 -0
  142. package/src/tui/index.ts +29 -0
  143. package/src/tui/list.test.ts +84 -0
  144. package/src/tui/list.ts +72 -0
  145. package/src/tui/parity.ts +41 -0
  146. package/src/tui/pills.test.ts +40 -0
  147. package/src/tui/pills.ts +43 -0
  148. package/src/tui/progress.test.ts +164 -0
  149. package/src/tui/progress.ts +120 -0
  150. package/src/tui/spinner.test.ts +111 -0
  151. package/src/tui/spinner.ts +96 -0
  152. package/src/tui/status.test.ts +68 -0
  153. package/src/tui/status.ts +66 -0
  154. package/src/upgrade.test.ts +298 -0
  155. package/src/upgrade.ts +205 -0
  156. package/src/uri.ts +199 -0
  157. package/src/wizard.test.ts +441 -0
  158. package/src/wizard.ts +662 -0
  159. package/src/xdg.test.ts +202 -0
  160. package/src/xdg.ts +280 -0
  161. package/test/output/cols.test.ts +148 -0
  162. package/test/output/csv.test.ts +129 -0
  163. package/test/output/dispatch.test.ts +194 -0
  164. package/test/output/format_help.test.ts +131 -0
  165. package/test/output/formatter.test.ts +137 -0
  166. package/test/output/output_flag.test.ts +124 -0
  167. package/test/output/registry.test.ts +127 -0
  168. package/test/output/smoke-capture.ts +83 -0
  169. package/test/output/smoke.test.ts +142 -0
  170. package/test/output/template.test.ts +95 -0
  171. package/test/output/text.test.ts +75 -0
  172. package/test/testdata/api-fixture.json +115 -0
  173. package/test/testdata/query-vectors.json +187 -0
  174. package/tsconfig.json +12 -0
@@ -0,0 +1,690 @@
1
+ import { describe, it, expect, afterEach } from 'vitest';
2
+ import { createCLI, buildTheme, applyHelpTheme, Neon, Dark,
3
+ setCommandGroup, verboseCount, registerStream,
4
+ channel, getStreamDefs } from './cli';
5
+
6
+ describe('createCLI', () => {
7
+ it('returns { program, theme }', () => {
8
+ const result = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
9
+ expect(result).toHaveProperty('program');
10
+ expect(result).toHaveProperty('theme');
11
+ });
12
+
13
+ it('exposes -v/--version as option, not a subcommand', () => {
14
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
15
+ const subNames = program.commands.map(c => c.name());
16
+ expect(subNames).not.toContain('version');
17
+ expect(subNames).not.toContain('help');
18
+ expect(subNames).not.toContain('completion');
19
+ });
20
+
21
+ it('has --format, --quiet, --no-color global options', () => {
22
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
23
+ const optNames = program.options.map(o => o.long);
24
+ expect(optNames).toContain('--format');
25
+ expect(optNames).toContain('--quiet');
26
+ expect(optNames).toContain('--no-color');
27
+ });
28
+
29
+ it('--format defaults to table', () => {
30
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
31
+ program.parse(['node', 'mytool']);
32
+ expect(program.opts().format).toBe('table');
33
+ });
34
+ });
35
+
36
+ describe('Palette constants', () => {
37
+ it('Neon has expected hex values', () => {
38
+ expect(Neon.command).toBe('#7ED957');
39
+ expect(Neon.flag).toBe('#FF00FF');
40
+ });
41
+
42
+ it('Dark has expected hex values', () => {
43
+ expect(Dark.command).toBe('#C1FF72');
44
+ expect(Dark.flag).toBe('#FF66C4');
45
+ });
46
+ });
47
+
48
+ // ANSI SGR sequences expected per color.
49
+ const ANSI_WHITE = '\x1b[1;38;2;255;255;255m'; // #FFFFFF bold (section headers)
50
+ const ANSI_FLAG = '\x1b[38;2;255;0;255m'; // #FF00FF (Neon.Flag)
51
+ const ANSI_CMD = '\x1b[38;2;126;217;87m'; // #7ED957 (Neon.Command)
52
+ const ANSI_ARG = '\x1b[38;2;181;232;155m'; // #B5E89B (Argument)
53
+ const ANSI_RESET = '\x1b[0m';
54
+
55
+ describe('applyHelpTheme', () => {
56
+ afterEach(() => {
57
+ // Clean up any NO_COLOR env override.
58
+ delete process.env['NO_COLOR'];
59
+ });
60
+
61
+ it('help output contains ANSI color codes when color enabled', () => {
62
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
63
+ program.command('run').description('Run something');
64
+ const help = program.helpInformation();
65
+ expect(help).toContain('\x1b[');
66
+ });
67
+
68
+ it('section header (FLAGS:) is white bold', () => {
69
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
70
+ const help = program.helpInformation();
71
+ expect(help).toContain(ANSI_WHITE + 'FLAGS:' + ANSI_RESET);
72
+ });
73
+
74
+ it('flag names are colored Neon.Flag (#FF00FF)', () => {
75
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
76
+ const help = program.helpInformation();
77
+ // --help flag should appear colored.
78
+ expect(help).toContain(ANSI_FLAG + '--help' + ANSI_RESET);
79
+ });
80
+
81
+ it('command names are colored Neon.Command (#7ED957)', () => {
82
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
83
+ program.command('run').description('Run something');
84
+ const help = program.helpInformation();
85
+ // The "COMMANDS:" section header appears colored (parity.json renames "Commands:" → "COMMANDS:").
86
+ expect(help).toContain(ANSI_WHITE + 'COMMANDS:' + ANSI_RESET);
87
+ });
88
+
89
+ it('no ANSI codes when --no-color in argv', () => {
90
+ const origArgv = process.argv;
91
+ try {
92
+ process.argv = ['node', 'mytool', '--no-color'];
93
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
94
+ const help = program.helpInformation();
95
+ expect(help).not.toContain('\x1b[');
96
+ } finally {
97
+ process.argv = origArgv;
98
+ }
99
+ });
100
+
101
+ it('no ANSI codes when NO_COLOR env var set', () => {
102
+ process.env['NO_COLOR'] = '1';
103
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
104
+ const help = program.helpInformation();
105
+ expect(help).not.toContain('\x1b[');
106
+ });
107
+
108
+ it('applyHelpTheme with noColor=true suppresses all ANSI', () => {
109
+ const { program, theme } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
110
+ // Re-apply with noColor=true to override.
111
+ applyHelpTheme(program, theme, true);
112
+ const help = program.helpInformation();
113
+ expect(help).not.toContain('\x1b[');
114
+ });
115
+
116
+ it('argument words in angle brackets are colored #B5E89B', () => {
117
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
118
+ program.command('build <target>').description('Build a target');
119
+ const help = program.helpInformation();
120
+ expect(help).toContain(ANSI_ARG);
121
+ });
122
+
123
+ it('hyphenated words in descriptions are NOT flag-colored', () => {
124
+ // --quiet already registered by createCLI; no need to add again.
125
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
126
+ const help = program.helpInformation();
127
+ expect(help).not.toContain(ANSI_FLAG + '-essential' + ANSI_RESET);
128
+ expect(help).toContain(ANSI_FLAG + '--quiet' + ANSI_RESET);
129
+ });
130
+
131
+ it('ALL-CAPS words in descriptions are NOT arg-colored', () => {
132
+ // --no-color already registered by createCLI; no need to add again.
133
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
134
+ const help = program.helpInformation();
135
+ expect(help).not.toContain(ANSI_ARG + 'ANSI' + ANSI_RESET);
136
+ });
137
+
138
+ it('command names in COMMANDS section are colored accent', () => {
139
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
140
+ program.command('deploy').description('Deploy the app');
141
+ const help = program.helpInformation();
142
+ expect(help).toContain(ANSI_CMD + 'deploy' + ANSI_RESET);
143
+ });
144
+
145
+ it('descriptions are colored muted', () => {
146
+ const { program, theme } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
147
+ program.command('deploy').description('Deploy the app');
148
+ const help = program.helpInformation();
149
+ const ansiMuted = `\x1b[38;2;${parseInt(theme.muted.slice(1,3),16)};${parseInt(theme.muted.slice(3,5),16)};${parseInt(theme.muted.slice(5,7),16)}m`;
150
+ expect(help).toContain(ansiMuted);
151
+ });
152
+ });
153
+
154
+ // ---------------------------------------------------------------------------
155
+ // Help structure — line-by-line parity
156
+ // ---------------------------------------------------------------------------
157
+
158
+ /** Strip ANSI SGR sequences from a string. */
159
+ function stripANSI(s: string): string {
160
+ return s.replace(/\x1b\[[^m]*m/g, '');
161
+ }
162
+
163
+ /** Return plain (ANSI-stripped) help lines for a program with one subcommand. */
164
+ function helpLines(): string[] {
165
+ const { program } = createCLI({ name: 'mytool', version: '1.2.3', description: 'A tool' });
166
+ program.command('sub').description('A subcommand');
167
+ return stripANSI(program.helpInformation()).split('\n');
168
+ }
169
+
170
+ describe('help structure — line-by-line parity', () => {
171
+ it('first non-empty line is the Usage line', () => {
172
+ const lines = helpLines();
173
+ const first = lines.find(l => l.trim().length > 0) ?? '';
174
+ expect(first).toMatch(/^Usage:/);
175
+ });
176
+
177
+ it('description appears before any section header', () => {
178
+ const lines = helpLines();
179
+ const descIdx = lines.findIndex(l => l.includes('A tool'));
180
+ const secIdx = lines.findIndex(l => /^(COMMANDS|FLAGS|Options|Commands):/.test(l.trim()));
181
+ expect(descIdx).toBeGreaterThanOrEqual(0);
182
+ expect(secIdx).toBeGreaterThanOrEqual(0);
183
+ expect(descIdx).toBeLessThan(secIdx);
184
+ });
185
+
186
+ it('COMMANDS: section appears before FLAGS: section', () => {
187
+ const lines = helpLines();
188
+ const cmdIdx = lines.findIndex(l => l.trim() === 'COMMANDS:');
189
+ const flagIdx = lines.findIndex(l => l.trim() === 'FLAGS:');
190
+ expect(cmdIdx).toBeGreaterThanOrEqual(0);
191
+ expect(flagIdx).toBeGreaterThanOrEqual(0);
192
+ expect(cmdIdx).toBeLessThan(flagIdx);
193
+ });
194
+
195
+ it('sub command appears under COMMANDS: section', () => {
196
+ const lines = helpLines();
197
+ const cmdIdx = lines.findIndex(l => l.trim() === 'COMMANDS:');
198
+ const subIdx = lines.findIndex(l => /^\s+sub\b/.test(l));
199
+ expect(subIdx).toBeGreaterThan(cmdIdx);
200
+ });
201
+
202
+ it('--format appears under FLAGS: section', () => {
203
+ const lines = helpLines();
204
+ const flagIdx = lines.findIndex(l => l.trim() === 'FLAGS:');
205
+ const fmtIdx = lines.findIndex(l => l.includes('--format'));
206
+ expect(fmtIdx).toBeGreaterThan(flagIdx);
207
+ });
208
+
209
+ it('--quiet appears under FLAGS: section', () => {
210
+ const lines = helpLines();
211
+ const flagIdx = lines.findIndex(l => l.trim() === 'FLAGS:');
212
+ const quietIdx = lines.findIndex(l => l.includes('--quiet'));
213
+ expect(quietIdx).toBeGreaterThan(flagIdx);
214
+ });
215
+
216
+ it('--no-color appears under FLAGS: section', () => {
217
+ const lines = helpLines();
218
+ const flagIdx = lines.findIndex(l => l.trim() === 'FLAGS:');
219
+ const ncIdx = lines.findIndex(l => l.includes('--no-color'));
220
+ expect(ncIdx).toBeGreaterThan(flagIdx);
221
+ });
222
+
223
+ it('no "help" or "completion" appears as a command entry', () => {
224
+ const lines = helpLines();
225
+ const cmdIdx = lines.findIndex(l => l.trim() === 'COMMANDS:');
226
+ const flagIdx = lines.findIndex(l => l.trim() === 'FLAGS:');
227
+ const cmdLines = lines.slice(cmdIdx + 1, flagIdx);
228
+ expect(cmdLines.some(l => /^\s+help\b/.test(l))).toBe(false);
229
+ expect(cmdLines.some(l => /^\s+completion\b/.test(l))).toBe(false);
230
+ });
231
+
232
+ it('section headers are COMMANDS and FLAGS (not Commands and Options)', () => {
233
+ const lines = helpLines();
234
+ expect(lines.some(l => l.trim() === 'Commands:')).toBe(false);
235
+ expect(lines.some(l => l.trim() === 'Options:')).toBe(false);
236
+ expect(lines.some(l => l.trim() === 'COMMANDS:')).toBe(true);
237
+ expect(lines.some(l => l.trim() === 'FLAGS:')).toBe(true);
238
+ });
239
+
240
+ it('flags section contains --format, --quiet, --no-color, --no-hints', () => {
241
+ const lines = helpLines();
242
+ const flagIdx = lines.findIndex(l => l.trim() === 'FLAGS:');
243
+ const flagLines = lines.slice(flagIdx + 1).filter(l => l.trim().length > 0);
244
+ for (const flag of ['--format', '--quiet', '--no-color', '--no-hints']) {
245
+ expect(flagLines.some(l => l.includes(flag))).toBe(true);
246
+ }
247
+ });
248
+ });
249
+
250
+ describe('CLIConfig disable', () => {
251
+ it('disable.format=true → no format key in opts after parse', () => {
252
+ const { program } = createCLI({
253
+ name: 'mytool', version: '1.0.0', description: 'A tool',
254
+ disable: { format: true },
255
+ });
256
+ program.parse(['node', 'mytool']);
257
+ expect(program.opts()).not.toHaveProperty('format');
258
+ });
259
+
260
+ it('disable.quiet=true → no quiet key in opts after parse', () => {
261
+ const { program } = createCLI({
262
+ name: 'mytool', version: '1.0.0', description: 'A tool',
263
+ disable: { quiet: true },
264
+ });
265
+ program.parse(['node', 'mytool']);
266
+ expect(program.opts()).not.toHaveProperty('quiet');
267
+ });
268
+
269
+ it('disable.noColor=true → no color key in opts after parse', () => {
270
+ const { program } = createCLI({
271
+ name: 'mytool', version: '1.0.0', description: 'A tool',
272
+ disable: { noColor: true },
273
+ });
274
+ program.parse(['node', 'mytool']);
275
+ // commander uses camelCase 'color' for --no-color
276
+ expect(program.opts()).not.toHaveProperty('noColor');
277
+ });
278
+
279
+ it('disable.format=true does NOT suppress --no-hints', () => {
280
+ const { program } = createCLI({
281
+ name: 'mytool', version: '1.0.0', description: 'A tool',
282
+ disable: { format: true },
283
+ });
284
+ program.parse(['node', 'mytool']);
285
+ expect(program.opts()).not.toHaveProperty('format');
286
+ expect(program.opts()).toHaveProperty('hints');
287
+ });
288
+ });
289
+
290
+ describe('CLIConfig globals', () => {
291
+ it('single global flag appears in opts with correct default', () => {
292
+ const { program } = createCLI({
293
+ name: 'mytool', version: '1.0.0', description: 'A tool',
294
+ globals: [{ name: 'endpoint', usage: 'API endpoint URL', default: 'http://localhost' }],
295
+ });
296
+ program.parse(['node', 'mytool']);
297
+ expect(program.opts().endpoint).toBe('http://localhost');
298
+ });
299
+
300
+ it('global flag with short → shorthand registered', () => {
301
+ const { program } = createCLI({
302
+ name: 'mytool', version: '1.0.0', description: 'A tool',
303
+ globals: [{ name: 'debug', short: 'D', usage: 'Debug output', default: 'off' }],
304
+ });
305
+ program.parse(['node', 'mytool', '-D', 'on']);
306
+ expect(program.opts().debug).toBe('on');
307
+ });
308
+
309
+ it('global flag with no default → empty string', () => {
310
+ const { program } = createCLI({
311
+ name: 'mytool', version: '1.0.0', description: 'A tool',
312
+ globals: [{ name: 'token', usage: 'Auth token' }],
313
+ });
314
+ program.parse(['node', 'mytool']);
315
+ expect(program.opts().token).toBe('');
316
+ });
317
+ });
318
+
319
+ describe('help.showAliases', () => {
320
+ it('aliases hidden by default', () => {
321
+ const { program } = createCLI({ name: 'mytool', version: '1.0.0', description: 'A tool' });
322
+ program.command('deploy').alias('d').description('Deploy the app');
323
+ const help = stripANSI(program.helpInformation());
324
+ expect(help).toContain('deploy');
325
+ expect(help).not.toContain('deploy|d');
326
+ expect(help).not.toContain('(aliases:');
327
+ });
328
+
329
+ it('aliases shown when showAliases=true', () => {
330
+ const { program } = createCLI({
331
+ name: 'mytool', version: '1.0.0', description: 'A tool',
332
+ help: { showAliases: true },
333
+ });
334
+ program.command('deploy').alias('d').aliases(['dp']).description('Deploy the app');
335
+ const help = stripANSI(program.helpInformation());
336
+ expect(help).toContain('deploy');
337
+ expect(help).toContain('d, dp');
338
+ });
339
+ });
340
+
341
+ // ---------------------------------------------------------------------------
342
+ // Command groups — COMMANDS vs MANAGEMENT with --help-all
343
+ // ---------------------------------------------------------------------------
344
+
345
+ /** Strip ANSI SGR sequences from a string (duplicated for self-contained block). */
346
+ function stripAnsi(s: string): string {
347
+ return s.replace(/\x1b\[[^m]*m/g, '');
348
+ }
349
+
350
+ describe('command groups', () => {
351
+ it('management commands hidden by default', () => {
352
+ const { program } = createCLI({
353
+ name: 'mytool', version: '1.0.0', description: 'A tool',
354
+ groups: [{ id: 'management', title: 'MANAGEMENT', hidden: true }],
355
+ });
356
+ program.command('run').description('Run something');
357
+ const mgmt = program.command('config').description('Manage config');
358
+ setCommandGroup(mgmt, 'management');
359
+
360
+ const help = stripAnsi(program.helpInformation());
361
+ expect(help).toContain('COMMANDS:');
362
+ expect(help).toContain('run');
363
+ expect(help).not.toContain('MANAGEMENT:');
364
+ expect(help).not.toContain('config');
365
+ });
366
+
367
+ it('management commands shown with --help-all', () => {
368
+ const origArgv = process.argv;
369
+ try {
370
+ process.argv = ['node', 'mytool', '--help-all'];
371
+ const { program } = createCLI({
372
+ name: 'mytool', version: '1.0.0', description: 'A tool',
373
+ groups: [{ id: 'management', title: 'MANAGEMENT', hidden: true }],
374
+ });
375
+ program.command('run').description('Run something');
376
+ const mgmt = program.command('config').description('Manage config');
377
+ setCommandGroup(mgmt, 'management');
378
+
379
+ const help = stripAnsi(program.helpInformation());
380
+ expect(help).toContain('COMMANDS:');
381
+ expect(help).toContain('run');
382
+ expect(help).toContain('MANAGEMENT:');
383
+ expect(help).toContain('config');
384
+ } finally {
385
+ process.argv = origArgv;
386
+ }
387
+ });
388
+
389
+ it('custom group renders with title', () => {
390
+ const { program } = createCLI({
391
+ name: 'mytool', version: '1.0.0', description: 'A tool',
392
+ groups: [{ id: 'advanced', title: 'ADVANCED COMMANDS' }],
393
+ });
394
+ program.command('run').description('Run something');
395
+ const adv = program.command('debug').description('Debug internals');
396
+ setCommandGroup(adv, 'advanced');
397
+
398
+ const help = stripAnsi(program.helpInformation());
399
+ expect(help).toContain('COMMANDS:');
400
+ expect(help).toContain('run');
401
+ expect(help).toContain('ADVANCED COMMANDS:');
402
+ expect(help).toContain('debug');
403
+ });
404
+
405
+ it('ungrouped commands stay in default COMMANDS section', () => {
406
+ const { program } = createCLI({
407
+ name: 'mytool', version: '1.0.0', description: 'A tool',
408
+ groups: [{ id: 'management', title: 'MANAGEMENT', hidden: true }],
409
+ });
410
+ program.command('run').description('Run something');
411
+ program.command('build').description('Build something');
412
+ const mgmt = program.command('config').description('Manage config');
413
+ setCommandGroup(mgmt, 'management');
414
+
415
+ const help = stripAnsi(program.helpInformation());
416
+ const lines = help.split('\n');
417
+ const cmdIdx = lines.findIndex(l => l.trim() === 'COMMANDS:');
418
+ const flagIdx = lines.findIndex(l => l.trim() === 'FLAGS:');
419
+ const cmdSection = lines.slice(cmdIdx + 1, flagIdx).join('\n');
420
+ expect(cmdSection).toContain('run');
421
+ expect(cmdSection).toContain('build');
422
+ expect(cmdSection).not.toContain('config');
423
+ });
424
+ });
425
+
426
+ // ---------------------------------------------------------------------------
427
+ // Per-group help — --help-<id> + help <id> subcommand
428
+ // ---------------------------------------------------------------------------
429
+
430
+ describe('per-group help', () => {
431
+ /** Helper: create a CLI with management + extras groups and commands. */
432
+ function makeGroupedCLI() {
433
+ const { program } = createCLI({
434
+ name: 'mytool', version: '1.0.0', description: 'A tool',
435
+ groups: [
436
+ { id: 'management', title: 'MANAGEMENT', hidden: true },
437
+ { id: 'extras', title: 'EXTRAS' },
438
+ ],
439
+ });
440
+ program.command('run').description('Run something');
441
+ program.command('build').description('Build something');
442
+ const cfg = program.command('config').description('Manage config');
443
+ setCommandGroup(cfg, 'management');
444
+ const diag = program.command('diagnostics').description('Run diagnostics');
445
+ setCommandGroup(diag, 'management');
446
+ const bonus = program.command('bonus').description('Bonus feature');
447
+ setCommandGroup(bonus, 'extras');
448
+ return program;
449
+ }
450
+
451
+ it('per-group --help-management shows only management commands', () => {
452
+ const origArgv = process.argv;
453
+ try {
454
+ process.argv = ['node', 'mytool', '--help-management'];
455
+ const program = makeGroupedCLI();
456
+ const help = stripAnsi(program.helpInformation());
457
+ expect(help).toContain('MANAGEMENT:');
458
+ expect(help).toContain('config');
459
+ expect(help).toContain('diagnostics');
460
+ expect(help).toContain('FLAGS:');
461
+ // Should NOT contain other groups or default commands section
462
+ expect(help).not.toContain('COMMANDS:');
463
+ expect(help).not.toContain('run');
464
+ expect(help).not.toContain('build');
465
+ expect(help).not.toContain('EXTRAS:');
466
+ expect(help).not.toContain('bonus');
467
+ } finally {
468
+ process.argv = origArgv;
469
+ }
470
+ });
471
+
472
+ it('help management subcommand shows only management commands', () => {
473
+ const origArgv = process.argv;
474
+ try {
475
+ process.argv = ['node', 'mytool', '--help-management'];
476
+ const program = makeGroupedCLI();
477
+ const help = stripAnsi(program.helpInformation());
478
+ // Same assertions as --help-management
479
+ expect(help).toContain('MANAGEMENT:');
480
+ expect(help).toContain('config');
481
+ expect(help).not.toContain('COMMANDS:');
482
+ expect(help).not.toContain('run');
483
+ } finally {
484
+ process.argv = origArgv;
485
+ }
486
+ });
487
+
488
+ it('help all shows everything', () => {
489
+ const origArgv = process.argv;
490
+ try {
491
+ process.argv = ['node', 'mytool', '--help-all'];
492
+ const program = makeGroupedCLI();
493
+ const help = stripAnsi(program.helpInformation());
494
+ expect(help).toContain('COMMANDS:');
495
+ expect(help).toContain('run');
496
+ expect(help).toContain('build');
497
+ expect(help).toContain('MANAGEMENT:');
498
+ expect(help).toContain('config');
499
+ expect(help).toContain('EXTRAS:');
500
+ expect(help).toContain('bonus');
501
+ } finally {
502
+ process.argv = origArgv;
503
+ }
504
+ });
505
+
506
+ it('--help-extras shows only extras group', () => {
507
+ const origArgv = process.argv;
508
+ try {
509
+ process.argv = ['node', 'mytool', '--help-extras'];
510
+ const program = makeGroupedCLI();
511
+ const help = stripAnsi(program.helpInformation());
512
+ expect(help).toContain('EXTRAS:');
513
+ expect(help).toContain('bonus');
514
+ expect(help).toContain('FLAGS:');
515
+ expect(help).not.toContain('COMMANDS:');
516
+ expect(help).not.toContain('MANAGEMENT:');
517
+ } finally {
518
+ process.argv = origArgv;
519
+ }
520
+ });
521
+ });
522
+
523
+ describe('buildTheme', () => {
524
+ it('defaults to Neon palette when no accent', () => {
525
+ const theme = buildTheme();
526
+ expect(theme.palette.command).toBe(Neon.command);
527
+ expect(theme.palette.flag).toBe(Neon.flag);
528
+ expect(theme.accent).toBe(Neon.command);
529
+ expect(theme.secondary).toBe(Neon.flag);
530
+ });
531
+
532
+ it('overrides palette.command when accent provided', () => {
533
+ const theme = buildTheme('#FF0000');
534
+ expect(theme.palette.command).toBe('#FF0000');
535
+ expect(theme.accent).toBe('#FF0000');
536
+ // flag unchanged
537
+ expect(theme.palette.flag).toBe(Neon.flag);
538
+ });
539
+
540
+ it('has correct semantic colors', () => {
541
+ const theme = buildTheme();
542
+ expect(theme.muted).toBe('#858183');
543
+ expect(theme.error).toBe('#ED4A5E');
544
+ expect(theme.success).toBe('#52CF84');
545
+ });
546
+
547
+ it('createCLI wires accent into theme', () => {
548
+ const { theme } = createCLI({
549
+ name: 'mytool', version: '1.0.0', description: 'A tool', accent: '#AABBCC',
550
+ });
551
+ expect(theme.accent).toBe('#AABBCC');
552
+ expect(theme.palette.command).toBe('#AABBCC');
553
+ });
554
+
555
+ it('createCLI without accent uses Neon default', () => {
556
+ const { theme } = createCLI({ name: 'mytool', version: '1.0.0', description: 'A tool' });
557
+ expect(theme.accent).toBe(Neon.command);
558
+ });
559
+ });
560
+
561
+ // ---------------------------------------------------------------------------
562
+ // Verbose flag (-V)
563
+ // ---------------------------------------------------------------------------
564
+
565
+ describe('verbose flag', () => {
566
+ it('default verbose count is 0', () => {
567
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
568
+ program.parse(['node', 't']);
569
+ expect(program.opts().verbose).toBe(0);
570
+ });
571
+
572
+ it('single -V yields count 1', () => {
573
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
574
+ program.parse(['node', 't', '-V']);
575
+ expect(program.opts().verbose).toBe(1);
576
+ });
577
+
578
+ it('stacked -VV yields count 2', () => {
579
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
580
+ program.parse(['node', 't', '-VV']);
581
+ expect(program.opts().verbose).toBe(2);
582
+ });
583
+
584
+ it('--verbose --verbose stacks to 2', () => {
585
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
586
+ program.parse(['node', 't', '--verbose', '--verbose']);
587
+ expect(program.opts().verbose).toBe(2);
588
+ });
589
+
590
+ it('verboseCount accessor reads from preAction hook', async () => {
591
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
592
+ let count = -1;
593
+ program.command('run').action(function (this: any) {
594
+ count = verboseCount(this);
595
+ });
596
+ await program.parseAsync(['node', 't', '-VV', 'run']);
597
+ expect(count).toBe(2);
598
+ });
599
+
600
+ it('quiet overrides verbose in preAction', async () => {
601
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
602
+ let count = -1;
603
+ program.command('run').action(function (this: any) {
604
+ count = verboseCount(this);
605
+ });
606
+ await program.parseAsync(['node', 't', '-VV', '--quiet', 'run']);
607
+ expect(count).toBe(0);
608
+ });
609
+ });
610
+
611
+ // ---------------------------------------------------------------------------
612
+ // Named streams
613
+ // ---------------------------------------------------------------------------
614
+
615
+ describe('named streams', () => {
616
+ it('registerStream adds --stream option', () => {
617
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
618
+ const sub = program.command('run');
619
+ registerStream(sub, 'sql', 'SQL queries');
620
+ const optNames = sub.options.map(o => o.long);
621
+ expect(optNames).toContain('--stream');
622
+ });
623
+
624
+ it('channel returns noop when stream not enabled', () => {
625
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
626
+ const sub = program.command('run').action(() => {});
627
+ registerStream(sub, 'sql', 'SQL queries');
628
+ program.parse(['node', 't', 'run']);
629
+ const ch = channel(sub, 'sql');
630
+ // noop writer returns true but writes nothing
631
+ expect(ch.write('test')).toBe(true);
632
+ });
633
+
634
+ it('channel writes prefixed output when stream enabled', () => {
635
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
636
+ const sub = program.command('run').action(() => {});
637
+ registerStream(sub, 'sql', 'SQL queries');
638
+ program.parse(['node', 't', 'run', '--stream', 'sql']);
639
+
640
+ const chunks: string[] = [];
641
+ const origWrite = process.stderr.write;
642
+ process.stderr.write = ((s: any) => {
643
+ chunks.push(String(s)); return true;
644
+ }) as any;
645
+ try {
646
+ channel(sub, 'sql').write('SELECT 1\n');
647
+ } finally {
648
+ process.stderr.write = origWrite;
649
+ }
650
+ expect(chunks[0]).toBe('[sql] SELECT 1\n');
651
+ });
652
+
653
+ it('comma-separated --stream enables multiple streams', () => {
654
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
655
+ const sub = program.command('run').action(() => {});
656
+ registerStream(sub, 'sql', 'SQL queries');
657
+ registerStream(sub, 'http', 'HTTP requests');
658
+ program.parse(['node', 't', 'run', '--stream', 'sql,http']);
659
+
660
+ const chunks: string[] = [];
661
+ const origWrite = process.stderr.write;
662
+ process.stderr.write = ((s: any) => {
663
+ chunks.push(String(s)); return true;
664
+ }) as any;
665
+ try {
666
+ channel(sub, 'sql').write('q\n');
667
+ channel(sub, 'http').write('r\n');
668
+ } finally {
669
+ process.stderr.write = origWrite;
670
+ }
671
+ expect(chunks).toEqual(['[sql] q\n', '[http] r\n']);
672
+ });
673
+
674
+ it('getStreamDefs returns registered streams', () => {
675
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
676
+ const sub = program.command('run');
677
+ registerStream(sub, 'sql', 'SQL queries');
678
+ registerStream(sub, 'http', 'HTTP requests');
679
+ const defs = getStreamDefs(sub);
680
+ expect(defs).toHaveLength(2);
681
+ expect(defs[0]).toEqual({ name: 'sql', description: 'SQL queries' });
682
+ expect(defs[1]).toEqual({ name: 'http', description: 'HTTP requests' });
683
+ });
684
+
685
+ it('getStreamDefs returns empty for unregistered commands', () => {
686
+ const { program } = createCLI({ name: 't', version: '0.1.0', description: 't' });
687
+ const sub = program.command('run');
688
+ expect(getStreamDefs(sub)).toEqual([]);
689
+ });
690
+ });