@ultimat3/testing 30.7.0 → 30.9.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/CLAUDE.md +5 -9
- package/README.md +1 -1
- package/package.json +17 -17
- package/src/define-exercise.ts +36 -12
- package/src/drill-writes.ts +3 -1
- package/src/errors.ts +24 -93
- package/src/exercise-actor.ts +28 -4
- package/src/exercise-judge.ts +78 -0
- package/src/exercise-observed.ts +93 -0
- package/src/exercise-override-input.ts +51 -9
- package/src/exercise-report.ts +5 -0
- package/src/exercise-request.ts +7 -24
- package/src/exercise-setup.ts +34 -17
- package/src/exercise-subjects.ts +20 -5
- package/src/exercise-sweeps.ts +12 -7
- package/src/exercise.ts +134 -119
- package/src/fixture-island.ts +1 -1
- package/src/fixture-jobs.ts +23 -4
- package/src/island-errors.ts +82 -0
- package/src/plan-conditions.ts +2 -2
- package/src/plan-oracle.ts +38 -11
- package/src/plan-probes.ts +94 -0
- package/src/plan-sweep-fix.ts +12 -1
- package/src/plan-sweep.ts +37 -12
- package/src/scale-sweep-graph.ts +3 -0
- package/src/scale-sweep-run.ts +17 -8
- package/src/sealed-dns-resolver.ts +56 -0
- package/src/sealed-dns.ts +63 -64
- package/src/sealed-network.ts +13 -12
- package/src/sealed-node-sockets.ts +63 -0
- package/src/sealed-sockets.ts +13 -8
- package/src/sealed-udp.ts +118 -0
- package/src/seed-graph-plan.ts +11 -0
- package/src/seed-graph.ts +9 -2
- package/src/subject-proof.ts +12 -2
- package/src/tenant-proof-report.ts +79 -29
- package/src/tenant-proof.ts +7 -2
package/CLAUDE.md
CHANGED
|
@@ -16,7 +16,7 @@ import is `registry-isolation.ts`, own entry point, off the barrel. Full rule: `
|
|
|
16
16
|
| No mocks of the DB | clone a template database; `template-db.ts` is the only DB path |
|
|
17
17
|
| No wall clock | `withFrozenClock` / `advanceClock`; `Date.now()` is frozen by the preload |
|
|
18
18
|
| Frozen ≠ different | `globalThis.Date` becomes a subclass; `FrozenDate[Symbol.hasInstance]` brands on the `[[DateValue]]` slot (`Date.prototype.getTime.call(value)` throws or not) |
|
|
19
|
-
| No unmocked egress | `sealed-network.ts` seals `fetch`; `sealed-sockets.ts` `WebSocket`, `Bun.connect` (not loopback); `sealed-dns
|
|
19
|
+
| No unmocked egress | `sealed-network.ts` seals `fetch`; `sealed-sockets.ts` `WebSocket`, `Bun.connect`, `node:*`, UDP (not loopback); `sealed-dns*.ts` every resolver, named imports too (wiki `Testing.md`); a miss is `X_TEST_NETWORK_SEALED` |
|
|
20
20
|
| One `mockFetch`, and it grows | a verb in the match, ordered answers, a pinned dial's name (`mock-route.ts`) and `requestLog` (`request-log.ts`). A stream bound for a real server logs `null` (teeing drains it). `original(input, init)` goes as handed: `tls`, `unix`, `proxy` are in no `Request`. `cassette()`: below |
|
|
21
21
|
| Self is not egress | a port core's `markListening()` announced passes through — a socket test never unseals |
|
|
22
22
|
| Offline is a state, not a mock | `network.offline()` / `.drop()` fail every dial, loopback too, as `X_TEST_NETWORK_OFFLINE` before any mock: the app's offline path runs |
|
|
@@ -35,7 +35,7 @@ import is `registry-isolation.ts`, own entry point, off the barrel. Full rule: `
|
|
|
35
35
|
| Every message thunk needs a test that PROVOKES it | a thunk is a function, so an unread message is an UNCOVERED function and `bun run scripts/coverage-gate.ts --package testing` is what says so |
|
|
36
36
|
| Test names | the filename picks the step; a typed helper prefixes its own name. `testName(type, name)` is for an outer `describe` only — never on the inner `test` too, the prefix would print twice |
|
|
37
37
|
| One body shape, five registrars | `test` (= `fixtureTest`), `unitTest`, `contractTest`, `liveTest`, `jobTest` all take `FixtureBody` + `{ timeoutMs }`: `unitTest('…', async ({ runJobs }) => …)`. Never `describe(testName('unit', …))` + `test` to get a fixture |
|
|
38
|
-
| `runJobs` says who and what | `{ actor }` is the WORKER's identity (the org stays the job's declared tenant), `{ tenantId }` the enqueuer's row; `executions[n].result` is the body's return. A fresh event bus per fixture |
|
|
38
|
+
| `runJobs` says who and what | `{ actor }` is the WORKER's identity (the org stays the job's declared tenant), `{ tenantId }` the enqueuer's row; `executions[n].result` is the body's return. A fresh event bus per fixture. Bodies run on the preload's frozen clock (`ctx.clock`), so a `backfill()` throttle moves `Date.now()` instead of sleeping |
|
|
39
39
|
| A `runJobs` pass is a real worker's `tick()` | `fixture-jobs.ts` builds an unstarted `jobWorker` per pass, so admission runs: `concurrency` waits and `whenBusy: 'fail'` refuses (`X_JOB_KEY_BUSY`) exactly as in a fleet. Full rule: `docs/history/testing.md` |
|
|
40
40
|
| Per-test state is `per-test-reset.ts` | the app preload's `beforeEach` resets the jobs event bus — only when `@ultimat3/jobs` is already loaded, so the reset never loads it |
|
|
41
41
|
| A `mock.module` ends with its file | `module-mock-boundary.ts` wraps it (and `vi.mock`/`jest.mock`): the real namespace is snapshotted on a file's first mock, so its top-level code RUNS, and mocked back at the boundary. A preload's mock is left. A real one that throws on load: the next file fails `X_TEST_MODULE_MOCK_UNRESTORABLE` |
|
|
@@ -51,7 +51,7 @@ import is `registry-isolation.ts`, own entry point, off the barrel. Full rule: `
|
|
|
51
51
|
| A passkey test signs for real | `softAuthenticator` is `@ultimat3/auth/soft-authenticator`'s, never re-exported here (one value, one path: `flight-copies`) and never a second one (auth's own suites need it, and auth cannot import testing) |
|
|
52
52
|
| A reached job is crash-proven | `exercise-crash.ts`: `crashProof` with the control's input and actor, after the tenant proof; only `diverged` is red (`X_JOB_CRASH_DIVERGED`), its place `crashEffectPlace` (`inside` a step via jobs' `currentStep()`, or `after` one). Its world is `alone`: only the job's own row is claimed, a child is an `enqueue:` effect, never run. Committed: job bodies escape transactions |
|
|
53
53
|
| The tenant proof is taint only | `tenant-proof.ts`: B, A, B; B is A's role minus `tenancy:cross`. Leak = seeded canary or unhanded id (`tenant-canary.ts`); write = moved digest. Only `crossed` is red |
|
|
54
|
-
| Every statement the pass sent is planned | `plan-sweep.ts` (`sweepPlans`): first relation an app table, `PREPARE` in a READ-ONLY transaction with seq/bitmap scans
|
|
54
|
+
| Every statement the pass sent is planned | `plan-sweep.ts` (`sweepPlans`): first relation an app table, `PREPARE` in a READ-ONLY transaction with seq/bitmap scans off (sorts too, re-planned, for a limited read's sort), judged by `plan-oracle.ts` (keys: `plan-sort-key.ts`, conditions: `plan-conditions.ts`); red = `X_QUERY_UNINDEXED` / `X_QUERY_SORT_UNSERVED` with the `indexes:` entry (`plan-sweep-fix.ts`). `scan:` or `expectedSeqScan` is exempt, listed; credited: a partial unique whose predicate the statement states, equality-then-range index under a limit, every `or` arm a declared index leads, a whole read a keyed join side probes by a declared index (`plan-probes.ts`); no generic plan is `X_QUERY_PLAN_UNAVAILABLE`, never a pass |
|
|
55
55
|
| A shape that grows with the rows is red | `scale-sweep-run.ts`: two more tenants at `SCALE_SIZES` (2, 8 rows a table), every reached primitive once in each; `judgeScale` (pure) reds `X_QUERY_SCALES_WITH_ROWS`. Under `expectedQueryLoop` it is listed with its reason |
|
|
56
56
|
| An erase is PROVED, not asserted | `subject-proof.ts` (`subjectProof`, run by cli's `exercise-subject.ts`): canaries in every table, one person erased, every stored row searched; a survivor outside `retain` is `X_SUBJECT_ERASE_INCOMPLETE`, naming table and column, never the value |
|
|
57
57
|
| A captured failure replays as a test | `repro.ts`: `reproduce(bundle, { driver })` seeds the rows it read, freezes the clock at its instant, invokes as its actor; masked fields are stood in (`repro-stand-in.ts`). An unregistered name ANSWERS as the failure. `reproJob(runJobs, failure)` for a dead job. What `x repro --write` emits |
|
|
@@ -121,9 +121,7 @@ Commands: `bun test`, `bunx tsc --noEmit -p tsconfig.json`.
|
|
|
121
121
|
Entry points: `.` (the API), `./preload` (side effects for bunfig) and `./registry-isolation`
|
|
122
122
|
(`isolateEntityRegistry()`, kept off `.` because it loads `@ultimat3/entity`).
|
|
123
123
|
|
|
124
|
-
|
|
125
|
-
import `@ultimat3/core` and nothing else, so they cost a tier-0 test nothing. The whole-chain proof is
|
|
126
|
-
`examples/dummy/apps/web/app/settings/settings.island.test.ts`. Full rule: `docs/history/testing.md`.
|
|
124
|
+
The island fixture's cost and its whole-chain proof: `docs/history/testing.md`.
|
|
127
125
|
|
|
128
126
|
## The browser-backed e2e driver (moved here from `@ultimat3/cli` in 22.0.0)
|
|
129
127
|
|
|
@@ -150,9 +148,7 @@ Raw CDP: `@ultimat3/core` and Bun, nothing else; `cli` imports it over `cli -> t
|
|
|
150
148
|
| `packages/cli/src/verify-e2e.ts` | `withE2eApp` (cli's): in an app on a machine with Chrome, the `e2e` step runs the suite with that preload; otherwise exactly as before |
|
|
151
149
|
| `cdp-errors.ts` | one constructor per way the browser half refuses |
|
|
152
150
|
|
|
153
|
-
**Absent by default
|
|
154
|
-
answers `false` and the gate's `e2e` step refuses rather than passing over a browser it lacks.
|
|
155
|
-
GitHub-hosted `ubuntu-latest` ships Chrome at `/usr/bin/google-chrome`.
|
|
151
|
+
**Absent by default** until `installE2eDriver`: [`docs/history/testing.md`](../../docs/history/testing.md#the-browser-backed-e2e-driver-moved-here-from-ultimat3cli-in-2200).
|
|
156
152
|
|
|
157
153
|
How the raw-CDP driver behaves — the pipe, the load event, deadlines, the lossy `evaluate`, the
|
|
158
154
|
refusing `update()` — is [`docs/history/testing.md`](../../docs/history/testing.md#the-e2e-driver-in-brief-moved-2026-10-01).
|
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ unitTest('the clock moves only when the test moves it', ({ clock }) => {
|
|
|
26
26
|
| `sealed-sockets.ts` | the `WebSocket` and `Bun.connect` patches, behind `sealed-network.ts`'s one gate |
|
|
27
27
|
| `factories.ts` | `defineFactory` — seeded rows, traits, associations, `build` vs `create` |
|
|
28
28
|
| `factory-registry.ts` | `factoriesFor(registry)` — one factory per entity, each value read off its column's declaration (default, enum, kind, money, nullability) through `$parse`, never off its name |
|
|
29
|
-
| `seed-graph.ts` | `seedGraph(entities, { tenants, perTable, seed, factories })` — rows for every table with no factory written: parents first, same-tenant keys, every declared rule kept. Pure, same seed same bytes |
|
|
29
|
+
| `seed-graph.ts` | `seedGraph(entities, { tenants, perTable, seed, factories, beside })` — rows for every table with no factory written: parents first, same-tenant keys, every declared rule kept. Pure, same seed same bytes |
|
|
30
30
|
| `repro.ts` | `reproduce(bundle, { driver })` and `reproJob(runJobs, failure)` — what a test `x repro --write` generated calls: captured reads seeded, clock frozen at the failure, the same actor and input (masked fields stood in from the schema); a dead job's completed steps replayed from storage |
|
|
31
31
|
| `seed-graph-plan.ts` | the plan's types and `insertSeedGraph(graph, writer)` — every insert, then every deferred key, sequentially |
|
|
32
32
|
| `seed-graph-values.ts` | one column's candidate values, off its declaration, kept only where `$parse` accepts them — `factoriesFor`'s source too |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/testing",
|
|
3
|
-
"version": "30.
|
|
3
|
+
"version": "30.9.0",
|
|
4
4
|
"description": "Test harness: cloned template DBs per worker, frozen clock, sealed network, 6 test types",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -46,21 +46,21 @@
|
|
|
46
46
|
"test": "bun test"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@ultimat3/action": "30.
|
|
50
|
-
"@ultimat3/cache": "30.
|
|
51
|
-
"@ultimat3/core": "30.
|
|
52
|
-
"@ultimat3/db": "30.
|
|
53
|
-
"@ultimat3/entity": "30.
|
|
54
|
-
"@ultimat3/http": "30.
|
|
55
|
-
"@ultimat3/i18n": "30.
|
|
56
|
-
"@ultimat3/jobs": "30.
|
|
57
|
-
"@ultimat3/mail": "30.
|
|
58
|
-
"@ultimat3/policy": "30.
|
|
59
|
-
"@ultimat3/pwa": "30.
|
|
60
|
-
"@ultimat3/query": "30.
|
|
61
|
-
"@ultimat3/realtime": "30.
|
|
62
|
-
"@ultimat3/render": "30.
|
|
63
|
-
"@ultimat3/schema": "30.
|
|
64
|
-
"@ultimat3/time": "30.
|
|
49
|
+
"@ultimat3/action": "30.9.0",
|
|
50
|
+
"@ultimat3/cache": "30.9.0",
|
|
51
|
+
"@ultimat3/core": "30.9.0",
|
|
52
|
+
"@ultimat3/db": "30.9.0",
|
|
53
|
+
"@ultimat3/entity": "30.9.0",
|
|
54
|
+
"@ultimat3/http": "30.9.0",
|
|
55
|
+
"@ultimat3/i18n": "30.9.0",
|
|
56
|
+
"@ultimat3/jobs": "30.9.0",
|
|
57
|
+
"@ultimat3/mail": "30.9.0",
|
|
58
|
+
"@ultimat3/policy": "30.9.0",
|
|
59
|
+
"@ultimat3/pwa": "30.9.0",
|
|
60
|
+
"@ultimat3/query": "30.9.0",
|
|
61
|
+
"@ultimat3/realtime": "30.9.0",
|
|
62
|
+
"@ultimat3/render": "30.9.0",
|
|
63
|
+
"@ultimat3/schema": "30.9.0",
|
|
64
|
+
"@ultimat3/time": "30.9.0"
|
|
65
65
|
}
|
|
66
66
|
}
|
package/src/define-exercise.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// `defineExercise({ publishPost: { input, actor }, sendInvoice: { off: 'why' } })`: an app's ONE
|
|
2
|
-
// declaration of each primitive's exercise
|
|
3
|
-
//
|
|
4
|
-
//
|
|
2
|
+
// declaration of each primitive's exercise, read by `x exercise` and every `.contract({ exercise })`.
|
|
3
|
+
// A blank `off` is refused at declaration, an unknown name when the pass runs. A key is a bare name
|
|
4
|
+
// or a kind-qualified one (`'job:eraseAccount'`), which wins over the bare name for its kind.
|
|
5
5
|
|
|
6
6
|
import type { ExerciseOptions } from '@ultimat3/action';
|
|
7
7
|
import { renderCauseValue, renderFixLiteral, UltimateError } from '@ultimat3/core';
|
|
@@ -24,8 +24,26 @@ export interface ExerciseOverrides {
|
|
|
24
24
|
readonly actorColumn?: string;
|
|
25
25
|
/** By entity name: the pass's graph builds those rows with them (`seedGraph`'s `factories`). */
|
|
26
26
|
readonly factories?: Readonly<Record<string, SeedFactory>>;
|
|
27
|
-
/** The app's
|
|
28
|
-
readonly setup?: ExerciseSetup;
|
|
27
|
+
/** The app's hooks, run once each, in order, inside the pass's sealed room (`exercise-setup.ts`). */
|
|
28
|
+
readonly setup?: ExerciseSetup | readonly ExerciseSetup[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The three kinds an entry may be qualified by. */
|
|
32
|
+
export type ExerciseKind = 'action' | 'query' | 'job';
|
|
33
|
+
|
|
34
|
+
/** `'job:eraseAccount'`: the key that names one kind's primitive when another shares its name. */
|
|
35
|
+
export const exerciseEntryKey = (kind: ExerciseKind, name: string): string => `${kind}:${name}`;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The entry `kind` `name` runs with: its qualified key's, else its bare name's. An action and a job
|
|
39
|
+
* sharing a name each read their own — the bare key alone fed one's input to the other.
|
|
40
|
+
*/
|
|
41
|
+
export function overrideFor(
|
|
42
|
+
overrides: ExerciseOverrides | undefined,
|
|
43
|
+
kind: ExerciseKind,
|
|
44
|
+
name: string,
|
|
45
|
+
): ExerciseOverride | undefined {
|
|
46
|
+
return overrides?.entries.get(exerciseEntryKey(kind, name)) ?? overrides?.entries.get(name);
|
|
29
47
|
}
|
|
30
48
|
|
|
31
49
|
/** What the whole pass needs to know of the app, beside the per-primitive entries. */
|
|
@@ -49,9 +67,10 @@ export interface ExerciseAppOptions {
|
|
|
49
67
|
* Run in the exercise process once the network is sealed, before the first row: install the
|
|
50
68
|
* `mockFetch` routes or `cassette()` your vendor-calling handlers need, so they run for real.
|
|
51
69
|
* What it returns (a cassette, any `Disposable`) is disposed after the pass. `allowHost` here is
|
|
52
|
-
* `X_EXERCISE_SETUP_UNSEALED` — the pass never sends a call out.
|
|
70
|
+
* `X_EXERCISE_SETUP_UNSEALED` — the pass never sends a call out. An array runs in order and is
|
|
71
|
+
* disposed in reverse; one that throws disposes those that already ran.
|
|
53
72
|
*/
|
|
54
|
-
readonly setup?: ExerciseSetup;
|
|
73
|
+
readonly setup?: ExerciseSetup | readonly ExerciseSetup[];
|
|
55
74
|
}
|
|
56
75
|
|
|
57
76
|
/** `X_CONTRACT_EXERCISE_UNDECLARED`, 39's code for the same silence: an `off` with no reason. */
|
|
@@ -86,16 +105,21 @@ export function defineExercise(
|
|
|
86
105
|
}
|
|
87
106
|
|
|
88
107
|
/**
|
|
89
|
-
* Every override names a registered action, query or job —
|
|
90
|
-
*
|
|
91
|
-
*
|
|
108
|
+
* Every override names a registered action, query or job — bare, or qualified by the kind it is
|
|
109
|
+
* registered as — checked when the pass runs, because that is when the registries are full.
|
|
110
|
+
* `X_DECLARATION_UNKNOWN`, the code an unknown name answers everywhere else; the cause lists what
|
|
111
|
+
* IS registered so the typo is visible.
|
|
92
112
|
*/
|
|
93
113
|
export function assertOverridesKnown(
|
|
94
114
|
overrides: ExerciseOverrides | undefined,
|
|
95
|
-
|
|
115
|
+
registered: readonly { readonly kind: ExerciseKind; readonly name: string }[],
|
|
96
116
|
): void {
|
|
97
|
-
const
|
|
117
|
+
const keys = new Set(
|
|
118
|
+
registered.flatMap(({ kind, name }) => [name, exerciseEntryKey(kind, name)]),
|
|
119
|
+
);
|
|
120
|
+
const unknown = [...(overrides?.entries.keys() ?? [])].filter((key) => !keys.has(key)).sort();
|
|
98
121
|
if (unknown.length === 0) return;
|
|
122
|
+
const known = new Set(registered.map(({ name }) => name));
|
|
99
123
|
throw unknownDeclaration(unknown, known, 'defineExercise() overrides');
|
|
100
124
|
}
|
|
101
125
|
|
package/src/drill-writes.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { bindDrawn } from '@ultimat3/core';
|
|
7
7
|
import type { StandardSchemaV1 } from '@ultimat3/schema';
|
|
8
8
|
import type { ExerciseOverrides } from './define-exercise';
|
|
9
|
+
import { overrideFor } from './define-exercise';
|
|
9
10
|
import type { DrillTarget } from './drill-load';
|
|
10
11
|
import type { Sampler } from './exercise-input';
|
|
11
12
|
import { loadSampler } from './exercise-input';
|
|
@@ -53,7 +54,8 @@ function verdictOf(
|
|
|
53
54
|
if (/[:*]/.test(candidate.path))
|
|
54
55
|
return { reason: 'its path takes a parameter the load cannot bind' };
|
|
55
56
|
if (!candidate.anonymous) return { reason: 'its policy refuses an anonymous caller' };
|
|
56
|
-
|
|
57
|
+
// Kind-aware, as the pass: an `'action:<name>'` entry wins over a bare one a job may share.
|
|
58
|
+
const override = overrideFor(overrides, 'action', candidate.name);
|
|
57
59
|
if (override === undefined) return { bind: undefined };
|
|
58
60
|
if ('off' in override) return { reason: `exercise.ts turns it off: ${override.off.trim()}` };
|
|
59
61
|
if (override.actor !== null) return { reason: 'exercise.ts runs it as a signed-in actor' };
|
package/src/errors.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
// The X_* codes owned by @ultimat3/testing. A test failure has to be as actionable as a runtime
|
|
2
2
|
// failure — the fix line here is the mock to add, the service to start, or the seed to freeze.
|
|
3
|
-
import {
|
|
4
|
-
registerErrorCodes,
|
|
5
|
-
renderCauseValue,
|
|
6
|
-
renderFixLiteral,
|
|
7
|
-
UltimateError,
|
|
8
|
-
} from '@ultimat3/core';
|
|
3
|
+
import { registerErrorCodes, UltimateError } from '@ultimat3/core';
|
|
9
4
|
|
|
10
5
|
// `X_TEST_REGISTRY_LEAK` is declared below, in this registry; its message lives beside it.
|
|
11
6
|
export { RegistryLeakError } from './registry-leak-error';
|
|
@@ -72,6 +67,7 @@ export const TESTING_ERROR_CODES = [
|
|
|
72
67
|
'X_EXERCISE_SETUP_UNSEALED',
|
|
73
68
|
'X_JOB_CRASH_UNBOUNDED',
|
|
74
69
|
'X_EXERCISE_ACTOR_USER_UNSEATED',
|
|
70
|
+
'X_EXERCISE_INPUT_THREW',
|
|
75
71
|
] as const;
|
|
76
72
|
|
|
77
73
|
export type TestingErrorCode = (typeof TESTING_ERROR_CODES)[number];
|
|
@@ -139,6 +135,7 @@ export const TESTING_ERROR_TITLES: Readonly<Record<TestingErrorCode, string>> =
|
|
|
139
135
|
X_EXERCISE_SETUP_UNSEALED: 'an exercise setup let a host through the seal',
|
|
140
136
|
X_JOB_CRASH_UNBOUNDED: 'a crash proof needs more kill points than its bound',
|
|
141
137
|
X_EXERCISE_ACTOR_USER_UNSEATED: "the exercise pass could not seat its actor's x_users row",
|
|
138
|
+
X_EXERCISE_INPUT_THREW: "an exercise entry's input threw",
|
|
142
139
|
};
|
|
143
140
|
|
|
144
141
|
// Titles must be registered for `format()` to render the contract's first line. Every code above is
|
|
@@ -153,6 +150,25 @@ registerErrorCodes(
|
|
|
153
150
|
// No `docs:` below: `UltimateError` fills core's `ERROR_DOCS_URL`, one page per code set (a wiki
|
|
154
151
|
// TABLE ROW has no anchor; per-code links answered 404 until 9.x). Restating it here would drift.
|
|
155
152
|
|
|
153
|
+
/** Which dial refused, and so which repair: `dns-record` is a query no mock answers (an MX, a TXT,
|
|
154
|
+
* a reverse lookup), so its fix opens with the one line that lifts it. */
|
|
155
|
+
export type SealedTransport = 'fetch' | 'socket' | 'dns' | 'dns-record';
|
|
156
|
+
|
|
157
|
+
const sealedFix = (url: string, transport: SealedTransport): string => {
|
|
158
|
+
const host = hostOf(url);
|
|
159
|
+
const allow = `allowHost('${host}') if it must be real`;
|
|
160
|
+
switch (transport) {
|
|
161
|
+
case 'socket':
|
|
162
|
+
return `allowHost('${host}') # only if it must be real — otherwise inject the transport: pass the code under test its \`connect\`/\`client\` option, or dial a Bun.listen server on 127.0.0.1 (loopback is never sealed)`;
|
|
163
|
+
case 'dns':
|
|
164
|
+
return `mockFetch('https://${host}/*', () => new Response('{}')) # a mocked host resolves — or ${allow}`;
|
|
165
|
+
case 'dns-record':
|
|
166
|
+
return `allowHost('${host}') # only if it must be real — a mockFetch route answers an address lookup, never this record or a reverse lookup: inject the resolver into the code under test otherwise`;
|
|
167
|
+
case 'fetch':
|
|
168
|
+
return `mockFetch('${url}', () => new Response('{}')) — or ${allow}`;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
156
172
|
/**
|
|
157
173
|
* A test reached the network without a mock or an allowlist entry. Always a bug, never a flake.
|
|
158
174
|
* `transport` picks the repair: a `fetch` is mocked, a socket (`WebSocket`, `Bun.connect`) has no
|
|
@@ -163,21 +179,14 @@ export class NetworkSealedError extends UltimateError {
|
|
|
163
179
|
url: string;
|
|
164
180
|
method: string;
|
|
165
181
|
allowed: readonly string[];
|
|
166
|
-
transport?:
|
|
182
|
+
transport?: SealedTransport | undefined;
|
|
167
183
|
}) {
|
|
168
|
-
const allowCall = `allowHost('${hostOf(input.url)}')`;
|
|
169
|
-
const allow = `${allowCall} if it must be real`;
|
|
170
184
|
super({
|
|
171
185
|
code: 'X_TEST_NETWORK_SEALED',
|
|
172
186
|
cause: `${input.method} ${input.url} was not mocked (allowed hosts: ${
|
|
173
187
|
input.allowed.length > 0 ? input.allowed.join(', ') : 'none'
|
|
174
188
|
})`,
|
|
175
|
-
fix:
|
|
176
|
-
input.transport === 'socket'
|
|
177
|
-
? `${allowCall} # only if it must be real — otherwise inject the transport: pass the code under test its \`connect\`/\`client\` option, or dial a Bun.listen server on 127.0.0.1 (loopback is never sealed)`
|
|
178
|
-
: input.transport === 'dns'
|
|
179
|
-
? `mockFetch('https://${hostOf(input.url)}/*', () => new Response('{}')) # a mocked host resolves — or ${allow}`
|
|
180
|
-
: `mockFetch('${input.url}', () => new Response('{}')) — or ${allow}`,
|
|
189
|
+
fix: sealedFix(input.url, input.transport ?? 'fetch'),
|
|
181
190
|
});
|
|
182
191
|
}
|
|
183
192
|
}
|
|
@@ -420,81 +429,3 @@ export class NetworkRaceError extends UltimateError {
|
|
|
420
429
|
});
|
|
421
430
|
}
|
|
422
431
|
}
|
|
423
|
-
|
|
424
|
-
/** Neither path is controlled by the framework: both arrive from the test's own call. */
|
|
425
|
-
const ISLAND_PLACEHOLDER = '<the island path the cause names>';
|
|
426
|
-
|
|
427
|
-
/**
|
|
428
|
-
* The builder produced no chunk for the file the test named. Listing what it DID build is the
|
|
429
|
-
* whole value: a path relative to the test file instead of to the app root, a typo, and an island
|
|
430
|
-
* outside the discovery glob are one symptom and three different edits, and only the list tells
|
|
431
|
-
* them apart. An empty list is its own answer — the glob found nothing at all under this root.
|
|
432
|
-
*/
|
|
433
|
-
export class IslandNotBuiltError extends UltimateError {
|
|
434
|
-
constructor(input: { file: string; root: string; built: readonly string[] }) {
|
|
435
|
-
const file = renderFixLiteral(input.file, ISLAND_PLACEHOLDER);
|
|
436
|
-
super({
|
|
437
|
-
code: 'X_TEST_ISLAND_NOT_BUILT',
|
|
438
|
-
cause:
|
|
439
|
-
input.built.length === 0
|
|
440
|
-
? `no island was built under ${renderCauseValue(input.root)}, so ${renderCauseValue(input.file)} cannot be mounted`
|
|
441
|
-
: `${renderCauseValue(input.file)} is not in the bundle built from ${renderCauseValue(input.root)}; it built ${input.built.join(', ')}`,
|
|
442
|
-
fix:
|
|
443
|
-
input.built.length === 0
|
|
444
|
-
? 'x g island <name> --at apps/web/site — then set root to the directory holding apps/'
|
|
445
|
-
: `mountIsland({ build, root, file: ${renderFixLiteral(input.built[0], ISLAND_PLACEHOLDER)} }) — the path is app-root-relative, not relative to the test, and ${file} is not one of them`,
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
export const islandNotBuilt = (
|
|
451
|
-
file: string,
|
|
452
|
-
root: string,
|
|
453
|
-
built: readonly string[],
|
|
454
|
-
): UltimateError => new IslandNotBuiltError({ file, root, built });
|
|
455
|
-
|
|
456
|
-
/** A name that can be written as a JSX tag. `export default` and `export { x as 'a b' }` are both
|
|
457
|
-
* legal ES and neither can — a paste emitting one is a syntax error in the reader's own file. */
|
|
458
|
-
const JSX_NAME = /^[A-Za-z_$][\w$]*$/;
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* The export the paste renders. A component first — an initial capital that is not the whole name,
|
|
462
|
-
* which is what separates `Counter` from a `PROPS` constant — and any other usable identifier
|
|
463
|
-
* after it, because a lowercase component still compiles and beats a name the reader must invent.
|
|
464
|
-
*/
|
|
465
|
-
const componentOf = (exported: readonly string[]): string | undefined => {
|
|
466
|
-
const usable = exported.filter((name) => JSX_NAME.test(name) && name !== 'default');
|
|
467
|
-
return usable.find((name) => /^[A-Z]/.test(name) && name !== name.toUpperCase()) ?? usable[0];
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
/**
|
|
471
|
-
* The chunk built and imported, and exports no `mount`. Distinct from a build failure: the island
|
|
472
|
-
* compiles, ships and is served — the hydration runtime calls `m.mount(el, props)` and the browser
|
|
473
|
-
* throws on a page that passed every gate. Naming the exports it DOES have is what separates a
|
|
474
|
-
* renamed export from a file that exports only its component.
|
|
475
|
-
*
|
|
476
|
-
* The paste is built from THOSE exports rather than from a sketch. It read
|
|
477
|
-
* `render(() => <Island {...props} />, el)` with `props: Props` until 2026-08-21, and none of
|
|
478
|
-
* `Island`, `Props` or `render` exists in the file the reader was told to paste it into — three
|
|
479
|
-
* compile errors handed out as the remedy for one. Two of them the error already knew the answer
|
|
480
|
-
* to, from its own cause; the third is an import, so the import is in the line.
|
|
481
|
-
*/
|
|
482
|
-
export class IslandMountMissingError extends UltimateError {
|
|
483
|
-
constructor(input: { file: string; exported: readonly string[] }) {
|
|
484
|
-
const file = renderFixLiteral(input.file, ISLAND_PLACEHOLDER);
|
|
485
|
-
const component = componentOf(input.exported);
|
|
486
|
-
super({
|
|
487
|
-
code: 'X_TEST_ISLAND_NO_MOUNT',
|
|
488
|
-
cause: `the chunk built from ${renderCauseValue(input.file)} exports ${
|
|
489
|
-
input.exported.length === 0 ? 'nothing' : input.exported.join(', ')
|
|
490
|
-
} — the hydration runtime calls m.mount(el, props)`,
|
|
491
|
-
fix:
|
|
492
|
-
component === undefined
|
|
493
|
-
? `${file} exports nothing a mount could render — x g island <name> --at apps/web/site writes an island whose mount() is already there, and its shape is the one to copy`
|
|
494
|
-
: `in ${file} add: import { render } from 'solid-js/web'; export function mount(el: HTMLElement, props: Parameters<typeof ${component}>[0]): () => void { el.textContent = ''; return render(() => <${component} {...props} />, el); }`,
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
export const islandMountMissing = (file: string, exported: readonly string[]): UltimateError =>
|
|
500
|
-
new IslandMountMissingError({ file, exported });
|
package/src/exercise-actor.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// because the set — anonymous plus one per declared role — is `@ultimat3/cli`'s `devActors()`,
|
|
4
4
|
// and a second "every role" builder here would drift from the one the policy panel shows.
|
|
5
5
|
|
|
6
|
-
import type { Actor } from '@ultimat3/core';
|
|
6
|
+
import type { Actor, ActorFactMap } from '@ultimat3/core';
|
|
7
7
|
import type { NamedActor, Policy } from '@ultimat3/policy';
|
|
8
8
|
import { policyMatrix, rolesGranting } from '@ultimat3/policy';
|
|
9
9
|
import type { ExerciseOverride } from './define-exercise';
|
|
@@ -21,10 +21,34 @@ export type ActorChoice =
|
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* An override's actor, moved into the primitive's tenant: every primitive gets a fresh pair of
|
|
24
|
-
* tenants, so an actor declared once in `exercise.ts` cannot know the org it will act in.
|
|
24
|
+
* tenants, so an actor declared once in `exercise.ts` cannot know the org it will act in. A fact
|
|
25
|
+
* naming the org it was declared in moves too, as the input's `orgId` does
|
|
26
|
+
* (`exercise-override-input.ts`): else `memberOrgIds.includes(input.orgId)` denies every run.
|
|
25
27
|
*/
|
|
26
|
-
export const inTenant = (actor: Actor | null, tenant: string): Actor | null =>
|
|
27
|
-
actor === null
|
|
28
|
+
export const inTenant = (actor: Actor | null, tenant: string): Actor | null => {
|
|
29
|
+
if (actor === null) return null;
|
|
30
|
+
const facts = rehomedFacts(actor.facts, actor.orgId, tenant);
|
|
31
|
+
return Object.freeze({ ...actor, orgId: tenant, ...(facts === undefined ? {} : { facts }) });
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** Each top-level fact equal to `from` — or an array holding it — with `to` in its place. */
|
|
35
|
+
function rehomedFacts(
|
|
36
|
+
facts: ActorFactMap | undefined,
|
|
37
|
+
from: string | undefined,
|
|
38
|
+
to: string,
|
|
39
|
+
): ActorFactMap | undefined {
|
|
40
|
+
if (facts === undefined || from === undefined || from === to) return facts;
|
|
41
|
+
const moved: Record<string, unknown> = {};
|
|
42
|
+
// An app augments `ActorFacts`, so its value types say nothing here: each is read as unknown.
|
|
43
|
+
const entries: [string, unknown][] = Object.entries(facts);
|
|
44
|
+
for (const [key, value] of entries) {
|
|
45
|
+
if (value === from) moved[key] = to;
|
|
46
|
+
else if (Array.isArray(value) && value.includes(from)) {
|
|
47
|
+
moved[key] = value.map((one: unknown) => (one === from ? to : one));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return Object.keys(moved).length === 0 ? facts : Object.freeze({ ...facts, ...moved });
|
|
51
|
+
}
|
|
28
52
|
|
|
29
53
|
/**
|
|
30
54
|
* A role's actor id in one tenant: a uuid, as `ctx.actor.id` always is in production (an
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// What one primitive's control call made of its record, judged: reached, or unreached with the
|
|
2
|
+
// code, the reason and the paste line — under the entry key its primitive is written as, which is
|
|
3
|
+
// `kind:name` when two kinds share the name (`labelFor`).
|
|
4
|
+
|
|
5
|
+
import type { ExerciseVerdict } from '@ultimat3/core';
|
|
6
|
+
import { isUltimateError, renderThrowable } from '@ultimat3/core';
|
|
7
|
+
import { exerciseEntryKey } from './define-exercise';
|
|
8
|
+
import { driftOverrideLine } from './exercise-drift-hint';
|
|
9
|
+
import type { Sampler } from './exercise-input';
|
|
10
|
+
import { idStem } from './exercise-input';
|
|
11
|
+
import type { Observed } from './exercise-report';
|
|
12
|
+
import { overrideLine } from './exercise-report';
|
|
13
|
+
import type { Subject } from './exercise-subjects';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Who `subject` is, in a key and on the room: its bare name, or `kind:name` when another kind is
|
|
17
|
+
* registered under the same name — so neither reads the other's entry, statements or proofs.
|
|
18
|
+
*/
|
|
19
|
+
export const labelFor = (subjects: readonly Subject[]) => {
|
|
20
|
+
const kinds = new Map<string, Set<string>>();
|
|
21
|
+
for (const { kind, name } of subjects) kinds.set(name, (kinds.get(name) ?? new Set()).add(kind));
|
|
22
|
+
return (subject: Pick<Subject, 'kind' | 'name'>): string =>
|
|
23
|
+
(kinds.get(subject.name)?.size ?? 0) > 1
|
|
24
|
+
? exerciseEntryKey(subject.kind, subject.name)
|
|
25
|
+
: subject.name;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const codeOf = (error: unknown): string | undefined => {
|
|
29
|
+
if (isUltimateError(error)) return error.code;
|
|
30
|
+
return typeof error === 'string' ? /^(X_[A-Z0-9_]+)/.exec(error)?.[1] : undefined;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const reasonOf = (error: unknown): string =>
|
|
34
|
+
isUltimateError(error) ? error.cause : typeof error === 'string' ? error : renderThrowable(error);
|
|
35
|
+
|
|
36
|
+
export interface Run {
|
|
37
|
+
readonly verdict: ExerciseVerdict;
|
|
38
|
+
readonly answered: boolean;
|
|
39
|
+
readonly lastError: unknown;
|
|
40
|
+
readonly lastInput: unknown;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function judged(
|
|
44
|
+
subject: Subject,
|
|
45
|
+
key: string,
|
|
46
|
+
run: Run,
|
|
47
|
+
observed: Observed,
|
|
48
|
+
sampler: Sampler,
|
|
49
|
+
) {
|
|
50
|
+
const idFields = (input: unknown): readonly string[] =>
|
|
51
|
+
typeof input === 'object' && input !== null
|
|
52
|
+
? Object.keys(input).filter((key) => idStem(key) !== undefined)
|
|
53
|
+
: [];
|
|
54
|
+
const unreached = (code: string | undefined, reason: string) => ({
|
|
55
|
+
outcome: 'unreached' as const,
|
|
56
|
+
...(code === undefined ? {} : { code }),
|
|
57
|
+
reason,
|
|
58
|
+
override: overrideLine(key, idFields(run.lastInput)),
|
|
59
|
+
});
|
|
60
|
+
if (observed.refusedHosts.length > 0) {
|
|
61
|
+
// A call out is a refusal whatever the handler made of it: sealed, so never sent.
|
|
62
|
+
const hosts = [...new Set(observed.refusedHosts)].join(', ');
|
|
63
|
+
return unreached('X_TEST_NETWORK_SEALED', `called out to ${hosts}: sealed, never sent`);
|
|
64
|
+
}
|
|
65
|
+
if (run.verdict.kind === 'pass' && run.answered) return { outcome: 'reached' as const };
|
|
66
|
+
if (run.verdict.kind === 'drift') {
|
|
67
|
+
// The field no draw can fill, named: `...drawn` alone told the author nothing to write.
|
|
68
|
+
const fields = idFields(run.lastInput);
|
|
69
|
+
return {
|
|
70
|
+
...unreached('X_CONTRACT_DRIFT', 'its input schema refused every generated value'),
|
|
71
|
+
override: driftOverrideLine(key, subject.input, sampler, fields),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (run.verdict.kind === 'defect') {
|
|
75
|
+
return unreached(codeOf(run.verdict.error) ?? 'X_CONTRACT_DEFECT', run.verdict.defect);
|
|
76
|
+
}
|
|
77
|
+
return unreached(codeOf(run.lastError), reasonOf(run.lastError));
|
|
78
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Several recordings of one primitive, as one: the control records each CALL alone, so what its
|
|
2
|
+
// override's `input` did between two calls (a select, a row, a mail) is never the primitive's.
|
|
3
|
+
|
|
4
|
+
import type { EffectPayload, ExerciseEffect, Observed } from './exercise-report';
|
|
5
|
+
import type { StatementTally } from './scale-sweep';
|
|
6
|
+
|
|
7
|
+
const EMPTY: Observed = { statements: 0, effects: [], refusedHosts: [], payloads: [], shapes: [] };
|
|
8
|
+
|
|
9
|
+
/** A count effect's identity: two segments' inserts into one table are one effect, summed. */
|
|
10
|
+
const countKey = (effect: ExerciseEffect): string | undefined => {
|
|
11
|
+
if (effect.kind === 'row') return `row\u0000${effect.entity}\u0000${effect.op}`;
|
|
12
|
+
if (effect.kind === 'mail') return `mail\u0000${effect.mail}`;
|
|
13
|
+
if (effect.kind === 'enqueue') return `enqueue\u0000${effect.job}`;
|
|
14
|
+
return undefined;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const added = (one: ExerciseEffect, other: ExerciseEffect): ExerciseEffect => {
|
|
18
|
+
if (one.kind === 'row' && other.kind === 'row') return { ...one, rows: one.rows + other.rows };
|
|
19
|
+
if (one.kind === 'mail' && other.kind === 'mail')
|
|
20
|
+
return { ...one, count: one.count + other.count };
|
|
21
|
+
if (one.kind === 'enqueue' && other.kind === 'enqueue') {
|
|
22
|
+
return { ...one, count: one.count + other.count };
|
|
23
|
+
}
|
|
24
|
+
return one;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function mergeEffects(lists: readonly (readonly ExerciseEffect[])[]): readonly ExerciseEffect[] {
|
|
28
|
+
const out: ExerciseEffect[] = [];
|
|
29
|
+
const at = new Map<string, number>();
|
|
30
|
+
for (const effect of lists.flat()) {
|
|
31
|
+
const key = countKey(effect);
|
|
32
|
+
const index = key === undefined ? undefined : at.get(key);
|
|
33
|
+
if (index === undefined) {
|
|
34
|
+
if (key !== undefined) at.set(key, out.length);
|
|
35
|
+
out.push(effect);
|
|
36
|
+
} else out[index] = added(out[index] as ExerciseEffect, effect);
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** `effect.mail[0]` of the second segment is the merged list's `effect.mail[n]`, n its offset. */
|
|
42
|
+
function mergePayloads(lists: readonly (readonly EffectPayload[])[]): readonly EffectPayload[] {
|
|
43
|
+
const offsets = new Map<string, number>();
|
|
44
|
+
const out: EffectPayload[] = [];
|
|
45
|
+
for (const list of lists) {
|
|
46
|
+
const seen = new Map<string, number>();
|
|
47
|
+
for (const payload of list) {
|
|
48
|
+
const match = /^(.*)\[(\d+)\]$/.exec(payload.path);
|
|
49
|
+
if (match === null) {
|
|
50
|
+
out.push(payload);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const [, base = '', index = '0'] = match;
|
|
54
|
+
const shifted = (offsets.get(base) ?? 0) + Number(index);
|
|
55
|
+
seen.set(base, Math.max(seen.get(base) ?? 0, Number(index) + 1));
|
|
56
|
+
out.push({ ...payload, path: `${base}[${shifted}]` });
|
|
57
|
+
}
|
|
58
|
+
for (const [base, count] of seen) offsets.set(base, (offsets.get(base) ?? 0) + count);
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function mergeShapes(lists: readonly (readonly StatementTally[])[]): readonly StatementTally[] {
|
|
64
|
+
const byPrint = new Map<string, StatementTally>();
|
|
65
|
+
for (const shape of lists.flat()) {
|
|
66
|
+
const known = byPrint.get(shape.fingerprint);
|
|
67
|
+
byPrint.set(
|
|
68
|
+
shape.fingerprint,
|
|
69
|
+
known === undefined
|
|
70
|
+
? shape
|
|
71
|
+
: {
|
|
72
|
+
...known,
|
|
73
|
+
...(shape.expected === undefined ? {} : { expected: shape.expected }),
|
|
74
|
+
count: known.count + shape.count,
|
|
75
|
+
unexpected: known.unexpected + shape.unexpected,
|
|
76
|
+
},
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return [...byPrint.values()];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The segments as one recording: counts summed, lists in order. None is the empty recording. */
|
|
83
|
+
export function mergeObserved(segments: readonly Observed[]): Observed {
|
|
84
|
+
if (segments.length === 0) return EMPTY;
|
|
85
|
+
if (segments.length === 1) return segments[0] as Observed;
|
|
86
|
+
return {
|
|
87
|
+
statements: segments.reduce((sum, one) => sum + one.statements, 0),
|
|
88
|
+
effects: mergeEffects(segments.map((one) => one.effects)),
|
|
89
|
+
refusedHosts: segments.flatMap((one) => one.refusedHosts),
|
|
90
|
+
payloads: mergePayloads(segments.map((one) => one.payloads)),
|
|
91
|
+
shapes: mergeShapes(segments.map((one) => one.shapes)),
|
|
92
|
+
};
|
|
93
|
+
}
|