@testsprite/testsprite-cli 0.10.0 → 0.12.0

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 (61) hide show
  1. package/README.md +65 -41
  2. package/dist/commands/auth.d.ts +5 -1
  3. package/dist/commands/auth.js +17 -5
  4. package/dist/commands/ci.js +34 -4
  5. package/dist/commands/init.d.ts +6 -1
  6. package/dist/commands/init.js +13 -5
  7. package/dist/commands/project-env.d.ts +90 -0
  8. package/dist/commands/project-env.js +486 -0
  9. package/dist/commands/project.d.ts +19 -0
  10. package/dist/commands/project.js +92 -25
  11. package/dist/commands/test.d.ts +42 -4
  12. package/dist/commands/test.js +344 -49
  13. package/dist/commands/testlist.js +29 -3
  14. package/dist/commands/tunnel.d.ts +2 -2
  15. package/dist/commands/tunnel.js +139 -31
  16. package/dist/index.js +42 -14
  17. package/dist/lib/client-factory.d.ts +4 -1
  18. package/dist/lib/client-factory.js +14 -4
  19. package/dist/lib/client-tag.d.ts +13 -0
  20. package/dist/lib/client-tag.js +44 -0
  21. package/dist/lib/conflict-reason.d.ts +43 -2
  22. package/dist/lib/conflict-reason.js +100 -14
  23. package/dist/lib/credentials.d.ts +4 -0
  24. package/dist/lib/credentials.js +43 -4
  25. package/dist/lib/cron.js +8 -1
  26. package/dist/lib/errors.d.ts +44 -1
  27. package/dist/lib/errors.js +69 -12
  28. package/dist/lib/gh-output.d.ts +25 -11
  29. package/dist/lib/gh-output.js +36 -13
  30. package/dist/lib/http.d.ts +21 -2
  31. package/dist/lib/http.js +45 -8
  32. package/dist/lib/local-target.d.ts +32 -0
  33. package/dist/lib/local-target.js +77 -0
  34. package/dist/lib/output.d.ts +27 -1
  35. package/dist/lib/output.js +22 -3
  36. package/dist/lib/response-schemas.d.ts +5 -2
  37. package/dist/lib/response-schemas.js +39 -1
  38. package/dist/lib/runs.types.d.ts +50 -2
  39. package/dist/lib/target-url.d.ts +1 -1
  40. package/dist/lib/target-url.js +35 -12
  41. package/dist/lib/telemetry.d.ts +174 -4
  42. package/dist/lib/telemetry.js +286 -13
  43. package/dist/lib/tunnel-session.d.ts +13 -2
  44. package/dist/lib/tunnel-session.js +92 -23
  45. package/dist/lib/tunnel.types.d.ts +3 -1
  46. package/dist/lib/version-notice.d.ts +20 -1
  47. package/dist/lib/version-notice.js +59 -12
  48. package/dist/vendor/tunnel-client/client.d.ts +11 -0
  49. package/dist/vendor/tunnel-client/client.js +383 -56
  50. package/dist/vendor/tunnel-client/config.d.ts +11 -3
  51. package/dist/vendor/tunnel-client/config.js +11 -3
  52. package/dist/vendor/tunnel-client/types.d.ts +26 -1
  53. package/dist/vendor/tunnel-client/types.js +1 -0
  54. package/dist/vendor/tunnel-client/ws-compat.d.ts +3 -2
  55. package/dist/vendor/tunnel-client/ws-compat.js +3 -2
  56. package/dist/version.d.ts +1 -1
  57. package/dist/version.js +1 -1
  58. package/package.json +2 -1
  59. package/skills/testsprite-onboard.skill.md +74 -59
  60. package/skills/testsprite-verify.codex.md +82 -97
  61. package/skills/testsprite-verify.skill.md +64 -15
@@ -34,14 +34,24 @@ import { ApiError } from './errors.js';
34
34
  // the rest of `127.0.0.0/8`, which `--local` cannot dial by name at all. See
35
35
  // `'loopback-other'` below for that distinction.
36
36
  const LOCAL_DEV_RUNTIME_HINT = "This looks like a local-dev target. Run it with `testsprite test run <test-id> --local <port>` instead — it tunnels this machine's loopback address to the test runner (frontend tests only; requires an API key with the `run:tunnel` scope).";
