@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,124 @@
1
+ import { describe, it, expect, vi, afterEach } 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[], opts?: Parameters<typeof registerOutputFlags>[1]) {
11
+ const program = new Command()
12
+ .name('test')
13
+ .exitOverride()
14
+ .configureOutput({ writeOut: () => {}, writeErr: () => {} });
15
+ registerOutputFlags(program, opts);
16
+ program.action(() => {});
17
+ program.parse(['node', 'test', ...argv], { from: 'node' });
18
+ return program;
19
+ }
20
+
21
+ function captureStdout(): { restore: () => void; out: () => string } {
22
+ const chunks: Buffer[] = [];
23
+ const spy = vi.spyOn(process.stdout, 'write').mockImplementation(
24
+ ((c: string | Buffer) => {
25
+ chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(c));
26
+ return true;
27
+ }) as typeof process.stdout.write,
28
+ );
29
+ return { restore: () => spy.mockRestore(), out: () => Buffer.concat(chunks).toString() };
30
+ }
31
+
32
+ const dir = mkdtempSync(join(tmpdir(), 'sdk-ts-out-'));
33
+ afterEach(() => {});
34
+
35
+ describe('dispatch — --output stdout default', () => {
36
+ it('default writes to process.stdout', async () => {
37
+ const program = makeProgram([]);
38
+ const cap = captureStdout();
39
+ try {
40
+ await dispatch(program, [{ id: '1' }]);
41
+ } finally {
42
+ cap.restore();
43
+ }
44
+ expect(cap.out()).toContain('id');
45
+ expect(cap.out()).toContain('1');
46
+ });
47
+
48
+ it('-o - writes to stdout (sentinel)', async () => {
49
+ const program = makeProgram(['-o', '-']);
50
+ const cap = captureStdout();
51
+ try {
52
+ await dispatch(program, [{ id: '1' }]);
53
+ } finally {
54
+ cap.restore();
55
+ }
56
+ expect(cap.out()).toContain('id');
57
+ });
58
+ });
59
+
60
+ describe('dispatch — --output file', () => {
61
+ it('-o file writes + readback', async () => {
62
+ const path = join(dir, 'out1.json');
63
+ const program = makeProgram(['--format', 'json', '-o', path]);
64
+ await dispatch(program, [{ id: '1' }]);
65
+ expect(JSON.parse(readFileSync(path, 'utf8'))).toEqual([{ id: '1' }]);
66
+ rmSync(path);
67
+ });
68
+
69
+ it('overwrites existing file', async () => {
70
+ const path = join(dir, 'out2.json');
71
+ let program = makeProgram(['--format', 'json', '-o', path]);
72
+ await dispatch(program, [{ a: 1 }]);
73
+ program = makeProgram(['--format', 'json', '-o', path]);
74
+ await dispatch(program, [{ b: 2 }]);
75
+ expect(JSON.parse(readFileSync(path, 'utf8'))).toEqual([{ b: 2 }]);
76
+ rmSync(path);
77
+ });
78
+ });
79
+
80
+ describe('dispatch — extension inference', () => {
81
+ it('-o file.json with default --format infers json', async () => {
82
+ const path = join(dir, 'out.json');
83
+ const program = makeProgram(['-o', path]);
84
+ await dispatch(program, [{ id: '1' }]);
85
+ expect(JSON.parse(readFileSync(path, 'utf8'))).toEqual([{ id: '1' }]);
86
+ rmSync(path);
87
+ });
88
+
89
+ it('-o file.csv with default --format infers csv', async () => {
90
+ const path = join(dir, 'out.csv');
91
+ const program = makeProgram(['-o', path]);
92
+ await dispatch(program, [{ id: '1' }]);
93
+ expect(readFileSync(path, 'utf8')).toBe('id\n1\n');
94
+ rmSync(path);
95
+ });
96
+
97
+ it('explicit --format wins when matching extension', async () => {
98
+ const path = join(dir, 'out.json');
99
+ const program = makeProgram(['--format', 'json', '-o', path]);
100
+ await dispatch(program, [{ id: '1' }]);
101
+ expect(JSON.parse(readFileSync(path, 'utf8'))).toEqual([{ id: '1' }]);
102
+ rmSync(path);
103
+ });
104
+
105
+ it('mismatch error: explicit --format json + .csv extension', async () => {
106
+ const path = join(dir, 'out.csv');
107
+ const program = makeProgram(['--format', 'json', '-o', path]);
108
+ await expect(dispatch(program, [{ id: '1' }])).rejects.toThrow(
109
+ /format "json" does not match output extension "\.csv"/,
110
+ );
111
+ });
112
+ });
113
+
114
+ describe('registerOutputFlags — disable', () => {
115
+ it('disable.output suppresses -o', () => {
116
+ const program = makeProgram([], { disable: { output: true } });
117
+ expect(program.options.find(o => o.long === '--output')).toBeUndefined();
118
+ });
119
+
120
+ it('disable.format suppresses --format', () => {
121
+ const program = makeProgram([], { disable: { format: true } });
122
+ expect(program.options.find(o => o.long === '--format')).toBeUndefined();
123
+ });
124
+ });
@@ -0,0 +1,127 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { Registry, newRegistry, defaultRegistry } from '../../src/output/registry';
3
+ import type { Formatter } from '../../src/output/formatter';
4
+
5
+ function fakeFormatter(key: string, exts: readonly string[] = []): Formatter {
6
+ return {
7
+ key,
8
+ extensions: exts,
9
+ options: [],
10
+ render: () => {},
11
+ };
12
+ }
13
+
14
+ describe('Registry — register/lookup', () => {
15
+ it('registers and looks up a formatter', () => {
16
+ const r = newRegistry();
17
+ const f = fakeFormatter('foo');
18
+ r.register(f);
19
+ expect(r.lookup('foo')).toBe(f);
20
+ });
21
+
22
+ it('lookup returns undefined for unknown key', () => {
23
+ const r = newRegistry();
24
+ expect(r.lookup('nope')).toBeUndefined();
25
+ });
26
+
27
+ it('throws on duplicate key', () => {
28
+ const r = newRegistry();
29
+ r.register(fakeFormatter('foo'));
30
+ expect(() => r.register(fakeFormatter('foo'))).toThrow(/already registered/);
31
+ });
32
+
33
+ it('throws on empty key', () => {
34
+ const r = newRegistry();
35
+ expect(() => r.register(fakeFormatter(''))).toThrow(/key is empty/);
36
+ });
37
+ });
38
+
39
+ describe('Registry — override', () => {
40
+ it('replaces existing key', () => {
41
+ const r = newRegistry();
42
+ const a = fakeFormatter('foo');
43
+ const b = fakeFormatter('foo');
44
+ r.register(a);
45
+ r.override(b);
46
+ expect(r.lookup('foo')).toBe(b);
47
+ });
48
+
49
+ it('registers when key absent', () => {
50
+ const r = newRegistry();
51
+ const f = fakeFormatter('foo');
52
+ r.override(f);
53
+ expect(r.lookup('foo')).toBe(f);
54
+ });
55
+
56
+ it('throws on empty key', () => {
57
+ const r = newRegistry();
58
+ expect(() => r.override(fakeFormatter(''))).toThrow(/key is empty/);
59
+ });
60
+ });
61
+
62
+ describe('Registry — keys/formatters', () => {
63
+ it('keys() returns sorted slice', () => {
64
+ const r = newRegistry();
65
+ r.register(fakeFormatter('json'));
66
+ r.register(fakeFormatter('csv'));
67
+ r.register(fakeFormatter('table'));
68
+ expect(r.keys()).toEqual(['csv', 'json', 'table']);
69
+ });
70
+
71
+ it('formatters() returns sorted by key', () => {
72
+ const r = newRegistry();
73
+ const a = fakeFormatter('zzz');
74
+ const b = fakeFormatter('aaa');
75
+ r.register(a);
76
+ r.register(b);
77
+ expect(r.formatters().map(f => f.key)).toEqual(['aaa', 'zzz']);
78
+ });
79
+ });
80
+
81
+ describe('Registry — extensionMap', () => {
82
+ it('builds ext→key map', () => {
83
+ const r = newRegistry();
84
+ r.register(fakeFormatter('json', ['.json']));
85
+ r.register(fakeFormatter('yaml', ['.yaml', '.yml']));
86
+ const m = r.extensionMap();
87
+ expect(m.get('.json')).toBe('json');
88
+ expect(m.get('.yaml')).toBe('yaml');
89
+ expect(m.get('.yml')).toBe('yaml');
90
+ });
91
+
92
+ it('lowercases extensions', () => {
93
+ const r = newRegistry();
94
+ r.register(fakeFormatter('csv', ['.CSV']));
95
+ expect(r.extensionMap().get('.csv')).toBe('csv');
96
+ });
97
+
98
+ it('alphabetical key order is the deterministic tie-break on collision', () => {
99
+ const r = newRegistry();
100
+ // both claim ".x"; "z" registered first but "a" wins (later in iteration
101
+ // order is keys-sorted, "a" then "z" — "z" wins because it overwrites).
102
+ // Actually iteration is sorted asc, so last seen ("z") wins.
103
+ r.register(fakeFormatter('z', ['.x']));
104
+ r.register(fakeFormatter('a', ['.x']));
105
+ expect(r.extensionMap().get('.x')).toBe('z');
106
+ });
107
+ });
108
+
109
+ describe('Registry — isolation', () => {
110
+ it('newRegistry instances are isolated', () => {
111
+ const a = newRegistry();
112
+ const b = newRegistry();
113
+ a.register(fakeFormatter('foo'));
114
+ expect(b.lookup('foo')).toBeUndefined();
115
+ });
116
+
117
+ it('Registry constructor produces isolated instance', () => {
118
+ const r = new Registry();
119
+ expect(r.keys().length).toBe(0);
120
+ });
121
+ });
122
+
123
+ describe('Registry — defaultRegistry singleton', () => {
124
+ it('exists at module level', () => {
125
+ expect(defaultRegistry).toBeInstanceOf(Registry);
126
+ });
127
+ });
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Captures verbatim output for each smoke command listed in T-1011 so the
3
+ * track report can include real-world output samples. Run with:
4
+ *
5
+ * pnpm exec tsx test/output/smoke-capture.ts
6
+ */
7
+ import { Command } from 'commander';
8
+ import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
9
+ import { tmpdir } from 'node:os';
10
+ import { join } from 'node:path';
11
+ import { dispatch } from '../../src/output/dispatch';
12
+ import { registerOutputFlags } from '../../src/output/flags';
13
+ import '../../src/output/builtins';
14
+
15
+ function makeProgram(argv: readonly string[]) {
16
+ const program = new Command()
17
+ .name('cli')
18
+ .exitOverride()
19
+ .configureOutput({ writeOut: () => {}, writeErr: () => {} });
20
+ registerOutputFlags(program);
21
+ program.action(() => {});
22
+ program.parse(['node', 'cli', ...argv], { from: 'node' });
23
+ return program;
24
+ }
25
+
26
+ const rows = [
27
+ { id: '1', name: 'Alice', status: 'active' },
28
+ { id: '2', name: 'Bob', status: 'idle' },
29
+ ];
30
+
31
+ const tmp = mkdtempSync(join(tmpdir(), 'smoke-cap-'));
32
+
33
+ async function run() {
34
+ const out = (label: string) => process.stderr.write(`\n=== ${label} ===\n`);
35
+
36
+ // 1. cli list
37
+ out('cli list');
38
+ await dispatch(makeProgram([]), rows);
39
+
40
+ // 2. cli list --format json
41
+ out('cli list --format json');
42
+ await dispatch(makeProgram(['--format', 'json']), rows);
43
+
44
+ // 3. cli list --format csv --format-opt delimiter=';'
45
+ out("cli list --format csv --format-opt delimiter=';'");
46
+ await dispatch(makeProgram(['--format', 'csv', '--format-opt', 'delimiter=;']), rows);
47
+
48
+ // 4. cli list --cols name,status -o /tmp/x.csv
49
+ const path1 = join(tmp, 'x.csv');
50
+ out(`cli list --cols name,status -o ${path1}`);
51
+ await dispatch(makeProgram(['--cols', 'name,status', '-o', path1]), rows);
52
+ process.stdout.write(`(file ${path1}):\n${readFileSync(path1, 'utf8')}`);
53
+ rmSync(path1);
54
+
55
+ // 5. cli list -o /tmp/x.json (ext infers json)
56
+ const path2 = join(tmp, 'x.json');
57
+ out(`cli list -o ${path2} (ext infers json)`);
58
+ await dispatch(makeProgram(['-o', path2]), rows);
59
+ process.stdout.write(`(file ${path2}):\n${readFileSync(path2, 'utf8')}`);
60
+ rmSync(path2);
61
+
62
+ // 6. cli list -o /tmp/x.csv --format json (mismatch)
63
+ const path3 = join(tmp, 'x.csv');
64
+ out(`cli list -o ${path3} --format json (mismatch)`);
65
+ try {
66
+ await dispatch(makeProgram(['-o', path3, '--format', 'json']), rows);
67
+ } catch (err) {
68
+ process.stdout.write(`error: ${(err as Error).message}\n`);
69
+ }
70
+
71
+ // 7. cli list --format-help
72
+ out('cli list --format-help');
73
+ await dispatch(makeProgram(['--format-help']), []);
74
+
75
+ // 8. cli list --format-help csv
76
+ out('cli list --format-help csv');
77
+ await dispatch(makeProgram(['--format-help', 'csv']), []);
78
+ }
79
+
80
+ run().catch(err => {
81
+ console.error(err);
82
+ process.exit(1);
83
+ });
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Smoke tests mirroring T-1011's manual checklist. Each test exercises a
3
+ * dispatch path end-to-end against an in-memory writer or temp file so the
4
+ * suite is hermetic but covers the same surface a real `cli list` invocation
5
+ * would.
6
+ */
7
+ import { describe, it, expect, vi } from 'vitest';
8
+ import { Command } from 'commander';
9
+ import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
10
+ import { tmpdir } from 'node:os';
11
+ import { join } from 'node:path';
12
+ import { dispatch } from '../../src/output/dispatch';
13
+ import { registerOutputFlags } from '../../src/output/flags';
14
+ import '../../src/output/builtins';
15
+
16
+ function makeProgram(argv: readonly string[]) {
17
+ const program = new Command()
18
+ .name('cli')
19
+ .exitOverride()
20
+ .configureOutput({ writeOut: () => {}, writeErr: () => {} });
21
+ registerOutputFlags(program);
22
+ program.action(() => {});
23
+ // Smoke harness: invokes the root command directly. The "list" subcommand
24
+ // in the spec checklist would be a real command in adopter CLIs; here we
25
+ // exercise the same flag surface against the root program.
26
+ program.parse(['node', 'cli', ...argv], { from: 'node' });
27
+ return program;
28
+ }
29
+
30
+ function captureStdout(): { restore: () => void; out: () => string } {
31
+ const chunks: Buffer[] = [];
32
+ const spy = vi.spyOn(process.stdout, 'write').mockImplementation(
33
+ ((c: string | Buffer) => {
34
+ chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(c));
35
+ return true;
36
+ }) as typeof process.stdout.write,
37
+ );
38
+ return { restore: () => spy.mockRestore(), out: () => Buffer.concat(chunks).toString() };
39
+ }
40
+
41
+ const tmp = mkdtempSync(join(tmpdir(), 'sdk-ts-smoke-'));
42
+ const rows = [
43
+ { id: '1', name: 'Alice', status: 'active' },
44
+ { id: '2', name: 'Bob', status: 'idle' },
45
+ ];
46
+
47
+ describe('smoke — T-1011 checklist', () => {
48
+ it('cli list (default table)', async () => {
49
+ const program = makeProgram([]);
50
+ const cap = captureStdout();
51
+ try {
52
+ await dispatch(program, rows);
53
+ } finally {
54
+ cap.restore();
55
+ }
56
+ const out = cap.out();
57
+ expect(out).toContain('id');
58
+ expect(out).toContain('name');
59
+ expect(out).toContain('status');
60
+ expect(out).toContain('Alice');
61
+ });
62
+
63
+ it('cli list --format json', async () => {
64
+ const program = makeProgram(['--format', 'json']);
65
+ const cap = captureStdout();
66
+ try {
67
+ await dispatch(program, rows);
68
+ } finally {
69
+ cap.restore();
70
+ }
71
+ expect(JSON.parse(cap.out())).toEqual(rows);
72
+ });
73
+
74
+ it("cli list --format csv --format-opt delimiter=';'", async () => {
75
+ const program = makeProgram(['--format', 'csv', '--format-opt', 'delimiter=;']);
76
+ const cap = captureStdout();
77
+ try {
78
+ await dispatch(program, rows);
79
+ } finally {
80
+ cap.restore();
81
+ }
82
+ const lines = cap.out().split('\n');
83
+ expect(lines[0]).toBe('id;name;status');
84
+ expect(lines[1]).toBe('1;Alice;active');
85
+ });
86
+
87
+ it('cli list --cols name,status -o /tmp/x.csv', async () => {
88
+ const path = join(tmp, 'x.csv');
89
+ const program = makeProgram(['--cols', 'name,status', '-o', path]);
90
+ await dispatch(program, rows);
91
+ expect(readFileSync(path, 'utf8')).toBe('name,status\nAlice,active\nBob,idle\n');
92
+ rmSync(path);
93
+ });
94
+
95
+ it('cli list -o /tmp/x.json (ext infers json)', async () => {
96
+ const path = join(tmp, 'x.json');
97
+ const program = makeProgram(['-o', path]);
98
+ await dispatch(program, rows);
99
+ expect(JSON.parse(readFileSync(path, 'utf8'))).toEqual(rows);
100
+ rmSync(path);
101
+ });
102
+
103
+ it('cli list -o /tmp/x.csv --format json (mismatch error)', async () => {
104
+ const path = join(tmp, 'x.csv');
105
+ const program = makeProgram(['-o', path, '--format', 'json']);
106
+ await expect(dispatch(program, rows)).rejects.toThrow(
107
+ /format "json" does not match output extension "\.csv"/,
108
+ );
109
+ });
110
+
111
+ it('cli list --format-help', async () => {
112
+ const program = makeProgram(['--format-help']);
113
+ const cap = captureStdout();
114
+ try {
115
+ await dispatch(program, []);
116
+ } finally {
117
+ cap.restore();
118
+ }
119
+ const out = cap.out();
120
+ expect(out).toContain('format');
121
+ expect(out).toContain('csv');
122
+ expect(out).toContain('json');
123
+ expect(out).toContain('table');
124
+ expect(out).toContain('text');
125
+ expect(out).toContain('yaml');
126
+ });
127
+
128
+ it('cli list --format-help csv', async () => {
129
+ const program = makeProgram(['--format-help', 'csv']);
130
+ const cap = captureStdout();
131
+ try {
132
+ await dispatch(program, []);
133
+ } finally {
134
+ cap.restore();
135
+ }
136
+ const out = cap.out();
137
+ expect(out).toContain('delimiter');
138
+ expect(out).toContain('no-header');
139
+ expect(out).toContain('quote-all');
140
+ expect(out).toContain('crlf');
141
+ });
142
+ });
@@ -0,0 +1,95 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { Command } from 'commander';
3
+ import { renderTemplate } from '../../src/output/template';
4
+ import { dispatch } from '../../src/output/dispatch';
5
+ import { registerOutputFlags } from '../../src/output/flags';
6
+ import '../../src/output/builtins';
7
+
8
+ function makeStream() {
9
+ const chunks: string[] = [];
10
+ const stream = {
11
+ write: vi.fn((c: string) => {
12
+ chunks.push(c);
13
+ return true;
14
+ }),
15
+ } as unknown as NodeJS.WritableStream;
16
+ return { stream, captured: () => chunks.join('') };
17
+ }
18
+
19
+ function makeProgram(argv: readonly string[]) {
20
+ const program = new Command()
21
+ .name('test')
22
+ .exitOverride()
23
+ .configureOutput({ writeOut: () => {}, writeErr: () => {} });
24
+ registerOutputFlags(program);
25
+ program.action(() => {});
26
+ program.parse(['node', 'test', ...argv], { from: 'node' });
27
+ return program;
28
+ }
29
+
30
+ function captureStdout(): { restore: () => void; out: () => string } {
31
+ const chunks: Buffer[] = [];
32
+ const spy = vi.spyOn(process.stdout, 'write').mockImplementation(
33
+ ((c: string | Buffer) => {
34
+ chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(c));
35
+ return true;
36
+ }) as typeof process.stdout.write,
37
+ );
38
+ return {
39
+ restore: () => spy.mockRestore(),
40
+ out: () => Buffer.concat(chunks).toString(),
41
+ };
42
+ }
43
+
44
+ const rows = [
45
+ { id: '1', name: 'Alice' },
46
+ { id: '2', name: 'Bob' },
47
+ ];
48
+
49
+ describe('renderTemplate', () => {
50
+ it('renders simple iteration', async () => {
51
+ const s = makeStream();
52
+ const tpl = '<% it.items.forEach(function(item){ %><%= item.name %>;<% }) %>';
53
+ await renderTemplate(s.stream, tpl, rows);
54
+ expect(s.captured()).toBe('Alice;Bob;');
55
+ });
56
+
57
+ it('exposes cols list', async () => {
58
+ const s = makeStream();
59
+ const tpl = '<%= it.cols.join(",") %>';
60
+ await renderTemplate(s.stream, tpl, rows);
61
+ expect(s.captured()).toBe('id,name');
62
+ });
63
+
64
+ it('wraps parse errors', async () => {
65
+ const s = makeStream();
66
+ await expect(renderTemplate(s.stream, '<% invalid', rows)).rejects.toThrow(
67
+ /template error:/,
68
+ );
69
+ });
70
+ });
71
+
72
+ describe('dispatch — --template', () => {
73
+ it('runs template via Commander wiring', async () => {
74
+ const program = makeProgram([
75
+ '--template', '<%= it.items[0].name %>',
76
+ ]);
77
+ const cap = captureStdout();
78
+ try {
79
+ await dispatch(program, rows);
80
+ } finally {
81
+ cap.restore();
82
+ }
83
+ expect(cap.out()).toBe('Alice');
84
+ });
85
+
86
+ it('--template + --cols throws mutual-exclusion error', async () => {
87
+ const program = makeProgram([
88
+ '--template', '<%= 1 %>',
89
+ '--cols', 'id',
90
+ ]);
91
+ await expect(dispatch(program, rows)).rejects.toThrow(
92
+ /--template and --cols are mutually exclusive/,
93
+ );
94
+ });
95
+ });
@@ -0,0 +1,75 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { textFormatter } from '../../src/output/formatters/text';
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' },
19
+ { id: '2', name: 'Bob' },
20
+ ];
21
+
22
+ describe('text — kv style (default)', () => {
23
+ it('emits key=value lines, blank line between records', () => {
24
+ const s = makeStream();
25
+ textFormatter.render(s.stream, rows, { style: 'kv', separator: '=' }, []);
26
+ expect(s.captured()).toBe('id=1\nname=Alice\n\nid=2\nname=Bob\n');
27
+ });
28
+
29
+ it('honors custom separator', () => {
30
+ const s = makeStream();
31
+ textFormatter.render(s.stream, rows, { style: 'kv', separator: ' -> ' }, []);
32
+ expect(s.captured()).toBe('id -> 1\nname -> Alice\n\nid -> 2\nname -> Bob\n');
33
+ });
34
+ });
35
+
36
+ describe('text — lines style', () => {
37
+ it('emits tab-separated rows, no header', () => {
38
+ const s = makeStream();
39
+ textFormatter.render(s.stream, rows, { style: 'lines' }, []);
40
+ expect(s.captured()).toBe('1\tAlice\n2\tBob\n');
41
+ });
42
+ });
43
+
44
+ describe('text — paragraph style', () => {
45
+ it('emits Record N: blocks with field: value lines', () => {
46
+ const s = makeStream();
47
+ textFormatter.render(s.stream, rows, { style: 'paragraph' }, []);
48
+ expect(s.captured()).toBe(
49
+ 'Record 1:\n id: 1\n name: Alice\n\nRecord 2:\n id: 2\n name: Bob\n',
50
+ );
51
+ });
52
+ });
53
+
54
+ describe('text — cols filter', () => {
55
+ it('limits to selected columns in kv', () => {
56
+ const s = makeStream();
57
+ textFormatter.render(s.stream, rows, { style: 'kv', separator: '=' }, ['name']);
58
+ expect(s.captured()).toBe('name=Alice\n\nname=Bob\n');
59
+ });
60
+ });
61
+
62
+ describe('text — empty input', () => {
63
+ it('produces no output', () => {
64
+ const s = makeStream();
65
+ textFormatter.render(s.stream, [], {}, []);
66
+ expect(s.captured()).toBe('');
67
+ });
68
+ });
69
+
70
+ describe('text — registry', () => {
71
+ it('registered as "text" with .txt extension', () => {
72
+ expect(defaultRegistry.lookup('text')).toBe(textFormatter);
73
+ expect(defaultRegistry.extensionMap().get('.txt')).toBe('text');
74
+ });
75
+ });