@crvouga/mockingbird-service-sentry 1.1.0 → 1.3.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/dist/index.d.ts CHANGED
@@ -10,88 +10,16 @@ interface FetchAPI$1 {
10
10
  fetch(request: Request): Promise<Response>;
11
11
  }
12
12
 
13
- /** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
14
- type SqliteValue$1 = null | number | bigint | string | Uint8Array | boolean;
15
- /** Mutation counters returned by {@link SqliteStatement.run}. */
16
- type SqliteRunResult$1 = {
17
- changes: number;
18
- lastInsertRowid: number | bigint;
13
+ type MountedAPI = FetchAPI & {
14
+ readonly prefix: string;
15
+ GET: FetchHandler;
16
+ POST: FetchHandler;
17
+ PUT: FetchHandler;
18
+ PATCH: FetchHandler;
19
+ DELETE: FetchHandler;
20
+ HEAD: FetchHandler;
21
+ OPTIONS: FetchHandler;
19
22
  };
20
- /**
21
- * Prepared statement bound to a {@link SqliteClient}.
22
- *
23
- * Pass bind values as rest arguments on each call (no sticky `bind()`).
24
- */
25
- interface SqliteStatement$1 {
26
- run(...params: SqliteValue$1[]): SqliteRunResult$1;
27
- all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
28
- get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
29
- }
30
- /**
31
- * Sync SQLite client port owned by Mockingbird.
32
- *
33
- * Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
34
- * `bun:sqlite` instances all work when they expose this surface.
35
- */
36
- interface SqliteClient$1 {
37
- exec(sql: string): void;
38
- prepare(sql: string): SqliteStatement$1;
39
- transaction<T>(fn: () => T): T;
40
- }
41
-
42
- /** Every stored record carries a monotonically increasing sequence for stable ordering. */
43
- type Stored<T> = {
44
- seq: number;
45
- value: T;
46
- };
47
- type ListRecordsOptions<T> = {
48
- /** Keep only records passing the predicate. */
49
- where?: (value: T, seq: number) => boolean;
50
- /** Sort order; default newest first. */
51
- order?: "newest" | "oldest";
52
- };
53
- /**
54
- * A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
55
- * sequence, never by id lexicographic order, so list semantics stay stable.
56
- */
57
- declare class Collection<T> {
58
- private readonly sqlite;
59
- /** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
60
- readonly namespace: string;
61
- /** Table name inside the namespace. Stable across resets of the rows themselves. */
62
- readonly collectionName: string;
63
- constructor(sqlite: SqliteClient$1,
64
- /** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
65
- namespace: string,
66
- /** Table name inside the namespace. Stable across resets of the rows themselves. */
67
- collectionName: string);
68
- private bumpCollectionSeq;
69
- nextSequence(): number;
70
- get(id: string): T | undefined;
71
- has(id: string): boolean;
72
- /** Insert a new record, assigning it the next sequence number. */
73
- insert(id: string, value: T): Stored<T>;
74
- /** Replace an existing record's value, keeping its position. */
75
- update(id: string, value: T): Stored<T> | undefined;
76
- delete(id: string): boolean;
77
- /** How many records the collection holds, without reading them. */
78
- count(): number;
79
- list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
80
- id: string;
81
- }>;
82
- }
83
-
84
- /**
85
- * Sequential id source persisted in SQLite. Ids are deterministic for a given
86
- * sequence history (`cus_` + 14 opaque chars), so reproductions stay stable.
87
- */
88
- declare class IdSequence {
89
- private readonly sqlite;
90
- private readonly namespace;
91
- private readonly salt;
92
- constructor(sqlite: SqliteClient$1, namespace: string, salt?: string);
93
- next(prefix: string, length?: number): string;
94
- }
95
23
 
96
24
  /** A stable identifier for a point in a {@link Timeline}. */
97
25
  type CheckpointId = string;
