@crvouga/mockingbird-service-junction 0.1.2 → 0.2.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/CHANGELOG.md +31 -0
- package/README.md +447 -144
- package/dist/chunk-4C6VYRJN.js +18 -0
- package/dist/chunk-4C6VYRJN.js.map +7 -0
- package/dist/chunk-KKNULV7X.js +406 -0
- package/dist/chunk-KKNULV7X.js.map +7 -0
- package/dist/chunk-MA3HSUVD.js +7771 -0
- package/dist/chunk-MA3HSUVD.js.map +7 -0
- package/dist/chunk-MD4KYEIM.js +145758 -0
- package/dist/chunk-MD4KYEIM.js.map +7 -0
- package/dist/cli.js +214 -0
- package/dist/cli.js.map +7 -0
- package/dist/corpus.d.ts +97 -0
- package/dist/corpus.js +8 -0
- package/dist/corpus.js.map +7 -0
- package/dist/index.d.ts +641 -2
- package/dist/index.js +65 -6082
- package/dist/index.js.map +4 -4
- package/dist/server.d.ts +978 -0
- package/dist/server.js +16 -0
- package/dist/server.js.map +7 -0
- package/package.json +23 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
import { Hono } from 'hono';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* The single source of time for a service.
|
|
5
|
+
*
|
|
6
|
+
* Every timestamp a mock writes reads from here, so a suite moves time instead of
|
|
7
|
+
* sleeping: appointment windows, result delays and expiries become reachable in
|
|
8
|
+
* milliseconds. A frozen clock also makes timestamps reproducible from a seed.
|
|
9
|
+
*/
|
|
10
|
+
type ClockState = {
|
|
11
|
+
/** Current epoch milliseconds. */
|
|
12
|
+
now: number;
|
|
13
|
+
/** True while time does not advance on its own. */
|
|
14
|
+
frozen: boolean;
|
|
15
|
+
/** Milliseconds this clock adds to its underlying source. */
|
|
16
|
+
offsetMs: number;
|
|
17
|
+
};
|
|
18
|
+
type Clock = {
|
|
19
|
+
now(): number;
|
|
20
|
+
/** Pin the clock to an exact instant, keeping it frozen if it already was. */
|
|
21
|
+
set(epochMs: number): void;
|
|
22
|
+
/** Move the clock forward, or back with a negative delta. */
|
|
23
|
+
advance(deltaMs: number): void;
|
|
24
|
+
/** Stop time at the current instant. */
|
|
25
|
+
freeze(): void;
|
|
26
|
+
/** Resume from the current instant. */
|
|
27
|
+
unfreeze(): void;
|
|
28
|
+
/** Drop back to the underlying source, live. */
|
|
29
|
+
reset(): void;
|
|
30
|
+
state(): ClockState;
|
|
31
|
+
};
|
|
32
|
+
|
|
3
33
|
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
4
34
|
type SqliteValue$1 = null | number | bigint | string | Uint8Array | boolean;
|
|
5
35
|
/** Mutation counters returned by {@link SqliteStatement.run}. */
|
|
@@ -29,12 +59,183 @@ interface SqliteClient$1 {
|
|
|
29
59
|
transaction<T>(fn: () => T): T;
|
|
30
60
|
}
|
|
31
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Seeded pseudo-random numbers, so anything a mock invents — ids, jitter, which
|
|
64
|
+
* request a percentage fault hits — is reproducible from a seed.
|
|
65
|
+
*
|
|
66
|
+
* mulberry32: small, fast, and stable across runtimes, which matters more here
|
|
67
|
+
* than statistical quality.
|
|
68
|
+
*/
|
|
69
|
+
type Rng = {
|
|
70
|
+
/** Next value in `[0, 1)`. */
|
|
71
|
+
next(): number;
|
|
72
|
+
/** Next integer in `[min, max]`. */
|
|
73
|
+
int(min: number, max: number): number;
|
|
74
|
+
/** Restart the stream from its seed. */
|
|
75
|
+
reset(): void;
|
|
76
|
+
seed: number;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A deliberate failure injected in front of an operation.
|
|
81
|
+
*
|
|
82
|
+
* This is how a suite reaches the vendor's failure modes without the vendor: the
|
|
83
|
+
* quota error that only appears when a shared sandbox is full, the 429 that only
|
|
84
|
+
* appears under load, the 5xx that proves a retry path works.
|
|
85
|
+
*/
|
|
86
|
+
type FaultRule = {
|
|
87
|
+
/** Stable id, so a suite can retire exactly the rule it added. */
|
|
88
|
+
id: string;
|
|
89
|
+
/** Fault only this operation. Omit to match every operation. */
|
|
90
|
+
operationId?: string;
|
|
91
|
+
/** Fault only this HTTP method, case-insensitive. Omit to match every method. */
|
|
92
|
+
method?: string;
|
|
93
|
+
/** Fault only paths starting with this prefix. Omit to match every path. */
|
|
94
|
+
pathPrefix?: string;
|
|
95
|
+
/**
|
|
96
|
+
* Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
|
|
97
|
+
* an in-process caller usually wants, and what a parallel worker usually does not:
|
|
98
|
+
* rules added through `POST /__admin/faults` default to the calling namespace.
|
|
99
|
+
*/
|
|
100
|
+
namespace?: string;
|
|
101
|
+
status: number;
|
|
102
|
+
/** Response body, serialized as JSON. A string is sent as-is. */
|
|
103
|
+
body?: unknown;
|
|
104
|
+
headers?: Record<string, string>;
|
|
105
|
+
/** Retire the rule after this many faults. Omit to keep it until removed. */
|
|
106
|
+
count?: number;
|
|
107
|
+
/** Fault this fraction of matching requests, `0`–`1`. Default `1`. */
|
|
108
|
+
rate?: number;
|
|
109
|
+
/** Hold the response back this long, to exercise timeouts. */
|
|
110
|
+
delayMs?: number;
|
|
111
|
+
};
|
|
112
|
+
/** What a request looks like to the fault matcher. */
|
|
113
|
+
type FaultCandidate = {
|
|
114
|
+
operationId: string | undefined;
|
|
115
|
+
method: string;
|
|
116
|
+
path: string;
|
|
117
|
+
namespace: string;
|
|
118
|
+
};
|
|
119
|
+
type FaultRegistry = {
|
|
120
|
+
add(rule: FaultRule): FaultRule;
|
|
121
|
+
list(): (FaultRule & {
|
|
122
|
+
remaining: number | null;
|
|
123
|
+
hits: number;
|
|
124
|
+
})[];
|
|
125
|
+
remove(id: string): boolean;
|
|
126
|
+
clear(): void;
|
|
127
|
+
/**
|
|
128
|
+
* The fault this request should get, or `undefined` to let it through.
|
|
129
|
+
* Consumes one of the matching rule's remaining uses.
|
|
130
|
+
*/
|
|
131
|
+
take(candidate: FaultCandidate): Promise<{
|
|
132
|
+
id: string;
|
|
133
|
+
response: Response;
|
|
134
|
+
} | undefined>;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/** One handled request, as the structured log sees it. */
|
|
138
|
+
type RequestLog = {
|
|
139
|
+
service: string;
|
|
140
|
+
namespace: string;
|
|
141
|
+
operationId: string | undefined;
|
|
142
|
+
method: string;
|
|
143
|
+
path: string;
|
|
144
|
+
status: number;
|
|
145
|
+
durationMs: number;
|
|
146
|
+
/** True when the path matched no operation in the contract. */
|
|
147
|
+
unmatched: boolean;
|
|
148
|
+
/** Set when a fault rule produced the response. */
|
|
149
|
+
faultId?: string;
|
|
150
|
+
};
|
|
151
|
+
type MetricsReport = {
|
|
152
|
+
requests: number;
|
|
153
|
+
/** Counts keyed `<operationId> <status>`. */
|
|
154
|
+
byOperation: Record<string, number>;
|
|
155
|
+
/**
|
|
156
|
+
* Paths that matched no operation, most frequent first.
|
|
157
|
+
*
|
|
158
|
+
* This is the early-warning signal: a consumer calling something the mock does
|
|
159
|
+
* not implement shows up here as a count, before it fails a suite as a 404.
|
|
160
|
+
*/
|
|
161
|
+
unmatched: {
|
|
162
|
+
method: string;
|
|
163
|
+
path: string;
|
|
164
|
+
count: number;
|
|
165
|
+
}[];
|
|
166
|
+
faults: number;
|
|
167
|
+
totalDurationMs: number;
|
|
168
|
+
};
|
|
169
|
+
type Metrics = {
|
|
170
|
+
record(entry: RequestLog): void;
|
|
171
|
+
report(): MetricsReport;
|
|
172
|
+
reset(): void;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* A point-in-time copy of everything a service namespace holds.
|
|
177
|
+
*
|
|
178
|
+
* All service state lives in the two core tables keyed by namespace, so a snapshot
|
|
179
|
+
* is generic: any service gets per-test rollback without knowing its own schema.
|
|
180
|
+
* Restoring is much cheaper than rebuilding a namespace from a corpus.
|
|
181
|
+
*/
|
|
182
|
+
type NamespaceSnapshot = {
|
|
183
|
+
namespace: string;
|
|
184
|
+
records: {
|
|
185
|
+
collection: string;
|
|
186
|
+
id: string;
|
|
187
|
+
seq: number;
|
|
188
|
+
value: string;
|
|
189
|
+
}[];
|
|
190
|
+
sequences: {
|
|
191
|
+
name: string;
|
|
192
|
+
kind: string;
|
|
193
|
+
value: number;
|
|
194
|
+
}[];
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Anything that can answer a Fetch `Request` with a `Response`.
|
|
199
|
+
*
|
|
200
|
+
* Every Mockingbird service implements this, and every runtime adapter consumes it.
|
|
201
|
+
* It is the only contract shared across the whole graph.
|
|
202
|
+
*/
|
|
203
|
+
interface FetchAPI$1 {
|
|
204
|
+
fetch(request: Request): Promise<Response>;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** What the runtime needs from a service: a Fetch handler it can reset. */
|
|
208
|
+
type ServiceInstance = FetchAPI$1 & {
|
|
209
|
+
reset(): Promise<void>;
|
|
210
|
+
};
|
|
211
|
+
type ServiceRuntime<T extends ServiceInstance> = FetchAPI$1 & {
|
|
212
|
+
readonly name: string;
|
|
213
|
+
readonly sqlite: SqliteClient$1;
|
|
214
|
+
readonly clock: Clock;
|
|
215
|
+
readonly faults: FaultRegistry;
|
|
216
|
+
readonly metrics: Metrics;
|
|
217
|
+
readonly rng: Rng;
|
|
218
|
+
/** The instance behind `namespace` (the default one when omitted), created on first use. */
|
|
219
|
+
instance(namespace?: string): T;
|
|
220
|
+
/** Public names of every namespace created so far. */
|
|
221
|
+
namespaces(): string[];
|
|
222
|
+
/** Reset one namespace, or every namespace with `"*"`. */
|
|
223
|
+
reset(namespace?: string): Promise<void>;
|
|
224
|
+
snapshot(namespace?: string): NamespaceSnapshot;
|
|
225
|
+
restore(snapshot: NamespaceSnapshot, namespace?: string): void;
|
|
226
|
+
};
|
|
227
|
+
|
|
32
228
|
/** Options every provider constructor accepts. */
|
|
33
229
|
type APIOptions = {
|
|
34
230
|
/** Sync SQLite client. Defaults to `@crvouga/mockingbird-service-sqlite`. */
|
|
35
231
|
sqlite?: SqliteClient$1;
|
|
36
232
|
/** Clock used for `created`-style fields. Default `Date.now`. */
|
|
37
233
|
now?: () => number;
|
|
234
|
+
/**
|
|
235
|
+
* Storage namespace for this instance's records. Instances sharing one SQLite
|
|
236
|
+
* client stay isolated when their namespaces differ. Defaults to the service name.
|
|
237
|
+
*/
|
|
238
|
+
namespace?: string;
|
|
38
239
|
};
|
|
39
240
|
|
|
40
241
|
/** Bind values accepted by Mockingbird's SQLite port (matches sqlite-mem / better-sqlite3). */
|
|
@@ -139,6 +340,104 @@ type WebhookDeliveryAttempt = {
|
|
|
139
340
|
timeout_ms: number;
|
|
140
341
|
acknowledged: boolean;
|
|
141
342
|
};
|
|
343
|
+
type OrderEventRecord = {
|
|
344
|
+
id: number;
|
|
345
|
+
created_at: string;
|
|
346
|
+
status: string;
|
|
347
|
+
status_detail: null;
|
|
348
|
+
};
|
|
349
|
+
type OrderTransactionEmbed = {
|
|
350
|
+
id: string;
|
|
351
|
+
status: "active" | "completed" | "cancelled";
|
|
352
|
+
orders: Array<{
|
|
353
|
+
id: string;
|
|
354
|
+
low_level_status: string;
|
|
355
|
+
low_level_status_created_at: string;
|
|
356
|
+
origin: string;
|
|
357
|
+
parent_id: null;
|
|
358
|
+
created_at: string;
|
|
359
|
+
updated_at: string;
|
|
360
|
+
}>;
|
|
361
|
+
};
|
|
362
|
+
type OrderRecord = {
|
|
363
|
+
id: string;
|
|
364
|
+
user_id: string;
|
|
365
|
+
team_id: string;
|
|
366
|
+
patient_details: Record<string, unknown>;
|
|
367
|
+
patient_address: Record<string, unknown>;
|
|
368
|
+
lab_test: LabTestRecord;
|
|
369
|
+
details: Record<string, unknown>;
|
|
370
|
+
sample_id: string | null;
|
|
371
|
+
notes: null;
|
|
372
|
+
clinical_notes: string | null;
|
|
373
|
+
passthrough: string | null;
|
|
374
|
+
/** Echoed from the request's opaque provider account id; absent when the order had none. */
|
|
375
|
+
lab_account_id?: string;
|
|
376
|
+
created_at: string;
|
|
377
|
+
updated_at: string;
|
|
378
|
+
events: OrderEventRecord[];
|
|
379
|
+
status: string;
|
|
380
|
+
last_event: OrderEventRecord;
|
|
381
|
+
physician: {
|
|
382
|
+
first_name: string;
|
|
383
|
+
last_name: string;
|
|
384
|
+
npi: string;
|
|
385
|
+
};
|
|
386
|
+
health_insurance_id: null;
|
|
387
|
+
requisition_form_url: string | null;
|
|
388
|
+
shipping_details: null;
|
|
389
|
+
has_abn: boolean;
|
|
390
|
+
billing_type: string;
|
|
391
|
+
priority: boolean;
|
|
392
|
+
activate_by: null;
|
|
393
|
+
icd_codes: string[] | null;
|
|
394
|
+
interpretation: string | null;
|
|
395
|
+
has_missing_results: boolean | null;
|
|
396
|
+
result_types: string[] | null;
|
|
397
|
+
expected_result_by_date: string | null;
|
|
398
|
+
worst_case_result_by_date: string | null;
|
|
399
|
+
origin: string;
|
|
400
|
+
order_transaction: OrderTransactionEmbed;
|
|
401
|
+
};
|
|
402
|
+
/**
|
|
403
|
+
* A lab account as the ordering rules read it. Structurally `LabAccountRecord` from
|
|
404
|
+
* lab-accounts.ts, restated here because that module imports this one.
|
|
405
|
+
*/
|
|
406
|
+
type StoredLabAccount = {
|
|
407
|
+
id: string;
|
|
408
|
+
lab: string;
|
|
409
|
+
org_id: string | null;
|
|
410
|
+
status: "active" | "pending" | "suspended" | "ready_to_launch";
|
|
411
|
+
delegated_flow: "order_delegated" | "result_delegated" | "fully_delegated" | "not_delegated";
|
|
412
|
+
provider_account_id: string;
|
|
413
|
+
account_name: string | null;
|
|
414
|
+
default_clinical_notes: string | null;
|
|
415
|
+
business_units: string[] | null;
|
|
416
|
+
allowed_billing: Record<string, readonly string[]>;
|
|
417
|
+
team_id_allowlist: readonly string[];
|
|
418
|
+
};
|
|
419
|
+
/**
|
|
420
|
+
* A chosen result for one order, installed through the admin API. Replaces the
|
|
421
|
+
* generated biomarker lines, so a suite can assert on an exact payload — an abnormal
|
|
422
|
+
* panel, a critical value, a missing marker — instead of whatever the generator made.
|
|
423
|
+
*/
|
|
424
|
+
type ResultFixture = {
|
|
425
|
+
/** The fixture's name, for logs and `GET /__admin/results`. */
|
|
426
|
+
name: string;
|
|
427
|
+
results?: unknown[];
|
|
428
|
+
missing_results?: unknown[] | null;
|
|
429
|
+
interpretation?: string;
|
|
430
|
+
/** Served from `GET /v3/order/{id}/result/pdf` instead of the generated PDF. */
|
|
431
|
+
pdf_base64?: string;
|
|
432
|
+
};
|
|
433
|
+
/**
|
|
434
|
+
* How serviceability reads answer a ZIP the loaded corpus has no record for.
|
|
435
|
+
*
|
|
436
|
+
* - `synthetic`: invent plausible coverage. Right for property tests that walk random ZIPs.
|
|
437
|
+
* - `corpus`: refuse with a documented Mockingbird error. Right for a mock standing in for
|
|
438
|
+
* the vendor, where invented coverage would silently disagree with production.
|
|
439
|
+
*/
|
|
440
|
+
type GeoMode = "synthetic" | "corpus";
|
|
142
441
|
type WebhookPublisher = (event: JunctionWebhookEvent) => void;
|
|
143
442
|
type JunctionWebhookOptions = {
|
|
144
443
|
seed?: number;
|
|
@@ -156,6 +455,50 @@ interface FetchAPI {
|
|
|
156
455
|
fetch(request: Request): Promise<Response>;
|
|
157
456
|
}
|
|
158
457
|
|
|
458
|
+
type LabAccountStatus = "active" | "pending" | "suspended" | "ready_to_launch";
|
|
459
|
+
type LabAccountDelegatedFlow = "order_delegated" | "result_delegated" | "fully_delegated" | "not_delegated";
|
|
460
|
+
type LabAccountRecord = {
|
|
461
|
+
id: string;
|
|
462
|
+
lab: string;
|
|
463
|
+
org_id: string | null;
|
|
464
|
+
status: LabAccountStatus;
|
|
465
|
+
delegated_flow: LabAccountDelegatedFlow;
|
|
466
|
+
provider_account_id: string;
|
|
467
|
+
account_name: string | null;
|
|
468
|
+
default_clinical_notes: string | null;
|
|
469
|
+
business_units: string[] | null;
|
|
470
|
+
allowed_billing: Record<string, readonly string[]>;
|
|
471
|
+
team_id_allowlist: readonly string[];
|
|
472
|
+
};
|
|
473
|
+
/** The 50 `USState` values from the docs, alphabetical by state name. */
|
|
474
|
+
declare const US_STATES: readonly string[];
|
|
475
|
+
declare const TEAM_LAB_ACCOUNTS: readonly LabAccountRecord[];
|
|
476
|
+
/**
|
|
477
|
+
* A lab account as a consumer configures it. Mirrors `ClientFacingLabAccount`, with
|
|
478
|
+
* every field but `id` and `lab` optional, plus a `states` shorthand for the common
|
|
479
|
+
* case of an account that client-bills in a fixed set of states.
|
|
480
|
+
*/
|
|
481
|
+
type LabAccountInput = {
|
|
482
|
+
id: string;
|
|
483
|
+
/** Lab slug, e.g. `"quest"`, `"labcorp"`, `"bioreference"`. */
|
|
484
|
+
lab: string;
|
|
485
|
+
status?: LabAccountStatus;
|
|
486
|
+
delegated_flow?: LabAccountDelegatedFlow;
|
|
487
|
+
account_name?: string | null;
|
|
488
|
+
provider_account_id?: string;
|
|
489
|
+
org_id?: string | null;
|
|
490
|
+
business_units?: string[] | null;
|
|
491
|
+
default_clinical_notes?: string | null;
|
|
492
|
+
/** Billing type → states it is allowed in. Takes precedence over `states`. */
|
|
493
|
+
allowed_billing?: Record<string, readonly string[]>;
|
|
494
|
+
/** Shorthand for `allowed_billing: { client_bill: states }`. Default: every state. */
|
|
495
|
+
states?: readonly string[];
|
|
496
|
+
/** Teams the account is linked to. Default: the mock team, so it is selectable. */
|
|
497
|
+
team_id_allowlist?: readonly string[];
|
|
498
|
+
};
|
|
499
|
+
/** Normalize a consumer's configuration into the record the ordering rules read. */
|
|
500
|
+
declare const labAccountFromInput: (input: LabAccountInput) => LabAccountRecord;
|
|
501
|
+
|
|
159
502
|
/**
|
|
160
503
|
* Version of the sealed-corpus on-disk format. Bumped whenever the shape changes so a
|
|
161
504
|
* stale recording is rejected instead of half-applied.
|
|
@@ -171,6 +514,8 @@ type SealedCorpus = {
|
|
|
171
514
|
version: typeof SEALED_CORPUS_VERSION;
|
|
172
515
|
recordedAt: string;
|
|
173
516
|
source: string;
|
|
517
|
+
/** SHA-256 of the recording's content, written by `corpus pull`. Identifies it in logs. */
|
|
518
|
+
fingerprint?: string;
|
|
174
519
|
observations: Record<string, GetCacheEntry>;
|
|
175
520
|
catalog: {
|
|
176
521
|
labTests: LabTestRecord[];
|
|
@@ -199,6 +544,150 @@ type SeedReport = {
|
|
|
199
544
|
cacheEntries: number;
|
|
200
545
|
};
|
|
201
546
|
|
|
547
|
+
/**
|
|
548
|
+
* Signed webhook delivery, the way Junction delivers through Svix.
|
|
549
|
+
*
|
|
550
|
+
* Signing lives here, next to the events, so a consumer never hand-rolls it. The
|
|
551
|
+
* signature is standard Svix — `v1,` + base64 HMAC-SHA256, keyed by the base64 part of
|
|
552
|
+
* `whsec_…`, over `"<svix-id>.<svix-timestamp>.<body>"` — so the official `svix`
|
|
553
|
+
* verifier, and a receiver written for real Junction webhooks, accept it unchanged.
|
|
554
|
+
*/
|
|
555
|
+
type WebhookEndpoint = {
|
|
556
|
+
url: string;
|
|
557
|
+
/** Svix signing secret, `whsec_<base64>`. A bare base64 secret is accepted too. */
|
|
558
|
+
secret: string;
|
|
559
|
+
/**
|
|
560
|
+
* Delay before each attempt, in ms; the first entry delays the first attempt. Default:
|
|
561
|
+
* Svix's schedule (immediately, 5 s, 5 min, 30 min, 2 h, 5 h, 10 h, 10 h).
|
|
562
|
+
*/
|
|
563
|
+
retryDelaysMs?: readonly number[];
|
|
564
|
+
/** Abort an attempt that takes longer than this. Default 15 s, like Svix. */
|
|
565
|
+
timeoutMs?: number;
|
|
566
|
+
/** Added to every delivery, e.g. a routing scope for a shared receiver. */
|
|
567
|
+
headers?: Record<string, string>;
|
|
568
|
+
fetch?: (request: Request) => Promise<Response>;
|
|
569
|
+
};
|
|
570
|
+
type WebhookAttempt = {
|
|
571
|
+
attempt: number;
|
|
572
|
+
/** Wall-clock time of the attempt, ISO-8601. */
|
|
573
|
+
at: string;
|
|
574
|
+
/** Receiver's status code, or `null` when the request never completed. */
|
|
575
|
+
status: number | null;
|
|
576
|
+
error: string | null;
|
|
577
|
+
durationMs: number;
|
|
578
|
+
};
|
|
579
|
+
type WebhookDelivery = {
|
|
580
|
+
messageId: string;
|
|
581
|
+
namespace: string;
|
|
582
|
+
event: JunctionWebhookEvent;
|
|
583
|
+
state: "pending" | "delivered" | "failed";
|
|
584
|
+
attempts: WebhookAttempt[];
|
|
585
|
+
};
|
|
586
|
+
type WebhookDispatcher = {
|
|
587
|
+
/** Queue an event for delivery. Returns immediately; delivery runs in the background. */
|
|
588
|
+
publish(event: JunctionWebhookEvent, namespace: string): WebhookDelivery;
|
|
589
|
+
deliveries(namespace?: string): WebhookDelivery[];
|
|
590
|
+
/** Deliver a message again now, as a new attempt, whatever its state. */
|
|
591
|
+
replay(messageId: string): Promise<WebhookDelivery | undefined>;
|
|
592
|
+
/** Run every pending retry now instead of waiting out its delay. */
|
|
593
|
+
flush(): Promise<void>;
|
|
594
|
+
/** Resolve once no delivery is in flight. */
|
|
595
|
+
idle(): Promise<void>;
|
|
596
|
+
/** Cancel pending retries and forget every delivery. */
|
|
597
|
+
clear(namespace?: string): void;
|
|
598
|
+
};
|
|
599
|
+
/** The `svix-signature` value for one message: `v1,<base64 HMAC-SHA256>`. */
|
|
600
|
+
declare const signSvix: (secret: string, messageId: string, timestampSeconds: number, body: string) => Promise<string>;
|
|
601
|
+
/**
|
|
602
|
+
* Check a delivery's signature the way the `svix` verifier does: any `v1,` entry in the
|
|
603
|
+
* space-separated header matches, and the timestamp is within `toleranceSeconds`.
|
|
604
|
+
*/
|
|
605
|
+
declare const verifySvix: (secret: string, headers: {
|
|
606
|
+
"svix-id": string;
|
|
607
|
+
"svix-timestamp": string;
|
|
608
|
+
"svix-signature": string;
|
|
609
|
+
}, body: string, options?: {
|
|
610
|
+
toleranceSeconds?: number;
|
|
611
|
+
now?: () => number;
|
|
612
|
+
}) => Promise<boolean>;
|
|
613
|
+
declare const createWebhookDispatcher: (endpoint: WebhookEndpoint) => WebhookDispatcher;
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Every order status the contract defines, by collection method (from the vendored
|
|
617
|
+
* OpenAPI enum). An admin transition may only target a status real Junction can emit.
|
|
618
|
+
*/
|
|
619
|
+
declare const ORDER_STATUSES_BY_METHOD: Readonly<Record<string, readonly string[]>>;
|
|
620
|
+
/**
|
|
621
|
+
* Friendly transition targets, per method, mapped onto the contract's own statuses.
|
|
622
|
+
* A name is absent for a method when no real status means it — walk-in tests have no
|
|
623
|
+
* draw event, so `collected` is refused there rather than approximated.
|
|
624
|
+
*/
|
|
625
|
+
declare const TRANSITION_ALIASES: Readonly<Record<string, Readonly<Record<string, string>>>>;
|
|
626
|
+
/**
|
|
627
|
+
* Named results, as the flags `simulate_order` accepts. They steer the generator, so
|
|
628
|
+
* the payload keeps Junction's exact shape; `PUT /__admin/results/{order}` installs an
|
|
629
|
+
* exact payload instead.
|
|
630
|
+
*/
|
|
631
|
+
declare const RESULT_FIXTURES: Readonly<Record<string, Record<string, unknown>>>;
|
|
632
|
+
/**
|
|
633
|
+
* Junction failure modes a suite can switch on by name. `sandbox_user_quota` reproduces
|
|
634
|
+
* the shared-sandbox error byte for byte (message as observed from a real sandbox team);
|
|
635
|
+
* the others are shape-plausible, not verified against Junction.
|
|
636
|
+
*/
|
|
637
|
+
declare const FAULT_PRESETS: Readonly<Record<string, Omit<FaultRule, "id">>>;
|
|
638
|
+
/** Resolve a target (`completed`, `completed.completed`, or a full status) for a method. */
|
|
639
|
+
declare const resolveTransition: (method: string, target: string) => {
|
|
640
|
+
status: string;
|
|
641
|
+
} | {
|
|
642
|
+
error: string;
|
|
643
|
+
};
|
|
644
|
+
type JunctionAdminOptions = {
|
|
645
|
+
webhooks: WebhookDispatcher | undefined;
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
declare const DEFAULT_JUNCTION_BASE_URL = "https://api.sandbox.tryvital.io";
|
|
649
|
+
/** Key prefixes of sandbox team keys. `corpus pull` and `verify` refuse any other key. */
|
|
650
|
+
declare const SANDBOX_KEY_PREFIXES: readonly ["sk_us_", "sk_eu_"];
|
|
651
|
+
declare const isSandboxKey: (key: string) => boolean;
|
|
652
|
+
type PullCorpusOptions = {
|
|
653
|
+
apiKey: string;
|
|
654
|
+
baseUrl?: string;
|
|
655
|
+
/** ZIPs to record area and PSC coverage for. Default: the shipped coverage set. */
|
|
656
|
+
zips?: readonly string[];
|
|
657
|
+
/**
|
|
658
|
+
* Refresh only `zips` on top of this corpus, keeping everything else. How a single
|
|
659
|
+
* ZIP is added to a committed corpus without re-recording the whole thing.
|
|
660
|
+
*/
|
|
661
|
+
base?: SealedCorpus;
|
|
662
|
+
/** Skip the team's catalog and lab accounts (coverage only). */
|
|
663
|
+
coverageOnly?: boolean;
|
|
664
|
+
fetch?: (request: Request) => Promise<Response>;
|
|
665
|
+
/** Pause between vendor calls, to stay under rate limits. Default 50 ms. */
|
|
666
|
+
minIntervalMs?: number;
|
|
667
|
+
onProgress?: (message: string) => void;
|
|
668
|
+
};
|
|
669
|
+
/** SHA-256 over a corpus's content — not its recording time — so a re-pull of unchanged data matches. */
|
|
670
|
+
declare const fingerprintCorpus: (corpus: SealedCorpus) => Promise<string>;
|
|
671
|
+
/** Record a sealed corpus from a real Junction team. Read-only: it only issues GETs. */
|
|
672
|
+
declare const pullCorpus: (options: PullCorpusOptions) => Promise<SealedCorpus>;
|
|
673
|
+
type SetDiff = {
|
|
674
|
+
added: string[];
|
|
675
|
+
removed: string[];
|
|
676
|
+
changed: string[];
|
|
677
|
+
};
|
|
678
|
+
type CorpusDiff = {
|
|
679
|
+
identical: boolean;
|
|
680
|
+
observations: SetDiff;
|
|
681
|
+
labTests: SetDiff;
|
|
682
|
+
labAccounts: SetDiff;
|
|
683
|
+
zips: {
|
|
684
|
+
added: string[];
|
|
685
|
+
removed: string[];
|
|
686
|
+
};
|
|
687
|
+
};
|
|
688
|
+
/** What changed between two recordings: coverage, catalog, accounts and raw observations. */
|
|
689
|
+
declare const diffCorpus: (before: SealedCorpus, after: SealedCorpus) => CorpusDiff;
|
|
690
|
+
|
|
202
691
|
/** High-signal ZIPs for availability prefetch (full routing corpus still used for area/psc). */
|
|
203
692
|
declare const PSC_AVAILABILITY_ZIPS: readonly ["85004", "85234", "11050", "10006", "90012", "96101", "92101", "60601", "33130", "98104", "75034", "07030"];
|
|
204
693
|
/**
|
|
@@ -483,6 +972,105 @@ declare const reshapeCoverageGeoCommand: (command: LogicalCommand, _state: Explo
|
|
|
483
972
|
labIds?: readonly number[];
|
|
484
973
|
}) => LogicalCommand;
|
|
485
974
|
|
|
975
|
+
type JunctionRuntimeOptions = {
|
|
976
|
+
/**
|
|
977
|
+
* Recorded vendor data every namespace starts from. The served mock loads the corpus
|
|
978
|
+
* shipped at `@crvouga/mockingbird-service-junction/corpus` unless told otherwise.
|
|
979
|
+
*/
|
|
980
|
+
corpus?: SealedCorpus;
|
|
981
|
+
/** Default: `corpus` when a corpus is loaded, otherwise `synthetic`. */
|
|
982
|
+
geo?: GeoMode;
|
|
983
|
+
/** The team's lab accounts; see {@link JunctionAPIOptions.labAccounts}. */
|
|
984
|
+
labAccounts?: readonly LabAccountInput[];
|
|
985
|
+
/** Deliver signed webhooks here, with Svix's retry schedule. */
|
|
986
|
+
webhooks?: WebhookEndpoint;
|
|
987
|
+
/** Called in-process for every webhook event, delivered or not. */
|
|
988
|
+
onWebhook?: (event: Parameters<WebhookPublisher>[0], namespace: string) => void;
|
|
989
|
+
sqlite?: SqliteClient;
|
|
990
|
+
clock?: Clock;
|
|
991
|
+
/** Seeds fault rates and webhook retry jitter. */
|
|
992
|
+
seed?: number | string;
|
|
993
|
+
/** Require `x-mockingbird-admin-key` on `/__admin/*`. */
|
|
994
|
+
adminKey?: string;
|
|
995
|
+
onLog?: (entry: RequestLog) => void;
|
|
996
|
+
};
|
|
997
|
+
type JunctionRuntime = ServiceRuntime<JunctionAPI> & {
|
|
998
|
+
/** Signed delivery, when `webhooks` was configured. */
|
|
999
|
+
readonly webhooks: WebhookDispatcher | undefined;
|
|
1000
|
+
};
|
|
1001
|
+
/**
|
|
1002
|
+
* The Junction mock with Mockingbird's full service contract: unauthenticated
|
|
1003
|
+
* `/health`, the `/__admin/*` control plane, per-request namespaces
|
|
1004
|
+
* (`x-mockingbird-namespace`), clock control, fault injection and request metrics.
|
|
1005
|
+
* Runtime-neutral: serve it with any Fetch-native server, or use `./server` for Node.
|
|
1006
|
+
*/
|
|
1007
|
+
declare const createRuntime: (options?: JunctionRuntimeOptions) => JunctionRuntime;
|
|
1008
|
+
|
|
1009
|
+
/**
|
|
1010
|
+
* Appointment scheduling surface: phlebotomy + PSC availability/booking lifecycle,
|
|
1011
|
+
* cancellation reasons, area serviceability, and PSC site info.
|
|
1012
|
+
*
|
|
1013
|
+
* Slot data is generated deterministically (seeded by zip/site/date), so walks are
|
|
1014
|
+
* reproducible without any provider-side state.
|
|
1015
|
+
*/
|
|
1016
|
+
|
|
1017
|
+
/** Status of the unknown-ZIP error: unused by Junction, and not retried by HTTP clients. */
|
|
1018
|
+
declare const UNKNOWN_ZIP_STATUS = 424;
|
|
1019
|
+
declare const UNKNOWN_ZIP_ERROR_TYPE = "MOCKINGBIRD_UNKNOWN_ZIP";
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* Differential verification: the same requests against real Junction and the mock,
|
|
1023
|
+
* with every divergence reported. Two checks:
|
|
1024
|
+
*
|
|
1025
|
+
* - drift: each recorded corpus observation re-fetched from the real API. A stale
|
|
1026
|
+
* entry means the corpus no longer matches production and should be re-pulled.
|
|
1027
|
+
* - scenario: a fixed script of stateful calls (users, catalog, serviceability, and
|
|
1028
|
+
* optionally orders) run against both sides, comparing status codes, response
|
|
1029
|
+
* shapes, and — for the documented error bodies — exact bytes.
|
|
1030
|
+
*
|
|
1031
|
+
* Only a sandbox team should be used: the scenario creates, and always deletes, one
|
|
1032
|
+
* user (and, with `orders`, places and cancels one order).
|
|
1033
|
+
*/
|
|
1034
|
+
type VerifyOptions = {
|
|
1035
|
+
realKey: string;
|
|
1036
|
+
realUrl?: string;
|
|
1037
|
+
/** The mock under test, e.g. `createRuntime({ corpus })`. */
|
|
1038
|
+
mock: {
|
|
1039
|
+
fetch(request: Request): Promise<Response>;
|
|
1040
|
+
};
|
|
1041
|
+
corpus: SealedCorpus;
|
|
1042
|
+
/** Re-fetch at most this many observations (spread evenly). Default: all of them. */
|
|
1043
|
+
sample?: number;
|
|
1044
|
+
/** Skip the drift check. */
|
|
1045
|
+
skipDrift?: boolean;
|
|
1046
|
+
/** Also place, read and cancel an order on the real team. */
|
|
1047
|
+
orders?: boolean;
|
|
1048
|
+
fetch?: (request: Request) => Promise<Response>;
|
|
1049
|
+
minIntervalMs?: number;
|
|
1050
|
+
onProgress?: (message: string) => void;
|
|
1051
|
+
};
|
|
1052
|
+
type Divergence = {
|
|
1053
|
+
check: string;
|
|
1054
|
+
kind: "status" | "shape" | "body" | "error";
|
|
1055
|
+
/** JSON path of the first difference, when there is one. */
|
|
1056
|
+
at?: string;
|
|
1057
|
+
real: unknown;
|
|
1058
|
+
mock: unknown;
|
|
1059
|
+
};
|
|
1060
|
+
type VerifyReport = {
|
|
1061
|
+
ok: boolean;
|
|
1062
|
+
drift: {
|
|
1063
|
+
checked: number;
|
|
1064
|
+
stale: number;
|
|
1065
|
+
};
|
|
1066
|
+
scenario: {
|
|
1067
|
+
steps: number;
|
|
1068
|
+
divergent: number;
|
|
1069
|
+
};
|
|
1070
|
+
divergences: Divergence[];
|
|
1071
|
+
};
|
|
1072
|
+
declare const verifyAgainstReal: (options: VerifyOptions) => Promise<VerifyReport>;
|
|
1073
|
+
|
|
486
1074
|
declare const JUNCTION_NAMESPACE = "junction";
|
|
487
1075
|
/**
|
|
488
1076
|
* Stateful mock of the Junction (Vital) API user and lab-testing surfaces.
|
|
@@ -493,13 +1081,38 @@ declare const JUNCTION_NAMESPACE = "junction";
|
|
|
493
1081
|
type JunctionAPIOptions = APIOptions & {
|
|
494
1082
|
onWebhook?: WebhookPublisher;
|
|
495
1083
|
webhook?: JunctionWebhookOptions;
|
|
1084
|
+
/** Install a recorded corpus at construction; it is re-applied on every `reset()`. */
|
|
1085
|
+
corpus?: SealedCorpus;
|
|
1086
|
+
/**
|
|
1087
|
+
* How serviceability reads answer a ZIP the corpus does not cover. Default: `corpus`
|
|
1088
|
+
* when a corpus is given (refuse, loudly), otherwise `synthetic` (invent coverage).
|
|
1089
|
+
*/
|
|
1090
|
+
geo?: GeoMode;
|
|
1091
|
+
/**
|
|
1092
|
+
* The team's lab accounts, replacing the built-in fixtures (and any from the corpus).
|
|
1093
|
+
* `create_order` routes, accepts and rejects `lab_account_id` against these.
|
|
1094
|
+
*/
|
|
1095
|
+
labAccounts?: readonly LabAccountInput[];
|
|
1096
|
+
};
|
|
1097
|
+
/** What a loaded corpus covers, for `/health` and startup logs. */
|
|
1098
|
+
type CorpusInfo = {
|
|
1099
|
+
label: string;
|
|
1100
|
+
recordedAt: string;
|
|
1101
|
+
source: string;
|
|
1102
|
+
observations: number;
|
|
1103
|
+
labTests: number;
|
|
1104
|
+
labAccounts: number;
|
|
1105
|
+
zips: number;
|
|
496
1106
|
};
|
|
1107
|
+
/** A short, stable name for a corpus: its recording date plus a digest of what it holds. */
|
|
1108
|
+
declare const corpusLabel: (corpus: SealedCorpus) => string;
|
|
497
1109
|
declare class JunctionAPI implements FetchAPI {
|
|
498
1110
|
readonly app: Hono;
|
|
499
1111
|
readonly sqlite: SqliteClient;
|
|
500
1112
|
private readonly service;
|
|
501
1113
|
private readonly state;
|
|
502
1114
|
private corpus;
|
|
1115
|
+
private labAccountConfig;
|
|
503
1116
|
constructor(options?: JunctionAPIOptions);
|
|
504
1117
|
fetch(request: Request): Promise<Response>;
|
|
505
1118
|
reset(): Promise<void>;
|
|
@@ -508,6 +1121,32 @@ declare class JunctionAPI implements FetchAPI {
|
|
|
508
1121
|
* labs, and lab accounts with the recording's exact values. Re-applied on `reset()`.
|
|
509
1122
|
*/
|
|
510
1123
|
installCorpus(corpus: SealedCorpus): void;
|
|
1124
|
+
/**
|
|
1125
|
+
* Which lab accounts the ordering rules see, in order of precedence: the configured
|
|
1126
|
+
* ones, then a corpus's real accounts, then the built-in fixtures (which a corpus
|
|
1127
|
+
* recorded from a team with no accounts leaves in place).
|
|
1128
|
+
*/
|
|
1129
|
+
private seedLabAccounts;
|
|
1130
|
+
/** Replace the team's lab accounts; `undefined` restores the default. Kept across resets. */
|
|
1131
|
+
configureLabAccounts(accounts: readonly LabAccountInput[] | undefined): StoredLabAccount[];
|
|
1132
|
+
labAccounts(): StoredLabAccount[];
|
|
1133
|
+
corpusInfo(): CorpusInfo | undefined;
|
|
1134
|
+
get geoMode(): GeoMode;
|
|
1135
|
+
set geoMode(mode: GeoMode);
|
|
1136
|
+
order(id: string): OrderRecord | undefined;
|
|
1137
|
+
orders(): OrderRecord[];
|
|
1138
|
+
/**
|
|
1139
|
+
* Put an order straight into a status (a full `<status>.<method>.<event>` value) and
|
|
1140
|
+
* publish the webhook a real transition would. `flags` takes the same keys as
|
|
1141
|
+
* `simulate_order`'s body: `interpretation`, `result_types`, `has_missing_results`.
|
|
1142
|
+
*/
|
|
1143
|
+
transitionOrder(id: string, status: string, options: {
|
|
1144
|
+
now: () => number;
|
|
1145
|
+
flags?: Record<string, unknown> | null;
|
|
1146
|
+
}): OrderRecord | undefined;
|
|
1147
|
+
/** Serve `fixture` as this order's results, replacing the generated ones. */
|
|
1148
|
+
installResultFixture(orderId: string, fixture: ResultFixture): boolean;
|
|
1149
|
+
resultFixture(orderId: string): ResultFixture | undefined;
|
|
511
1150
|
seedFrom(source: SeedSource, observations?: SeedObservations): Promise<SeedReport>;
|
|
512
1151
|
ensureLabTests(source: SeedSource, ids: readonly string[]): Promise<number>;
|
|
513
1152
|
ensureOrders(source: SeedSource, ids: readonly string[]): Promise<number>;
|
|
@@ -516,5 +1155,5 @@ declare class JunctionAPI implements FetchAPI {
|
|
|
516
1155
|
webhookDeliveryAttempts(): WebhookDeliveryAttempt[];
|
|
517
1156
|
}
|
|
518
1157
|
|
|
519
|
-
export { AVAILABILITY_ADDRESS, AVAILABILITY_START_DATE, COVERAGE_ZIPS, JUNCTION_NAMESPACE, JunctionAPI, PHLEBOTOMY_AVAILABILITY_ZIPS, PSC_AVAILABILITY_ZIPS, PSC_LAB_IDS, SEALED_CORPUS_VERSION, document, observationCacheKey, operationIds, parseSealedCorpus, prefetchCoverageObservations, reshapeCoverageGeoCommand, supportedOperationIds };
|
|
520
|
-
export type { FetchAPI, GetCacheEntry, JunctionAPIOptions, JunctionWebhookEvent, JunctionWebhookOptions, OperationId, SealedCorpus, SeedObservations, SeedReport, SeedSource, SqliteClient, SupportedOperationId, WebhookPublisher };
|
|
1158
|
+
export { AVAILABILITY_ADDRESS, AVAILABILITY_START_DATE, COVERAGE_ZIPS, DEFAULT_JUNCTION_BASE_URL, FAULT_PRESETS, JUNCTION_NAMESPACE, JunctionAPI, ORDER_STATUSES_BY_METHOD, PHLEBOTOMY_AVAILABILITY_ZIPS, PSC_AVAILABILITY_ZIPS, PSC_LAB_IDS, RESULT_FIXTURES, SANDBOX_KEY_PREFIXES, SEALED_CORPUS_VERSION, TEAM_LAB_ACCOUNTS, TRANSITION_ALIASES, UNKNOWN_ZIP_ERROR_TYPE, UNKNOWN_ZIP_STATUS, US_STATES, corpusLabel, createRuntime, createWebhookDispatcher, diffCorpus, document, fingerprintCorpus, isSandboxKey, labAccountFromInput, observationCacheKey, operationIds, parseSealedCorpus, prefetchCoverageObservations, pullCorpus, reshapeCoverageGeoCommand, resolveTransition, signSvix, supportedOperationIds, verifyAgainstReal, verifySvix };
|
|
1159
|
+
export type { CorpusDiff, CorpusInfo, Divergence, FetchAPI, GeoMode, GetCacheEntry, JunctionAPIOptions, JunctionAdminOptions, JunctionRuntime, JunctionRuntimeOptions, JunctionWebhookEvent, JunctionWebhookOptions, LabAccountDelegatedFlow, LabAccountInput, LabAccountRecord, LabAccountStatus, OperationId, OrderRecord, PullCorpusOptions, ResultFixture, SealedCorpus, SeedObservations, SeedReport, SeedSource, SetDiff, SqliteClient, StoredLabAccount, SupportedOperationId, VerifyOptions, VerifyReport, WebhookAttempt, WebhookDelivery, WebhookDispatcher, WebhookEndpoint, WebhookPublisher };
|