@ultimat3/testing 30.6.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 +3 -3
- package/README.md +1 -1
- package/package.json +17 -17
- package/src/define-exercise.ts +53 -17
- package/src/drill-writes.ts +3 -1
- package/src/errors.ts +9 -90
- package/src/exercise-actor-user.ts +63 -0
- package/src/exercise-actor.ts +64 -11
- package/src/exercise-crash.ts +7 -3
- package/src/exercise-drift-hint.ts +28 -0
- package/src/exercise-input.ts +3 -0
- package/src/exercise-job-tenant.ts +10 -5
- package/src/exercise-judge.ts +78 -0
- package/src/exercise-observed.ts +93 -0
- package/src/exercise-override-input.ts +56 -6
- package/src/exercise-report.ts +43 -2
- 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 +57 -12
- package/src/exercise.ts +141 -109
- package/src/fixture-island.ts +1 -1
- package/src/index.ts +4 -0
- package/src/island-errors.ts +82 -0
- package/src/job-crash-points.ts +36 -0
- package/src/job-crash-proof.ts +23 -6
- package/src/job-sim-errors.ts +24 -0
- package/src/plan-conditions.ts +106 -0
- package/src/plan-oracle.ts +93 -57
- package/src/plan-probes.ts +94 -0
- package/src/plan-sort-key.ts +65 -0
- package/src/plan-sweep-fix.ts +99 -22
- package/src/plan-sweep-report.ts +48 -4
- 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-values.ts +20 -11
- package/src/seed-graph.ts +39 -3
- package/src/subject-proof.ts +12 -2
- package/src/tenant-proof-report.ts +84 -25
- package/src/tenant-proof.ts +39 -6
- package/src/seed-pattern-sample.ts +0 -216
|
@@ -9,9 +9,13 @@ export interface TenantOf {
|
|
|
9
9
|
tenantFor(input: unknown): string | undefined;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* `seated`: the input names the tenant. `fixed`: no field it reads moves the org it answers.
|
|
14
|
+
* `none`: `tenant: 'none'` — no org reaches the run, so whoever enqueues it the run is the same.
|
|
15
|
+
*/
|
|
13
16
|
export type JobTenantBinding =
|
|
14
17
|
| { readonly kind: 'seated'; readonly input: unknown }
|
|
18
|
+
| { readonly kind: 'none'; readonly input: unknown }
|
|
15
19
|
| { readonly kind: 'fixed'; readonly declared: string | undefined };
|
|
16
20
|
|
|
17
21
|
type Path = readonly string[];
|
|
@@ -85,16 +89,17 @@ function setAt(input: unknown, path: Path, value: unknown): unknown {
|
|
|
85
89
|
}
|
|
86
90
|
|
|
87
91
|
/**
|
|
88
|
-
* `input` naming `tenant` wherever `handle.tenantFor` reads it.
|
|
89
|
-
*
|
|
90
|
-
* `tenant`
|
|
92
|
+
* `input` naming `tenant` wherever `handle.tenantFor` reads it. One already naming it comes back as
|
|
93
|
+
* given; a `'none'` job comes back as given, as `none` — the tenant proof's write half reads it
|
|
94
|
+
* (`tenant-proof.ts`, `TenantAttack.tenantless`). A read path is kept only if setting it moves the
|
|
95
|
+
* answer to `tenant` — a field read and then ignored is left as drawn.
|
|
91
96
|
*/
|
|
92
97
|
export function bindJobTenant(handle: TenantOf, input: unknown, tenant: string): JobTenantBinding {
|
|
93
98
|
const declared = answer(handle, input);
|
|
94
99
|
if (declared === tenant) return { kind: 'seated', input };
|
|
95
100
|
const reads = pathsRead(handle, input);
|
|
96
101
|
// `tenant: 'none'` is never called: no org named, none read, so none to disagree with.
|
|
97
|
-
if (declared === undefined && reads.length === 0) return { kind: '
|
|
102
|
+
if (declared === undefined && reads.length === 0) return { kind: 'none', input };
|
|
98
103
|
const moving = reads.filter((path) => answer(handle, setAt(input, path, tenant)) === tenant);
|
|
99
104
|
const bound = moving.reduce<unknown>((value, path) => setAt(value, path, tenant), input);
|
|
100
105
|
return moving.length > 0 && answer(handle, bound) === tenant
|
|
@@ -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>;
|
|
@@ -15,13 +22,56 @@ export function overrideInput(
|
|
|
15
22
|
name: string,
|
|
16
23
|
override: ExerciseOverride | undefined,
|
|
17
24
|
): OverrideInput | undefined {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
);
|
|
25
|
+
// Any entry but `off` may bind: `{ input }` alone keeps the pass's own actor and still binds.
|
|
26
|
+
const bind = bindDrawn(override === undefined || 'off' in override ? undefined : override.input);
|
|
21
27
|
if (bind === undefined) return undefined;
|
|
22
|
-
|
|
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),
|
|
37
|
+
declared?.orgId,
|
|
38
|
+
actor?.orgId,
|
|
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
|
+
};
|
|
23
57
|
}
|
|
24
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The bound input, re-homed with its actor: the pass seats a declared actor in each tenant it
|
|
61
|
+
* seeds, so a top-level value equal to the org the entry DECLARED (`orgId: '<org>'`, the same
|
|
62
|
+
* literal the contract test runs with) is that tenant's. Without it A's own call names an org its
|
|
63
|
+
* actor is not in, and a policy comparing the two denies every run.
|
|
64
|
+
*/
|
|
65
|
+
function rehomed(input: unknown, from: string | undefined, to: string | undefined): unknown {
|
|
66
|
+
if (from === undefined || to === undefined || from === to || !isPlainObject(input)) return input;
|
|
67
|
+
return Object.fromEntries(
|
|
68
|
+
Object.entries(input).map(([key, value]) => [key, value === from ? to : value]),
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const isPlainObject = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
|
73
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
74
|
+
|
|
25
75
|
/**
|
|
26
76
|
* Whether an override's input is declared `async` — state the pass computes, which a load that
|
|
27
77
|
* draws with no pass behind it (`x drill`) cannot reproduce. The function's own tag, never a call:
|
|
@@ -29,6 +79,6 @@ export function overrideInput(
|
|
|
29
79
|
*/
|
|
30
80
|
export const isAsyncInput = (override: ExerciseOverride | undefined): boolean =>
|
|
31
81
|
override !== undefined &&
|
|
32
|
-
'
|
|
82
|
+
!('off' in override) &&
|
|
33
83
|
override.input !== undefined &&
|
|
34
84
|
Object.prototype.toString.call(override.input) === '[object AsyncFunction]';
|
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. */
|
|
@@ -79,12 +84,21 @@ export interface CrashFinding {
|
|
|
79
84
|
readonly inside?: string;
|
|
80
85
|
}
|
|
81
86
|
|
|
82
|
-
/**
|
|
87
|
+
/**
|
|
88
|
+
* The crash proof's verdict on a reached job: killed at every distinct step boundary, a loop's rows
|
|
89
|
+
* as one (`crashProof`). `boundaries` is the kills, `crossed` the boundaries they stand for.
|
|
90
|
+
*/
|
|
83
91
|
export type CrashProof =
|
|
84
|
-
| {
|
|
92
|
+
| {
|
|
93
|
+
readonly verdict: 'held';
|
|
94
|
+
readonly boundaries: number;
|
|
95
|
+
readonly crossed?: number;
|
|
96
|
+
readonly reruns: number;
|
|
97
|
+
}
|
|
85
98
|
| {
|
|
86
99
|
readonly verdict: 'diverged';
|
|
87
100
|
readonly boundaries: number;
|
|
101
|
+
readonly crossed?: number;
|
|
88
102
|
/** Lost answers that reran a step: at-least-once, counted, never red. */
|
|
89
103
|
readonly reruns: number;
|
|
90
104
|
readonly findings: readonly CrashFinding[];
|
|
@@ -118,6 +132,33 @@ export interface ExerciseRecord {
|
|
|
118
132
|
readonly plans?: PlanProof;
|
|
119
133
|
/** On a reached primitive: its statement shapes at 2 and at 8 rows a table (`scale-sweep.ts`). */
|
|
120
134
|
readonly scale?: ScaleProof;
|
|
135
|
+
/** Wall time of each proof that ran on it; `ms` above is the control's own call. */
|
|
136
|
+
readonly proofMs?: ProofMs;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Milliseconds per proof of one record. No `plans`: one statement is planned once for every
|
|
141
|
+
* primitive that sent it, so its time is the pass's (`ExercisePhaseMs.plans`), never one record's.
|
|
142
|
+
*/
|
|
143
|
+
export interface ProofMs {
|
|
144
|
+
/** B's two legs and the snapshots around them, the control's own call excepted. */
|
|
145
|
+
readonly tenant?: number;
|
|
146
|
+
/** Every run the crash proof made. */
|
|
147
|
+
readonly crash?: number;
|
|
148
|
+
/** Its calls at both sizes; seeding the sized graphs is the pass's (`ExercisePhaseMs.scale`). */
|
|
149
|
+
readonly scale?: number;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Milliseconds per phase of one pass, so a slow pass names its slow part. */
|
|
153
|
+
export interface ExercisePhaseMs {
|
|
154
|
+
/** The pass's one seed graph, written. */
|
|
155
|
+
readonly seed: number;
|
|
156
|
+
/** Every primitive's control and its tenant and crash proofs. */
|
|
157
|
+
readonly primitives: number;
|
|
158
|
+
/** The scale sweep: its two sized graphs written, every reached primitive called in each. */
|
|
159
|
+
readonly scale: number;
|
|
160
|
+
/** The plan sweep: every distinct statement the pass sent, planned. */
|
|
161
|
+
readonly plans: number;
|
|
121
162
|
}
|
|
122
163
|
|
|
123
164
|
/** What an effect carried, for the tenant proof's read half: searched, never reported as is. */
|
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
|
}
|
package/src/exercise-setup.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// `defineExercise(…, { setup })`, run: the app's hook inside the pass's sealed room, before the
|
|
2
2
|
// first row is seeded, disposed before the room restores the network. What it installs — a
|
|
3
3
|
// `mockFetch` route, a `cassette()` — answers a vendor call; `allowHost` would send one for real.
|
|
4
|
+
// Several hooks (one per feature group) run in order and are undone in reverse.
|
|
4
5
|
|
|
5
6
|
import { renderCauseValue, UltimateError } from '@ultimat3/core';
|
|
6
7
|
import type { ExerciseRoom } from './exercise-report';
|
|
@@ -35,30 +36,46 @@ async function release(handle: unknown): Promise<void> {
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
|
|
39
|
+
* Every handle released, last first, and then the room — each whatever the one before it threw;
|
|
40
|
+
* the first throw is what the caller sees, once nothing is left installed.
|
|
41
|
+
*/
|
|
42
|
+
async function releaseAll(handles: readonly unknown[], room: ExerciseRoom): Promise<void> {
|
|
43
|
+
let failed = false;
|
|
44
|
+
let first: unknown;
|
|
45
|
+
for (const handle of [...handles].reverse()) {
|
|
46
|
+
try {
|
|
47
|
+
await release(handle);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (!failed) first = error;
|
|
50
|
+
failed = true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
await room[Symbol.asyncDispose]();
|
|
54
|
+
if (failed) throw first;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* `room` with each setup run inside it, in order. Its dispose releases what they returned FIRST,
|
|
59
|
+
* in reverse — a cassette's own check reads the routes the pass used — then the room. A setup that
|
|
60
|
+
* throws disposes those that already ran and the room; one that lets a host through, all of them.
|
|
41
61
|
*/
|
|
42
62
|
export async function withExerciseSetup(
|
|
43
63
|
room: ExerciseRoom,
|
|
44
|
-
setup: ExerciseSetup | undefined,
|
|
64
|
+
setup: ExerciseSetup | readonly ExerciseSetup[] | undefined,
|
|
45
65
|
): Promise<ExerciseRoom> {
|
|
46
|
-
|
|
66
|
+
const setups = setup === undefined ? [] : typeof setup === 'function' ? [setup] : setup;
|
|
67
|
+
if (setups.length === 0) return room;
|
|
47
68
|
const before = new Set(captureNetwork().allowed);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
handle = await setup();
|
|
51
|
-
} catch (error) {
|
|
52
|
-
await room[Symbol.asyncDispose]();
|
|
53
|
-
throw error;
|
|
54
|
-
}
|
|
55
|
-
const dispose = async (): Promise<void> => {
|
|
69
|
+
const handles: unknown[] = [];
|
|
70
|
+
for (const one of setups) {
|
|
56
71
|
try {
|
|
57
|
-
await
|
|
58
|
-
}
|
|
59
|
-
await room
|
|
72
|
+
handles.push(await one());
|
|
73
|
+
} catch (error) {
|
|
74
|
+
await releaseAll(handles, room);
|
|
75
|
+
throw error;
|
|
60
76
|
}
|
|
61
|
-
}
|
|
77
|
+
}
|
|
78
|
+
const dispose = (): Promise<void> => releaseAll(handles, room);
|
|
62
79
|
const opened = captureNetwork().allowed.filter((host) => !before.has(host));
|
|
63
80
|
if (opened.length > 0) {
|
|
64
81
|
await dispose();
|
package/src/exercise-subjects.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import type { Actor } from '@ultimat3/core';
|
|
7
7
|
import { renderCauseValue, renderFixShellArg, UltimateError } from '@ultimat3/core';
|
|
8
|
+
import type { ExerciseRequestOption } from '@ultimat3/http';
|
|
8
9
|
import type { AnyJobHandle, JobExecution } from '@ultimat3/jobs';
|
|
9
10
|
import { registeredJobs } from '@ultimat3/jobs';
|
|
10
11
|
import type { Policy } from '@ultimat3/policy';
|
|
@@ -97,7 +98,15 @@ const jobSubject = (handle: AnyJobHandle): Subject => ({
|
|
|
97
98
|
},
|
|
98
99
|
});
|
|
99
100
|
|
|
100
|
-
|
|
101
|
+
/** The `request` an action's or a query's entry declares, by kind: a job's call has none. */
|
|
102
|
+
export type RequestOf = (
|
|
103
|
+
kind: 'action' | 'query',
|
|
104
|
+
name: string,
|
|
105
|
+
) => ExerciseRequestOption | undefined;
|
|
106
|
+
|
|
107
|
+
export async function subjectsOf(
|
|
108
|
+
requestOf: RequestOf = () => undefined,
|
|
109
|
+
): Promise<readonly Subject[]> {
|
|
101
110
|
// On demand, as every fixture here loads them: the barrel stays off `action` and `query`.
|
|
102
111
|
const { invoke, listActions } = await import('@ultimat3/action');
|
|
103
112
|
const { listQueries, runQuery } = await import('@ultimat3/query');
|
|
@@ -110,8 +119,11 @@ export async function subjectsOf(): Promise<readonly Subject[]> {
|
|
|
110
119
|
policy: target.policy,
|
|
111
120
|
permissions: target.describe().permissions,
|
|
112
121
|
call: async (input, actor) => ({
|
|
113
|
-
output: await withExerciseRequest(
|
|
114
|
-
|
|
122
|
+
output: await withExerciseRequest(
|
|
123
|
+
target.name,
|
|
124
|
+
actor,
|
|
125
|
+
() => invoke(target, input, { surface: 'http' }),
|
|
126
|
+
requestOf('action', target.name),
|
|
115
127
|
),
|
|
116
128
|
}),
|
|
117
129
|
}),
|
|
@@ -124,8 +136,11 @@ export async function subjectsOf(): Promise<readonly Subject[]> {
|
|
|
124
136
|
policy: target.policy,
|
|
125
137
|
permissions: target.describe().permissions,
|
|
126
138
|
call: async (input, actor) => ({
|
|
127
|
-
output: await withExerciseRequest(
|
|
128
|
-
|
|
139
|
+
output: await withExerciseRequest(
|
|
140
|
+
target.name,
|
|
141
|
+
actor,
|
|
142
|
+
() => runQuery(target, input, { surface: 'http' }),
|
|
143
|
+
requestOf('query', target.name),
|
|
129
144
|
),
|
|
130
145
|
}),
|
|
131
146
|
}),
|