@@ -182,6 +110,91 @@ declare class Timeline<T> {
182
110
  interface FetchAPI {
183
111
  fetch(request: Request): Promise<Response>;
184
112
  }
113
+ /** A bare function form of {@link FetchAPI}, compatible with `Bun.serve`, workerd, and Deno. */
114
+ type FetchHandler = (request: Request) => Promise<Response>;
115
+
116
+ /** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
117
+ type SqliteValue$1 = null | number | bigint | string | Uint8Array | boolean;
118
+ /** Mutation counters returned by {@link SqliteStatement.run}. */
119
+ type SqliteRunResult$1 = {
120
+ changes: number;
121
+ lastInsertRowid: number | bigint;
122
+ };
123
+ /**
124
+ * Prepared statement bound to a {@link SqliteClient}.
125
+ *
126
+ * Pass bind values as rest arguments on each call (no sticky `bind()`).
127
+ */
128
+ interface SqliteStatement$1 {
129
+ run(...params: SqliteValue$1[]): SqliteRunResult$1;
130
+ all<T = Record<string, unknown>>(...params: SqliteValue$1[]): T[];
131
+ get<T = Record<string, unknown>>(...params: SqliteValue$1[]): T | undefined;
132
+ }
133
+ /**
134
+ * Sync SQLite client port owned by Mockingbird.
135
+ *
136
+ * Duck-typed so `@crvouga/mockingbird-service-sqlite` `Database`, better-sqlite3, and wrapped
137
+ * `bun:sqlite` instances all work when they expose this surface.
138
+ */
139
+ interface SqliteClient$1 {
140
+ exec(sql: string): void;
141
+ prepare(sql: string): SqliteStatement$1;
142
+ transaction<T>(fn: () => T): T;
143
+ }
144
+
145
+ /** Every stored record carries a monotonically increasing sequence for stable ordering. */
146
+ type Stored<T> = {
147
+ seq: number;
148
+ value: T;
149
+ };
150
+ type ListRecordsOptions<T> = {
151
+ /** Keep only records passing the predicate. */
152
+ where?: (value: T, seq: number) => boolean;
153
+ /** Sort order; default newest first. */
154
+ order?: "newest" | "oldest";
155
+ };
156
+ /**
157
+ * A SQLite-backed table of JSON records addressed by id. Ordering is by insertion
158
+ * sequence, never by id lexicographic order, so list semantics stay stable.
159
+ */
160
+ declare class Collection<T> {
161
+ private readonly sqlite;
162
+ /** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
163
+ readonly namespace: string;
164
+ /** Table name inside the namespace. Stable across resets of the rows themselves. */
165
+ readonly collectionName: string;
166
+ constructor(sqlite: SqliteClient$1,
167
+ /** Storage namespace. Admin introspection uses this to ignore another namespace's tables. */
168
+ namespace: string,
169
+ /** Table name inside the namespace. Stable across resets of the rows themselves. */
170
+ collectionName: string);
171
+ private bumpCollectionSeq;
172
+ nextSequence(): number;
173
+ get(id: string): T | undefined;
174
+ has(id: string): boolean;
175
+ /** Insert a new record, assigning it the next sequence number. */
176
+ insert(id: string, value: T): Stored<T>;
177
+ /** Replace an existing record's value, keeping its position. */
178
+ update(id: string, value: T): Stored<T> | undefined;
179
+ delete(id: string): boolean;
180
+ /** How many records the collection holds, without reading them. */
181
+ count(): number;
182
+ list(options?: ListRecordsOptions<T>): Array<Stored<T> & {
183
+ id: string;
184
+ }>;
185
+ }
186
+
187
+ /**
188
+ * Sequential id source persisted in SQLite. Ids are deterministic for a given
189
+ * sequence history (`cus_` + 14 opaque chars), so reproductions stay stable.
190
+ */
191
+ declare class IdSequence {
192
+ private readonly sqlite;
193
+ private readonly namespace;
194
+ private readonly salt;
195
+ constructor(sqlite: SqliteClient$1, namespace: string, salt?: string);
196
+ next(prefix: string, length?: number): string;
197
+ }
185
198
 
186
199
  /** Options every provider constructor accepts. */
187
200
  type APIOptions = {
@@ -215,7 +228,7 @@ type ClockState = {
215
228
  now: number;
216
229
  /** True while time does not advance on its own. */
217
230
  frozen: boolean;
218
- /** Milliseconds this clock adds to its underlying source. */
231
+ /** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
219
232
  offsetMs: number;
220
233
  };
221
234
  type Clock = {
@@ -251,7 +264,10 @@ type Rng = {
251
264
  state(): number;
252
265
  /** Restore a state previously returned by {@link state}. */
253
266
  setState(state: number): void;
254
- seed: number;
267
+ /** Restart the stream from a new seed. `reset` then returns to this seed. */
268
+ reseed(seed: number | string): void;
269
+ /** The seed the stream currently starts from. */
270
+ readonly seed: number;
255
271
  };
256
272
 
257
273
  /**
@@ -260,44 +276,83 @@ type Rng = {
260
276
  * This is how a suite reaches the vendor's failure modes without the vendor: the
261
277
  * quota error that only appears when a shared sandbox is full, the 429 that only
262
278
  * appears under load, the 5xx that proves a retry path works.
279
+ *
280
+ * A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
281
+ * it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
282
+ * and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
263
283
  */
264
284
  type FaultRule = {
265
- /** Stable id, so a suite can retire exactly the rule it added. */
285
+ /** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
266
286
  id: string;
267
287
  /** Fault only this operation. Omit to match every operation. */
268
288
  operationId?: string;
269
289
  /** Fault only this HTTP method, case-insensitive. Omit to match every method. */
270
290
  method?: string;
291
+ /** Fault only this exact path. */
292
+ path?: string;
271
293
  /** Fault only paths starting with this prefix. Omit to match every path. */
272
294
  pathPrefix?: string;
295
+ /** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
296
+ pathPattern?: string;
297
+ /** Fault only requests carrying these headers. `"*"` matches any value. */
298
+ requestHeaders?: Record<string, string>;
299
+ /** Fault only requests carrying these query parameters. `"*"` matches any value. */
300
+ query?: Record<string, string>;
273
301
  /**
274
302
  * Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
275
303
  * an in-process caller usually wants, and what a parallel worker usually does not:
276
304
  * rules added through `POST /__admin/faults` default to the calling namespace.
277
305
  */
278
306
  namespace?: string;
307
+ /** Let this many matching requests through first. */
308
+ after?: number;
309
+ /** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
310
+ every?: number;
311
+ /** Retire the rule after this many faults. Omit to keep it until removed. */
312
+ count?: number;
313
+ /** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
314
+ rate?: number;
315
+ /**
316
+ * Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
317
+ * clock when the rule was added. Listed as epoch ms.
318
+ */
319
+ from?: Instant | Duration;
320
+ /** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
321
+ until?: Instant | Duration;
279
322
  /**
280
- * Status of the injected response. Omit for a rule that only delays (`delayMs` /
281
- * `latencyMs`), only drops the connection (`drop`), or only switches on an `effect`:
282
- * the request then still reaches the service.
323
+ * Status of the injected response. Omit for a rule that only delays, times out, drops,
324
+ * truncates, or switches on an `effect`: the request then still reaches the service.
283
325
  */
284
326
  status?: number;
285
327
  /** Response body, serialized as JSON. A string is sent as-is. */
286
328
  body?: unknown;
329
+ /** Response headers. */
287
330
  headers?: Record<string, string>;
288
- /** Retire the rule after this many faults. Omit to keep it until removed. */
289
- count?: number;
290
- /** Fault this fraction of matching requests, `0`–`1`. Default `1`. */
291
- rate?: number;
292
- /** Hold the response back this long, to exercise timeouts. */
331
+ /** Hold the response back this long. Listed as `delayMs`. */
332
+ delay?: Duration;
333
+ /** `delay`, in milliseconds. */
293
334
  delayMs?: number;
294
335
  /** Alias of `delayMs`. */
295
336
  latencyMs?: number;
337
+ /** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
338
+ jitter?: Duration;
339
+ jitterMs?: number;
340
+ /**
341
+ * Never answer: hold the request this long, then drop the connection. Listed as
342
+ * `timeoutMs`. With virtual time the wait is spent on the mock clock.
343
+ */
344
+ timeout?: Duration;
345
+ timeoutMs?: number;
296
346
  /**
297
347
  * Drop the connection instead of answering: an in-process `fetch` rejects with a
298
348
  * `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
299
349
  */
300
350
  drop?: boolean;
351
+ /**
352
+ * Cut the response body off after this many bytes: the status and headers arrive, then
353
+ * the connection dies mid-body.
354
+ */
355
+ truncate?: number;
301
356
  /**
302
357
  * A named service behaviour to switch on for the matching request instead of (or
303
358
  * before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
@@ -306,8 +361,8 @@ type FaultRule = {
306
361
  effect?: string;
307
362
  /** Parameters for `effect`. */
308
363
  params?: Record<string, unknown>;
309
- /** From the preset this rule was expanded from, if any. */
310
- preset?: string;
364
+ /** The preset this rule was expanded from, if any. */
365
+ fromPreset?: string;
311
366
  };
312
367
  /** A fault that fired for one request. */
313
368
  type FaultHit = {
@@ -315,6 +370,10 @@ type FaultHit = {
315
370
  /** The injected response; absent when the rule only delays, drops, or sets an effect. */
316
371
  response?: Response;
317
372
  drop?: boolean;
373
+ /** Set with `drop` when the rule held the request for its `timeout` first. */
374
+ timeout?: boolean;
375
+ /** Bytes of response body to let through before the connection dies. */
376
+ truncate?: number;
318
377
  effect?: {
319
378
  name: string;
320
379
  params: Record<string, unknown>;
@@ -339,38 +398,200 @@ type FaultCandidate = {
339
398
  method: string;
340
399
  path: string;
341
400
  namespace: string;
401
+ /** Request headers and query, for rules that match on them. */
402
+ headers?: Headers;
403
+ query?: URLSearchParams;
404
+ /** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
405
+ now?: number;
406
+ /** How to spend a delay for this request. Default: the registry's own sleep. */
407
+ wait?: (ms: number) => Promise<void>;
408
+ };
409
+ /** A stored rule and how often it was consulted. */
410
+ type FaultListing = FaultRule & {
411
+ /** Faults left before the rule retires. `null` when it has no `count`. */
412
+ remaining: number | null;
413
+ /** Times the rule faulted a request. */
414
+ hits: number;
415
+ /** Requests that matched the rule, faulted or not. */
416
+ seen: number;
417
+ };
418
+ /** A namespace's own rules, with their counters, in the order they are tried. */
419
+ type FaultSnapshot = {
420
+ /**
421
+ * `position` is how many `"*"` rules are tried before the rule, which is what its place
422
+ * in the order means to a request and holds in any process.
423
+ */
424
+ rules: (FaultListing & {
425
+ position?: number;
426
+ })[];
427
+ /** How many `fault_<n>` ids the namespace has handed out. */
428
+ nextId?: number;
342
429
  };
343
430
  type FaultRegistry = {
344
- isolateNamespaces(): void;
345
- snapshot(namespace: string): {
346
- rules: (FaultRule & {
347
- remaining: number | null;
348
- hits: number;
349
- position: number;
350
- })[];
351
- rngState: number;
352
- };
431
+ snapshot(namespace: string): FaultSnapshot;
432
+ /** Add a rule, or replace the rule with the same id in the same namespace. */
353
433
  add(rule: FaultRule): FaultRule;
354
- list(): (FaultRule & {
355
- remaining: number | null;
356
- hits: number;
357
- })[];
358
- remove(id: string): boolean;
359
- clear(): void;
360
- restore(namespace: string, state: {
361
- rules: (FaultRule & {
362
- remaining: number | null;
363
- hits: number;
364
- position: number;
365
- })[];
366
- rngState: number;
367
- }): void;
434
+ /** The next unused `fault_<n>` id in `namespace`. */
435
+ nextId(namespace: string): string;
436
+ /** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
437
+ list(namespace?: string): FaultListing[];
438
+ get(id: string, namespace?: string): FaultListing | undefined;
439
+ /** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
440
+ remove(id: string, namespace?: string): boolean;
441
+ /** Remove every rule, or one namespace's own rules. */
442
+ clear(namespace?: string): void;
443
+ /** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
444
+ restore(namespace: string, state: FaultSnapshot): void;
368
445
  /**
369
446
  * Every fault this request should get, in rule order, stopping at the first that answers
370
447
  * or drops (effect-only and delay-only rules let later rules match too). Consumes one of
371
448
  * each matching rule's remaining uses.
372
449
  */
373
450
  take(candidate: FaultCandidate): Promise<FaultHit[]>;
451
+ /**
452
+ * Fire these rules at one request without storing them: no matching, no schedule, in order,
453
+ * stopping at the first that answers or drops. Jitter draws from the namespace's stream.
454
+ */
455
+ fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
456
+ };
457
+
458
+ /**
459
+ * The admin controls every mock shares, as one document.
460
+ *
461
+ * The same shape is accepted in three places, and means the same thing in each:
462
+ *
463
+ * - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
464
+ * `MOCKINGBIRD_ADMIN_CONTROLS`
465
+ * - over HTTP: `PUT /__admin/controls`
466
+ * - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
467
+ *
468
+ * Every control is deterministic: given the same seed, clock and requests, a mock answers
469
+ * the same bytes. Nothing here reads the wall clock or `Math.random`.
470
+ */
471
+ /** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
472
+ type Duration = number | string;
473
+ /** Epoch milliseconds, or an ISO-8601 string. */
474
+ type Instant = number | string;
475
+ type ClockControls = {
476
+ /** Move the clock to this instant. */
477
+ now?: Instant;
478
+ /** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
479
+ frozen?: boolean;
480
+ /**
481
+ * Step the clock forward by this much before every vendor request. With a frozen clock
482
+ * each request still gets its own, reproducible timestamp.
483
+ */
484
+ autoAdvance?: Duration;
485
+ };
486
+ /** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
487
+ type LatencyRuleInput = {
488
+ delay?: Duration;
489
+ jitter?: Duration;
490
+ };
491
+ type LatencyControls = LatencyRuleInput & {
492
+ /** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
493
+ operations?: Record<string, Duration | LatencyRuleInput>;
494
+ };
495
+ /** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
496
+ type FaultInput = (Omit<FaultRule, "id"> & {
497
+ id?: string;
498
+ }) | ({
499
+ preset: string;
500
+ } & Partial<FaultRule>);
501
+ /** Identifies a {@link SnapshotDocument}. */
502
+ declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
503
+ /**
504
+ * A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
505
+ * `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
506
+ * `records` are required, so a hand-written seed file is a valid snapshot.
507
+ */
508
+ type SnapshotDocument = {
509
+ format: typeof SNAPSHOT_FORMAT;
510
+ /** The service that wrote it. A snapshot is rejected by any other service. */
511
+ service?: string;
512
+ /**
513
+ * Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
514
+ * the stored text of a row that is not a plain record, and replaces `value` when present.
515
+ */
516
+ records: {
517
+ collection: string;
518
+ id: string;
519
+ seq?: number;
520
+ value?: unknown;
521
+ raw?: string;
522
+ }[];
523
+ /** Id counters, so the next generated id continues where the snapshot left off. */
524
+ sequences?: {
525
+ name: string;
526
+ kind: string;
527
+ value: number;
528
+ }[];
529
+ clock?: {
530
+ now: number;
531
+ frozen: boolean;
532
+ autoAdvanceMs?: number;
533
+ };
534
+ seed?: number | string;
535
+ /** Positions of the seeded random streams. */
536
+ rng?: {
537
+ state: number;
538
+ faults?: number;
539
+ latency?: number;
540
+ };
541
+ faults?: unknown;
542
+ latency?: unknown;
543
+ virtualTime?: boolean;
544
+ };
545
+ /** Controls for one namespace. */
546
+ type NamespaceControls = {
547
+ /** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
548
+ seed?: number | string;
549
+ clock?: ClockControls;
550
+ /**
551
+ * Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
552
+ * wall clock: the request returns at once, the clock has moved, and the journal records
553
+ * the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
554
+ */
555
+ virtualTime?: boolean;
556
+ /** Baseline response time for every vendor request. A bare duration is its `delay`. */
557
+ latency?: Duration | LatencyControls;
558
+ faults?: readonly FaultInput[];
559
+ /** Start from this state instead of the service's own seed data. */
560
+ snapshot?: SnapshotDocument;
561
+ };
562
+ /** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
563
+ type MockControls = NamespaceControls & {
564
+ /** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
565
+ credentials?: Record<string, string>;
566
+ /** Overrides for named namespaces. A key given here replaces the shared value. */
567
+ namespaces?: Record<string, NamespaceControls>;
568
+ };
569
+ /** A latency rule in milliseconds. */
570
+ type LatencyRule = {
571
+ delayMs: number;
572
+ jitterMs: number;
573
+ };
574
+ /** Baseline latency for a namespace, normalized. */
575
+ type LatencyState = LatencyRule & {
576
+ operations: Record<string, LatencyRule>;
577
+ };
578
+ /** What {@link controlHeaders} turns into request headers. */
579
+ type ControlHeaderInput = {
580
+ namespace?: string;
581
+ adminKey?: string;
582
+ branch?: string;
583
+ at?: string;
584
+ reset?: boolean;
585
+ restore?: string;
586
+ seed?: number | string;
587
+ time?: Instant | Date;
588
+ advance?: Duration;
589
+ virtualTime?: boolean;
590
+ delay?: Duration;
591
+ timeout?: Duration;
592
+ /** A status, `"drop"`, a preset name, or a fault rule. */
593
+ fault?: number | string | Record<string, unknown>;
594
+ snapshot?: string;
374
595
  };
375
596
 
376
597
  /**
@@ -410,6 +631,8 @@ type RequestLog = {
410
631
  unmatched: boolean;
411
632
  /** Set when a fault rule produced the response. */
412
633
  faultId?: string;
634
+ /** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
635
+ delayMs?: number;
413
636
  /** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
414
637
  ids?: Record<string, string>;
415
638
  /** Set when the service created a resource the request referred to but that did not exist. */
@@ -455,6 +678,12 @@ type JournalQuery = {
455
678
  /** Only this namespace. Omit for every namespace, oldest first across all of them. */
456
679
  namespace?: string;
457
680
  operationId?: string;
681
+ /** Only this HTTP method, case-insensitive. */
682
+ method?: string;
683
+ /** Only this exact path. */
684
+ path?: string;
685
+ /** Only requests this fault rule answered or shaped. */
686
+ faultId?: string;
458
687
  status?: number;
459
688
  /** Only entries at or after this instant (epoch ms). */
460
689
  since?: number;
@@ -469,6 +698,50 @@ type Journal = {
469
698
  clear(namespace?: string): void;
470
699
  };
471
700
 
701
+ /**
702
+ * Baseline response time, per namespace: how long every vendor request takes before any
703
+ * fault is considered. Jitter draws from a stream the caller owns, one per namespace and
704
+ * separate from the fault stream, so adding latency never shifts which requests a `rate`
705
+ * fault hits.
706
+ */
707
+ type LatencyRegistry = {
708
+ get(namespace: string): LatencyState;
709
+ set(namespace: string, state: LatencyState): LatencyState;
710
+ /** Remove one namespace's latency, or every namespace's. */
711
+ clear(namespace?: string): void;
712
+ /** Milliseconds this request waits. Draws jitter, so call it once per request. */
713
+ take(namespace: string, operationId: string | undefined): number;
714
+ };
715
+
716
+ /** Clock state as the admin API reports it. */
717
+ type ControlClock = ClockState & {
718
+ /** `now`, as ISO-8601. */
719
+ iso: string;
720
+ /** Milliseconds the clock steps before every vendor request. */
721
+ autoAdvanceMs: number;
722
+ };
723
+ /** One namespace's controls: the body of `GET /__admin/controls`. */
724
+ type ControlState = {
725
+ namespace: string;
726
+ seed: number | string;
727
+ clock: ControlClock;
728
+ virtualTime: boolean;
729
+ latency: LatencyState;
730
+ faults: FaultListing[];
731
+ };
732
+
733
+ /** What `GET /__admin/snapshots` lists for each snapshot. */
734
+ type SnapshotInfo = {
735
+ id: string;
736
+ namespace: string;
737
+ /** Stored rows in the snapshot. */
738
+ records: number;
739
+ /** The namespace clock when it was taken, epoch ms. */
740
+ at: number;
741
+ /** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
742
+ portable: boolean;
743
+ };
744
+
472
745
  /** Credential → namespace mapping behind `PUT /__admin/credentials`. */
473
746
  type CredentialRegistry = {
474
747
  set(credential: string, namespace: string): void;
@@ -623,7 +896,10 @@ type ServiceInstance = FetchAPI & {
623
896
  };
624
897
  type ServiceTimelineState = Readonly<{
625
898
  snapshot: NamespaceSnapshot;
626
- clock: Readonly<ReturnType<Clock["state"]>>;
899
+ /** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
900
+ clock: Readonly<ClockState & {
901
+ pinned?: boolean;
902
+ }>;
627
903
  rngState: number;
628
904
  }>;
629
905
  type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
@@ -631,17 +907,23 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
631
907
  /** Stop package-owned lifecycle timers when a supervisor closes the runtime. */
632
908
  stop?(): void;
633
909
  readonly name: string;
910
+ /** Same-origin Fetch handler and App Router method exports, sharing this runtime. */
911
+ mount(prefix: string): MountedAPI;
634
912
  readonly sqlite: SqliteClient$1;
635
913
  readonly clock: Clock;
636
914
  readonly faults: FaultRegistry;
915
+ /** Baseline response time per namespace. */
916
+ readonly latency: LatencyRegistry;
637
917
  readonly metrics: Metrics;
638
918
  readonly journal: Journal;
639
919
  readonly rng: Rng;
640
920
  readonly credentials: CredentialRegistry;
641
921
  /** The webhook hub, when the service has outbound webhooks. */
642
922
  readonly webhooks: WebhookHub | undefined;
643
- /** Enable independent namespace clocks and PRNGs before serving a fleet. */
644
- isolateNamespaces(): void;
923
+ /**
924
+ * A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
925
+ * offset and a freeze of its own, so moving one never moves another.
926
+ */
645
927
  namespaceClock(namespace?: string): Clock;
646
928
  namespaceOf(request: Request): string;
647
929
  /** Opaque in-process checkpoint including records, controls and webhook history. */
@@ -653,8 +935,38 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
653
935
  instance(namespace?: string): T;
654
936
  /** Public names of every namespace created so far. */
655
937
  namespaces(): string[];
656
- /** Reset one namespace, or every namespace with `"*"`. */
938
+ /** Return one namespace to its boot state, or every namespace with `"*"`. */
657
939
  reset(namespace?: string): Promise<void>;
940
+ /** Reset a namespace and forget it, releasing everything it held. */
941
+ dropNamespace(namespace: string): Promise<void>;
942
+ /** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
943
+ controls(namespace?: string): ControlState & {
944
+ snapshots: SnapshotInfo[];
945
+ };
946
+ /** Apply a control document to a namespace. A given key replaces that control. */
947
+ configure(controls: NamespaceControls, namespace?: string): ControlState & {
948
+ snapshots: SnapshotInfo[];
949
+ };
950
+ /** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
951
+ readonly snapshots: {
952
+ /** Capture the namespace now. Reusing a name replaces that snapshot. */
953
+ create(options?: {
954
+ name?: string;
955
+ namespace?: string;
956
+ }): SnapshotInfo;
957
+ list(namespace?: string): SnapshotInfo[];
958
+ /** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
959
+ export(id: string, namespace?: string): SnapshotDocument;
960
+ /** Store a document as a snapshot without restoring it. */
961
+ import(document: SnapshotDocument, options?: {
962
+ name?: string;
963
+ namespace?: string;
964
+ }): SnapshotInfo;
965
+ restore(id: string, namespace?: string): void;
966
+ delete(id: string, namespace?: string): boolean;
967
+ };
968
+ /** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
969
+ headers(input: ControlHeaderInput): Record<string, string>;
658
970
  snapshot(namespace?: string): NamespaceSnapshot;
659
971
  restore(snapshot: NamespaceSnapshot, namespace?: string): void;
660
972
  /** Capture the current branch. Mutating HTTP calls do this automatically. */
@@ -970,6 +1282,8 @@ type SentryRuntimeOptions = Omit<SentryAPIOptions, "now" | "namespace"> & {
970
1282
  clock?: Clock;
971
1283
  seed?: number | string;
972
1284
  adminKey?: string;
1285
+ /** Admin controls to boot with: clock, latency, faults, seed, a starting snapshot. */
1286
+ adminControls?: MockControls;
973
1287
  onLog?: (entry: RequestLog) => void;
974
1288
  };
975
1289
  type SentryRuntime = ServiceRuntime<SentryAPI>;