@hop-top/kit 0.5.0-alpha.1 → 0.5.0-alpha.10
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 +251 -0
- package/LICENSE +1 -1
- package/LICENSE-cobra.txt +174 -0
- package/NOTICE +15 -0
- package/README.md +55 -259
- package/dist/aim-DZHwwiIc.d.ts +139 -0
- package/dist/aim.d.ts +1 -116
- package/dist/aim.js +13 -414
- package/dist/alias-D9_qFlyP.d.ts +174 -0
- package/dist/alias.d.ts +2 -73
- package/dist/alias.js +27 -207
- package/dist/api-DAexsu2H.d.ts +77 -0
- package/dist/api.d.ts +1 -62
- package/dist/api.js +10 -164
- package/dist/auth-2-jfgiHn.d.ts +34 -0
- package/dist/auth.d.ts +1 -26
- package/dist/auth.js +6 -40
- package/dist/chunk-2JV3A7OP.js +1894 -0
- package/dist/chunk-3FIKGFKI.js +54 -0
- package/dist/chunk-4UE5TCMQ.js +104 -0
- package/dist/chunk-6HFAEZ52.js +29 -0
- package/dist/chunk-6SVOHNIC.js +17 -0
- package/dist/chunk-72XZWQX7.js +504 -0
- package/dist/chunk-ABQSE7AI.js +513 -0
- package/dist/chunk-BDNNPRX6.js +41 -0
- package/dist/chunk-BLS2OBIQ.js +263 -0
- package/dist/chunk-BRNEKPWX.js +37 -0
- package/dist/chunk-CSJWB6D3.js +151 -0
- package/dist/chunk-GGWJRWWA.js +309 -0
- package/dist/chunk-GHPJJV5L.js +613 -0
- package/dist/chunk-IXVQ62NZ.js +334 -0
- package/dist/chunk-JKMQSAJP.js +174 -0
- package/dist/chunk-JZ22ZUFA.js +1131 -0
- package/dist/chunk-K7VCKJ27.js +814 -0
- package/dist/chunk-KVG7YCTS.js +115 -0
- package/dist/chunk-L23O2FFE.js +51 -0
- package/dist/chunk-MQXYDYRD.js +968 -0
- package/dist/chunk-MUIQVU6E.js +251 -0
- package/dist/chunk-NLMS7HGP.js +178 -0
- package/dist/chunk-POR5L2DG.js +247 -0
- package/dist/chunk-PSFYJGDG.js +1445 -0
- package/dist/chunk-QJVSKKRQ.js +369 -0
- package/dist/chunk-QPUCXQXT.js +67 -0
- package/dist/chunk-QQQYCWSC.js +240 -0
- package/dist/chunk-R5U7XKVJ.js +16 -0
- package/dist/chunk-SNVGDYX7.js +187 -0
- package/dist/chunk-T46YPHKN.js +669 -0
- package/dist/chunk-UKZIAQA7.js +26 -0
- package/dist/chunk-WBVO43HU.js +355 -0
- package/dist/chunk-WUOAQVOA.js +86 -0
- package/dist/chunk-WWUKGJB5.js +90 -0
- package/dist/chunk-XJTRY453.js +868 -0
- package/dist/chunk-YGYQUFGY.js +219 -0
- package/dist/chunk-YOBAUCVN.js +583 -0
- package/dist/cli-C5B5tnpA.d.ts +400 -0
- package/dist/cli.d.ts +7 -309
- package/dist/cli.js +70 -945
- package/dist/completion-csVun6SP.d.ts +112 -0
- package/dist/completion-values-By-mnjNt.d.ts +142 -0
- package/dist/completion-values.d.ts +1 -0
- package/dist/completion-values.js +29 -0
- package/dist/completion.d.ts +3 -0
- package/dist/completion.js +34 -0
- package/dist/compliance-AxFf-uMy.d.ts +60 -0
- package/dist/compliance.d.ts +1 -0
- package/dist/compliance.js +17 -0
- package/dist/config-CaK60ByQ.d.ts +241 -0
- package/dist/config.d.ts +1 -97
- package/dist/config.js +8 -78
- package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
- package/dist/errcorrect.d.ts +1 -37
- package/dist/errcorrect.js +8 -68
- package/dist/error-Do8Qji43.d.ts +231 -0
- package/dist/id/index.d.ts +2 -83
- package/dist/id/index.js +14 -65
- package/dist/index-BKJZClJx.d.ts +1486 -0
- package/dist/index-C3Zw4wDJ.d.ts +359 -0
- package/dist/index-DCe1boBz.d.ts +328 -0
- package/dist/index-VLDcI5NF.d.ts +95 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +132 -0
- package/dist/llm-DYuR7_uX.d.ts +1069 -0
- package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
- package/dist/llm-anthropic.d.ts +2 -0
- package/dist/llm-anthropic.js +13 -0
- package/dist/llm-openai-DyFrucmV.d.ts +87 -0
- package/dist/llm-openai.d.ts +2 -0
- package/dist/llm-openai.js +19 -0
- package/dist/llm.d.ts +1 -188
- package/dist/llm.js +101 -356
- package/dist/log-CkaxMX3U.d.ts +115 -0
- package/dist/log.d.ts +2 -0
- package/dist/log.js +14 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/index.js +184 -0
- package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
- package/dist/netpolicy.d.ts +1 -0
- package/dist/netpolicy.js +25 -0
- package/dist/output-CjZWmL8s.d.ts +420 -0
- package/dist/output.d.ts +3 -213
- package/dist/output.js +137 -447
- package/dist/parity-CvMuYcVI.d.ts +61 -0
- package/dist/progress-CAzyxeWl.d.ts +38 -0
- package/dist/progress.d.ts +1 -30
- package/dist/progress.js +6 -55
- package/dist/provenance-SMFHm5gx.d.ts +30 -0
- package/dist/provenance.d.ts +1 -24
- package/dist/provenance.js +6 -31
- package/dist/routellm-B0nv59mH.d.ts +111 -0
- package/dist/routellm.d.ts +2 -64
- package/dist/routellm.js +13 -255
- package/dist/rpc-D31Lg0Kc.d.ts +32 -0
- package/dist/rpc.d.ts +2 -25
- package/dist/rpc.js +4 -98
- package/dist/safety-CUaYEjZc.d.ts +30 -0
- package/dist/safety.d.ts +1 -24
- package/dist/safety.js +6 -43
- package/dist/scope-BrhYB4ij.d.ts +141 -0
- package/dist/scope.d.ts +1 -121
- package/dist/scope.js +20 -377
- package/dist/serve-D8yWMrTc.d.ts +646 -0
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +75 -0
- package/dist/sqlstore.js +6 -39
- package/dist/stream-CrjeCp0M.d.ts +44 -0
- package/dist/stream.d.ts +1 -35
- package/dist/stream.js +8 -51
- package/dist/telemetry/index.d.ts +1 -315
- package/dist/telemetry/index.js +26 -599
- package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
- package/dist/toolspec.d.ts +2 -0
- package/dist/toolspec.js +37 -0
- package/dist/tui/index.d.ts +6 -298
- package/dist/tui/index.js +17 -381
- package/dist/upgrade-p_zh3pvV.d.ts +102 -0
- package/dist/upgrade.d.ts +1 -95
- package/dist/upgrade.js +6 -114
- package/dist/xdg-DB7Shoib.d.ts +179 -0
- package/dist/xdg.d.ts +1 -169
- package/dist/xdg.js +14 -128
- package/package.json +192 -24
- package/src/README.md +15 -0
- package/src/alias.conformance.test.ts +250 -0
- package/src/alias.test.ts +206 -2
- package/src/alias.ts +196 -21
- package/src/cli.help-command.test.ts +105 -0
- package/src/cli.offline.test.ts +93 -0
- package/src/cli.parity.test.ts +192 -0
- package/src/cli.test.ts +95 -1
- package/src/cli.ts +195 -25
- package/src/completion-scripts.ts +16 -0
- package/src/completion-values.test.ts +76 -16
- package/src/completion-values.ts +170 -66
- package/src/completion.conformance.test.ts +297 -0
- package/src/completion.test.ts +256 -145
- package/src/completion.ts +767 -121
- package/src/compliance.test.ts +196 -2
- package/src/compliance.ts +440 -20
- package/src/config-merge.test.ts +425 -0
- package/src/config.test.ts +228 -1
- package/src/config.ts +429 -16
- package/src/gen/crud_pb.ts +7 -7
- package/src/id/README.md +38 -0
- package/src/id/contract.test.ts +1 -1
- package/src/id/index.test.ts +1 -1
- package/src/id/index.ts +3 -2
- package/src/index.test.ts +71 -0
- package/src/index.ts +46 -0
- package/src/llm-anthropic.conformance.test.ts +1135 -0
- package/src/llm-anthropic.test.ts +152 -0
- package/src/llm-anthropic.ts +940 -0
- package/src/llm-choice-hermetic.test.ts +335 -0
- package/src/llm-config.test.ts +232 -0
- package/src/llm-config.ts +106 -0
- package/src/llm-endpoint.ts +254 -0
- package/src/llm-error.ts +17 -0
- package/src/llm-errors.ts +152 -0
- package/src/llm-file.ts +126 -0
- package/src/llm-http.ts +317 -0
- package/src/llm-keys.ts +167 -0
- package/src/llm-media.conformance.test.ts +87 -0
- package/src/llm-media.test.ts +279 -0
- package/src/llm-media.ts +322 -0
- package/src/llm-openai.conformance.test.ts +407 -0
- package/src/llm-openai.test.ts +510 -0
- package/src/llm-openai.ts +708 -0
- package/src/llm-payload.ts +155 -0
- package/src/llm-quote.ts +46 -0
- package/src/llm-routellm.conformance.test.ts +44 -0
- package/src/llm-toolnames.conformance.test.ts +112 -0
- package/src/llm-toolnames.test.ts +132 -0
- package/src/llm-toolnames.ts +298 -0
- package/src/llm-uri.ts +99 -0
- package/src/llm.test.ts +43 -15
- package/src/llm.toolcall.test.ts +175 -0
- package/src/llm.ts +602 -190
- package/src/log.parity.test.ts +105 -0
- package/src/log.test.ts +270 -1
- package/src/log.ts +228 -49
- package/src/mcp/README.md +63 -0
- package/src/mcp/args-separator.test.ts +164 -0
- package/src/mcp/args.test.ts +223 -0
- package/src/mcp/args.ts +160 -0
- package/src/mcp/audit.test.ts +520 -0
- package/src/mcp/audit.ts +70 -0
- package/src/mcp/bridge.test.ts +222 -0
- package/src/mcp/bridge.ts +291 -0
- package/src/mcp/conformance.test.ts +357 -0
- package/src/mcp/dispatch.ts +359 -0
- package/src/mcp/identity.test.ts +166 -0
- package/src/mcp/identity.ts +72 -0
- package/src/mcp/index.ts +176 -0
- package/src/mcp/legacy-notify.test.ts +59 -0
- package/src/mcp/legacy.ts +367 -0
- package/src/mcp/mcp.test.ts +707 -0
- package/src/mcp/modern-confirm.test.ts +520 -0
- package/src/mcp/modern-confirm.ts +484 -0
- package/src/mcp/modern.ts +788 -0
- package/src/mcp/stdio.test.ts +313 -0
- package/src/mcp/stdio.ts +254 -0
- package/src/mcp/tasks.ts +82 -0
- package/src/mcp/testtree.ts +250 -0
- package/src/mcp/toolname.test.ts +147 -0
- package/src/mcp/types.ts +689 -0
- package/src/netpolicy.test.ts +280 -0
- package/src/netpolicy.ts +256 -0
- package/src/output/README.md +46 -0
- package/src/output/builtins.ts +5 -5
- package/src/output/dispatch.ts +10 -2
- package/src/output/error.ts +459 -0
- package/src/output/formatter.ts +29 -5
- package/src/output/formatters/README.md +50 -0
- 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 +64 -3
- package/src/published-types.test.ts +133 -0
- package/src/routellm.test.ts +209 -28
- package/src/routellm.toolcall.test.ts +521 -0
- package/src/routellm.ts +213 -177
- package/src/router/README.md +44 -0
- package/src/router/server.test.ts +41 -0
- package/src/router/server.ts +11 -0
- package/src/safety.test.ts +19 -9
- package/src/scope-defaults.json +29 -29
- package/src/serve.test.ts +1257 -0
- package/src/serve.ts +1665 -0
- package/src/telemetry/README.md +48 -0
- package/src/telemetry/client.test.ts +31 -0
- package/src/telemetry/client.ts +12 -6
- package/src/telemetry/offline.test.ts +64 -0
- package/src/toolspec-internal.ts +279 -0
- package/src/toolspec-manifest.test.ts +167 -0
- package/src/toolspec-manifest.ts +410 -0
- package/src/toolspec-spec-schemas.ts +12 -0
- package/src/toolspec-spec.test.ts +273 -0
- package/src/toolspec-spec.ts +458 -0
- package/src/toolspec.from-command.test.ts +350 -0
- package/src/toolspec.test.ts +2 -2
- package/src/toolspec.ts +222 -0
- package/src/triton/README.md +34 -0
- package/src/tui/README.md +42 -0
- package/src/tui/parity.test.ts +120 -31
- package/src/tui/parity.ts +58 -7
- package/src/wizard.test.ts +7 -7
- package/test/llm/conformance-helpers.ts +100 -0
- package/test/output/csv.test.ts +164 -0
- package/test/output/dispatch.test.ts +1 -2
- package/test/output/error.test.ts +277 -0
- package/test/output/smoke-capture.ts +2 -2
- package/test/output/smoke.test.ts +2 -2
- package/test/output/taxonomy-contract.test.ts +148 -0
- package/test/setup-env.ts +16 -0
- package/test/toolspec/fixture.ts +79 -0
- package/tsconfig.json +3 -1
- package/tsconfig.test.json +10 -0
- package/vitest.config.ts +9 -0
- package/src/tui/parity.json +0 -74
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# telemetry
|
|
2
|
+
|
|
3
|
+
## What it answers
|
|
4
|
+
|
|
5
|
+
What a TS adopter needs before emitting usage telemetry: the mode gate (`off`, `anon`, `full`), the anonymous rotatable install id, the consent reader, the redactor, and a non-blocking `Client` with jsonl and https sinks. Default-denied: a fresh install never emits. Wrong module for Factor 11 provenance metadata on payloads (`@hop-top/kit/provenance`) and for the compliance checker (`@hop-top/kit/compliance`).
|
|
6
|
+
|
|
7
|
+
## Use it when
|
|
8
|
+
|
|
9
|
+
- record an event: `new Client(opts)`, `client.record(event, attrs)`, `await client.shutdown(ms)`
|
|
10
|
+
- know which tier applies: `resolveMode(process.env)`
|
|
11
|
+
- read or rotate the install id: `getInstallId()`, `rotate()`
|
|
12
|
+
- check the operator's decision: `loadConsent()`
|
|
13
|
+
- scrub before your own sink: `redact(value)`, `redactString(s)`
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { Client } from '@hop-top/kit/telemetry';
|
|
19
|
+
|
|
20
|
+
const client = new Client({ sink: 'jsonl', sinkFile: '/tmp/kit-events.jsonl', sdkVersion: '0.0.0' });
|
|
21
|
+
client.record('cmd.invoked', { command: 'launch', exit_code: 0 });
|
|
22
|
+
await client.shutdown(5_000);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`record` is a no-op until consent is granted and `KIT_TELEMETRY_MODE` is `anon` or `full`.
|
|
26
|
+
|
|
27
|
+
## Contract
|
|
28
|
+
|
|
29
|
+
- Envelope shape is [`sdk/docs/telemetry-event-schema.md`](../../../docs/telemetry-event-schema.md); keys are wire snake_case.
|
|
30
|
+
- `record` returns synchronously and never throws; the queue is bounded and drops count.
|
|
31
|
+
- Consent and mode are consulted on each `record`, so a live consent flip takes effect without restart.
|
|
32
|
+
- Redactor placeholders are `<redacted:email>`, `<redacted:ipv4>`, `<redacted:ipv6>`, `<redacted:token>`, and `$HOME` for home-path prefixes.
|
|
33
|
+
- The envelope carries a free-form `event` plus `attrs` where Go carries `CommandPath`; this is a documented divergence.
|
|
34
|
+
- Parity: [`sdk/tests/cross-lang/expected/envelope.json`](../../../tests/cross-lang/expected/envelope.json), driven by `sdk/tests/cross-lang/run.sh` from [`fixtures/consent.yaml`](../../../tests/cross-lang/fixtures/consent.yaml) and [`fixtures/install_id.bytes`](../../../tests/cross-lang/fixtures/install_id.bytes).
|
|
35
|
+
|
|
36
|
+
## Neighbours
|
|
37
|
+
|
|
38
|
+
- `@hop-top/kit/provenance`: `_meta` on output payloads
|
|
39
|
+
- `@hop-top/kit/compliance`: 12-factor checks, including the telemetry consent factor
|
|
40
|
+
- `@hop-top/kit/xdg`: state and config paths the install id and consent file live under
|
|
41
|
+
- Go reference: [`go/runtime/telemetry`](../../../../go/runtime/telemetry/README.md); Python port: [`hop_top_kit/telemetry`](../../../py/hop_top_kit/telemetry/README.md)
|
|
42
|
+
|
|
43
|
+
## See also
|
|
44
|
+
|
|
45
|
+
- [`sdk/ts/README.md`](../../README.md), section "Telemetry": env vars, redactor, envelope
|
|
46
|
+
- [`docs/adopters/guides/telemetry.md`](../../../../docs/adopters/guides/telemetry.md)
|
|
47
|
+
- [`docs/adopters/reference/telemetry-compliance.md`](../../../../docs/adopters/reference/telemetry-compliance.md)
|
|
48
|
+
- [`sdk/tests/cross-lang/README.md`](../../../tests/cross-lang/README.md)
|
|
@@ -232,6 +232,37 @@ describe('Client.https sink', () => {
|
|
|
232
232
|
}
|
|
233
233
|
});
|
|
234
234
|
|
|
235
|
+
// Telemetry is logging-class egress: --offline stops traffic the user
|
|
236
|
+
// asked for, it is not a second consent gate on diagnostics. The sink
|
|
237
|
+
// must therefore still ship while the process is marked offline.
|
|
238
|
+
//
|
|
239
|
+
// The endpoint is deliberately remote: loopback is exempt from the
|
|
240
|
+
// guard by design, so a 127.0.0.1 target could never fail this test.
|
|
241
|
+
it('ships while offline: --offline must not mute telemetry', async () => {
|
|
242
|
+
const { install, setProcessOffline } = await import('../netpolicy');
|
|
243
|
+
await grantConsent();
|
|
244
|
+
process.env.KIT_TELEMETRY_MODE = 'anon';
|
|
245
|
+
|
|
246
|
+
const mockFetch = vi.fn(async () => new Response(null, { status: 202 }));
|
|
247
|
+
vi.stubGlobal('fetch', mockFetch);
|
|
248
|
+
// Guard the global exactly as createCLI does at start-up.
|
|
249
|
+
install();
|
|
250
|
+
setProcessOffline(true);
|
|
251
|
+
|
|
252
|
+
try {
|
|
253
|
+
const c = new Client({
|
|
254
|
+
sink: 'https',
|
|
255
|
+
endpoint: 'https://telemetry.example.com/v1/events',
|
|
256
|
+
});
|
|
257
|
+
c.record('probe', { i: 1 });
|
|
258
|
+
await c.shutdown(2_000);
|
|
259
|
+
|
|
260
|
+
expect(mockFetch).toHaveBeenCalledTimes(1);
|
|
261
|
+
} finally {
|
|
262
|
+
setProcessOffline(false);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
235
266
|
it('retries once on 5xx, then drops', async () => {
|
|
236
267
|
await grantConsent();
|
|
237
268
|
process.env.KIT_TELEMETRY_MODE = 'anon';
|
package/src/telemetry/client.ts
CHANGED
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
* a fail-closed posture.
|
|
19
19
|
* - **Default Mode is Off**: `resolveMode()` is consulted at the same
|
|
20
20
|
* gating step. Anything other than `anon` / `full` no-ops.
|
|
21
|
-
* - **Envelope shape**: matches the cross-language event
|
|
22
|
-
* for the common fields. NOTE:
|
|
23
|
-
*
|
|
24
|
-
*
|
|
21
|
+
* - **Envelope shape**: matches the cross-language event schema
|
|
22
|
+
* for the common fields. NOTE: the TS+Py SDKs diverge from Go by
|
|
23
|
+
* carrying free-form `event` (string) + `attrs` (object) at the
|
|
24
|
+
* envelope root.
|
|
25
25
|
* - **install_id caching**: `getInstallId()` does file I/O; we resolve
|
|
26
26
|
* it lazily and cache the resolved promise.
|
|
27
27
|
*
|
|
@@ -36,6 +36,7 @@ import * as yaml from 'js-yaml';
|
|
|
36
36
|
|
|
37
37
|
import { Mode, resolveMode } from './mode';
|
|
38
38
|
import { getInstallId } from './installId';
|
|
39
|
+
import { observabilityFetch } from '../netpolicy';
|
|
39
40
|
import { consentPath, legacyConsentPath } from './consent';
|
|
40
41
|
import { redact } from './redact';
|
|
41
42
|
|
|
@@ -68,7 +69,7 @@ const DEFAULT_QUEUE_SIZE = 1024;
|
|
|
68
69
|
const FLUSH_INTERVAL_MS = 5_000;
|
|
69
70
|
/** Threshold that triggers an early flush before the interval fires. */
|
|
70
71
|
const FLUSH_BATCH_TRIGGER = 64;
|
|
71
|
-
/** HTTPS connect timeout (
|
|
72
|
+
/** HTTPS connect timeout (telemetry must never block the caller). */
|
|
72
73
|
const HTTPS_CONNECT_TIMEOUT_MS = 5_000;
|
|
73
74
|
/** HTTPS overall timeout. */
|
|
74
75
|
const HTTPS_TOTAL_TIMEOUT_MS = 10_000;
|
|
@@ -412,7 +413,12 @@ export class Client {
|
|
|
412
413
|
HTTPS_CONNECT_TIMEOUT_MS,
|
|
413
414
|
);
|
|
414
415
|
try {
|
|
415
|
-
|
|
416
|
+
// Telemetry is logging-class egress: --offline stops traffic
|
|
417
|
+
// the user asked for, not diagnostics. Consent and mode already
|
|
418
|
+
// govern whether anything is emitted, so this sink deliberately
|
|
419
|
+
// uses the unguarded fetch captured before install() patched the
|
|
420
|
+
// global.
|
|
421
|
+
const res = await observabilityFetch()(this.endpoint, {
|
|
416
422
|
method: 'POST',
|
|
417
423
|
headers,
|
|
418
424
|
body,
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, expect, it, afterEach, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
install,
|
|
5
|
+
observabilityFetch,
|
|
6
|
+
setProcessOffline,
|
|
7
|
+
isOfflineError,
|
|
8
|
+
} from '../netpolicy';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Telemetry is logging-class egress: `--offline` stops traffic the user
|
|
12
|
+
* asked for, it is not a second consent gate on diagnostics. Consent and
|
|
13
|
+
* telemetry mode already govern whether anything is emitted at all.
|
|
14
|
+
*
|
|
15
|
+
* Destinations here are deliberately remote. Loopback is exempt from the
|
|
16
|
+
* guard by design, so a 127.0.0.1 target would pass with or without the
|
|
17
|
+
* carve-out and could never fail.
|
|
18
|
+
*/
|
|
19
|
+
describe('telemetry exemption from --offline', () => {
|
|
20
|
+
const REMOTE = 'https://telemetry.example.com/v1/events';
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
setProcessOffline(false);
|
|
24
|
+
vi.restoreAllMocks();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('observability fetch reaches a remote endpoint while offline', async () => {
|
|
28
|
+
const base = vi.fn(async () => new Response('', { status: 200 }));
|
|
29
|
+
globalThis.fetch = base as unknown as typeof globalThis.fetch;
|
|
30
|
+
|
|
31
|
+
// Re-import so the pristine reference is captured from the stub.
|
|
32
|
+
vi.resetModules();
|
|
33
|
+
const fresh = await import('../netpolicy');
|
|
34
|
+
fresh.install();
|
|
35
|
+
fresh.setProcessOffline(true);
|
|
36
|
+
|
|
37
|
+
const res = await fresh.observabilityFetch()(REMOTE, { method: 'POST' });
|
|
38
|
+
expect(res.status).toBe(200);
|
|
39
|
+
expect(base).toHaveBeenCalledTimes(1);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('keeps the carve-out narrow: ordinary fetch is still refused', async () => {
|
|
43
|
+
const base = vi.fn(async () => new Response('', { status: 200 }));
|
|
44
|
+
globalThis.fetch = base as unknown as typeof globalThis.fetch;
|
|
45
|
+
|
|
46
|
+
install();
|
|
47
|
+
setProcessOffline(true);
|
|
48
|
+
|
|
49
|
+
let caught: unknown;
|
|
50
|
+
try {
|
|
51
|
+
await globalThis.fetch(REMOTE, { method: 'GET' });
|
|
52
|
+
} catch (err) {
|
|
53
|
+
caught = err;
|
|
54
|
+
}
|
|
55
|
+
expect(isOfflineError(caught)).toBe(true);
|
|
56
|
+
expect(base).not.toHaveBeenCalled();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('observability fetch is not the guarded global', () => {
|
|
60
|
+
install();
|
|
61
|
+
expect(observabilityFetch()).not.toBe(globalThis.fetch);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal helpers shared by the toolspec modules. Not re-exported
|
|
3
|
+
* from the package's `toolspec` entry.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
Command as CommanderCommand,
|
|
8
|
+
Option as CommanderOption,
|
|
9
|
+
} from "commander";
|
|
10
|
+
|
|
11
|
+
export type Annotations = Record<string, string>;
|
|
12
|
+
|
|
13
|
+
export function annotationsOf(cmd: CommanderCommand): Annotations {
|
|
14
|
+
return (cmd as { annotations?: Annotations }).annotations ?? {};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function splitCSV(s: string | undefined): string[] | undefined {
|
|
18
|
+
if (!s) return undefined;
|
|
19
|
+
const out = s.split(",").map((p) => p.trim()).filter((p) => p !== "");
|
|
20
|
+
return out.length > 0 ? out : undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// --- commander facts ---
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* cmd's declared positional arguments (commander registeredArguments),
|
|
27
|
+
* in order, `required` set only on required ones and `variadic` only
|
|
28
|
+
* on a `<name...>` / `[name...]` one. Shared by the toolspec
|
|
29
|
+
* projection and the MCP bridge so both read positionals the same
|
|
30
|
+
* way.
|
|
31
|
+
*/
|
|
32
|
+
export function declaredArgs(
|
|
33
|
+
cmd: CommanderCommand,
|
|
34
|
+
): Array<{ name: string; required?: boolean; variadic?: boolean }> {
|
|
35
|
+
return cmd.registeredArguments.map((a) => {
|
|
36
|
+
const out: { name: string; required?: boolean; variadic?: boolean } = { name: a.name() };
|
|
37
|
+
if (a.required) out.required = true;
|
|
38
|
+
if (a.variadic) out.variadic = true;
|
|
39
|
+
return out;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Parse a kit/args annotation the way Go's cmdmeta.ParseArgs does: a
|
|
45
|
+
* comma list, whitespace trimmed, empties dropped; `name?` optional,
|
|
46
|
+
* `name...` variadic (one or more), `name...?` optional variadic
|
|
47
|
+
* (zero or more); the dots come before the `?`, any other suffix is
|
|
48
|
+
* part of the name. Undefined when the annotation is absent or blank.
|
|
49
|
+
*/
|
|
50
|
+
export function parseKitArgs(
|
|
51
|
+
raw: string | undefined,
|
|
52
|
+
): Array<{ name: string; optional: boolean; variadic: boolean }> | undefined {
|
|
53
|
+
const entries = splitCSV(raw);
|
|
54
|
+
if (!entries) return undefined;
|
|
55
|
+
return entries.map((entry) => {
|
|
56
|
+
let name = entry;
|
|
57
|
+
let optional = false;
|
|
58
|
+
let variadic = false;
|
|
59
|
+
if (name.endsWith("?")) {
|
|
60
|
+
optional = true;
|
|
61
|
+
name = name.slice(0, -1);
|
|
62
|
+
}
|
|
63
|
+
if (name.endsWith("...")) {
|
|
64
|
+
variadic = true;
|
|
65
|
+
name = name.slice(0, -3);
|
|
66
|
+
}
|
|
67
|
+
return { name, optional, variadic };
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Map a commander option onto pflag's Type() and DefValue spelling,
|
|
73
|
+
* which is what the Go manifest and ToolSpec carry: a bool is
|
|
74
|
+
* "false", a slice "[a,b]", a value flag its default or "".
|
|
75
|
+
*/
|
|
76
|
+
export function flagTypeAndDefault(opt: CommanderOption): [string, string] {
|
|
77
|
+
const def = opt.defaultValue;
|
|
78
|
+
const declared = pflagTypes.get(opt);
|
|
79
|
+
if (declared) return [declared, def === undefined ? "" : String(def)];
|
|
80
|
+
if (opt.variadic) {
|
|
81
|
+
const items = def === undefined ? [] : Array.isArray(def) ? def : [def];
|
|
82
|
+
return ["stringSlice", `[${items.map(String).join(",")}]`];
|
|
83
|
+
}
|
|
84
|
+
// A negated --no-x is its own pflag bool, unset (false) by default;
|
|
85
|
+
// commander's default for it belongs to the x attribute, not the flag.
|
|
86
|
+
if (opt.negate) return ["bool", "false"];
|
|
87
|
+
if (takesNoValue(opt)) return ["bool", def === undefined ? "false" : String(def)];
|
|
88
|
+
return ["string", def === undefined ? "" : String(def)];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The closed value set a commander option declares (`.choices()`), or undefined. */
|
|
92
|
+
export function flagChoices(opt: CommanderOption): string[] | undefined {
|
|
93
|
+
const choices = (opt as { argChoices?: string[] }).argChoices;
|
|
94
|
+
return choices && choices.length > 0 ? [...choices] : undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Whether opt takes no value: a plain boolean flag or a negated one
|
|
99
|
+
* (`--no-x`). commander's isBoolean() is false for negated options,
|
|
100
|
+
* yet they are value-less all the same, so pflag-shaped projections
|
|
101
|
+
* (a `no-x` bool) must not rely on isBoolean() alone.
|
|
102
|
+
*/
|
|
103
|
+
export function takesNoValue(opt: CommanderOption): boolean {
|
|
104
|
+
return !opt.required && !opt.optional;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Whether cmd has an action handler (cobra Runnable). commander 15
|
|
109
|
+
* exposes no public accessor, so this reads the private field.
|
|
110
|
+
*/
|
|
111
|
+
export function isRunnable(cmd: CommanderCommand): boolean {
|
|
112
|
+
return (cmd as unknown as { _actionHandler?: unknown })._actionHandler != null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Hidden per the parent's help (public API; no private _hidden read). */
|
|
116
|
+
export function isHidden(cmd: CommanderCommand, parent: CommanderCommand): boolean {
|
|
117
|
+
return !parent.createHelp().visibleCommands(parent).includes(cmd);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export const BUILTIN_NAMES = new Set([
|
|
121
|
+
"help",
|
|
122
|
+
"completion",
|
|
123
|
+
"man",
|
|
124
|
+
"__complete",
|
|
125
|
+
"__completeNoDesc",
|
|
126
|
+
]);
|
|
127
|
+
|
|
128
|
+
export function isBuiltin(cmd: CommanderCommand, parent: CommanderCommand): boolean {
|
|
129
|
+
return BUILTIN_NAMES.has(cmd.name()) || parent.name() === "completion";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function byName(a: { name(): string }, b: { name(): string }): number {
|
|
133
|
+
const x = a.name();
|
|
134
|
+
const y = b.name();
|
|
135
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// --- Go encoding/json byte parity ---
|
|
139
|
+
|
|
140
|
+
/** Go's json string escaping (HTML-safe, U+2028/2029 escaped). */
|
|
141
|
+
export function goQuote(s: string): string {
|
|
142
|
+
return JSON.stringify(s)
|
|
143
|
+
.replace(/</g, "\\u003c")
|
|
144
|
+
.replace(/>/g, "\\u003e")
|
|
145
|
+
.replace(/&/g, "\\u0026")
|
|
146
|
+
.replace(/\u2028/g, "\\u2028")
|
|
147
|
+
.replace(/\u2029/g, "\\u2029");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Re-emit raw JSON text the way Go's Encoder.SetIndent("", " ")
|
|
152
|
+
* does for an embedded RawJSON: tokens verbatim (numbers keep their
|
|
153
|
+
* spelling), insignificant whitespace dropped, strings HTML-escaped,
|
|
154
|
+
* structure re-indented at the given depth. Throws on invalid JSON,
|
|
155
|
+
* as Go's encoder errors.
|
|
156
|
+
*/
|
|
157
|
+
export function reindentRaw(raw: string, depth: number): string {
|
|
158
|
+
JSON.parse(raw); // validity gate
|
|
159
|
+
let out = "";
|
|
160
|
+
let level = depth;
|
|
161
|
+
let needIndent = false;
|
|
162
|
+
const nl = (lvl: number) => "\n" + " ".repeat(lvl);
|
|
163
|
+
for (let i = 0; i < raw.length; i++) {
|
|
164
|
+
const c = raw[i];
|
|
165
|
+
if (c === " " || c === "\t" || c === "\n" || c === "\r") continue;
|
|
166
|
+
if (needIndent && c !== "]" && c !== "}") {
|
|
167
|
+
out += nl(level);
|
|
168
|
+
needIndent = false;
|
|
169
|
+
}
|
|
170
|
+
if (c === '"') {
|
|
171
|
+
let j = i + 1;
|
|
172
|
+
while (raw[j] !== '"') j += raw[j] === "\\" ? 2 : 1;
|
|
173
|
+
out += goQuote(JSON.parse(raw.slice(i, j + 1)) as string);
|
|
174
|
+
i = j;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
switch (c) {
|
|
178
|
+
case "{":
|
|
179
|
+
case "[":
|
|
180
|
+
out += c;
|
|
181
|
+
level++;
|
|
182
|
+
needIndent = true;
|
|
183
|
+
break;
|
|
184
|
+
case "}":
|
|
185
|
+
case "]":
|
|
186
|
+
level--;
|
|
187
|
+
if (needIndent) needIndent = false;
|
|
188
|
+
else out += nl(level);
|
|
189
|
+
out += c;
|
|
190
|
+
break;
|
|
191
|
+
case ",":
|
|
192
|
+
out += c + nl(level);
|
|
193
|
+
break;
|
|
194
|
+
case ":":
|
|
195
|
+
out += ": ";
|
|
196
|
+
break;
|
|
197
|
+
default:
|
|
198
|
+
out += c;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Go encoding/json (Encoder.SetIndent("", " ")) bytes for v; keys in rawKeys hold raw JSON text. */
|
|
205
|
+
export function encodeGoJSON(v: unknown, depth: number, rawKeys: Set<string>, key?: string): string {
|
|
206
|
+
if (key !== undefined && rawKeys.has(key) && typeof v === "string") {
|
|
207
|
+
return reindentRaw(v, depth);
|
|
208
|
+
}
|
|
209
|
+
if (typeof v === "string") return goQuote(v);
|
|
210
|
+
if (typeof v === "boolean" || typeof v === "number") return JSON.stringify(v);
|
|
211
|
+
if (Array.isArray(v)) {
|
|
212
|
+
if (v.length === 0) return "[]";
|
|
213
|
+
const pad = " ".repeat(depth + 1);
|
|
214
|
+
const items = v.map((x) => pad + encodeGoJSON(x, depth + 1, rawKeys));
|
|
215
|
+
return "[\n" + items.join(",\n") + "\n" + " ".repeat(depth) + "]";
|
|
216
|
+
}
|
|
217
|
+
if (v && typeof v === "object") {
|
|
218
|
+
const entries = Object.entries(v).filter(([, x]) => x !== undefined);
|
|
219
|
+
if (entries.length === 0) return "{}";
|
|
220
|
+
const pad = " ".repeat(depth + 1);
|
|
221
|
+
const items = entries.map(
|
|
222
|
+
([k, x]) => pad + goQuote(k) + ": " + encodeGoJSON(x, depth + 1, rawKeys, k),
|
|
223
|
+
);
|
|
224
|
+
return "{\n" + items.join(",\n") + "\n" + " ".repeat(depth) + "}";
|
|
225
|
+
}
|
|
226
|
+
return "null";
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Visit every command below program depth-first, children in name
|
|
231
|
+
* order (cobra's default sorting), with its Go-style path (root name
|
|
232
|
+
* first) and whether it is reserved: its depth-1 verb is in
|
|
233
|
+
* `reserved`, or it or an ancestor carries kit/reserved=true.
|
|
234
|
+
*/
|
|
235
|
+
export function walkCommands(
|
|
236
|
+
program: CommanderCommand,
|
|
237
|
+
reserved: Iterable<string> | undefined,
|
|
238
|
+
visit: (
|
|
239
|
+
cmd: CommanderCommand,
|
|
240
|
+
parent: CommanderCommand,
|
|
241
|
+
path: string[],
|
|
242
|
+
reserved: boolean,
|
|
243
|
+
) => void,
|
|
244
|
+
): void {
|
|
245
|
+
const reservedSet = new Set(reserved ?? []);
|
|
246
|
+
const walk = (
|
|
247
|
+
cmd: CommanderCommand,
|
|
248
|
+
parent: CommanderCommand,
|
|
249
|
+
path: string[],
|
|
250
|
+
depthOne: string,
|
|
251
|
+
reservedTree: boolean,
|
|
252
|
+
): void => {
|
|
253
|
+
const inReserved = reservedTree || annotationsOf(cmd)["kit/reserved"] === "true";
|
|
254
|
+
visit(cmd, parent, path, inReserved || reservedSet.has(depthOne));
|
|
255
|
+
for (const child of [...cmd.commands].sort(byName)) {
|
|
256
|
+
if (child.name() === "") continue;
|
|
257
|
+
walk(child, cmd, [...path, child.name()], depthOne, inReserved);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
for (const child of [...program.commands].sort(byName)) {
|
|
261
|
+
if (child.name() === "") continue;
|
|
262
|
+
walk(child, program, [program.name(), child.name()], child.name(), false);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* pflag type names for options whose commander shape cannot say
|
|
268
|
+
* (commander has no typed options), e.g. a float --min.
|
|
269
|
+
*/
|
|
270
|
+
export const pflagTypes = new WeakMap<CommanderOption, string>();
|
|
271
|
+
|
|
272
|
+
/** Go fmt "%.0f": round half to even. */
|
|
273
|
+
export function goFormat0(x: number): string {
|
|
274
|
+
const floor = Math.floor(x);
|
|
275
|
+
const diff = x - floor;
|
|
276
|
+
let r = diff > 0.5 ? floor + 1 : diff < 0.5 ? floor : floor % 2 === 0 ? floor : floor + 1;
|
|
277
|
+
if (Object.is(r, -0)) r = 0;
|
|
278
|
+
return String(r);
|
|
279
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { Command, Option } from "commander";
|
|
5
|
+
import { FIXTURES, fixtureProgram } from "../test/toolspec/fixture";
|
|
6
|
+
import {
|
|
7
|
+
buildManifest,
|
|
8
|
+
LATEST_SCHEMA_VERSION,
|
|
9
|
+
negotiateSchemaVersion,
|
|
10
|
+
renderManifestJSON,
|
|
11
|
+
SCHEMA_VERSION_ENV,
|
|
12
|
+
} from "./toolspec";
|
|
13
|
+
|
|
14
|
+
describe("buildManifest cross-language fixture", () => {
|
|
15
|
+
for (const [file, includeDeprecated] of [
|
|
16
|
+
["toolspec-manifest.json", false],
|
|
17
|
+
["toolspec-manifest.include-deprecated.json", true],
|
|
18
|
+
] as const) {
|
|
19
|
+
it(`matches the Go bytes in ${file}`, () => {
|
|
20
|
+
const { program, tree } = fixtureProgram();
|
|
21
|
+
const manifest = buildManifest(program, "1.2", {
|
|
22
|
+
includeDeprecated,
|
|
23
|
+
reserved: tree.reserved,
|
|
24
|
+
});
|
|
25
|
+
const want = fs.readFileSync(path.join(FIXTURES, file), "utf-8");
|
|
26
|
+
expect(renderManifestJSON(manifest)).toBe(want);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
it("throws on a malformed output schema, as Go's encoder does", () => {
|
|
31
|
+
const program = new Command("t");
|
|
32
|
+
const c = program.command("x").action(() => {});
|
|
33
|
+
(c as Command & { annotations?: Record<string, string> }).annotations = {
|
|
34
|
+
"kit/output-schema": "{not json",
|
|
35
|
+
};
|
|
36
|
+
expect(() => renderManifestJSON(buildManifest(program, "1.1"))).toThrow();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("emits an empty commands array for a bare program", () => {
|
|
40
|
+
const out = renderManifestJSON(buildManifest(new Command("t"), "1.0"));
|
|
41
|
+
expect(out).toBe(
|
|
42
|
+
'{\n "tool": "t",\n "version": "",\n "schema_version": "1.0",\n "commands": []\n}\n',
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("negotiateSchemaVersion", () => {
|
|
48
|
+
const cases: Array<[string, string, string]> = [
|
|
49
|
+
["1.0", "", "1.0"],
|
|
50
|
+
["1.0", "1.0", "1.0"],
|
|
51
|
+
["1.0", "1.1", "1.1"],
|
|
52
|
+
["1.0", "1.2", "1.2"],
|
|
53
|
+
["1.0", "2.0", "1.2"],
|
|
54
|
+
["1.0", "1.9", "1.2"],
|
|
55
|
+
["1.0", "0.9", "1.0"],
|
|
56
|
+
["1.0", "garbage", "1.0"],
|
|
57
|
+
["1.0", "1", "1.0"],
|
|
58
|
+
["1.0", "1.1x", "1.0"],
|
|
59
|
+
["1.0", "+1.1", "1.0"],
|
|
60
|
+
["1.0", "1.x", "1.0"],
|
|
61
|
+
["1.0", "1.1.0", "1.0"],
|
|
62
|
+
["1.0", " 1.1 ", "1.1"],
|
|
63
|
+
["1.1", "", "1.1"],
|
|
64
|
+
["1.1", "1.0", "1.1"],
|
|
65
|
+
["1.1", "1.1", "1.1"],
|
|
66
|
+
["1.1", "1.2", "1.2"],
|
|
67
|
+
["1.1", "2.0", "1.2"],
|
|
68
|
+
["1.1", "junk", "1.1"],
|
|
69
|
+
["1.2", "", "1.2"],
|
|
70
|
+
["1.2", "1.0", "1.2"],
|
|
71
|
+
["1.2", "1.1", "1.2"],
|
|
72
|
+
["1.2", "2.0", "1.2"],
|
|
73
|
+
["1.2", "junk", "1.2"],
|
|
74
|
+
["2.0", "1.1", "2.0"],
|
|
75
|
+
["2.0", "3.0", "2.0"],
|
|
76
|
+
["v1", "1.1", "v1"],
|
|
77
|
+
["", "1.1", ""],
|
|
78
|
+
];
|
|
79
|
+
for (const [declared, requested, want] of cases) {
|
|
80
|
+
it(`${JSON.stringify(declared)} + ${JSON.stringify(requested)} -> ${want}`, () => {
|
|
81
|
+
expect(negotiateSchemaVersion(declared, requested)).toBe(want);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
it("pins the env name and latest version", () => {
|
|
86
|
+
expect(LATEST_SCHEMA_VERSION).toBe("1.2");
|
|
87
|
+
expect(SCHEMA_VERSION_ENV).toBe("KIT_TOOLSPEC_SCHEMA");
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("buildManifest schema 1.2 fields", () => {
|
|
92
|
+
it("projects required, choices and variadic as Go does", () => {
|
|
93
|
+
const program = new Command("t");
|
|
94
|
+
const attach = program.command("attach").action(() => {});
|
|
95
|
+
attach.addOption(
|
|
96
|
+
new Option("--mode <mode>", "Attach mode").choices(["fast", "safe"]).default("fast").makeOptionMandatory(),
|
|
97
|
+
);
|
|
98
|
+
attach.option("--label <text>", "Free-form label");
|
|
99
|
+
(attach as Command & { annotations?: Record<string, string> }).annotations = {
|
|
100
|
+
"kit/args": "name, note?, files...?",
|
|
101
|
+
};
|
|
102
|
+
program.command("cat <files...>").action(() => {});
|
|
103
|
+
|
|
104
|
+
const [a, c] = buildManifest(program, "1.2").commands;
|
|
105
|
+
expect(a.args).toEqual([
|
|
106
|
+
{ name: "name", required: true },
|
|
107
|
+
{ name: "note" },
|
|
108
|
+
{ name: "files", variadic: true },
|
|
109
|
+
]);
|
|
110
|
+
expect(a.flags).toEqual([
|
|
111
|
+
{ name: "label", type: "string", description: "Free-form label" },
|
|
112
|
+
{
|
|
113
|
+
name: "mode",
|
|
114
|
+
type: "string",
|
|
115
|
+
description: "Attach mode",
|
|
116
|
+
default: "fast",
|
|
117
|
+
required: true,
|
|
118
|
+
choices: ["fast", "safe"],
|
|
119
|
+
},
|
|
120
|
+
]);
|
|
121
|
+
// Keys land in Go struct order so the encoder matches Go's bytes.
|
|
122
|
+
expect(Object.keys(a.flags![1])).toEqual([
|
|
123
|
+
"name", "type", "description", "default", "required", "choices",
|
|
124
|
+
]);
|
|
125
|
+
expect(Object.keys(a.args![2])).toEqual(["name", "variadic"]);
|
|
126
|
+
// commander's own <files...> marks variadic when kit/args is absent.
|
|
127
|
+
expect(c.args).toEqual([{ name: "files", required: true, variadic: true }]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("reads the kit/args grammar exactly as Go: dots before the ?", () => {
|
|
131
|
+
const program = new Command("t");
|
|
132
|
+
const run = program.command("run").action(() => {});
|
|
133
|
+
(run as Command & { annotations?: Record<string, string> }).annotations = {
|
|
134
|
+
"kit/args": " ids... ,, odd?... , plain.. ",
|
|
135
|
+
};
|
|
136
|
+
expect(buildManifest(program, "1.2").commands[0].args).toEqual([
|
|
137
|
+
{ name: "ids", required: true, variadic: true },
|
|
138
|
+
{ name: "odd?", required: true, variadic: true },
|
|
139
|
+
{ name: "plain..", required: true },
|
|
140
|
+
]);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe("buildManifest negatable flags", () => {
|
|
145
|
+
it("types --no-x bool with default false, as pflag does", () => {
|
|
146
|
+
const program = new Command("t");
|
|
147
|
+
program
|
|
148
|
+
.command("run")
|
|
149
|
+
.option("--no-color", "c")
|
|
150
|
+
.option("--hints", "on")
|
|
151
|
+
.option("--no-hints", "off")
|
|
152
|
+
.action(() => {});
|
|
153
|
+
expect(buildManifest(program, "1.1").commands[0].flags).toEqual([
|
|
154
|
+
{ name: "hints", type: "bool", description: "on", default: "false" },
|
|
155
|
+
{ name: "no-color", type: "bool", description: "c", default: "false" },
|
|
156
|
+
{ name: "no-hints", type: "bool", description: "off", default: "false" },
|
|
157
|
+
]);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("keeps a negated flag's default false whatever commander's default", () => {
|
|
161
|
+
const program = new Command("t");
|
|
162
|
+
program.command("run").option("--no-cache", "nc", true).action(() => {});
|
|
163
|
+
expect(buildManifest(program, "1.1").commands[0].flags).toEqual([
|
|
164
|
+
{ name: "no-cache", type: "bool", description: "nc", default: "false" },
|
|
165
|
+
]);
|
|
166
|
+
});
|
|
167
|
+
});
|