@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,129 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { csvFormatter } from '../../src/output/formatters/csv';
3
+ import { defaultRegistry } from '../../src/output/registry';
4
+ import '../../src/output/builtins';
5
+
6
+ function makeStream() {
7
+ const chunks: string[] = [];
8
+ const stream = {
9
+ write: vi.fn((c: string) => {
10
+ chunks.push(c);
11
+ return true;
12
+ }),
13
+ } as unknown as NodeJS.WritableStream;
14
+ return { stream, captured: () => chunks.join('') };
15
+ }
16
+
17
+ const rows = [
18
+ { id: '1', name: 'Alice', notes: 'a, b' },
19
+ { id: '2', name: 'Bob', notes: '"quoted"' },
20
+ ];
21
+
22
+ describe('csv — defaults', () => {
23
+ it('emits header + rows with comma delimiter and CRLF default off', () => {
24
+ const s = makeStream();
25
+ csvFormatter.render(
26
+ s.stream,
27
+ rows,
28
+ { delimiter: ',', 'no-header': false, 'quote-all': false, crlf: false },
29
+ [],
30
+ );
31
+ const out = s.captured();
32
+ expect(out.split('\n')[0]).toBe('id,name,notes');
33
+ expect(out).toContain('1,Alice,"a, b"');
34
+ expect(out).toContain('2,Bob,"""quoted"""');
35
+ expect(out).not.toContain('\r\n');
36
+ });
37
+ });
38
+
39
+ describe('csv — delimiter override', () => {
40
+ it('uses semicolon when set', () => {
41
+ const s = makeStream();
42
+ csvFormatter.render(
43
+ s.stream,
44
+ rows,
45
+ { delimiter: ';', 'no-header': false, 'quote-all': false, crlf: false },
46
+ [],
47
+ );
48
+ expect(s.captured().split('\n')[0]).toBe('id;name;notes');
49
+ });
50
+
51
+ it('throws on multi-char delimiter', () => {
52
+ const s = makeStream();
53
+ expect(() =>
54
+ csvFormatter.render(
55
+ s.stream,
56
+ rows,
57
+ { delimiter: '||', 'no-header': false, 'quote-all': false, crlf: false },
58
+ [],
59
+ ),
60
+ ).toThrow(/exactly one character/);
61
+ });
62
+ });
63
+
64
+ describe('csv — no-header', () => {
65
+ it('omits header row', () => {
66
+ const s = makeStream();
67
+ csvFormatter.render(
68
+ s.stream,
69
+ rows,
70
+ { delimiter: ',', 'no-header': true, 'quote-all': false, crlf: false },
71
+ [],
72
+ );
73
+ expect(s.captured().split('\n')[0]).toBe('1,Alice,"a, b"');
74
+ });
75
+ });
76
+
77
+ describe('csv — quote-all', () => {
78
+ it('quotes every field', () => {
79
+ const s = makeStream();
80
+ csvFormatter.render(
81
+ s.stream,
82
+ [{ a: '1', b: 'x' }],
83
+ { delimiter: ',', 'no-header': false, 'quote-all': true, crlf: false },
84
+ [],
85
+ );
86
+ const out = s.captured();
87
+ expect(out).toContain('"a","b"');
88
+ expect(out).toContain('"1","x"');
89
+ });
90
+ });
91
+
92
+ describe('csv — crlf', () => {
93
+ it('uses CRLF line endings', () => {
94
+ const s = makeStream();
95
+ csvFormatter.render(
96
+ s.stream,
97
+ [{ a: '1' }],
98
+ { delimiter: ',', 'no-header': false, 'quote-all': false, crlf: true },
99
+ [],
100
+ );
101
+ expect(s.captured()).toContain('\r\n');
102
+ });
103
+ });
104
+
105
+ describe('csv — empty input', () => {
106
+ it('produces no output', () => {
107
+ const s = makeStream();
108
+ csvFormatter.render(s.stream, [], {}, []);
109
+ expect(s.captured()).toBe('');
110
+ });
111
+ });
112
+
113
+ describe('csv — cols subset', () => {
114
+ it('filters to requested columns', () => {
115
+ const s = makeStream();
116
+ csvFormatter.render(s.stream, rows, {}, ['id', 'name']);
117
+ const out = s.captured();
118
+ expect(out.split('\n')[0]).toBe('id,name');
119
+ expect(out).not.toContain('notes');
120
+ });
121
+ });
122
+
123
+ describe('csv — registry', () => {
124
+ it('registered as "csv" with .csv extension on defaultRegistry', () => {
125
+ const f = defaultRegistry.lookup('csv');
126
+ expect(f).toBe(csvFormatter);
127
+ expect(defaultRegistry.extensionMap().get('.csv')).toBe('csv');
128
+ });
129
+ });
@@ -0,0 +1,194 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { Command } from 'commander';
3
+ import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
4
+ import { tmpdir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import { dispatch } from '../../src/output/dispatch';
7
+ import { registerOutputFlags } from '../../src/output/flags';
8
+ import '../../src/output/builtins';
9
+
10
+ function makeProgram(argv: readonly string[]) {
11
+ const program = new Command()
12
+ .name('test')
13
+ .exitOverride()
14
+ .configureOutput({
15
+ writeOut: () => {},
16
+ writeErr: () => {},
17
+ });
18
+ registerOutputFlags(program);
19
+ program.action(() => {});
20
+ program.parse(['node', 'test', ...argv], { from: 'node' });
21
+ return program;
22
+ }
23
+
24
+ function captureStdout(): { restore: () => void; out: () => string } {
25
+ const chunks: Buffer[] = [];
26
+ const original = process.stdout.write.bind(process.stdout);
27
+ const spy = vi.spyOn(process.stdout, 'write').mockImplementation(
28
+ ((c: string | Buffer) => {
29
+ chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(c));
30
+ return true;
31
+ }) as typeof process.stdout.write,
32
+ );
33
+ return {
34
+ restore: () => spy.mockRestore() && original.toString,
35
+ out: () => Buffer.concat(chunks).toString(),
36
+ };
37
+ }
38
+
39
+ describe('dispatch — --format-opt parsing', () => {
40
+ it('passes parsed options to formatter via key=value', async () => {
41
+ const program = makeProgram([
42
+ '--format', 'csv',
43
+ '--format-opt', 'delimiter=;',
44
+ '--format-opt', 'no-header=true',
45
+ ]);
46
+ const cap = captureStdout();
47
+ try {
48
+ await dispatch(program, [{ a: '1', b: 'x' }]);
49
+ } finally {
50
+ cap.restore();
51
+ }
52
+ expect(cap.out()).toBe('1;x\n');
53
+ });
54
+
55
+ it('throws on unknown option key with valid list', async () => {
56
+ const program = makeProgram([
57
+ '--format', 'csv',
58
+ '--format-opt', 'bogus=x',
59
+ ]);
60
+ await expect(dispatch(program, [{ a: '1' }])).rejects.toThrow(
61
+ /unknown option "bogus" \(valid: delimiter, no-header, quote-all, crlf\)/,
62
+ );
63
+ });
64
+
65
+ it('throws on type error', async () => {
66
+ const program = makeProgram([
67
+ '--format', 'json',
68
+ '--format-opt', 'indent=abc',
69
+ ]);
70
+ await expect(dispatch(program, { a: 1 })).rejects.toThrow(/not an int/);
71
+ });
72
+
73
+ it('key-only form for bool spec sets true', async () => {
74
+ const program = makeProgram([
75
+ '--format', 'csv',
76
+ '--format-opt', 'no-header',
77
+ ]);
78
+ const cap = captureStdout();
79
+ try {
80
+ await dispatch(program, [{ a: '1' }]);
81
+ } finally {
82
+ cap.restore();
83
+ }
84
+ expect(cap.out()).toBe('1\n');
85
+ });
86
+
87
+ it('repeated --format-opt accumulates', async () => {
88
+ const program = makeProgram([
89
+ '--format', 'csv',
90
+ '--format-opt', 'delimiter=|',
91
+ '--format-opt', 'no-header',
92
+ ]);
93
+ const cap = captureStdout();
94
+ try {
95
+ await dispatch(program, [{ a: '1', b: 'x' }]);
96
+ } finally {
97
+ cap.restore();
98
+ }
99
+ expect(cap.out()).toBe('1|x\n');
100
+ });
101
+
102
+ it('throws on out-of-enum value', async () => {
103
+ const program = makeProgram([
104
+ '--format', 'text',
105
+ '--format-opt', 'style=tree',
106
+ ]);
107
+ await expect(dispatch(program, [{ a: '1' }])).rejects.toThrow(/not in \{kv, lines, paragraph\}/);
108
+ });
109
+ });
110
+
111
+ describe('dispatch — default render', () => {
112
+ it('renders table by default', async () => {
113
+ const program = makeProgram([]);
114
+ const cap = captureStdout();
115
+ try {
116
+ await dispatch(program, [{ id: '1', name: 'Alice' }]);
117
+ } finally {
118
+ cap.restore();
119
+ }
120
+ expect(cap.out()).toContain('id');
121
+ expect(cap.out()).toContain('Alice');
122
+ });
123
+
124
+ it('honors --format json', async () => {
125
+ const program = makeProgram(['--format', 'json']);
126
+ const cap = captureStdout();
127
+ try {
128
+ await dispatch(program, [{ id: '1' }]);
129
+ } finally {
130
+ cap.restore();
131
+ }
132
+ expect(cap.out()).toContain('"id"');
133
+ });
134
+ });
135
+
136
+ describe('dispatch — -o / --output to file', () => {
137
+ it('writes output to the given path with -o', async () => {
138
+ const dir = mkdtempSync(join(tmpdir(), 'dispatch-o-'));
139
+ const out = join(dir, 'report.json');
140
+ try {
141
+ const program = makeProgram(['--format', 'json', '-o', out]);
142
+ await dispatch(program, [{ id: '1', name: 'Alice' }]);
143
+ const body = readFileSync(out, 'utf8');
144
+ expect(body).toContain('"id"');
145
+ expect(body).toContain('Alice');
146
+ } finally {
147
+ rmSync(dir, { recursive: true, force: true });
148
+ }
149
+ });
150
+
151
+ it('writes output to the given path with --output', async () => {
152
+ const dir = mkdtempSync(join(tmpdir(), 'dispatch-o-'));
153
+ const out = join(dir, 'report.json');
154
+ try {
155
+ const program = makeProgram(['--format', 'json', '--output', out]);
156
+ await dispatch(program, [{ id: '2' }]);
157
+ const body = readFileSync(out, 'utf8');
158
+ expect(body).toContain('"id"');
159
+ } finally {
160
+ rmSync(dir, { recursive: true, force: true });
161
+ }
162
+ });
163
+
164
+ it('subcommand reads -o from parent globals (optsWithGlobals)', async () => {
165
+ // Mirrors the parity test setup: -o is registered on the root program,
166
+ // a subcommand is invoked with `sub -o <file>`, and dispatch is called
167
+ // from the subcommand context. The flag must be visible via
168
+ // optsWithGlobals so subcommand handlers can locate the output path.
169
+ const dir = mkdtempSync(join(tmpdir(), 'dispatch-sub-'));
170
+ const out = join(dir, 'sub-report.json');
171
+ try {
172
+ let received: Command | null = null;
173
+ const program = new Command()
174
+ .name('test')
175
+ .exitOverride()
176
+ .configureOutput({ writeOut: () => {}, writeErr: () => {} });
177
+ registerOutputFlags(program);
178
+ const sub = program.command('do').action(function (this: Command) {
179
+ received = this;
180
+ });
181
+ // Force `sub` to be referenced for linters; commander stores by name.
182
+ void sub;
183
+ program.parse(['node', 'test', 'do', '-o', out, '--format', 'json'],
184
+ { from: 'node' });
185
+ expect(received).not.toBeNull();
186
+ await dispatch(received!, [{ k: 'v' }]);
187
+ const body = readFileSync(out, 'utf8');
188
+ expect(body).toContain('"k"');
189
+ expect(body).toContain('"v"');
190
+ } finally {
191
+ rmSync(dir, { recursive: true, force: true });
192
+ }
193
+ });
194
+ });
@@ -0,0 +1,131 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { Command } from 'commander';
3
+ import { dispatch } from '../../src/output/dispatch';
4
+ import { registerOutputFlags } from '../../src/output/flags';
5
+ import {
6
+ listFormats,
7
+ formatOptions,
8
+ renderFormatHelp,
9
+ } from '../../src/output/format_help';
10
+ import { defaultRegistry } from '../../src/output/registry';
11
+ import '../../src/output/builtins';
12
+
13
+ function makeStream() {
14
+ const chunks: string[] = [];
15
+ const stream = {
16
+ write: vi.fn((c: string) => {
17
+ chunks.push(c);
18
+ return true;
19
+ }),
20
+ } as unknown as NodeJS.WritableStream;
21
+ return { stream, captured: () => chunks.join('') };
22
+ }
23
+
24
+ function makeProgram(argv: readonly string[]) {
25
+ const program = new Command()
26
+ .name('test')
27
+ .exitOverride()
28
+ .configureOutput({ writeOut: () => {}, writeErr: () => {} });
29
+ registerOutputFlags(program);
30
+ program.action(() => {});
31
+ program.parse(['node', 'test', ...argv], { from: 'node' });
32
+ return program;
33
+ }
34
+
35
+ function captureStdout(): { restore: () => void; out: () => string } {
36
+ const chunks: Buffer[] = [];
37
+ const spy = vi.spyOn(process.stdout, 'write').mockImplementation(
38
+ ((c: string | Buffer) => {
39
+ chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(c));
40
+ return true;
41
+ }) as typeof process.stdout.write,
42
+ );
43
+ return {
44
+ restore: () => spy.mockRestore(),
45
+ out: () => Buffer.concat(chunks).toString(),
46
+ };
47
+ }
48
+
49
+ describe('listFormats', () => {
50
+ it('returns one row per registered formatter sorted by key', () => {
51
+ const rows = listFormats(defaultRegistry);
52
+ const keys = rows.map(r => r.format);
53
+ expect(keys).toEqual(['csv', 'json', 'table', 'text', 'yaml']);
54
+ });
55
+
56
+ it('csv row exposes options sorted alphabetically', () => {
57
+ const rows = listFormats(defaultRegistry);
58
+ const csv = rows.find(r => r.format === 'csv')!;
59
+ expect(csv.options).toBe('crlf, delimiter, no-header, quote-all');
60
+ });
61
+ });
62
+
63
+ describe('formatOptions', () => {
64
+ it('returns one row per OptionSpec for csv', () => {
65
+ const rows = formatOptions(defaultRegistry, 'csv');
66
+ expect(rows.map(r => r.name)).toEqual(['delimiter', 'no-header', 'quote-all', 'crlf']);
67
+ });
68
+
69
+ it('throws for unknown key with valid list', () => {
70
+ expect(() => formatOptions(defaultRegistry, 'bogus')).toThrow(
71
+ /unknown format "bogus"/,
72
+ );
73
+ });
74
+ });
75
+
76
+ describe('renderFormatHelp', () => {
77
+ it('without format → renders catalog table', () => {
78
+ const s = makeStream();
79
+ renderFormatHelp(s.stream, defaultRegistry, '');
80
+ const out = s.captured();
81
+ expect(out).toContain('format');
82
+ expect(out).toContain('csv');
83
+ expect(out).toContain('json');
84
+ });
85
+
86
+ it('with format=csv → renders csv options table', () => {
87
+ const s = makeStream();
88
+ renderFormatHelp(s.stream, defaultRegistry, 'csv');
89
+ const out = s.captured();
90
+ expect(out).toContain('delimiter');
91
+ expect(out).toContain('no-header');
92
+ });
93
+
94
+ it('with format=table (no options) → prints sentinel line', () => {
95
+ const s = makeStream();
96
+ renderFormatHelp(s.stream, defaultRegistry, 'table');
97
+ expect(s.captured()).toContain('"table" has no options');
98
+ });
99
+
100
+ it('throws for unknown format', () => {
101
+ const s = makeStream();
102
+ expect(() => renderFormatHelp(s.stream, defaultRegistry, 'bogus')).toThrow(
103
+ /unknown format "bogus"/,
104
+ );
105
+ });
106
+ });
107
+
108
+ describe('dispatch — --format-help short-circuit', () => {
109
+ it('--format-help (bare) lists registry', async () => {
110
+ const program = makeProgram(['--format-help']);
111
+ const cap = captureStdout();
112
+ try {
113
+ await dispatch(program, []);
114
+ } finally {
115
+ cap.restore();
116
+ }
117
+ expect(cap.out()).toContain('format');
118
+ expect(cap.out()).toContain('csv');
119
+ });
120
+
121
+ it('--format-help csv renders csv options', async () => {
122
+ const program = makeProgram(['--format-help', 'csv']);
123
+ const cap = captureStdout();
124
+ try {
125
+ await dispatch(program, []);
126
+ } finally {
127
+ cap.restore();
128
+ }
129
+ expect(cap.out()).toContain('delimiter');
130
+ });
131
+ });
@@ -0,0 +1,137 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import {
3
+ parseOptions,
4
+ type OptionSpec,
5
+ } from '../../src/output/formatter';
6
+
7
+ const stringSpec: OptionSpec = { name: 'delim', type: 'string', usage: 'sep' };
8
+ const intSpec: OptionSpec = { name: 'indent', type: 'int', usage: 'spaces' };
9
+ const boolSpec: OptionSpec = { name: 'no-header', type: 'bool', usage: 'omit' };
10
+ const enumSpec: OptionSpec = {
11
+ name: 'style',
12
+ type: 'enum',
13
+ enum: ['kv', 'lines', 'paragraph'],
14
+ usage: 's',
15
+ };
16
+
17
+ describe('parseOptions — string', () => {
18
+ it('keeps value as-is', () => {
19
+ const opts = parseOptions(['delim=,'], [stringSpec]);
20
+ expect(opts['delim']).toBe(',');
21
+ });
22
+
23
+ it('returns empty string when value side is empty', () => {
24
+ const opts = parseOptions(['delim='], [stringSpec]);
25
+ expect(opts['delim']).toBe('');
26
+ });
27
+ });
28
+
29
+ describe('parseOptions — int', () => {
30
+ it('coerces digits', () => {
31
+ const opts = parseOptions(['indent=4'], [intSpec]);
32
+ expect(opts['indent']).toBe(4);
33
+ });
34
+
35
+ it('accepts negative ints', () => {
36
+ const opts = parseOptions(['indent=-1'], [intSpec]);
37
+ expect(opts['indent']).toBe(-1);
38
+ });
39
+
40
+ it('throws on non-int', () => {
41
+ expect(() => parseOptions(['indent=abc'], [intSpec])).toThrow(/not an int/);
42
+ });
43
+
44
+ it('throws on float', () => {
45
+ expect(() => parseOptions(['indent=2.5'], [intSpec])).toThrow(/not an int/);
46
+ });
47
+ });
48
+
49
+ describe('parseOptions — bool', () => {
50
+ it('coerces "true"', () => {
51
+ const opts = parseOptions(['no-header=true'], [boolSpec]);
52
+ expect(opts['no-header']).toBe(true);
53
+ });
54
+
55
+ it('coerces "false"', () => {
56
+ const opts = parseOptions(['no-header=false'], [boolSpec]);
57
+ expect(opts['no-header']).toBe(false);
58
+ });
59
+
60
+ it('coerces "1" / "0"', () => {
61
+ expect(parseOptions(['no-header=1'], [boolSpec])['no-header']).toBe(true);
62
+ expect(parseOptions(['no-header=0'], [boolSpec])['no-header']).toBe(false);
63
+ });
64
+
65
+ it('key-only form for bool spec defaults to true', () => {
66
+ const opts = parseOptions(['no-header'], [boolSpec]);
67
+ expect(opts['no-header']).toBe(true);
68
+ });
69
+
70
+ it('throws on key-only form for non-bool spec', () => {
71
+ expect(() => parseOptions(['delim'], [stringSpec])).toThrow(/requires a value/);
72
+ });
73
+
74
+ it('throws on garbage bool value', () => {
75
+ expect(() => parseOptions(['no-header=yeah'], [boolSpec])).toThrow(/not a bool/);
76
+ });
77
+ });
78
+
79
+ describe('parseOptions — enum', () => {
80
+ it('accepts allowed value', () => {
81
+ const opts = parseOptions(['style=lines'], [enumSpec]);
82
+ expect(opts['style']).toBe('lines');
83
+ });
84
+
85
+ it('throws on out-of-enum', () => {
86
+ expect(() => parseOptions(['style=tree'], [enumSpec])).toThrow(
87
+ /not in \{kv, lines, paragraph\}/,
88
+ );
89
+ });
90
+ });
91
+
92
+ describe('parseOptions — unknown key', () => {
93
+ it('throws with valid-keys list', () => {
94
+ expect(() =>
95
+ parseOptions(['bogus=x'], [stringSpec, intSpec]),
96
+ ).toThrow(/unknown option "bogus" \(valid: delim, indent\)/);
97
+ });
98
+
99
+ it('throws on empty key', () => {
100
+ expect(() => parseOptions(['=x'], [stringSpec])).toThrow(/empty option key/);
101
+ });
102
+ });
103
+
104
+ describe('parseOptions — defaults', () => {
105
+ it('fills missing keys from spec defaults', () => {
106
+ const specs: OptionSpec[] = [
107
+ { name: 'delim', type: 'string', default: ',', usage: '' },
108
+ { name: 'crlf', type: 'bool', default: false, usage: '' },
109
+ ];
110
+ const opts = parseOptions([], specs);
111
+ expect(opts['delim']).toBe(',');
112
+ expect(opts['crlf']).toBe(false);
113
+ });
114
+
115
+ it('user value overrides default', () => {
116
+ const specs: OptionSpec[] = [
117
+ { name: 'delim', type: 'string', default: ',', usage: '' },
118
+ ];
119
+ const opts = parseOptions(['delim=;'], specs);
120
+ expect(opts['delim']).toBe(';');
121
+ });
122
+
123
+ it('omits undefined defaults', () => {
124
+ const specs: OptionSpec[] = [
125
+ { name: 'delim', type: 'string', usage: '' },
126
+ ];
127
+ const opts = parseOptions([], specs);
128
+ expect('delim' in opts).toBe(false);
129
+ });
130
+ });
131
+
132
+ describe('parseOptions — value with embedded =', () => {
133
+ it('splits on first = only', () => {
134
+ const opts = parseOptions(['delim=a=b=c'], [stringSpec]);
135
+ expect(opts['delim']).toBe('a=b=c');
136
+ });
137
+ });