37
- // Project/test creation happens before the caller necessarily has a test id
38
- // (and project create happens before they have a project at all). Lead those
39
- // callers through the required public project URL first, then name the per-run
40
- // loopback tunnel only after the test exists.
41
- const LOCAL_DEV_BOOTSTRAP_HINT = "TestSprite executes tests from the cloud, so a project's URL must be an internet-reachable address the runner can use. " +
42
- 'Set the project to its deployed or staging URL. ' +
43
- 'After a test exists, target an app on this machine for an individual run with ' +
44
- "`testsprite test run <test-id> --local <port>`; that tunnel is per-run and does not make localhost the project's URL.";
37
+ // Creation-time callers that take a per-run target (`test create
38
+ // --target-url`, which only applies with `--run`). A project or environment
39
+ // that names an app on this machine is created with `--local <port>`, so this
40
+ // says that instead of sending the reader after a public URL they may not have.
41
+ // It also serves the bind-all addresses (`0.0.0.0` / `::`), which are not
42
+ // loopback and are refused everywhere.
43
+ const LOCAL_DEV_BOOTSTRAP_HINT = 'TestSprite executes tests from the cloud, so the runner needs an address it can reach: ' +
44
+ 'a deployed or staging URL. For an app that only runs on this machine, create the project ' +
45
+ 'or environment with `--local <port>` instead of a URL, then run with ' +
46
+ '`testsprite test run <test-id> --local <port>`.';
47
+ /**
48
+ * `0.0.0.0` / `::` bind every interface, so an app listening there IS reachable
49
+ * on loopback — but the address itself is not loopback and the server refuses
50
+ * it as a stored URL. Naming the remedy is the whole point of the message.
51
+ */
52
+ const UNSPECIFIED_ADDRESS_REASON = 'the unspecified address (0.0.0.0 / ::) is not allowed — use 127.0.0.1 or ::1 instead';
53
+ const LOCAL_PROJECT_CREATE_HINT = 'Use --local <port> instead of --url for an app on this machine. ' +
54
+ 'Local projects are frontend-only and require the V3 project platform.';
45
55
  const DEFAULT_TARGET_URL_CALLER = {
46
56
  field: 'target-url',
47
57
  helpCommand: 'testsprite test run',
@@ -97,6 +107,8 @@ export function assertNotLocal(rawUrl, caller = DEFAULT_TARGET_URL_CALLER) {
97
107
  }
98
108
  }
99
109
  function localDevHintFor(hintContext) {
110
+ if (hintContext === 'local-project-create')
111
+ return LOCAL_PROJECT_CREATE_HINT;
100
112
  return hintContext === 'bootstrap' ? LOCAL_DEV_BOOTSTRAP_HINT : LOCAL_DEV_RUNTIME_HINT;
101
113
  }
102
114
  /**
@@ -147,9 +159,15 @@ export function disallowedIpReason(host) {
147
159
  return addressFamily === 4 ? disallowedIpv4Reason(bare) : undefined;
148
160
  }
149
161
  function disallowedIpv4Reason(address) {
150
- // Unspecified — binds every interface, including loopback.
162
+ // Unspecified — binds every interface, including loopback. Named precisely
163
+ // rather than lumped in as "localhost" (DEV-1305): since a loopback URL is
164
+ // now a legitimate STORED target, calling `0.0.0.0` a localhost target
165
+ // produced a self-contradicting refusal — "localhost targets are not
166
+ // allowed" above a hint saying to use `localhost`. It is also simply not
167
+ // loopback: the server refuses it as a stored URL, so the accurate remedy is
168
+ // the loopback address the app is also reachable on.
151
169
  if (address === '0.0.0.0') {
152
- return { reason: 'localhost targets are not allowed', hintKind: 'loopback' };
170
+ return { reason: UNSPECIFIED_ADDRESS_REASON, hintKind: 'loopback' };
153
171
  }
154
172
  // Only the literal 127.0.0.1 is in `--local-host`'s exact accepted set.
155
173
  if (ipv4LoopbackBlocklist.check(address, 'ipv4')) {
@@ -319,8 +337,13 @@ function reclassifyEmbeddedIpv4(address, label) {
319
337
  * it on the way in rather than trusting every caller to.
320
338
  */
