@specific.dev/spectest 0.4.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.
- package/package.json +38 -0
- package/src/browser.ts +824 -0
- package/src/components/index.ts +32 -0
- package/src/components/k3s.ts +1324 -0
- package/src/components/postgres.ts +281 -0
- package/src/components/replayFake.ts +515 -0
- package/src/daemon.ts +3910 -0
- package/src/index.ts +1601 -0
- package/src/ingress.ts +288 -0
- package/src/inspect.ts +604 -0
- package/src/record-secrets.ts +41 -0
- package/src/recorder.ts +659 -0
- package/src/resolver.ts +351 -0
- package/src/terminal.ts +740 -0
- package/src/vendor/rrweb-plugin-console-record.umd.js +520 -0
- package/src/vendor/rrweb-record.min.js +5 -0
package/src/recorder.ts
ADDED
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
// Per-test event recorder. The daemon installs a fresh recorder before
|
|
2
|
+
// running a test case and drains it afterwards. While a recorder is
|
|
3
|
+
// active, instrumented sites (the SDK's `expect`, the daemon's wrapped
|
|
4
|
+
// `ctx.exec`, and the test-scoped `fetch` wrapper) push structured
|
|
5
|
+
// events into it.
|
|
6
|
+
//
|
|
7
|
+
// When no test is running (production code paths, eval, etc.) the
|
|
8
|
+
// current recorder is `null` and the `record*` helpers are no-ops, so
|
|
9
|
+
// callers can invoke them unconditionally.
|
|
10
|
+
|
|
11
|
+
import { clearPendingNullish } from "./inspect.js";
|
|
12
|
+
|
|
13
|
+
const OUTPUT_SNIPPET_BYTES = 256 * 1024;
|
|
14
|
+
|
|
15
|
+
export type TestEvent =
|
|
16
|
+
| ExecEvent
|
|
17
|
+
| AssertionEvent
|
|
18
|
+
| HttpEvent
|
|
19
|
+
| KubeEvent
|
|
20
|
+
| BrowserEvent
|
|
21
|
+
| DbEvent
|
|
22
|
+
| TerminalEvent
|
|
23
|
+
| TerminalStepEvent
|
|
24
|
+
| WaitEvent
|
|
25
|
+
| FakeEvent
|
|
26
|
+
| EnvEvent;
|
|
27
|
+
|
|
28
|
+
interface BaseEvent {
|
|
29
|
+
/** Order of *start* within the test. Reserved when an op begins (see
|
|
30
|
+
* `reserveEvent`), so an op whose nested children finish — and record —
|
|
31
|
+
* before it does still sorts ahead of them. Ops that don't reserve get
|
|
32
|
+
* their seq at record (= finish) time. */
|
|
33
|
+
seq: number;
|
|
34
|
+
/** Milliseconds since the recorder started, captured when the op began
|
|
35
|
+
* (reserved) or, absent a reservation, when it was recorded. */
|
|
36
|
+
tOffsetMs: number;
|
|
37
|
+
/**
|
|
38
|
+
* Optional grouping pointer. When set, this event was emitted inside
|
|
39
|
+
* a larger logical step (e.g. an HTTP call that fed the predicate of
|
|
40
|
+
* a `ctx.poll(...)`) and should render nested under the parent in
|
|
41
|
+
* timelines. The parent event is identified by its `seq`.
|
|
42
|
+
*/
|
|
43
|
+
parentSeq?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ExecEvent extends BaseEvent {
|
|
47
|
+
kind: "exec";
|
|
48
|
+
service: string;
|
|
49
|
+
command: string;
|
|
50
|
+
exitCode: number;
|
|
51
|
+
/** stdout truncated to OUTPUT_SNIPPET_BYTES. */
|
|
52
|
+
stdout: string;
|
|
53
|
+
stdoutTruncated: boolean;
|
|
54
|
+
stderr: string;
|
|
55
|
+
stderrTruncated: boolean;
|
|
56
|
+
durationMs: number;
|
|
57
|
+
/**
|
|
58
|
+
* Links this exec to the asciicast `TerminalSessionRecord` of its run
|
|
59
|
+
* (mirroring `TerminalEvent.sessionId`): one exec step covers one full
|
|
60
|
+
* CLI run, so the whole recording belongs to this event. Absent on
|
|
61
|
+
* events recorded before exec runs were captured.
|
|
62
|
+
*/
|
|
63
|
+
sessionId?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface AssertionEvent extends BaseEvent {
|
|
67
|
+
kind: "assertion";
|
|
68
|
+
/** Matcher name, e.g. "toBe", "toEqual". */
|
|
69
|
+
matcher: string;
|
|
70
|
+
/** Whether this was `expect(x).not.toBe(...)`. */
|
|
71
|
+
negated: boolean;
|
|
72
|
+
passed: boolean;
|
|
73
|
+
/** Best-effort JSON-safe serialization. */
|
|
74
|
+
actual: unknown;
|
|
75
|
+
expected?: unknown;
|
|
76
|
+
/** Failure message produced by the matcher (only when `passed` is false). */
|
|
77
|
+
error?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Author-supplied label for a raw assertion made via
|
|
80
|
+
* `expectRaw(message, …)`. Renders as the assertion's summary
|
|
81
|
+
* ("ASSERT <message>") and serves as its diff-alignment key, since a raw
|
|
82
|
+
* assertion has no provenance `path`. Absent for provenance-linked
|
|
83
|
+
* `expect(...)` assertions.
|
|
84
|
+
*/
|
|
85
|
+
message?: string;
|
|
86
|
+
/**
|
|
87
|
+
* `seq` of the op (http / db / browser) whose return value this
|
|
88
|
+
* assertion drilled into. Set when `expect()` received a value wrapped
|
|
89
|
+
* by `inspect.wrap()`. The UI nests assertions with `sourceSeq` under
|
|
90
|
+
* the matching op; assertions without it render at top level.
|
|
91
|
+
*/
|
|
92
|
+
sourceSeq?: number;
|
|
93
|
+
/** JSON-path of property accesses from the op return value down to
|
|
94
|
+
* the asserted value (e.g. `["body", "user", "name"]`). Empty for
|
|
95
|
+
* direct asserts on the op return itself. */
|
|
96
|
+
path?: string[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface DbEvent extends BaseEvent {
|
|
100
|
+
kind: "db";
|
|
101
|
+
/** Service the query ran against (the key in `environment.services`). */
|
|
102
|
+
service: string;
|
|
103
|
+
/** SQL text. For tagged-template calls, values appear as `$1`, `$2`, … */
|
|
104
|
+
query: string;
|
|
105
|
+
/** Parameter values. Best-effort JSON-safe; large/binary values stringified. */
|
|
106
|
+
params?: unknown[];
|
|
107
|
+
/** Rows returned, when known. */
|
|
108
|
+
rowCount?: number;
|
|
109
|
+
/** Captured rows for table rendering in the web UI. Capped at
|
|
110
|
+
* MAX_DB_ROWS; `rowsTruncated` is set when there were more. */
|
|
111
|
+
rows?: unknown[];
|
|
112
|
+
rowsTruncated?: boolean;
|
|
113
|
+
/** Column names in the order Bun.SQL returned them. Derived from the
|
|
114
|
+
* first captured row, so omitted when no rows. */
|
|
115
|
+
columns?: string[];
|
|
116
|
+
durationMs: number;
|
|
117
|
+
/** Set if the driver threw. */
|
|
118
|
+
error?: string;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface HttpEvent extends BaseEvent {
|
|
122
|
+
kind: "http";
|
|
123
|
+
method: string;
|
|
124
|
+
url: string;
|
|
125
|
+
/** Truncated to OUTPUT_SNIPPET_BYTES. Only set for non-binary text bodies. */
|
|
126
|
+
requestBody?: string;
|
|
127
|
+
requestBodyTruncated?: boolean;
|
|
128
|
+
status?: number;
|
|
129
|
+
responseBody?: string;
|
|
130
|
+
responseBodyTruncated?: boolean;
|
|
131
|
+
durationMs: number;
|
|
132
|
+
/** Set if the request threw (network error, abort, etc.). */
|
|
133
|
+
error?: string;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A Kubernetes API call (from `ctx.svc.<k3s>.client.*` / `apply(...)`).
|
|
138
|
+
*
|
|
139
|
+
* The k3s component routes the kube client through `globalThis.fetch`, so
|
|
140
|
+
* the daemon's fetch wrapper first records it as a plain `http` event;
|
|
141
|
+
* the component then parses the request path and *reclassifies* that event
|
|
142
|
+
* into this richer shape via `recorderAnnotate`, so the UI can show
|
|
143
|
+
* `verb resource/name` (e.g. `list pods · default`) instead of an opaque
|
|
144
|
+
* `GET https://k8s.internal:6443/api/v1/...`. The underlying HTTP fields
|
|
145
|
+
* are retained so the request/response bodies (the actual K8s objects)
|
|
146
|
+
* still render in the detail view, and `expect(...)` on the returned
|
|
147
|
+
* object nests under this event exactly as it does for `http`.
|
|
148
|
+
*/
|
|
149
|
+
export interface KubeEvent extends BaseEvent {
|
|
150
|
+
kind: "kube";
|
|
151
|
+
/** API verb derived from the HTTP method + path shape: `get` | `list` |
|
|
152
|
+
* `watch` | `create` | `update` | `patch` | `delete` |
|
|
153
|
+
* `deletecollection`. */
|
|
154
|
+
verb: string;
|
|
155
|
+
/** API group — `""` for the core group (`/api/v1`), else e.g. `"apps"`,
|
|
156
|
+
* `"networking.k8s.io"`. */
|
|
157
|
+
group?: string;
|
|
158
|
+
/** API version, e.g. `"v1"`. */
|
|
159
|
+
apiVersion?: string;
|
|
160
|
+
/** Resource plural, e.g. `"pods"`, `"deployments"`, `"ingresses"`. */
|
|
161
|
+
resource?: string;
|
|
162
|
+
/** Subresource, when addressed, e.g. `"status"`, `"scale"`, `"log"`. */
|
|
163
|
+
subresource?: string;
|
|
164
|
+
/** Object name, when the request targets a single object. */
|
|
165
|
+
name?: string;
|
|
166
|
+
/** Namespace, when namespaced. Absent for cluster-scoped requests. */
|
|
167
|
+
namespace?: string;
|
|
168
|
+
// Underlying HTTP fields, retained for drill-down (mirror HttpEvent).
|
|
169
|
+
method: string;
|
|
170
|
+
url: string;
|
|
171
|
+
requestBody?: string;
|
|
172
|
+
requestBodyTruncated?: boolean;
|
|
173
|
+
status?: number;
|
|
174
|
+
responseBody?: string;
|
|
175
|
+
responseBodyTruncated?: boolean;
|
|
176
|
+
durationMs: number;
|
|
177
|
+
error?: string;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* One call to a fake's helper function (`ctx.fakes.<name>.<fn>(...)`).
|
|
182
|
+
* Helpers are user-authored functions that read or mutate the fake's
|
|
183
|
+
* private state, so this is the only window into what a test asked the
|
|
184
|
+
* fake — distinct from the `http` events the *app under test* generates
|
|
185
|
+
* when it actually calls the fake's endpoints.
|
|
186
|
+
*
|
|
187
|
+
* The return value is `wrap()`ped against this event's seq, so a later
|
|
188
|
+
* `expect(...)` that drills into it nests under this step in the
|
|
189
|
+
* timeline (same mechanism as http/db/exec).
|
|
190
|
+
*/
|
|
191
|
+
export interface FakeEvent extends BaseEvent {
|
|
192
|
+
kind: "fake";
|
|
193
|
+
/** Fake name — the key in `ctx.fakes`. */
|
|
194
|
+
fake: string;
|
|
195
|
+
/** Helper function called, e.g. `"lastCharge"`. */
|
|
196
|
+
member: string;
|
|
197
|
+
/** Call arguments (best-effort JSON-safe). */
|
|
198
|
+
args?: unknown[];
|
|
199
|
+
/** Return value (best-effort JSON-safe). Omitted when the helper threw
|
|
200
|
+
* or returned `undefined`. */
|
|
201
|
+
result?: unknown;
|
|
202
|
+
durationMs: number;
|
|
203
|
+
/** Set if the helper threw. */
|
|
204
|
+
error?: string;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* A runtime mutation of the environment — `ctx.startService` /
|
|
209
|
+
* `ctx.stopService` / `ctx.dnsName`, whether called from a test or from a
|
|
210
|
+
* fake handler reacting to the app under test. Distinct from the `http`
|
|
211
|
+
* event the app generates when it calls the fake: this is the
|
|
212
|
+
* infrastructure the fake (or test) created in response. No-op outside a
|
|
213
|
+
* running test (bootstrap / project-setup / eval), same as every record*.
|
|
214
|
+
*/
|
|
215
|
+
export interface EnvEvent extends BaseEvent {
|
|
216
|
+
kind: "env";
|
|
217
|
+
/** Which environment primitive ran. */
|
|
218
|
+
op: "startService" | "stopService" | "dnsName";
|
|
219
|
+
/** Service name (startService / stopService). */
|
|
220
|
+
service?: string;
|
|
221
|
+
/** Image reference started (startService). */
|
|
222
|
+
image?: string;
|
|
223
|
+
/** Resolved IP — the new container's (startService) or the target's (dnsName). */
|
|
224
|
+
ip?: string;
|
|
225
|
+
/** Hostname registered (dnsName). */
|
|
226
|
+
hostname?: string;
|
|
227
|
+
durationMs: number;
|
|
228
|
+
/** Set if the op threw. */
|
|
229
|
+
error?: string;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export type BrowserAction =
|
|
233
|
+
| "navigate"
|
|
234
|
+
| "evaluate"
|
|
235
|
+
| "waitFor"
|
|
236
|
+
| "click"
|
|
237
|
+
| "type"
|
|
238
|
+
| "press"
|
|
239
|
+
| "scroll"
|
|
240
|
+
| "scrollTo"
|
|
241
|
+
| "back"
|
|
242
|
+
| "forward"
|
|
243
|
+
| "reload"
|
|
244
|
+
| "screenshot";
|
|
245
|
+
|
|
246
|
+
export interface BrowserEvent extends BaseEvent {
|
|
247
|
+
kind: "browser";
|
|
248
|
+
action: BrowserAction;
|
|
249
|
+
/** Target URL (navigate). */
|
|
250
|
+
url?: string;
|
|
251
|
+
/** CSS selector (click, scrollTo). */
|
|
252
|
+
selector?: string;
|
|
253
|
+
/** Human-readable label for `evaluate` ops. */
|
|
254
|
+
description?: string;
|
|
255
|
+
/** Evaluated script, truncated. */
|
|
256
|
+
script?: string;
|
|
257
|
+
scriptTruncated?: boolean;
|
|
258
|
+
/** Typed text, truncated. */
|
|
259
|
+
text?: string;
|
|
260
|
+
textTruncated?: boolean;
|
|
261
|
+
/** Key name (press). */
|
|
262
|
+
key?: string;
|
|
263
|
+
/** Scroll/click coordinates. */
|
|
264
|
+
dx?: number;
|
|
265
|
+
dy?: number;
|
|
266
|
+
x?: number;
|
|
267
|
+
y?: number;
|
|
268
|
+
/** Screenshot image format. */
|
|
269
|
+
format?: string;
|
|
270
|
+
/** For `waitFor`: how many times the predicate was polled. */
|
|
271
|
+
attempts?: number;
|
|
272
|
+
durationMs: number;
|
|
273
|
+
error?: string;
|
|
274
|
+
/**
|
|
275
|
+
* Session this op belonged to. Set whenever the Browser was opened
|
|
276
|
+
* with a `BrowserSessionRecorder` attached (the daemon always does).
|
|
277
|
+
*/
|
|
278
|
+
sessionId?: string;
|
|
279
|
+
/**
|
|
280
|
+
* Wall-clock `Date.now()` captured at the moment this op *finished*.
|
|
281
|
+
* Lives in the same time base as rrweb's `event.timestamp` fields,
|
|
282
|
+
* so the dashboard can seek the player by computing
|
|
283
|
+
* `event.timestamp - events[0].timestamp`. Using the post-op time
|
|
284
|
+
* (rather than op start) means clicking a "type 'foo'" step lands
|
|
285
|
+
* on the frame where the field already shows the text.
|
|
286
|
+
*/
|
|
287
|
+
sessionTimestamp?: number;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Inline event for a single `ctx.terminal(...)` call. The heavy
|
|
292
|
+
* asciicast frames live separately on a `TerminalSessionRecord`
|
|
293
|
+
* (mirroring how `BrowserEvent` points at a `BrowserSessionRecord`).
|
|
294
|
+
* The UI shows this row in the step list and seeks the player to
|
|
295
|
+
* the session's start when clicked.
|
|
296
|
+
*/
|
|
297
|
+
export interface TerminalEvent extends BaseEvent {
|
|
298
|
+
kind: "terminal";
|
|
299
|
+
service: string;
|
|
300
|
+
command: string;
|
|
301
|
+
exitCode: number;
|
|
302
|
+
durationMs: number;
|
|
303
|
+
/** Links this event to its TerminalSessionRecord. */
|
|
304
|
+
sessionId: string;
|
|
305
|
+
/** PTY size used for the run. */
|
|
306
|
+
cols: number;
|
|
307
|
+
rows: number;
|
|
308
|
+
/** First N bytes of decoded output, for tooltip/inline preview. */
|
|
309
|
+
outputPreview: string;
|
|
310
|
+
outputTruncated: boolean;
|
|
311
|
+
/** Set if spawning the PTY itself failed. */
|
|
312
|
+
error?: string;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Action taken on an open interactive terminal session. */
|
|
316
|
+
export type TerminalStepAction =
|
|
317
|
+
| "send"
|
|
318
|
+
| "sendLine"
|
|
319
|
+
| "press"
|
|
320
|
+
| "waitFor"
|
|
321
|
+
| "exit"
|
|
322
|
+
| "close";
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* One operation on an open interactive terminal — analogous to
|
|
326
|
+
* `BrowserEvent`. The session's heavy asciicast frames live on the
|
|
327
|
+
* `TerminalSessionRecord`; this event just carries metadata so the
|
|
328
|
+
* step shows up in the timeline and the UI can seek the player.
|
|
329
|
+
*/
|
|
330
|
+
export interface TerminalStepEvent extends BaseEvent {
|
|
331
|
+
kind: "terminal-step";
|
|
332
|
+
/** Links this event back to its TerminalSessionRecord. */
|
|
333
|
+
sessionId: string;
|
|
334
|
+
/** Service the terminal is attached to (mirrors TerminalEvent.service). */
|
|
335
|
+
service: string;
|
|
336
|
+
/** Which Terminal method produced this event. */
|
|
337
|
+
action: TerminalStepAction;
|
|
338
|
+
/** Bytes sent (send/sendLine), truncated. */
|
|
339
|
+
text?: string;
|
|
340
|
+
textTruncated?: boolean;
|
|
341
|
+
/** Key name passed to `press`. */
|
|
342
|
+
key?: string;
|
|
343
|
+
/** Human label passed to `waitFor`. */
|
|
344
|
+
description?: string;
|
|
345
|
+
/** How many polls `waitFor` made. */
|
|
346
|
+
attempts?: number;
|
|
347
|
+
/** Whether `waitFor` matched (false means it timed out). */
|
|
348
|
+
matched?: boolean;
|
|
349
|
+
/** Exit code observed on `close`. */
|
|
350
|
+
exitCode?: number;
|
|
351
|
+
/** Rendered screen at the moment of the op (post-action), truncated. */
|
|
352
|
+
screenPreview: string;
|
|
353
|
+
screenTruncated: boolean;
|
|
354
|
+
/**
|
|
355
|
+
* Player-relative offset, in seconds, captured against the *same*
|
|
356
|
+
* clock the asciicast frames use (the terminal factory's spawn
|
|
357
|
+
* time). The UI seeks the asciinema-player to this value when the
|
|
358
|
+
* step is clicked. Persisting it directly avoids ever subtracting
|
|
359
|
+
* `step.tOffsetMs - session.openedAtMs` on the front-end, which is
|
|
360
|
+
* fragile because the two timestamps live in slightly different
|
|
361
|
+
* Date.now() frames (recorder vs. spawn).
|
|
362
|
+
*/
|
|
363
|
+
castTimeSec: number;
|
|
364
|
+
durationMs: number;
|
|
365
|
+
error?: string;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** An ordering slot claimed at op *start* and handed back to the matching
|
|
369
|
+
* `record*` call at op finish. Lets a triggering op (an `exec`/`fetch` that
|
|
370
|
+
* reaches the app, which calls a fake, which calls `ctx.startService`) keep a
|
|
371
|
+
* lower seq than the nested events it sets off — even though those nested
|
|
372
|
+
* events finish, and record, first. */
|
|
373
|
+
export interface EventReservation {
|
|
374
|
+
seq: number;
|
|
375
|
+
tOffsetMs: number;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
class Recorder {
|
|
379
|
+
private events: TestEvent[] = [];
|
|
380
|
+
private seq = 0;
|
|
381
|
+
private readonly start = Date.now();
|
|
382
|
+
|
|
383
|
+
/** Claim a seq + start offset now; pass the result to `push` at finish. */
|
|
384
|
+
reserve(): EventReservation {
|
|
385
|
+
return { seq: this.seq++, tOffsetMs: Date.now() - this.start };
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
push(
|
|
389
|
+
ev: Omit<TestEvent, "seq" | "tOffsetMs">,
|
|
390
|
+
reservation?: EventReservation,
|
|
391
|
+
): number {
|
|
392
|
+
// A new op invalidates any pending nullish-leaf note: a note must not
|
|
393
|
+
// survive past the op it belongs to and get adopted by a later, unrelated
|
|
394
|
+
// `expect`. The assertion event itself is exempt — its `expect()` already
|
|
395
|
+
// adopted (and consumed) the note before recording.
|
|
396
|
+
if (ev.kind !== "assertion") clearPendingNullish();
|
|
397
|
+
const seq = reservation?.seq ?? this.seq++;
|
|
398
|
+
this.events.push({
|
|
399
|
+
...ev,
|
|
400
|
+
seq,
|
|
401
|
+
tOffsetMs: reservation?.tOffsetMs ?? Date.now() - this.start,
|
|
402
|
+
} as TestEvent);
|
|
403
|
+
return seq;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
drain(): TestEvent[] {
|
|
407
|
+
return this.events;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** Index of the next event that would be pushed. Use with `truncate`
|
|
411
|
+
* to drop everything pushed since this index. The seq counter does
|
|
412
|
+
* not roll back — discarded seqs leave gaps. */
|
|
413
|
+
eventCount(): number {
|
|
414
|
+
return this.events.length;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Drop events with index >= `toLen`. The seq counter is unaffected
|
|
418
|
+
* (so seqs already handed out remain unique even though the events
|
|
419
|
+
* they named are gone). */
|
|
420
|
+
truncate(toLen: number): void {
|
|
421
|
+
if (toLen < this.events.length) {
|
|
422
|
+
this.events.length = toLen;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** Stamp `parentSeq` onto every event at index >= `startIdx`. Used
|
|
427
|
+
* by `ctx.poll` to group the kept iteration's events under the
|
|
428
|
+
* resulting wait event. */
|
|
429
|
+
markChildren(startIdx: number, parentSeq: number): void {
|
|
430
|
+
for (let i = startIdx; i < this.events.length; i++) {
|
|
431
|
+
const ev = this.events[i]!;
|
|
432
|
+
if (ev.seq === parentSeq) continue;
|
|
433
|
+
ev.parentSeq = parentSeq;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** Shallow-merge `patch` into the already-recorded event with this
|
|
438
|
+
* `seq`. Lets an instrumentation site enrich or reclassify an event
|
|
439
|
+
* after the fact — e.g. the k3s component upgrades the generic `http`
|
|
440
|
+
* event its API call produced into a Kubernetes-specific `kube` event
|
|
441
|
+
* once it has parsed the request. Searches from the end since the
|
|
442
|
+
* target is almost always the most recent event. No-op when no event
|
|
443
|
+
* carries that seq. */
|
|
444
|
+
annotate(seq: number, patch: Record<string, unknown>): void {
|
|
445
|
+
for (let i = this.events.length - 1; i >= 0; i--) {
|
|
446
|
+
const ev = this.events[i]!;
|
|
447
|
+
if (ev.seq === seq) {
|
|
448
|
+
Object.assign(ev, patch);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** Drop the single event carrying this `seq`. Used to retract an event
|
|
455
|
+
* that an instrumentation site recorded but then decided is noise — e.g.
|
|
456
|
+
* the dynamic kube client's internal discovery GET. Caller must ensure
|
|
457
|
+
* nothing references the seq (no child events/assertions hang off it).
|
|
458
|
+
* The seq counter does not roll back, so the seq stays retired. Searches
|
|
459
|
+
* from the end since the target is almost always the most recent event. */
|
|
460
|
+
remove(seq: number): void {
|
|
461
|
+
for (let i = this.events.length - 1; i >= 0; i--) {
|
|
462
|
+
if (this.events[i]!.seq === seq) {
|
|
463
|
+
this.events.splice(i, 1);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
let current: Recorder | null = null;
|
|
471
|
+
|
|
472
|
+
// Counter-based pause: `pauseRecording`/`resumeRecording` nest safely
|
|
473
|
+
// (a nested `ctx.poll` inside another `ctx.poll`'s predicate stays
|
|
474
|
+
// suppressed until its own resume balances out). Active record* sites
|
|
475
|
+
// no-op while `paused > 0`, so http/exec/db/etc. inside a paused
|
|
476
|
+
// region produce no events and the returned seq is `undefined` —
|
|
477
|
+
// which means the daemon's fetch wrapper also skips installing the
|
|
478
|
+
// inspector proxy on the Response.
|
|
479
|
+
let paused = 0;
|
|
480
|
+
|
|
481
|
+
export function pauseRecording(): void {
|
|
482
|
+
paused += 1;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
export function resumeRecording(): void {
|
|
486
|
+
paused = Math.max(0, paused - 1);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function active(): boolean {
|
|
490
|
+
return current !== null && paused === 0;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
export function startRecording(): void {
|
|
494
|
+
current = new Recorder();
|
|
495
|
+
paused = 0;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
export function stopRecording(): TestEvent[] {
|
|
499
|
+
if (!current) return [];
|
|
500
|
+
const evs = current.drain();
|
|
501
|
+
current = null;
|
|
502
|
+
paused = 0;
|
|
503
|
+
return evs;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
export function isRecording(): boolean {
|
|
507
|
+
return current !== null;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** Reserve an ordering slot at the start of an op so nested events it triggers
|
|
511
|
+
* (which finish — and record — first) still sort after it. Hand the returned
|
|
512
|
+
* reservation to the matching `record*` call at op finish. Returns `undefined`
|
|
513
|
+
* when nothing is recording, in which case `record*` falls back to allocating
|
|
514
|
+
* the seq at record time. */
|
|
515
|
+
export function reserveEvent(): EventReservation | undefined {
|
|
516
|
+
return active() ? current!.reserve() : undefined;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
export function recordExec(
|
|
520
|
+
ev: Omit<ExecEvent, "seq" | "tOffsetMs" | "kind">,
|
|
521
|
+
reservation?: EventReservation,
|
|
522
|
+
): number | undefined {
|
|
523
|
+
return active() ? current!.push({ kind: "exec", ...ev }, reservation) : undefined;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
export function recordAssertion(
|
|
527
|
+
ev: Omit<AssertionEvent, "seq" | "tOffsetMs" | "kind">,
|
|
528
|
+
): number | undefined {
|
|
529
|
+
return active() ? current!.push({ kind: "assertion", ...ev }) : undefined;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
export function recordHttp(
|
|
533
|
+
ev: Omit<HttpEvent, "seq" | "tOffsetMs" | "kind">,
|
|
534
|
+
reservation?: EventReservation,
|
|
535
|
+
): number | undefined {
|
|
536
|
+
return active() ? current!.push({ kind: "http", ...ev }, reservation) : undefined;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
export function recordBrowser(
|
|
540
|
+
ev: Omit<BrowserEvent, "seq" | "tOffsetMs" | "kind">,
|
|
541
|
+
reservation?: EventReservation,
|
|
542
|
+
): number | undefined {
|
|
543
|
+
return active() ? current!.push({ kind: "browser", ...ev }, reservation) : undefined;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
export function recordDb(
|
|
547
|
+
ev: Omit<DbEvent, "seq" | "tOffsetMs" | "kind">,
|
|
548
|
+
reservation?: EventReservation,
|
|
549
|
+
): number | undefined {
|
|
550
|
+
return active() ? current!.push({ kind: "db", ...ev }, reservation) : undefined;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
export function recordTerminal(
|
|
554
|
+
ev: Omit<TerminalEvent, "seq" | "tOffsetMs" | "kind">,
|
|
555
|
+
reservation?: EventReservation,
|
|
556
|
+
): number | undefined {
|
|
557
|
+
return active() ? current!.push({ kind: "terminal", ...ev }, reservation) : undefined;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
export function recordTerminalStep(
|
|
561
|
+
ev: Omit<TerminalStepEvent, "seq" | "tOffsetMs" | "kind">,
|
|
562
|
+
reservation?: EventReservation,
|
|
563
|
+
): number | undefined {
|
|
564
|
+
return active() ? current!.push({ kind: "terminal-step", ...ev }, reservation) : undefined;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
export function recordFake(
|
|
568
|
+
ev: Omit<FakeEvent, "seq" | "tOffsetMs" | "kind">,
|
|
569
|
+
reservation?: EventReservation,
|
|
570
|
+
): number | undefined {
|
|
571
|
+
return active() ? current!.push({ kind: "fake", ...ev }, reservation) : undefined;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
export function recordEnv(
|
|
575
|
+
ev: Omit<EnvEvent, "seq" | "tOffsetMs" | "kind">,
|
|
576
|
+
reservation?: EventReservation,
|
|
577
|
+
): number | undefined {
|
|
578
|
+
return active() ? current!.push({ kind: "env", ...ev }, reservation) : undefined;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Recorded once per `ctx.poll(...)` call. Stands in for the suppressed
|
|
583
|
+
* intermediate iterations and gives downstream tagged values
|
|
584
|
+
* (`wrap(polledValue, waitSeq)`) a single, stable event to point at —
|
|
585
|
+
* assertions on those values then link to the wait, not to one of N
|
|
586
|
+
* dropped polls.
|
|
587
|
+
*/
|
|
588
|
+
export interface WaitEvent extends BaseEvent {
|
|
589
|
+
kind: "wait";
|
|
590
|
+
description: string;
|
|
591
|
+
attempts: number;
|
|
592
|
+
durationMs: number;
|
|
593
|
+
passed: boolean;
|
|
594
|
+
/** Set when the predicate threw or the wait timed out. */
|
|
595
|
+
error?: string;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
export function recordWait(
|
|
599
|
+
ev: Omit<WaitEvent, "seq" | "tOffsetMs" | "kind">,
|
|
600
|
+
reservation?: EventReservation,
|
|
601
|
+
): number | undefined {
|
|
602
|
+
// Use `current` directly (not `active()`) so a `recordWait` at the
|
|
603
|
+
// end of a `ctx.poll` block lands even though the surrounding code
|
|
604
|
+
// just resumed from `paused`. Pushing this event is the whole point
|
|
605
|
+
// of the poll primitive.
|
|
606
|
+
return current?.push({ kind: "wait", ...ev }, reservation);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/** Number of events the active recorder has accumulated. Returns 0
|
|
610
|
+
* when no recorder is active. */
|
|
611
|
+
export function recorderEventCount(): number {
|
|
612
|
+
return current?.eventCount() ?? 0;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/** Drop events with index >= `toLen` from the active recorder. */
|
|
616
|
+
export function recorderTruncate(toLen: number): void {
|
|
617
|
+
current?.truncate(toLen);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** Stamp `parentSeq` onto every event from `startIdx` onward, so the
|
|
621
|
+
* UI groups them under the parent in the timeline. */
|
|
622
|
+
export function recorderMarkChildren(
|
|
623
|
+
startIdx: number,
|
|
624
|
+
parentSeq: number,
|
|
625
|
+
): void {
|
|
626
|
+
current?.markChildren(startIdx, parentSeq);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** Shallow-merge `patch` into the active recorder's event with this
|
|
630
|
+
* `seq` (enrich/reclassify after the fact). No-op when nothing is
|
|
631
|
+
* recording or no event carries that seq. */
|
|
632
|
+
export function recorderAnnotate(
|
|
633
|
+
seq: number,
|
|
634
|
+
patch: Record<string, unknown>,
|
|
635
|
+
): void {
|
|
636
|
+
current?.annotate(seq, patch);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/** Retract the active recorder's event with this `seq` (an instrumentation
|
|
640
|
+
* site recorded it, then decided it's noise). No-op when nothing is
|
|
641
|
+
* recording or no event carries that seq. */
|
|
642
|
+
export function recorderRemove(seq: number): void {
|
|
643
|
+
current?.remove(seq);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/** Best-effort JSON-safe deep clone; falls back to `String(v)`. */
|
|
647
|
+
export function safeSerialize(v: unknown): unknown {
|
|
648
|
+
if (v === undefined) return undefined;
|
|
649
|
+
try {
|
|
650
|
+
return JSON.parse(JSON.stringify(v));
|
|
651
|
+
} catch {
|
|
652
|
+
return String(v);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
export function truncateUtf8(s: string): { value: string; truncated: boolean } {
|
|
657
|
+
if (s.length <= OUTPUT_SNIPPET_BYTES) return { value: s, truncated: false };
|
|
658
|
+
return { value: s.slice(0, OUTPUT_SNIPPET_BYTES), truncated: true };
|
|
659
|
+
}
|