@ultimat3/testing 30.7.0 → 30.8.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 +1 -1
- 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 +3 -84
- 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 +34 -5
- 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 +9 -6
- package/src/exercise.ts +110 -108
- package/src/fixture-island.ts +1 -1
- package/src/island-errors.ts +82 -0
- package/src/plan-conditions.ts +2 -2
- package/src/plan-oracle.ts +28 -8
- package/src/plan-probes.ts +94 -0
- package/src/plan-sweep-fix.ts +12 -1
- package/src/scale-sweep-graph.ts +3 -0
- package/src/scale-sweep-run.ts +14 -7
- 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
|
@@ -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 and sorts off, 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
|
|
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 and sorts off, 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 |
|
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.8.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.8.0",
|
|
50
|
+
"@ultimat3/cache": "30.8.0",
|
|
51
|
+
"@ultimat3/core": "30.8.0",
|
|
52
|
+
"@ultimat3/db": "30.8.0",
|
|
53
|
+
"@ultimat3/entity": "30.8.0",
|
|
54
|
+
"@ultimat3/http": "30.8.0",
|
|
55
|
+
"@ultimat3/i18n": "30.8.0",
|
|
56
|
+
"@ultimat3/jobs": "30.8.0",
|
|
57
|
+
"@ultimat3/mail": "30.8.0",
|
|
58
|
+
"@ultimat3/policy": "30.8.0",
|
|
59
|
+
"@ultimat3/pwa": "30.8.0",
|
|
60
|
+
"@ultimat3/query": "30.8.0",
|
|
61
|
+
"@ultimat3/realtime": "30.8.0",
|
|
62
|
+
"@ultimat3/render": "30.8.0",
|
|
63
|
+
"@ultimat3/schema": "30.8.0",
|
|
64
|
+
"@ultimat3/time": "30.8.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
|
|
@@ -420,81 +417,3 @@ export class NetworkRaceError extends UltimateError {
|
|
|
420
417
|
});
|
|
421
418
|
}
|
|
422
419
|
}
|
|
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
|
+
}
|
|
@@ -4,9 +4,16 @@
|
|
|
4
4
|
// computed from the pass's own state, by one mechanism and no per-primitive case.
|
|
5
5
|
|
|
6
6
|
import type { Actor } from '@ultimat3/core';
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
bindDrawn,
|
|
9
|
+
isUltimateError,
|
|
10
|
+
renderExerciseKey,
|
|
11
|
+
renderFixShellArg,
|
|
12
|
+
renderThrowable,
|
|
13
|
+
} from '@ultimat3/core';
|
|
8
14
|
import type { ExerciseOverride } from './define-exercise';
|
|
9
15
|
import { withExerciseRequest } from './exercise-request';
|
|
16
|
+
import { testJobs } from './fixture-jobs';
|
|
10
17
|
|
|
11
18
|
/** `(drawn, actor) => input`, or `undefined` when the override binds nothing. */
|
|
12
19
|
export type OverrideInput = (drawn: unknown, actor: Actor | null) => Promise<unknown>;
|
|
@@ -18,13 +25,35 @@ export function overrideInput(
|
|
|
18
25
|
// Any entry but `off` may bind: `{ input }` alone keeps the pass's own actor and still binds.
|
|
19
26
|
const bind = bindDrawn(override === undefined || 'off' in override ? undefined : override.input);
|
|
20
27
|
if (bind === undefined) return undefined;
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
const entry = override === undefined || 'off' in override ? undefined : override;
|
|
29
|
+
const declared = entry?.actor;
|
|
30
|
+
// The entry's own request: the binder sees the cookies and headers the call itself will.
|
|
31
|
+
return async (drawn, actor) => {
|
|
32
|
+
// A queue of its own, as every call gets one: app code that enqueues has a driver, and what it
|
|
33
|
+
// queued is the input's, never counted as the primitive's enqueue.
|
|
34
|
+
await using _jobs = await testJobs();
|
|
35
|
+
return rehomed(
|
|
36
|
+
await withExerciseRequest(name, actor, async () => bind(drawn), entry?.request),
|
|
25
37
|
declared?.orgId,
|
|
26
38
|
actor?.orgId,
|
|
27
39
|
);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* What an entry's input that THREW makes of its primitive's record: unreached, under the throw's
|
|
45
|
+
* own code (an uncoded one is `X_EXERCISE_INPUT_THREW`), and a fix naming the entry. Never a
|
|
46
|
+
* defect: the throw is the app's exercise.ts, and blaming the handler sends the author elsewhere.
|
|
47
|
+
* `key` is the entry's key as it would be written — kind-qualified when a name is shared.
|
|
48
|
+
*/
|
|
49
|
+
export function inputThrew(name: string, key: string, error: unknown) {
|
|
50
|
+
const coded = isUltimateError(error);
|
|
51
|
+
return {
|
|
52
|
+
outcome: 'unreached' as const,
|
|
53
|
+
code: coded ? error.code : 'X_EXERCISE_INPUT_THREW',
|
|
54
|
+
reason: `its exercise.ts input threw before the call: ${coded ? error.cause : renderThrowable(error)}`,
|
|
55
|
+
fix: `x exercise ${renderFixShellArg(name, '<name>')} --json # after defineExercise({ ${renderExerciseKey(key)}: { input } }) in apps/web/exercise.ts returns a value (it runs as the seated actor, inside the call's request), or { off: '<why>' }`,
|
|
56
|
+
};
|
|
28
57
|
}
|
|
29
58
|
|
|
30
59
|
/**
|
package/src/exercise-report.ts
CHANGED
|
@@ -52,6 +52,11 @@ export interface TenantFinding {
|
|
|
52
52
|
/** WRITE_CROSSED: the table and the primary key of A's changed row */
|
|
53
53
|
readonly table?: string;
|
|
54
54
|
readonly id?: string;
|
|
55
|
+
/**
|
|
56
|
+
* LEAK from a `tenant: 'none'` job handed nothing of A's: a read across every org that opened no
|
|
57
|
+
* `crossTenant()` scope — its fix is that scope, never a repository scoped to an actor it lacks.
|
|
58
|
+
*/
|
|
59
|
+
readonly sweep?: true;
|
|
55
60
|
}
|
|
56
61
|
|
|
57
62
|
/** The tenant proof's verdict on one primitive: B attacked A's rows twice, around A's own call. */
|
package/src/exercise-request.ts
CHANGED
|
@@ -1,38 +1,21 @@
|
|
|
1
1
|
// The context an action or a query runs in during the exercise pass: an HTTP request's, built the
|
|
2
2
|
// way the pipeline builds one, because `surface: 'http'` is the call the pass makes. A bare `Ctx`
|
|
3
3
|
// has no inbound headers, so a session action's `useRequestContext()` answered X_NO_REQUEST, a 500.
|
|
4
|
+
// The build is `@ultimat3/http`'s `withRequestAs`, the one the contract step makes too.
|
|
4
5
|
|
|
5
6
|
import type { Actor } from '@ultimat3/core';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
/** Where the pass's requests say they came from: no host the app serves, and never sent. */
|
|
9
|
-
const EXERCISE_ORIGIN = 'https://exercise.test';
|
|
7
|
+
import type { ExerciseRequestOption } from '@ultimat3/http';
|
|
10
8
|
|
|
11
9
|
/**
|
|
12
|
-
* `fn` inside a fresh request as `actor
|
|
13
|
-
*
|
|
14
|
-
* `actor` — its impersonation path rebuilds a plain child context and drops the request's fields.
|
|
15
|
-
* The child is derived first for the services an actor binds, then the request's fields go over
|
|
16
|
-
* it, as `withAnonymousRequest` (`@ultimat3/http`) does.
|
|
10
|
+
* `fn` inside a fresh request as `actor`, carrying the entry's `request` (none: no cookie, no
|
|
11
|
+
* header). `@ultimat3/http` is loaded on the call, so the barrel stays off it.
|
|
17
12
|
*/
|
|
18
13
|
export async function withExerciseRequest<T>(
|
|
19
14
|
name: string,
|
|
20
15
|
actor: Actor | null,
|
|
21
16
|
fn: () => Promise<T>,
|
|
17
|
+
request?: ExerciseRequestOption,
|
|
22
18
|
): Promise<T> {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
url: new URL(`/${encodeURIComponent(name)}`, EXERCISE_ORIGIN),
|
|
26
|
-
method: 'POST',
|
|
27
|
-
role: 'web',
|
|
28
|
-
config: http.defineHttpConfig({ rateLimit: { scope: 'process' } }),
|
|
29
|
-
requestHeaders: new Headers(),
|
|
30
|
-
});
|
|
31
|
-
return runWithContext(http.asCtx(request), () =>
|
|
32
|
-
withChildContext({ actor: actor ?? anonymousActor() }, () => {
|
|
33
|
-
const child = useContext();
|
|
34
|
-
const scope = { ...child, ...request, actor: child.actor };
|
|
35
|
-
return runWithContext(http.asCtx(scope), fn);
|
|
36
|
-
}),
|
|
37
|
-
);
|
|
19
|
+
const { withRequestAs } = await import('@ultimat3/http');
|
|
20
|
+
return withRequestAs(name, actor, request, fn);
|
|
38
21
|
}
|