321
339
  function disallowedIpv6Reason(inner) {
322
- // Loopback (::1) and unspecified (::).
323
- if (inner === '::1' || inner === '::') {
340
+ // Unspecified (`::`) — see `disallowedIpv4Reason`'s note on why this is named
341
+ // separately from loopback.
342
+ if (inner === '::') {
343
+ return { reason: UNSPECIFIED_ADDRESS_REASON, hintKind: 'loopback' };
344
+ }
345
+ // Loopback (::1).
346
+ if (inner === '::1') {
324
347
  return { reason: 'localhost targets are not allowed', hintKind: 'loopback' };
325
348
  }
326
349
  // Embedded-IPv4 family, checked mapped → NAT64 → compatible. Standard /96
@@ -1,3 +1,4 @@
1
+ import { type RunConflict } from './conflict-reason.js';
1
2
  /**
2
3
  * Max time the flush-before-exit will wait on the beacon POST. Kept short so a
3
4
  * slow/unreachable backend adds at most this to a command's exit (including
@@ -6,13 +7,34 @@
6
7
  */
7
8
  export declare const TELEMETRY_TIMEOUT_MS = 1000;
8
9
  export type TelemetryOutcome = 'success' | 'error' | 'abort';
9
- export interface TelemetryOutcomeInput {
10
+ export interface WaitTimeoutTelemetry {
11
+ reason: 'wait_timeout';
12
+ cancelOutcome?: 'cancelled' | 'already_terminal' | 'failed' | 'skipped';
13
+ }
14
+ export interface TelemetryOutcomeInput extends Partial<WaitTimeoutTelemetry> {
10
15
  /** Leaf command path that ran, e.g. `test run`. Empty → skip (no command). */
11
16
  command: string;
12
17
  outcome: TelemetryOutcome;
13
18
  exitCode: number;
14
19
  /** Stable machine error code (e.g. `VALIDATION_ERROR`) — never a message. */
15
20
  errorCode?: string;
21
+ /**
22
+ * Whether `errorCode` was actually classified by the backend
23
+ * ('server') or fabricated client-side ('client' — a `--wait` timeout
24
+ * conversion, local pre-flight validation, a transport failure, an
25
+ * uncaught exception, …). Without this, a client-fabricated `UNSUPPORTED`
26
+ * (the `--wait` timeout bucket, exit 7) is indistinguishable in telemetry
27
+ * from a genuine backend 501 — see `classifyCliError()` for the exact
28
+ * discriminator.
29
+ */
30
+ errorOrigin?: 'client' | 'server';
31
+ /**
32
+ * The effective timeout deadline (seconds) for an error event caused by a
33
+ * client-side timeout — either a per-request timeout (`RequestTimeoutError`)
34
+ * or a `--wait`/`--timeout` poll deadline surfaced as `UNSUPPORTED`. Absent
35
+ * for every other error.
36
+ */
37
+ timeoutSeconds?: number;
16
38
  durationMs: number;
17
39
  /** Global flags, used to resolve config + fill context. */
18
40
  profile?: string;
@@ -48,12 +70,120 @@ export interface ResolvedTelemetryAuth {
48
70
  apiKey?: string;
49
71
  apiUrl: string;
50
72
  }
73
+ export type CiProvider = 'github' | 'gitlab' | 'circleci' | 'buildkite' | 'other' | 'none';
74
+ export type CiEvent = 'push' | 'pull_request' | 'schedule' | 'workflow_dispatch' | 'other';
75
+ /**
76
+ * Which CI system the process is running under, from the standard env markers
77
+ * each vendor documents. `other` is any CI that only sets the generic `CI`
78
+ * variable; `none` is an interactive/unknown context.
79
+ */
80
+ export declare function detectCiProvider(env: NodeJS.ProcessEnv): CiProvider;
81
+ /**
82
+ * The GitHub Actions event that triggered the workflow, bucketed to a fixed
83
+ * vocabulary (`other` for everything outside it, e.g. `release`). Only
84
+ * meaningful under GitHub; undefined for every other provider and when the
85
+ * runner did not set `GITHUB_EVENT_NAME`.
86
+ */
87
+ export declare function detectCiEvent(env: NodeJS.ProcessEnv, provider: CiProvider): CiEvent | undefined;
88
+ /**
89
+ * Stable, non-reversible grouping key for a CI repository: the first 16 hex
90
+ * chars of `sha256('testsprite-ci-repo:' + slug)`. The salt prefix keeps the
91
+ * digest from matching a plain `sha256(slug)` lookup table; the slug itself
92
+ * never leaves the runner.
93
+ */
94
+ export declare function hashRepository(slug: string): string;
95
+ /** The CI-context slice of the event (provider always present; the rest only when known). */
96
+ export declare function buildCiContext(env: NodeJS.ProcessEnv): {
97
+ ciProvider: CiProvider;
98
+ ciEvent?: CiEvent;
99
+ repoHash?: string;
100
+ };
101
+ /**
102
+ * Conflict reasons the beacon accepts for `conflictReason` (the backend DTO
103
+ * whitelist-strips anything else). Deliberately a SEPARATE vocabulary from
104
+ * `conflict-reason.ts`'s rendering type: `tunnel-required` is a server reason
105
+ * this CLI has no label for yet, and `not_found` is rendered but not counted.
106
+ */
107
+ export type TelemetryConflictReason = 'in_flight' | 'insufficient_credits' | 'billing_hold' | 'mcp_view_only' | 'local_address' | 'tunnel-required' | 'error';
108
+ /**
109
+ * Low-cardinality facts a command records about its own outcome before it
110
+ * returns, merged into the one event `index.ts` fires. Counts are non-negative
111
+ * integers; enums are closed. Nothing here can carry an id, a URL, or a message.
112
+ */
113
+ export interface TelemetryExtras {
114
+ /** Batch dispatch accounting (`test run --all`, `testlist run`). */
115
+ accepted?: number;
116
+ conflicts?: number;
117
+ deferred?: number;
118
+ skipped?: number;
119
+ /** Terminal verdict counts over the polled runs (`--wait` paths; 0/1 for a single run). */
120
+ passed?: number;
121
+ failed?: number;
122
+ blocked?: number;
123
+ timedOut?: number;
124
+ /** The most frequent conflict reason of the batch. */
125
+ conflictReason?: TelemetryConflictReason;
126
+ /** `ci init` facts. */
127
+ platform?: 'github';
128
+ force?: boolean;
129
+ workflowExisted?: boolean;
130
+ projectResolved?: 'flag' | 'auto';
131
+ }
132
+ /** What callers may hand the sanitizer: the typed shape, or an arbitrary bag it filters. */
133
+ export type TelemetryExtrasInput = TelemetryExtras | Readonly<Record<string, unknown>>;
134
+ /**
135
+ * Keep only the allowlisted keys with a value of the right shape. Pure: this is
136
+ * the single gate both the sink and the event builder run, so a stray field or
137
+ * a mistyped value can never reach the wire from either direction.
138
+ */
139
+ export declare function sanitizeTelemetryExtras(partial: TelemetryExtrasInput): TelemetryExtras;
140
+ /**
141
+ * Record outcome facts for the event this process will fire. Called by a
142
+ * command just before it returns/throws; merged per key, so a later call for
143
+ * the same key wins and different commands' keys never collide. Values outside
144
+ * the allowlist (or of the wrong shape) are dropped silently — a caller can
145
+ * never widen the wire contract from here.
146
+ */
147
+ export declare function recordTelemetryExtras(partial: TelemetryExtrasInput): void;
148
+ /** Return everything recorded so far and clear the sink (one event per process). */
149
+ export declare function takeTelemetryExtras(): TelemetryExtras;
150
+ /**
151
+ * Disjoint verdict counts over a set of polled runs — `passed` + `failed` +
152
+ * `blocked` + `timedOut` = total. Unlike the CI summary's `failed` (which folds
153
+ * blocked in), `blocked` is split out here because it is the signal that
154
+ * separates a broken target from a failing assertion.
155
+ */
156
+ export declare function batchOutcomeCounts(results: ReadonlyArray<{
157
+ status: string;
158
+ }>): {
159
+ passed: number;
160
+ failed: number;
161
+ blocked: number;
162
+ timedOut: number;
163
+ };
164
+ /**
165
+ * Record a batch command's dispatch accounting (and, under `--wait`, its
166
+ * verdict counts) in one call, so `test run --all` and `testlist run` cannot
167
+ * drift in how they count. `conflictReason` is the dominant reason of the
168
+ * batch; `results` are the polled runs (omit for a non-wait dispatch).
169
+ */
170
+ export declare function recordBatchOutcome(batch: {
171
+ accepted: number;
172
+ conflicts: readonly RunConflict[];
173
+ deferred: number;
174
+ skipped: number;
175
+ results?: ReadonlyArray<{
176
+ status: string;
177
+ }>;
178
+ }): void;
51
179
  /** The exact wire body — a flat allowlist mirroring the backend DTO. */
52
- export interface TelemetryEvent {
180
+ export interface TelemetryEvent extends Partial<WaitTimeoutTelemetry>, TelemetryExtras {
53
181
  command: string;
54
182
  outcome: TelemetryOutcome;
55
183
  exitCode?: number;
56
184
  errorCode?: string;
185
+ errorOrigin?: 'client' | 'server';
186
+ timeoutSeconds?: number;
57
187
  durationMs?: number;
58
188
  cliVersion?: string;
59
189
  os?: string;
@@ -62,21 +192,61 @@ export interface TelemetryEvent {
62
192
  ci?: boolean;
63
193
  /** Present (always `true`) only for a `test run --local` invocation; absent otherwise. */
64
194
  local?: boolean;
195
+ /** The validated `TESTSPRITE_CLIENT` tag (e.g. `github-action/v1`); absent when unset/invalid. */
196
+ client?: string;
197
+ ciProvider?: CiProvider;
198
+ ciEvent?: CiEvent;
199
+ repoHash?: string;
65
200
  }
66
201
  /**
67
202
  * Map a thrown CLI error to its telemetry disposition — mirrors the exit-code
68
203
  * mapping in `index.ts`'s top-level catch so telemetry and the process exit
69
204
  * code never disagree. Pure; safe to unit-test.
205
+ *
206
+ * Every branch below now fills `errorCode` — the two fallback branches
207
+ * (`CLIError`, and the final catch-all for a non-CLIError throw) previously
208
+ * left it `undefined`, which is why a meaningful share of `test run`
209
+ * invocations and `setup` failures landed in telemetry with `errorCode=None`.
210
+ *
211
+ * `errorOrigin` ('client' | 'server') resolves a real UNSUPPORTED
212
+ * misclassification: prod data showed genuine backend 501 UNSUPPORTED
213
+ * responses are rare compared to the PostHog events carrying that code,
214
+ * because every `--wait` timeout is ALSO converted to a client-fabricated
215
+ * `UNSUPPORTED`/exit-7 envelope (exit 7 is the timeout bucket by design;
216
+ * this patch does not touch that contract, only observability).
217
+ *
218
+ * Discriminator: for an `ApiError`, `err.httpStatus !== undefined` iff the
219
+ * code came from a real HTTP response (http.ts passes `response.status` into
220
+ * `ApiError.fromEnvelope` on every non-OK response it parses) — every
221
+ * CLI-fabricated envelope (`localValidationError`, `ApiError.authRequired()`,
222
+ * `TransportError`, and every `throw ApiError.fromEnvelope({... requestId:
223
+ * 'local' ...})` timeout conversion in commands/test.ts) never passes an
224
+ * `httpStatus`, so it reads `undefined`. `requestId` was considered and
225
+ * REJECTED as the discriminator: http.ts stamps a real, non-'local'
226
+ * `x-request-id` (`newRequestId()`) on every OUTGOING request before it knows
227
+ * whether a response will ever arrive, so `TransportError` (a DNS/TLS
228
+ * failure that never got a response) carries a real requestId despite being
229
+ * 100% client-side — `requestId === 'local'` would have misclassified it as
230
+ * 'server'. All non-`ApiError` branches (`RequestTimeoutError`,
231
+ * `InterruptError`, a plain `CLIError`, `CommanderError`, and a genuinely
232
+ * uncaught exception) never involve an HTTP response at all, so they are
233
+ * unconditionally 'client'.
70
234
  */
71
235
  export declare function classifyCliError(err: unknown): {
72
236
  outcome: TelemetryOutcome;
73
237
  exitCode: number;
74
238
  errorCode?: string;
239
+ errorOrigin?: 'client' | 'server';
240
+ timeoutSeconds?: number;
75
241
  };
76
242
  /** True when the operator has opted out via either supported env var. */
77
243
  export declare function isTelemetryOptedOut(env: NodeJS.ProcessEnv): boolean;
78
- /** Assemble the allowlisted wire event. No URL / message / flag value ever. */
79
- export declare function buildTelemetryEvent(input: TelemetryOutcomeInput, env: NodeJS.ProcessEnv, isTTY: boolean): TelemetryEvent;
244
+ /**
245
+ * Assemble the allowlisted wire event. No URL / message / flag value ever.
246
+ * `extras` are the per-command facts the sink collected (re-sanitized here so a
247
+ * direct caller cannot bypass the allowlist).
248
+ */
249
+ export declare function buildTelemetryEvent(input: TelemetryOutcomeInput, env: NodeJS.ProcessEnv, isTTY: boolean, extras?: TelemetryExtrasInput): TelemetryEvent;
80
250
  /**
81
251
  * Resolve just the (apiKey, apiUrl) pair telemetry needs. Called once from the
82
252
  * `index.ts` preAction hook — BEFORE the command's own action runs — so a