@hop-top/kit 0.5.0-alpha.1 → 0.5.0-alpha.3
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.
- package/CHANGELOG.md +107 -0
- package/README.md +333 -9
- package/dist/aim-CTp-kJW2.d.ts +139 -0
- package/dist/aim.d.ts +1 -116
- package/dist/alias-dhhr08Ei.d.ts +88 -0
- package/dist/alias.d.ts +2 -73
- package/dist/api-miiso81x.d.ts +77 -0
- package/dist/api.d.ts +1 -62
- package/dist/auth-2-jfgiHn.d.ts +34 -0
- package/dist/auth.d.ts +1 -26
- package/dist/cli-DLygZLi9.d.ts +450 -0
- package/dist/cli.d.ts +6 -309
- package/dist/cli.js +192 -63
- package/dist/config-BUY7_W2n.d.ts +103 -0
- package/dist/config.d.ts +1 -97
- package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
- package/dist/errcorrect.d.ts +1 -37
- package/dist/error-DISJKeDu.d.ts +144 -0
- package/dist/id/index.d.ts +2 -83
- package/dist/index-Bc5c7Ty5.d.ts +328 -0
- package/dist/index-C3Zw4wDJ.d.ts +359 -0
- package/dist/index-MLrRDvEG.d.ts +1073 -0
- package/dist/index-QSffbLnj.d.ts +94 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +6794 -0
- package/dist/llm-B5XUWH8T.d.ts +231 -0
- package/dist/llm.d.ts +1 -188
- package/dist/mcp/index.d.ts +2 -0
- package/dist/mcp/index.js +1453 -0
- package/dist/netpolicy-asvCfwi8.d.ts +138 -0
- package/dist/netpolicy.d.ts +1 -0
- package/dist/netpolicy.js +118 -0
- package/dist/output-4aXs91GY.d.ts +396 -0
- package/dist/output.d.ts +3 -213
- package/dist/output.js +567 -43
- package/dist/progress-CAzyxeWl.d.ts +38 -0
- package/dist/progress.d.ts +1 -30
- package/dist/provenance-SMFHm5gx.d.ts +30 -0
- package/dist/provenance.d.ts +1 -24
- package/dist/routellm-PGSt2D1y.d.ts +75 -0
- package/dist/routellm.d.ts +2 -64
- package/dist/rpc-D5xLEf8V.d.ts +32 -0
- package/dist/rpc.d.ts +2 -25
- package/dist/safety-CUaYEjZc.d.ts +30 -0
- package/dist/safety.d.ts +1 -24
- package/dist/scope-BrhYB4ij.d.ts +141 -0
- package/dist/scope.d.ts +1 -121
- package/dist/serve-DE-jscpb.d.ts +646 -0
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +1019 -0
- package/dist/stream-CrjeCp0M.d.ts +44 -0
- package/dist/stream.d.ts +1 -35
- package/dist/telemetry/index.d.ts +1 -315
- package/dist/telemetry/index.js +12 -1
- package/dist/tui/index.d.ts +5 -298
- package/dist/tui/index.js +3 -10
- package/dist/upgrade-p_zh3pvV.d.ts +102 -0
- package/dist/upgrade.d.ts +1 -95
- package/dist/xdg-DB7Shoib.d.ts +179 -0
- package/dist/xdg.d.ts +1 -169
- package/package.json +26 -3
- package/src/cli.offline.test.ts +93 -0
- package/src/cli.parity.test.ts +192 -0
- package/src/cli.ts +112 -12
- package/src/index.test.ts +58 -0
- package/src/index.ts +37 -0
- package/src/log.parity.test.ts +105 -0
- package/src/log.ts +64 -13
- package/src/mcp/bridge.test.ts +150 -0
- package/src/mcp/bridge.ts +246 -0
- package/src/mcp/conformance.test.ts +336 -0
- package/src/mcp/dispatch.ts +313 -0
- package/src/mcp/index.ts +138 -0
- package/src/mcp/legacy.ts +314 -0
- package/src/mcp/mcp.test.ts +697 -0
- package/src/mcp/modern-confirm.test.ts +520 -0
- package/src/mcp/modern-confirm.ts +452 -0
- package/src/mcp/modern.ts +723 -0
- package/src/mcp/tasks.ts +82 -0
- package/src/mcp/testtree.ts +250 -0
- package/src/mcp/types.ts +504 -0
- package/src/netpolicy.test.ts +202 -0
- package/src/netpolicy.ts +231 -0
- package/src/output/dispatch.ts +10 -2
- package/src/output/error.ts +316 -0
- package/src/output/formatter.ts +29 -5
- package/src/output/formatters/csv.ts +48 -12
- package/src/output/formatters/json.ts +4 -25
- package/src/output/formatters/project.ts +34 -0
- package/src/output/formatters/table.ts +8 -6
- package/src/output/formatters/text.ts +7 -3
- package/src/output/formatters/yaml.ts +5 -22
- package/src/output/projection.test.ts +298 -0
- package/src/output/projection.ts +33 -59
- package/src/output/template.ts +11 -21
- package/src/output.surface.test.ts +41 -0
- package/src/output.ts +41 -3
- package/src/serve.test.ts +1257 -0
- package/src/serve.ts +1665 -0
- package/src/telemetry/client.test.ts +31 -0
- package/src/telemetry/client.ts +7 -1
- package/src/telemetry/offline.test.ts +64 -0
- package/src/tui/parity.test.ts +120 -31
- package/src/tui/parity.ts +58 -7
- package/test/output/csv.test.ts +164 -0
- package/test/output/error.test.ts +246 -0
- package/src/tui/parity.json +0 -74
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module output/formatters/csv
|
|
3
3
|
*
|
|
4
|
-
* Built-in CSV formatter.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Built-in CSV formatter. Honors `delimiter`, `no-header`, `quote-all`,
|
|
5
|
+
* `crlf` options + `cols` filtering.
|
|
6
|
+
*
|
|
7
|
+
* Encoding is hand-rolled rather than delegated to csv-stringify. In its
|
|
8
|
+
* `windows` record-delimiter mode that library left a field containing an
|
|
9
|
+
* embedded LF UNQUOTED, so a single record decoded as two (or three)
|
|
10
|
+
* records — structurally invalid CSV, not merely a quoting difference. It
|
|
11
|
+
* also left a leading space bare where the other runtimes quote it.
|
|
12
|
+
*
|
|
13
|
+
* The rule: quote a field iff it contains the active delimiter, a double
|
|
14
|
+
* quote, LF, or CR, or begins with a unicode whitespace character. Trailing
|
|
15
|
+
* whitespace is not quoted. Internal quotes are doubled per RFC 4180.
|
|
16
|
+
*
|
|
17
|
+
* A quoted field's bytes are preserved verbatim in both line-ending modes and
|
|
18
|
+
* both quoting paths. RFC 4180's `escaped` production lists CR and LF as
|
|
19
|
+
* separate alternatives, so a bare CR between quotes is legal, and the W3C
|
|
20
|
+
* CSV on the Web note states that line endings within escaped cells are not
|
|
21
|
+
* normalised. The `crlf` option changes the record terminator and nothing
|
|
22
|
+
* else.
|
|
7
23
|
*/
|
|
8
24
|
|
|
9
|
-
import { stringify } from 'csv-stringify/sync';
|
|
10
25
|
import type { Formatter, Options } from '../formatter';
|
|
11
26
|
|
|
12
27
|
export const csvFormatter: Formatter = {
|
|
@@ -47,20 +62,41 @@ export const csvFormatter: Formatter = {
|
|
|
47
62
|
}
|
|
48
63
|
|
|
49
64
|
const records = rows.map(r => headers.map(h => stringify_cell(r[h])));
|
|
65
|
+
// Headers are prepended here rather than via a library `header` option to
|
|
66
|
+
// keep row layout uniform and make `no-header` a single decision point.
|
|
50
67
|
const csvRows = noHeader ? records : [headers, ...records];
|
|
51
68
|
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
// row layout uniform with csv format and to make `no-header` toggle
|
|
58
|
-
// a single decision point.
|
|
59
|
-
});
|
|
69
|
+
const eol = crlf ? '\r\n' : '\n';
|
|
70
|
+
let text = '';
|
|
71
|
+
for (const cells of csvRows) {
|
|
72
|
+
text += cells.map(c => encodeField(c, delimiter, quoteAll)).join(delimiter) + eol;
|
|
73
|
+
}
|
|
60
74
|
out.write(text);
|
|
61
75
|
},
|
|
62
76
|
};
|
|
63
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Quote a field iff it contains the delimiter, a quote, LF or CR, or begins
|
|
80
|
+
* with a unicode whitespace character. Note the asymmetry: a LEADING space
|
|
81
|
+
* forces quoting, a trailing one does not. `\.` alone on a line terminates a
|
|
82
|
+
* PostgreSQL COPY stream and is quoted defensively.
|
|
83
|
+
*/
|
|
84
|
+
function needsQuotes(field: string, delim: string): boolean {
|
|
85
|
+
if (field === '') return false;
|
|
86
|
+
if (field === '\\.') return true;
|
|
87
|
+
if (field.includes(delim) || field.includes('"') || field.includes('\n') || field.includes('\r')) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
return /^\s/u.test(field);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function encodeField(field: string, delim: string, quoteAll: boolean): string {
|
|
94
|
+
if (!quoteAll && !needsQuotes(field, delim)) return field;
|
|
95
|
+
// RFC 4180: an embedded quote is doubled. Everything else, CR and LF
|
|
96
|
+
// included, is written through untouched.
|
|
97
|
+
return `"${field.replace(/"/g, '""')}"`;
|
|
98
|
+
}
|
|
99
|
+
|
|
64
100
|
function normalise(v: unknown): Record<string, unknown>[] {
|
|
65
101
|
if (Array.isArray(v)) return v as Record<string, unknown>[];
|
|
66
102
|
return [v as Record<string, unknown>];
|
|
@@ -2,15 +2,13 @@
|
|
|
2
2
|
* @module output/formatters/json
|
|
3
3
|
*
|
|
4
4
|
* Built-in JSON formatter. Honors `indent` option (default 2) + trailing
|
|
5
|
-
* newline.
|
|
5
|
+
* newline. Emitted key order follows the ColumnSpec list, narrowed and
|
|
6
|
+
* reordered by `cols`; without either, the payload passes through untouched.
|
|
6
7
|
*/
|
|
7
8
|
|
|
8
9
|
import type { Formatter, Options } from '../formatter';
|
|
10
|
+
import { projectForEncoding } from './project';
|
|
9
11
|
|
|
10
|
-
/**
|
|
11
|
-
* Build the JSON formatter. The `columns` ref is optional; when provided
|
|
12
|
-
* (via dispatch with a ColumnSpec list), rows are projected before encode.
|
|
13
|
-
*/
|
|
14
12
|
export const jsonFormatter: Formatter = {
|
|
15
13
|
key: 'json',
|
|
16
14
|
extensions: ['.json'],
|
|
@@ -19,26 +17,7 @@ export const jsonFormatter: Formatter = {
|
|
|
19
17
|
],
|
|
20
18
|
render(out, data, opts: Options, cols) {
|
|
21
19
|
const indent = (opts['indent'] as number) ?? 2;
|
|
22
|
-
const value =
|
|
20
|
+
const value = projectForEncoding(data, cols);
|
|
23
21
|
out.write(JSON.stringify(value, null, indent) + '\n');
|
|
24
22
|
},
|
|
25
23
|
};
|
|
26
|
-
|
|
27
|
-
function projectForJson(
|
|
28
|
-
data: unknown,
|
|
29
|
-
cols: readonly string[],
|
|
30
|
-
): unknown {
|
|
31
|
-
if (cols.length === 0) return data;
|
|
32
|
-
if (Array.isArray(data)) {
|
|
33
|
-
return data.map(row => projectRow(row, cols));
|
|
34
|
-
}
|
|
35
|
-
return projectRow(data, cols);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function projectRow(row: unknown, cols: readonly string[]): unknown {
|
|
39
|
-
if (row === null || typeof row !== 'object') return row;
|
|
40
|
-
const r = row as Record<string, unknown>;
|
|
41
|
-
const out: Record<string, unknown> = {};
|
|
42
|
-
for (const c of cols) out[c] = r[c];
|
|
43
|
-
return out;
|
|
44
|
-
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/formatters/project
|
|
3
|
+
*
|
|
4
|
+
* Shared projection shim for the encoding formatters (json, yaml). Unlike
|
|
5
|
+
* table/csv/text, these two must preserve the payload's outer shape — a
|
|
6
|
+
* single object stays an object, an array stays an array — and must leave
|
|
7
|
+
* non-object payloads (scalars, arrays of scalars) untouched.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { projectRows } from '../projection';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Projects `data` for encoding, keeping its outer shape.
|
|
14
|
+
*
|
|
15
|
+
* `cols` arrives pre-resolved from dispatch and fixes the emitted key order;
|
|
16
|
+
* empty means pass `data` through as-is.
|
|
17
|
+
*/
|
|
18
|
+
export function projectForEncoding(
|
|
19
|
+
data: unknown,
|
|
20
|
+
cols: readonly string[],
|
|
21
|
+
): unknown {
|
|
22
|
+
if (cols.length === 0) return data;
|
|
23
|
+
|
|
24
|
+
if (Array.isArray(data)) {
|
|
25
|
+
const rows = data as readonly unknown[];
|
|
26
|
+
// Non-object entries have no columns to project; pass the payload through
|
|
27
|
+
// rather than flattening every element to an empty object.
|
|
28
|
+
if (rows.some(r => r === null || typeof r !== 'object')) return data;
|
|
29
|
+
return projectRows(rows, cols);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (data === null || typeof data !== 'object') return data;
|
|
33
|
+
return projectRows([data], cols)[0];
|
|
34
|
+
}
|
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
* @module output/formatters/table
|
|
3
3
|
*
|
|
4
4
|
* Built-in table formatter. Hand-rolled aligner (matches the original
|
|
5
|
-
* output.ts behaviour).
|
|
6
|
-
* row key order
|
|
5
|
+
* output.ts behaviour). Column order comes from the ColumnSpec list, or the
|
|
6
|
+
* first row's key order when none was supplied; `cols` narrows and reorders.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type { Formatter, Options } from '../formatter';
|
|
10
|
+
import { deriveHeaders } from '../projection';
|
|
10
11
|
|
|
11
12
|
export const tableFormatter: Formatter = {
|
|
12
13
|
key: 'table',
|
|
@@ -14,11 +15,12 @@ export const tableFormatter: Formatter = {
|
|
|
14
15
|
options: [],
|
|
15
16
|
render(out, data, _opts: Options, cols) {
|
|
16
17
|
const rows = normalise(data);
|
|
18
|
+
// Emptiness is a ROW-count decision: no rows means no output at all,
|
|
19
|
+
// not a bare header line.
|
|
17
20
|
if (rows.length === 0) return;
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
const headers =
|
|
21
|
-
cols.length > 0 ? cols.filter(c => allHeaders.includes(c)) : allHeaders;
|
|
22
|
+
// `cols` arrives pre-resolved from dispatch; empty means payload keys.
|
|
23
|
+
const headers = cols.length > 0 ? cols : deriveHeaders(rows);
|
|
22
24
|
if (headers.length === 0) return;
|
|
23
25
|
|
|
24
26
|
const cells = rows.map(row =>
|
|
@@ -28,7 +30,7 @@ export const tableFormatter: Formatter = {
|
|
|
28
30
|
Math.max(h.length, ...cells.map(r => r[ci].length)),
|
|
29
31
|
);
|
|
30
32
|
const pad = (s: string, w: number) => s + ' '.repeat(w - s.length);
|
|
31
|
-
const line = (parts: string[]) =>
|
|
33
|
+
const line = (parts: readonly string[]) =>
|
|
32
34
|
parts.map((c, i) => pad(c, widths[i])).join(' ');
|
|
33
35
|
|
|
34
36
|
out.write(line(headers) + '\n');
|
|
@@ -7,10 +7,14 @@
|
|
|
7
7
|
* - lines: tab-separated, no header, one row per line
|
|
8
8
|
* - paragraph: "Record N:" header line then " field: value" lines
|
|
9
9
|
*
|
|
10
|
+
* Column order comes from the ColumnSpec list, or the first row's key order
|
|
11
|
+
* when none was supplied; `cols` narrows and reorders.
|
|
12
|
+
*
|
|
10
13
|
* Zero deps. Mirrors Go text formatter byte-for-byte.
|
|
11
14
|
*/
|
|
12
15
|
|
|
13
16
|
import type { Formatter, Options } from '../formatter';
|
|
17
|
+
import { deriveHeaders } from '../projection';
|
|
14
18
|
|
|
15
19
|
const STYLE_KV = 'kv';
|
|
16
20
|
const STYLE_LINES = 'lines';
|
|
@@ -36,11 +40,11 @@ export const textFormatter: Formatter = {
|
|
|
36
40
|
],
|
|
37
41
|
render(out, data, opts: Options, cols) {
|
|
38
42
|
const rows = normalise(data);
|
|
43
|
+
// Emptiness is a ROW-count decision: no rows means no output at all.
|
|
39
44
|
if (rows.length === 0) return;
|
|
40
45
|
|
|
41
|
-
|
|
42
|
-
const headers =
|
|
43
|
-
cols.length > 0 ? cols.filter(c => allHeaders.includes(c)) : allHeaders;
|
|
46
|
+
// `cols` arrives pre-resolved from dispatch; empty means payload keys.
|
|
47
|
+
const headers = cols.length > 0 ? cols : deriveHeaders(rows);
|
|
44
48
|
if (headers.length === 0) return;
|
|
45
49
|
|
|
46
50
|
const style = ((opts['style'] as string) || STYLE_KV) as
|
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
* @module output/formatters/yaml
|
|
3
3
|
*
|
|
4
4
|
* Built-in YAML formatter. Honors `flow-level` option (default -1, mapping
|
|
5
|
-
* to js-yaml's default block style).
|
|
6
|
-
*
|
|
5
|
+
* to js-yaml's default block style). Emitted key order follows the ColumnSpec
|
|
6
|
+
* list, narrowed and reordered by `cols`; without either, the payload passes
|
|
7
|
+
* through untouched.
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
import * as yaml from 'js-yaml';
|
|
10
11
|
import type { Formatter, Options } from '../formatter';
|
|
12
|
+
import { projectForEncoding } from './project';
|
|
11
13
|
|
|
12
14
|
export const yamlFormatter: Formatter = {
|
|
13
15
|
key: 'yaml',
|
|
@@ -22,26 +24,7 @@ export const yamlFormatter: Formatter = {
|
|
|
22
24
|
],
|
|
23
25
|
render(out, data, opts: Options, cols) {
|
|
24
26
|
const flowLevel = (opts['flow-level'] as number) ?? -1;
|
|
25
|
-
const value =
|
|
27
|
+
const value = projectForEncoding(data, cols);
|
|
26
28
|
out.write(yaml.dump(value, { flowLevel }));
|
|
27
29
|
},
|
|
28
30
|
};
|
|
29
|
-
|
|
30
|
-
function projectForYaml(
|
|
31
|
-
data: unknown,
|
|
32
|
-
cols: readonly string[],
|
|
33
|
-
): unknown {
|
|
34
|
-
if (cols.length === 0) return data;
|
|
35
|
-
if (Array.isArray(data)) {
|
|
36
|
-
return data.map(row => projectRow(row, cols));
|
|
37
|
-
}
|
|
38
|
-
return projectRow(data, cols);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function projectRow(row: unknown, cols: readonly string[]): unknown {
|
|
42
|
-
if (row === null || typeof row !== 'object') return row;
|
|
43
|
-
const r = row as Record<string, unknown>;
|
|
44
|
-
const out: Record<string, unknown> = {};
|
|
45
|
-
for (const c of cols) out[c] = r[c];
|
|
46
|
-
return out;
|
|
47
|
-
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordering + header/key contract tests for the projection helpers and, more
|
|
3
|
+
* importantly, for the dispatch path that actually reaches them.
|
|
4
|
+
*
|
|
5
|
+
* Rules pinned here:
|
|
6
|
+
* 1. ColumnSpec list drives default column order + headers; payload key
|
|
7
|
+
* order is the fallback only when no ColumnSpec is supplied.
|
|
8
|
+
* 2. --cols reorders as well as selects — user order wins.
|
|
9
|
+
* 3. header == key universally.
|
|
10
|
+
* 4. Zero rows emits nothing, decided by ROW count not header count.
|
|
11
|
+
* 5. priority is accepted, stored, ignored.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
15
|
+
import { Command } from 'commander';
|
|
16
|
+
import { dispatch } from './dispatch';
|
|
17
|
+
import { registerOutputFlags } from './flags';
|
|
18
|
+
import { newRegistry } from './registry';
|
|
19
|
+
import { deriveHeaders, projectRows, resolveEffectiveCols } from './projection';
|
|
20
|
+
import { columnName, type ColumnSpec } from './formatter';
|
|
21
|
+
import './builtins';
|
|
22
|
+
|
|
23
|
+
function makeProgram(argv: readonly string[]) {
|
|
24
|
+
const program = new Command()
|
|
25
|
+
.name('test')
|
|
26
|
+
.exitOverride()
|
|
27
|
+
.configureOutput({ writeOut: () => {}, writeErr: () => {} });
|
|
28
|
+
registerOutputFlags(program);
|
|
29
|
+
program.action(() => {});
|
|
30
|
+
program.parse(['node', 'test', ...argv], { from: 'node' });
|
|
31
|
+
return program;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function captureStdout(): { restore: () => void; out: () => string } {
|
|
35
|
+
const chunks: Buffer[] = [];
|
|
36
|
+
const spy = vi.spyOn(process.stdout, 'write').mockImplementation(
|
|
37
|
+
((c: string | Buffer) => {
|
|
38
|
+
chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(c));
|
|
39
|
+
return true;
|
|
40
|
+
}) as typeof process.stdout.write,
|
|
41
|
+
);
|
|
42
|
+
return {
|
|
43
|
+
restore: () => spy.mockRestore(),
|
|
44
|
+
out: () => Buffer.concat(chunks).toString(),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function run(
|
|
49
|
+
argv: readonly string[],
|
|
50
|
+
data: unknown,
|
|
51
|
+
columns?: readonly ColumnSpec[],
|
|
52
|
+
): Promise<string> {
|
|
53
|
+
const program = makeProgram(argv);
|
|
54
|
+
const cap = captureStdout();
|
|
55
|
+
try {
|
|
56
|
+
await dispatch(program, data, columns ? { columns } : {});
|
|
57
|
+
} finally {
|
|
58
|
+
cap.restore();
|
|
59
|
+
}
|
|
60
|
+
return cap.out();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Rows whose own key order (notes, name, id) deliberately disagrees with the
|
|
65
|
+
* ColumnSpec order (id, name, notes) so the two rules are distinguishable.
|
|
66
|
+
*/
|
|
67
|
+
const rows = [
|
|
68
|
+
{ notes: 'a', name: 'Alice', id: '1' },
|
|
69
|
+
{ notes: 'b', name: 'Bob', id: '2' },
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
const cols: readonly ColumnSpec[] = [
|
|
73
|
+
{ header: 'id', key: 'id', priority: 9 },
|
|
74
|
+
{ header: 'name', key: 'name', priority: 8 },
|
|
75
|
+
{ header: 'notes', key: 'notes', priority: 2 },
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// Rule 1 — ColumnSpec list drives default order, through dispatch.
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
describe('rule 1 — ColumnSpec drives default column order', () => {
|
|
83
|
+
it('table uses ColumnSpec order, not payload key order', async () => {
|
|
84
|
+
const out = await run([], rows, cols);
|
|
85
|
+
expect(out.split('\n')[0]).toBe('id name notes');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('csv uses ColumnSpec order, not payload key order', async () => {
|
|
89
|
+
const out = await run(['--format', 'csv'], rows, cols);
|
|
90
|
+
expect(out).toBe('id,name,notes\n1,Alice,a\n2,Bob,b\n');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('json emits keys in ColumnSpec order', async () => {
|
|
94
|
+
const out = await run(['--format', 'json'], rows, cols);
|
|
95
|
+
const parsed = JSON.parse(out) as Array<Record<string, unknown>>;
|
|
96
|
+
expect(Object.keys(parsed[0])).toEqual(['id', 'name', 'notes']);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('yaml emits keys in ColumnSpec order', async () => {
|
|
100
|
+
const out = await run(['--format', 'yaml'], rows, cols);
|
|
101
|
+
expect(out.split('\n').slice(0, 4)).toEqual([
|
|
102
|
+
'- id: \'1\'',
|
|
103
|
+
' name: Alice',
|
|
104
|
+
' notes: a',
|
|
105
|
+
'- id: \'2\'',
|
|
106
|
+
]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('text/lines uses ColumnSpec order', async () => {
|
|
110
|
+
const out = await run(
|
|
111
|
+
['--format', 'text', '--format-opt', 'style=lines'],
|
|
112
|
+
rows,
|
|
113
|
+
cols,
|
|
114
|
+
);
|
|
115
|
+
expect(out).toBe('1\tAlice\ta\n2\tBob\tb\n');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('falls back to payload key order when no ColumnSpec supplied', async () => {
|
|
119
|
+
const out = await run(['--format', 'csv'], rows);
|
|
120
|
+
expect(out).toBe('notes,name,id\na,Alice,1\nb,Bob,2\n');
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// ---------------------------------------------------------------------------
|
|
125
|
+
// Rule 2 — --cols reorders as well as selects; user order wins.
|
|
126
|
+
// ---------------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
describe('rule 2 — --cols reorders, user order wins', () => {
|
|
129
|
+
it('table honors user order against ColumnSpec order', async () => {
|
|
130
|
+
const out = await run(['--cols', 'notes,id'], rows, cols);
|
|
131
|
+
expect(out.split('\n')[0]).toBe('notes id');
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('csv honors user order against ColumnSpec order', async () => {
|
|
135
|
+
const out = await run(['--format', 'csv', '--cols', 'notes,id'], rows, cols);
|
|
136
|
+
expect(out).toBe('notes,id\na,1\nb,2\n');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('json honors user order against ColumnSpec order', async () => {
|
|
140
|
+
const out = await run(
|
|
141
|
+
['--format', 'json', '--cols', 'name,id'],
|
|
142
|
+
rows,
|
|
143
|
+
cols,
|
|
144
|
+
);
|
|
145
|
+
const parsed = JSON.parse(out) as Array<Record<string, unknown>>;
|
|
146
|
+
expect(Object.keys(parsed[0])).toEqual(['name', 'id']);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('honors user order on the no-ColumnSpec fallback path too', async () => {
|
|
150
|
+
const out = await run(['--format', 'csv', '--cols', 'id,notes'], rows);
|
|
151
|
+
expect(out).toBe('id,notes\n1,a\n2,b\n');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('rejects an unknown --cols name against the ColumnSpec list', async () => {
|
|
155
|
+
await expect(run(['--cols', 'bogus'], rows, cols)).rejects.toThrow(
|
|
156
|
+
/unknown column "bogus" \(valid: id, name, notes\)/,
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('resolveEffectiveCols returns user cols verbatim, ignoring schema order', () => {
|
|
161
|
+
expect(resolveEffectiveCols(['notes', 'id'], cols)).toEqual([
|
|
162
|
+
'notes',
|
|
163
|
+
'id',
|
|
164
|
+
]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('resolveEffectiveCols falls back to ColumnSpec order when no --cols', () => {
|
|
168
|
+
expect(resolveEffectiveCols([], cols)).toEqual(['id', 'name', 'notes']);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('resolveEffectiveCols yields empty for payload-key fallback', () => {
|
|
172
|
+
expect(resolveEffectiveCols([], undefined)).toEqual([]);
|
|
173
|
+
expect(resolveEffectiveCols([], [])).toEqual([]);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
// Rule 3 — header == key.
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
describe('rule 3 — header == key', () => {
|
|
182
|
+
it('deriveHeaders reads the ColumnSpec list in order', () => {
|
|
183
|
+
expect(deriveHeaders(rows, cols)).toEqual(['id', 'name', 'notes']);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('deriveHeaders falls back to first-row key order', () => {
|
|
187
|
+
expect(deriveHeaders(rows)).toEqual(['notes', 'name', 'id']);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('projectRows keys output by the column name itself', () => {
|
|
191
|
+
const got = projectRows(rows, resolveEffectiveCols([], cols));
|
|
192
|
+
expect(Object.keys(got[0])).toEqual(['id', 'name', 'notes']);
|
|
193
|
+
expect(got[0]).toEqual({ id: '1', name: 'Alice', notes: 'a' });
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('projectRows emits keys in the exact order given', () => {
|
|
197
|
+
const got = projectRows(rows, ['notes', 'id']);
|
|
198
|
+
expect(Object.keys(got[0])).toEqual(['notes', 'id']);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('columnName returns the single effective name', () => {
|
|
202
|
+
expect(columnName({ header: 'id', key: 'id' })).toBe('id');
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('columnName rejects a header/key split so drift stays impossible', () => {
|
|
206
|
+
expect(() => columnName({ header: 'Name', key: 'name' })).toThrow(
|
|
207
|
+
/column "Name": header and key must match \(got key "name"\)/,
|
|
208
|
+
);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// ---------------------------------------------------------------------------
|
|
213
|
+
// Rule 4 — zero rows emits nothing, decided by row count.
|
|
214
|
+
// ---------------------------------------------------------------------------
|
|
215
|
+
|
|
216
|
+
describe('rule 4 — zero rows emits nothing', () => {
|
|
217
|
+
it('table emits nothing for an empty payload even with a ColumnSpec', async () => {
|
|
218
|
+
expect(await run([], [], cols)).toBe('');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('csv emits nothing for an empty payload even with a ColumnSpec', async () => {
|
|
222
|
+
expect(await run(['--format', 'csv'], [], cols)).toBe('');
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('text emits nothing for an empty payload even with a ColumnSpec', async () => {
|
|
226
|
+
expect(await run(['--format', 'text'], [], cols)).toBe('');
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
// ---------------------------------------------------------------------------
|
|
231
|
+
// Public API stability — ordering reaches formatters through `cols` alone.
|
|
232
|
+
// ---------------------------------------------------------------------------
|
|
233
|
+
|
|
234
|
+
describe('third-party formatters get ordering without signature changes', () => {
|
|
235
|
+
it('a 4-param formatter receives ColumnSpec order via cols', async () => {
|
|
236
|
+
const seen: string[][] = [];
|
|
237
|
+
const registry = newRegistry();
|
|
238
|
+
// Deliberately written against the documented 4-arg signature, with no
|
|
239
|
+
// knowledge of ColumnSpec — the case the interface must not break.
|
|
240
|
+
registry.register({
|
|
241
|
+
key: 'probe',
|
|
242
|
+
extensions: [],
|
|
243
|
+
options: [],
|
|
244
|
+
render(out, _data, _opts, cols) {
|
|
245
|
+
seen.push([...cols]);
|
|
246
|
+
out.write('ok');
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const program = makeProgram(['--format', 'probe']);
|
|
251
|
+
const cap = captureStdout();
|
|
252
|
+
try {
|
|
253
|
+
await dispatch(program, rows, { columns: cols, registry });
|
|
254
|
+
} finally {
|
|
255
|
+
cap.restore();
|
|
256
|
+
}
|
|
257
|
+
expect(seen).toEqual([['id', 'name', 'notes']]);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('a 4-param formatter sees user --cols order verbatim', async () => {
|
|
261
|
+
const seen: string[][] = [];
|
|
262
|
+
const registry = newRegistry();
|
|
263
|
+
registry.register({
|
|
264
|
+
key: 'probe',
|
|
265
|
+
extensions: [],
|
|
266
|
+
options: [],
|
|
267
|
+
render(out, _data, _opts, cols) {
|
|
268
|
+
seen.push([...cols]);
|
|
269
|
+
out.write('ok');
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const program = makeProgram(['--format', 'probe', '--cols', 'notes,id']);
|
|
274
|
+
const cap = captureStdout();
|
|
275
|
+
try {
|
|
276
|
+
await dispatch(program, rows, { columns: cols, registry });
|
|
277
|
+
} finally {
|
|
278
|
+
cap.restore();
|
|
279
|
+
}
|
|
280
|
+
expect(seen).toEqual([['notes', 'id']]);
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// ---------------------------------------------------------------------------
|
|
285
|
+
// Rule 5 — priority accepted, stored, ignored.
|
|
286
|
+
// ---------------------------------------------------------------------------
|
|
287
|
+
|
|
288
|
+
describe('rule 5 — priority is accepted and ignored', () => {
|
|
289
|
+
it('does not reorder or drop columns by priority', async () => {
|
|
290
|
+
const noisy: readonly ColumnSpec[] = [
|
|
291
|
+
{ header: 'id', key: 'id', priority: 1 },
|
|
292
|
+
{ header: 'name', key: 'name', priority: 99 },
|
|
293
|
+
{ header: 'notes', key: 'notes', priority: 50 },
|
|
294
|
+
];
|
|
295
|
+
const out = await run(['--format', 'csv'], rows, noisy);
|
|
296
|
+
expect(out).toBe('id,name,notes\n1,Alice,a\n2,Bob,b\n');
|
|
297
|
+
});
|
|
298
|
+
});
|