@ghostry/fabricator 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/Distribution/index.js +1 -1
- package/dist/esm/Enumeration/Enumerate.js +2 -4
- package/dist/esm/Error/index.js +34 -6
- package/dist/esm/Fabricator/Constructor.js +40 -33
- package/dist/esm/Fabricator/Fabricate.js +28 -0
- package/dist/esm/Instance/Core.js +14 -2
- package/dist/esm/Instance/Stack/Visible.js +6 -1
- package/dist/esm/Primitive/date/Defaults.js +12 -0
- package/dist/esm/Primitive/date/Fabricator.js +5 -10
- package/dist/esm/Primitive/date/Registry.js +8 -4
- package/dist/esm/Primitive/index.js +2 -0
- package/dist/esm/Primitive/null/Registry.js +2 -3
- package/dist/esm/Primitive/number/Fabricator.js +1 -1
- package/dist/esm/Primitive/number/Registry.js +1 -1
- package/dist/esm/Primitive/object/Fabricator.js +33 -11
- package/dist/esm/Primitive/object/Registry.js +90 -10
- package/dist/esm/Primitive/object/compute/Fabricator.js +10 -3
- package/dist/esm/Primitive/object/compute/Registry.js +20 -3
- package/dist/esm/Primitive/object/omit/Registry.js +5 -0
- package/dist/esm/Primitive/object/omit/index.js +2 -0
- package/dist/esm/Primitive/object/pick/Registry.js +5 -0
- package/dist/esm/Primitive/object/pick/index.js +2 -0
- package/dist/esm/Primitive/recursive/Fabricator.js +1 -0
- package/dist/esm/Primitive/recursive/Terminate.js +21 -9
- package/dist/esm/Primitive/undefined/Registry.js +2 -3
- package/dist/esm/index.js +1 -0
- package/dist/esm/internal.js +2 -2
- package/dist/types/Adapter/Types.d.ts +3 -2
- package/dist/types/Distribution/index.d.ts +10 -0
- package/dist/types/Enumeration/Types.d.ts +3 -3
- package/dist/types/Error/index.d.ts +71 -6
- package/dist/types/Fabricator/Fabricate.d.ts +61 -0
- package/dist/types/Fabricator/Types.d.ts +21 -11
- package/dist/types/Harnessing/Core.d.ts +7 -7
- package/dist/types/Instance/Core.d.ts +15 -8
- package/dist/types/Instance/Stack/Visible.d.ts +10 -0
- package/dist/types/Instance/Types.d.ts +30 -15
- package/dist/types/Primitive/always/Types.d.ts +2 -2
- package/dist/types/Primitive/array/Types.d.ts +4 -4
- package/dist/types/Primitive/bigint/Types.d.ts +2 -2
- package/dist/types/Primitive/boolean/Types.d.ts +2 -2
- package/dist/types/Primitive/choice/Types.d.ts +4 -4
- package/dist/types/Primitive/date/Defaults.d.ts +11 -0
- package/dist/types/Primitive/date/Registry.d.ts +6 -8
- package/dist/types/Primitive/date/Types.d.ts +12 -2
- package/dist/types/Primitive/derive/Types.d.ts +2 -2
- package/dist/types/Primitive/enum/Types.d.ts +2 -2
- package/dist/types/Primitive/index.d.ts +2 -0
- package/dist/types/Primitive/null/Types.d.ts +2 -2
- package/dist/types/Primitive/nullable/Types.d.ts +4 -4
- package/dist/types/Primitive/nullish/Types.d.ts +4 -4
- package/dist/types/Primitive/number/Types.d.ts +2 -2
- package/dist/types/Primitive/object/Fabricator.d.ts +13 -5
- package/dist/types/Primitive/object/Registry.d.ts +17 -1
- package/dist/types/Primitive/object/Types.d.ts +28 -9
- package/dist/types/Primitive/object/compute/Fabricator.d.ts +9 -8
- package/dist/types/Primitive/object/compute/Registry.d.ts +7 -1
- package/dist/types/Primitive/object/compute/Schema.d.ts +20 -0
- package/dist/types/Primitive/object/compute/Types.d.ts +14 -4
- package/dist/types/Primitive/object/omit/Registry.d.ts +7 -0
- package/dist/types/Primitive/object/omit/index.d.ts +2 -0
- package/dist/types/Primitive/object/omittable/Types.d.ts +4 -4
- package/dist/types/Primitive/object/optional/Types.d.ts +4 -4
- package/dist/types/Primitive/object/pick/Registry.d.ts +8 -0
- package/dist/types/Primitive/object/pick/index.d.ts +2 -0
- package/dist/types/Primitive/opaque/Types.d.ts +2 -2
- package/dist/types/Primitive/record/Types.d.ts +11 -6
- package/dist/types/Primitive/recursive/Fabricator.d.ts +9 -7
- package/dist/types/Primitive/recursive/Types.d.ts +12 -8
- package/dist/types/Primitive/recursive/self/Schema.d.ts +4 -4
- package/dist/types/Primitive/recursive/self/Types.d.ts +7 -5
- package/dist/types/Primitive/string/Constants.d.ts +7 -31
- package/dist/types/Primitive/string/Types.d.ts +2 -2
- package/dist/types/Primitive/symbol/Types.d.ts +2 -2
- package/dist/types/Primitive/tuple/Types.d.ts +7 -7
- package/dist/types/Primitive/undefinable/Types.d.ts +4 -4
- package/dist/types/Primitive/undefined/Types.d.ts +2 -2
- package/dist/types/Schema/Core.d.ts +10 -6
- package/dist/types/Schema/Satisfies.d.ts +31 -23
- package/dist/types/Schema/Types.d.ts +31 -18
- package/dist/types/Types.d.ts +24 -0
- package/dist/types/index.d.ts +45 -18
- package/dist/types/internal.d.ts +24 -4
- package/package.json +5 -5
- /package/dist/esm/Primitive/number/{defaults.js → Defaults.js} +0 -0
- /package/dist/types/Primitive/number/{defaults.d.ts → Defaults.d.ts} +0 -0
|
@@ -12,13 +12,20 @@ function Fabricator(context) {
|
|
|
12
12
|
trace,
|
|
13
13
|
fabricate: (params)=>{
|
|
14
14
|
if (!params) throw new FabricatorError.DetachedComputeError();
|
|
15
|
-
const
|
|
16
|
-
|
|
15
|
+
const meta = schema[Meta];
|
|
16
|
+
const fabricated = meta.narrowed ? narrow(params.fabricated, meta.dependencies) : params.fabricated;
|
|
17
|
+
const resolved = meta.resolve({
|
|
18
|
+
fabricated
|
|
17
19
|
});
|
|
18
|
-
const sourceKind =
|
|
20
|
+
const sourceKind = meta.source[Kind];
|
|
19
21
|
if (violatesKind(sourceKind, resolved)) throw new FabricatorError.ComputeResultMismatchError(sourceKind, resolved);
|
|
20
22
|
return resolved;
|
|
21
23
|
}
|
|
22
24
|
};
|
|
23
25
|
}
|
|
26
|
+
function narrow(fabricated, dependencies) {
|
|
27
|
+
const narrowed = {};
|
|
28
|
+
for (const key of dependencies)if (key in fabricated) narrowed[key] = fabricated[key];
|
|
29
|
+
return narrowed;
|
|
30
|
+
}
|
|
24
31
|
export { Fabricator, isObjectComputeFabricator };
|
|
@@ -1,14 +1,31 @@
|
|
|
1
|
+
import { FabricatorError } from "../../../Error/index.js";
|
|
1
2
|
import { Kind, Meta } from "../../../Types.js";
|
|
2
3
|
import { Schema } from "./Schema.js";
|
|
3
|
-
function make() {
|
|
4
|
+
function make(base) {
|
|
4
5
|
return (source)=>({
|
|
5
6
|
as: (resolve)=>Schema({
|
|
6
7
|
[Kind]: "object.compute",
|
|
7
8
|
[Meta]: {
|
|
8
9
|
source,
|
|
9
|
-
resolve
|
|
10
|
+
resolve,
|
|
11
|
+
dependencies: base,
|
|
12
|
+
narrowed: false
|
|
10
13
|
}
|
|
11
|
-
})
|
|
14
|
+
}),
|
|
15
|
+
from: (keys)=>{
|
|
16
|
+
for (const key of keys)if (!base.includes(key)) throw new FabricatorError.UnknownFieldError(key, base, "compute from");
|
|
17
|
+
return {
|
|
18
|
+
as: (resolve)=>Schema({
|
|
19
|
+
[Kind]: "object.compute",
|
|
20
|
+
[Meta]: {
|
|
21
|
+
source,
|
|
22
|
+
resolve,
|
|
23
|
+
dependencies: keys,
|
|
24
|
+
narrowed: true
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
};
|
|
28
|
+
}
|
|
12
29
|
});
|
|
13
30
|
}
|
|
14
31
|
const Registry = make;
|
|
@@ -58,15 +58,24 @@ function terminateAt(schema, path) {
|
|
|
58
58
|
case "object":
|
|
59
59
|
{
|
|
60
60
|
const s = schema;
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
const terminateFields = (definition)=>{
|
|
62
|
+
const next = {};
|
|
63
|
+
for (const key of Object.keys(definition))next[key] = terminateAt(definition[key], [
|
|
64
|
+
...path,
|
|
65
|
+
key
|
|
66
|
+
]);
|
|
67
|
+
return next;
|
|
68
|
+
};
|
|
69
|
+
const definition = terminateFields(s[Meta].definition);
|
|
70
|
+
if (!s[Meta].hidden) return withMeta(s, {
|
|
71
|
+
...s[Meta],
|
|
72
|
+
definition
|
|
73
|
+
});
|
|
74
|
+
const hidden = terminateFields(s[Meta].hidden);
|
|
67
75
|
return withMeta(s, {
|
|
68
76
|
...s[Meta],
|
|
69
|
-
definition
|
|
77
|
+
definition,
|
|
78
|
+
hidden
|
|
70
79
|
});
|
|
71
80
|
}
|
|
72
81
|
case "tuple":
|
|
@@ -168,8 +177,11 @@ function containsSelf(schema) {
|
|
|
168
177
|
case "object":
|
|
169
178
|
{
|
|
170
179
|
const s = schema;
|
|
171
|
-
const
|
|
172
|
-
|
|
180
|
+
const all = {
|
|
181
|
+
...s[Meta].hidden,
|
|
182
|
+
...s[Meta].definition
|
|
183
|
+
};
|
|
184
|
+
return Object.keys(all).some((key)=>containsSelf(all[key]));
|
|
173
185
|
}
|
|
174
186
|
case "tuple":
|
|
175
187
|
{
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Kind, Meta
|
|
1
|
+
import { Kind, Meta } from "../../Types.js";
|
|
2
2
|
import { Schema } from "./Schema.js";
|
|
3
3
|
const registry = Schema({
|
|
4
4
|
[Kind]: "undefined",
|
|
5
|
-
[Meta]: {}
|
|
6
|
-
[Produces]: void 0
|
|
5
|
+
[Meta]: {}
|
|
7
6
|
});
|
|
8
7
|
const Registry = registry;
|
|
9
8
|
export default Registry;
|
package/dist/esm/index.js
CHANGED
|
@@ -9,6 +9,7 @@ export { layer } from "./Random/index.js";
|
|
|
9
9
|
export { FabricatorError } from "./Error/index.js";
|
|
10
10
|
export { registry } from "./Schema/Registry.js";
|
|
11
11
|
export { effectiveDiscrete, toBound } from "./Bound.js";
|
|
12
|
+
export { classes, unicode } from "./Primitive/string/Constants.js";
|
|
12
13
|
export { sample, shuffle } from "./Distribution/index.js";
|
|
13
14
|
export { satisfies } from "./Schema/Satisfies.js";
|
|
14
15
|
export { initialize };
|
package/dist/esm/internal.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { Children, Kind, Meta, Produces } from "./Types.js";
|
|
2
|
-
export { isPlainObject } from "./Utility/Core.js";
|
|
2
|
+
export { isArray, isPlainObject } from "./Utility/Core.js";
|
|
3
3
|
export { plan, resolve } from "./Enumeration/Plan.js";
|
|
4
4
|
export { defaultAlgorithm, encode, randomSalt, toRandomSource, toStream, toStreamFromTrace } from "./Random/index.js";
|
|
5
|
-
export { weighted } from "./Distribution/index.js";
|
|
5
|
+
export { normalCdf, normalInv, weighted } from "./Distribution/index.js";
|
|
6
6
|
export { toStack as toSynchronousStack } from "./Instance/Stack/Sync.js";
|
|
@@ -101,7 +101,8 @@ export type Recurse<$Context, $Returnable> = (schema: any, context: $Context) =>
|
|
|
101
101
|
* package declares its own conversion entry point with its own return type
|
|
102
102
|
* (`toTypeBox<$S>(schema: $S): ToTypeBox<$S>`), so nothing has to compute a
|
|
103
103
|
* type-level mapping out of a stored value — which would need the
|
|
104
|
-
* interface-member-lookup encoding `
|
|
104
|
+
* interface-member-lookup encoding `Fabricated`'s `$Bindings` uses, for no
|
|
105
|
+
* gain.
|
|
105
106
|
*/
|
|
106
107
|
export interface Adapter<$Key extends string = string, $Context = unknown, $Returnable = unknown> {
|
|
107
108
|
readonly key: $Key;
|
|
@@ -115,7 +116,7 @@ export interface Adapter<$Key extends string = string, $Context = unknown, $Retu
|
|
|
115
116
|
* and why no adapter matches `{ [Adaptation]?: Record<Key, ...> }` directly.
|
|
116
117
|
* Every kind's `Core` declares `[Adaptation]` as _optional_, and a conditional
|
|
117
118
|
* matching an optional property against a schema that lacks it entirely still
|
|
118
|
-
* _succeeds_, inferring `unknown` (the same behavior `
|
|
119
|
+
* _succeeds_, inferring `unknown` (the same behavior `Fabricated` relies on for
|
|
119
120
|
* `[Produces]`) — so a direct match would resolve every unadapted schema to
|
|
120
121
|
* whatever the adapter's adaptation branch produces. Constraining the `infer`
|
|
121
122
|
* sends that case here, to `{}`, which then fails each adapter's `Record<Key,
|
|
@@ -110,6 +110,16 @@ export declare function discreteSampler(distribution: Distribution | undefined,
|
|
|
110
110
|
* clamped.
|
|
111
111
|
*/
|
|
112
112
|
export declare function sampler(distribution: Distribution, range: Range, stream: Stream): () => number;
|
|
113
|
+
/**
|
|
114
|
+
* Standard normal CDF via the Abramowitz & Stegun 7.1.26 approximation of the
|
|
115
|
+
* error function (|error| < 1.5e-7).
|
|
116
|
+
*/
|
|
117
|
+
export declare function normalCdf(x: number): number;
|
|
118
|
+
/**
|
|
119
|
+
* Inverse of the standard normal CDF (quantile function) via Peter Acklam's
|
|
120
|
+
* rational approximation (relative error < 1.15e-9).
|
|
121
|
+
*/
|
|
122
|
+
export declare function normalInv(p: number): number;
|
|
113
123
|
/**
|
|
114
124
|
* A uniform pick from `list`. One `stream.next()` per call; `list` is not
|
|
115
125
|
* mutated. An empty list throws {@link FabricatorError.EmptyItemsError} — there
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Kind as SchemaKind } from "../Primitive";
|
|
2
|
-
import type { AnySchema,
|
|
2
|
+
import type { AnySchema, Fabricated } from "../Schema/Types";
|
|
3
3
|
import type { Children, Kind, Meta } from "../Types";
|
|
4
4
|
import type { PlainObject } from "../Utility/Types";
|
|
5
5
|
/**
|
|
@@ -71,9 +71,9 @@ export type Limits = {
|
|
|
71
71
|
* The shape both `combinatorial` and `coverage` share — strategy and limit
|
|
72
72
|
* differ internally, neither visible here. Takes a Schema, not a built
|
|
73
73
|
* Fabricator: `AnySchema` excludes `Fabricator<any>` (which carries no
|
|
74
|
-
* `[Produces]`), so `
|
|
74
|
+
* `[Produces]`), so `Fabricated` can't silently degrade to `unknown`.
|
|
75
75
|
*/
|
|
76
|
-
export type Enumerable = <const $Schema extends AnySchema>(schema: $Schema) => Iterable<
|
|
76
|
+
export type Enumerable = <const $Schema extends AnySchema>(schema: $Schema) => Iterable<Fabricated<$Schema>>;
|
|
77
77
|
export type Resolvable = {
|
|
78
78
|
[Kind]: SchemaKind;
|
|
79
79
|
[Meta]: PlainObject<any>;
|
|
@@ -45,6 +45,24 @@ export declare namespace FabricatorError {
|
|
|
45
45
|
*/
|
|
46
46
|
limit: unknown);
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* `initialize({ clock })`, `fork({ clock })`, or `wrap({ clock }, …)` given
|
|
50
|
+
* an instant a `Date` cannot hold exactly — a fractional or non-finite
|
|
51
|
+
* number, one past ±8.64e15, or an invalid `Date`. Rejected rather than
|
|
52
|
+
* adjusted the way `Date`'s own `TimeClip` would, so the clock a run records
|
|
53
|
+
* is the clock the caller passed.
|
|
54
|
+
*/
|
|
55
|
+
class InvalidClockError extends FabricatorError {
|
|
56
|
+
/**
|
|
57
|
+
* The rejected clock, as given — a number or a `Date`.
|
|
58
|
+
*/
|
|
59
|
+
readonly clock: unknown;
|
|
60
|
+
constructor(
|
|
61
|
+
/**
|
|
62
|
+
* The rejected clock, as given — a number or a `Date`.
|
|
63
|
+
*/
|
|
64
|
+
clock: unknown);
|
|
65
|
+
}
|
|
48
66
|
/**
|
|
49
67
|
* A `self` placeholder resolved with no `T.recursive` expanding around it —
|
|
50
68
|
* only reachable by holding a `self` reference outside the `T.recursive(...)`
|
|
@@ -213,27 +231,57 @@ export declare namespace FabricatorError {
|
|
|
213
231
|
value: unknown);
|
|
214
232
|
}
|
|
215
233
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
234
|
+
* A key names a field the object schema does not define — most often a typo,
|
|
235
|
+
* which is why the known fields are listed. One failure reached from four
|
|
236
|
+
* places: `.override()`, `T.pick`, `T.omit`, and a compute field's
|
|
237
|
+
* `.from([...])`, told apart by `operation`.
|
|
218
238
|
*/
|
|
219
|
-
class
|
|
239
|
+
class UnknownFieldError extends FabricatorError {
|
|
220
240
|
/**
|
|
221
241
|
* The name that was not found.
|
|
222
242
|
*/
|
|
223
243
|
readonly field: string;
|
|
224
244
|
/**
|
|
225
|
-
* Every field the schema does define, in definition order.
|
|
245
|
+
* Every field the schema does define, in definition order. For `"compute
|
|
246
|
+
* from"`, the fields the enclosing `.refine()` stage can read.
|
|
226
247
|
*/
|
|
227
248
|
readonly available: ReadonlyArray<string>;
|
|
249
|
+
/**
|
|
250
|
+
* What named the unknown field, worded to read as the message's verb.
|
|
251
|
+
*/
|
|
252
|
+
readonly operation: "override" | "pick" | "omit" | "compute from";
|
|
228
253
|
constructor(
|
|
229
254
|
/**
|
|
230
255
|
* The name that was not found.
|
|
231
256
|
*/
|
|
232
257
|
field: string,
|
|
233
258
|
/**
|
|
234
|
-
* Every field the schema does define, in definition order.
|
|
259
|
+
* Every field the schema does define, in definition order. For `"compute
|
|
260
|
+
* from"`, the fields the enclosing `.refine()` stage can read.
|
|
261
|
+
*/
|
|
262
|
+
available: ReadonlyArray<string>,
|
|
263
|
+
/**
|
|
264
|
+
* What named the unknown field, worded to read as the message's verb.
|
|
265
|
+
*/
|
|
266
|
+
operation: "override" | "pick" | "omit" | "compute from");
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Compute fields whose declared dependencies form a loop, so none of them can
|
|
270
|
+
* resolve first. A compute field only names fields that already exist, but a
|
|
271
|
+
* later `.refine()` or `.extend()` can replace one of those with a compute
|
|
272
|
+
* field that reads it back. Thrown by that call, while building the schema,
|
|
273
|
+
* never at fabrication.
|
|
274
|
+
*/
|
|
275
|
+
class ComputeDependencyCycleError extends FabricatorError {
|
|
276
|
+
/**
|
|
277
|
+
* The compute fields on the loop, in the order they were reached.
|
|
235
278
|
*/
|
|
236
|
-
|
|
279
|
+
readonly fields: ReadonlyArray<string>;
|
|
280
|
+
constructor(
|
|
281
|
+
/**
|
|
282
|
+
* The compute fields on the loop, in the order they were reached.
|
|
283
|
+
*/
|
|
284
|
+
fields: ReadonlyArray<string>);
|
|
237
285
|
}
|
|
238
286
|
/**
|
|
239
287
|
* An override value does not fit the field it is meant to replace. Raised for
|
|
@@ -548,6 +596,23 @@ export declare namespace FabricatorError {
|
|
|
548
596
|
*/
|
|
549
597
|
given: string);
|
|
550
598
|
}
|
|
599
|
+
/**
|
|
600
|
+
* `fabricate(...)` handed anything but a Schema: a built Fabricator, a bare
|
|
601
|
+
* builder that still needs configuring (`T.array(x)`, `T.recursive(f)`), or a
|
|
602
|
+
* value that is no schema at all. Its type already rejects each; this is the
|
|
603
|
+
* runtime guard for an untyped caller.
|
|
604
|
+
*/
|
|
605
|
+
class FabricateArgumentError extends FabricatorError {
|
|
606
|
+
/**
|
|
607
|
+
* What `fabricate(...)` was handed.
|
|
608
|
+
*/
|
|
609
|
+
readonly argument: unknown;
|
|
610
|
+
constructor(
|
|
611
|
+
/**
|
|
612
|
+
* What `fabricate(...)` was handed.
|
|
613
|
+
*/
|
|
614
|
+
argument: unknown);
|
|
615
|
+
}
|
|
551
616
|
/**
|
|
552
617
|
* `wrap(overlay, block)` handed an `async` block while the lineage's ambient
|
|
553
618
|
* carrier is the synchronous one (`Instance/Stack/Sync.ts`).
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Ancestry, Stack } from "../Instance/Types";
|
|
2
|
+
import type { RandomSource } from "../Random/Types";
|
|
3
|
+
import type { AnySchema, Fabricated } from "../Schema/Types";
|
|
4
|
+
import type { Constructor } from "./Constructor";
|
|
5
|
+
import type { NaiveFabricator } from "./Types";
|
|
6
|
+
/**
|
|
7
|
+
* `new Fabricator(schema).fabricate()` in one call, without a build per call:
|
|
8
|
+
* the first call with a given schema in a given scope builds, and every later
|
|
9
|
+
* one continues that same Fabricator's streams. A repeated call therefore takes
|
|
10
|
+
* no construction ordinal, so it shifts nothing built after it.
|
|
11
|
+
*
|
|
12
|
+
* The key is the schema's identity. A schema held in a variable — including an
|
|
13
|
+
* overridden one, `const Admin = User.override({ ... })` — is reused. One
|
|
14
|
+
* written at the call site — `fabricate(User.override({ ... }))`, an inline
|
|
15
|
+
* `.whereby(...)` — is a new object on every call, so every call builds and
|
|
16
|
+
* takes an ordinal, exactly as `new Fabricator(...).fabricate()` would.
|
|
17
|
+
*
|
|
18
|
+
* A Schema only, never a built Fabricator: given one, neither reusing it nor
|
|
19
|
+
* caching a fresh build of it is the obvious reading, so the type rejects it
|
|
20
|
+
* (`fabricate?: never` — a Fabricator otherwise matches `AnySchema`
|
|
21
|
+
* structurally), and the runtime throws `FabricateArgumentError` for it or for
|
|
22
|
+
* anything else that is not a Schema.
|
|
23
|
+
*
|
|
24
|
+
* Identity rather than structure on purpose: a schema can carry functions
|
|
25
|
+
* (`.as` producers, compute and derive resolvers, `T.opaque`) and `always`/
|
|
26
|
+
* `enum` hold arbitrary values, none of which has a sound structural equality.
|
|
27
|
+
*/
|
|
28
|
+
export type Fabricate = <const $Schema extends AnySchema & {
|
|
29
|
+
readonly fabricate?: never;
|
|
30
|
+
}>(schema: $Schema) => Fabricated<$Schema>;
|
|
31
|
+
/**
|
|
32
|
+
* Every cached Fabricator of one lineage, keyed first by the `RandomSource`
|
|
33
|
+
* whose ordinal its build took, then by the schema it was built from. Both maps
|
|
34
|
+
* are weak: a per-test `wrap`'s source is released with its scope, and its
|
|
35
|
+
* entries with it.
|
|
36
|
+
*/
|
|
37
|
+
export type FabricatorCache = WeakMap<RandomSource, WeakMap<AnySchema, NaiveFabricator<unknown>>>;
|
|
38
|
+
/**
|
|
39
|
+
* Bind `fabricate` to one instance — the same `(source, stack, ancestry)` its
|
|
40
|
+
* `construct` closes over — and to its lineage's shared `fabricatorCache`.
|
|
41
|
+
*
|
|
42
|
+
* **Keyed by source before schema.** The source is the one `resolveScope` would
|
|
43
|
+
* take an ordinal from (`toEffectiveSource`): the innermost visible `wrap`
|
|
44
|
+
* frame's, else this instance's. Keyed by schema alone, a module-level `User`
|
|
45
|
+
* fabricated in one test would carry its stream into the next, so a test's data
|
|
46
|
+
* would depend on which tests ran before it — the order-dependence each
|
|
47
|
+
* per-test `wrap` exists to remove. Keyed by source, every fresh scope starts
|
|
48
|
+
* empty.
|
|
49
|
+
*
|
|
50
|
+
* **Shared across the lineage, not held per instance.** Inside a `wrap`, the
|
|
51
|
+
* receiver and the wrap's own scope both resolve the frame's source, and must
|
|
52
|
+
* reach the same cached Fabricator — otherwise interleaving
|
|
53
|
+
* `instance.fabricate(S)` with `scope.fabricate(S)` would draw two separate
|
|
54
|
+
* sequences, where `new Fabricator` keeps the implicit and explicit routes in
|
|
55
|
+
* agreement (`test/Wrap.test.ts`). Two lineages never resolve the same source,
|
|
56
|
+
* so one map per lineage shares nothing between `initialize()` calls.
|
|
57
|
+
*
|
|
58
|
+
* The source is resolved and the build made in one synchronous call, so
|
|
59
|
+
* `construct` resolves the same frame this function keyed by.
|
|
60
|
+
*/
|
|
61
|
+
export declare function Fabricate(construct: Constructor, source: RandomSource, stack: Stack, ancestry: Ancestry, fabricatorCache: FabricatorCache): Fabricate;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Adaptations, AdaptationsOf } from "../Adapter/Types";
|
|
2
2
|
import type { Primitive, Kind as SchemaKind } from "../Primitive";
|
|
3
|
-
import type { Algorithm, Trace } from "../Random/Types";
|
|
4
|
-
import type {
|
|
5
|
-
import type { Adaptation, Kind, Meta, Produces } from "../Types";
|
|
3
|
+
import type { Algorithm, RandomSource, Salt, Trace } from "../Random/Types";
|
|
4
|
+
import type { Fabricated } from "../Schema/Types";
|
|
5
|
+
import type { Adaptation, Kind, Meta, Produced, Produces } from "../Types";
|
|
6
6
|
import type { PlainObject } from "../Utility/Types";
|
|
7
7
|
/**
|
|
8
8
|
* Threaded through `Constructor.ts`'s `make` for every dispatch of one `new
|
|
@@ -26,8 +26,17 @@ import type { PlainObject } from "../Utility/Types";
|
|
|
26
26
|
*
|
|
27
27
|
* `algorithm` rather than the `RandomSource` itself: stream derivation depends
|
|
28
28
|
* on no per-source state, and a leaf has no business with
|
|
29
|
-
* `toConstructionTrace
|
|
30
|
-
*
|
|
29
|
+
* `toConstructionTrace`. `clock` is not a field of its own — it is always
|
|
30
|
+
* `trace.clock`.
|
|
31
|
+
*
|
|
32
|
+
* `fork` mints `T.recursive`'s private source, and is bound to this
|
|
33
|
+
* construction's _resolved_ algorithm and clock rather than the instance's: an
|
|
34
|
+
* active `wrap` frame's, or a pinned clock from a replayed {@link Trace}. A
|
|
35
|
+
* fork carries only its parent's algorithm and clock forward (its salt is the
|
|
36
|
+
* argument), so those two are all that make one fork differ from another, and
|
|
37
|
+
* the instance's own `source.fork` would silently give a recursive expansion
|
|
38
|
+
* the instance's "now" inside `wrap({ clock })`. An expansion rebinds it to its
|
|
39
|
+
* private source's `fork`, which carries the same pair.
|
|
31
40
|
*
|
|
32
41
|
* `self` is what makes `case "recursive.self"` resolve to "recurse one level
|
|
33
42
|
* deeper, right now" — absent outside any active recursion, which is how `case
|
|
@@ -36,6 +45,7 @@ import type { PlainObject } from "../Utility/Types";
|
|
|
36
45
|
export type ConstructionContext = {
|
|
37
46
|
toTrace: (path: ReadonlyArray<string>, kind: string) => Trace;
|
|
38
47
|
algorithm: Algorithm;
|
|
48
|
+
fork: (salt: Salt) => RandomSource;
|
|
39
49
|
self?: (() => unknown) | undefined;
|
|
40
50
|
};
|
|
41
51
|
/**
|
|
@@ -89,9 +99,9 @@ export type Fabricator<$T> = NaiveFabricator<$T> & {
|
|
|
89
99
|
* `fabricate` signature. Every kind's `construct()` output shares the exact
|
|
90
100
|
* same shape (`NaiveFabricator<$T>` plus `[Kind]`/`[Meta]`, see this file's
|
|
91
101
|
* `Fabricator<$T>`), so there's nothing to dispatch per kind here — unlike
|
|
92
|
-
* `
|
|
93
|
-
* pre-`construct()` case, this only ever needs to unwrap an
|
|
94
|
-
* `fabricate: () => $T`.
|
|
102
|
+
* `Schema/Types.ts`'s `Fabricated`, which reads a Schema's phantom `[Produces]`
|
|
103
|
+
* marker for the pre-`construct()` case, this only ever needs to unwrap an
|
|
104
|
+
* already-uniform `fabricate: () => $T`.
|
|
95
105
|
*/
|
|
96
106
|
export type Fabrication<$Fabricator extends NaiveFabricator<any>> = $Fabricator extends NaiveFabricator<infer $T> ? $T : never;
|
|
97
107
|
/**
|
|
@@ -100,7 +110,7 @@ export type Fabrication<$Fabricator extends NaiveFabricator<any>> = $Fabricator
|
|
|
100
110
|
* carries a required `as` method a built Fabricator never has) to the precise,
|
|
101
111
|
* kind-specific `Fabricator<...>` type it builds into. Mirrors
|
|
102
112
|
* `Adapter/TypeBox/index.ts`'s `ToTypeBox` dispatch. Falls back to the loose
|
|
103
|
-
* base `Fabricator<
|
|
113
|
+
* base `Fabricator<Fabricated<$Schema>>` for anything unmatched.
|
|
104
114
|
*
|
|
105
115
|
* No branch here needs to do anything about `[Adaptation]`: each kind's own
|
|
106
116
|
* `Fabricator<$Schema>` reads it back off `$Schema` via `AdaptationsOf`, so an
|
|
@@ -139,5 +149,5 @@ $Schema extends Primitive.object.Core ? Primitive.object.Fabricator<$Schema> : $
|
|
|
139
149
|
* (unbound) is the same default `self.Core` itself falls back to.
|
|
140
150
|
*/
|
|
141
151
|
$Schema extends Primitive.recursive.self.Core ? Primitive.recursive.self.Fabricator<[]> : $Schema extends Primitive.tuple.Core ? Primitive.tuple.Fabricator<$Schema> : $Schema extends Primitive.derive.Core ? Primitive.derive.Fabricator<$Schema> : $Schema extends Primitive.always.Core ? Primitive.always.Fabricator<$Schema> : $Schema extends Primitive.bigint.Core ? Primitive.bigint.Fabricator<$Schema> : $Schema extends Primitive.boolean.Core ? Primitive.boolean.Fabricator<$Schema> : $Schema extends Primitive.choice.Core ? Primitive.choice.Fabricator<$Schema> : $Schema extends Primitive.enum.Core ? Primitive.enum.Fabricator<$Schema> : $Schema extends Primitive.date.Core ? Primitive.date.Fabricator<$Schema> : $Schema extends Primitive.number.Core ? Primitive.number.Fabricator<$Schema> : $Schema extends Primitive.string.Core ? Primitive.string.Fabricator<$Schema> : $Schema extends Primitive.symbol.Core ? Primitive.symbol.Fabricator<$Schema> : $Schema extends Primitive.undefined.Core ? Primitive.undefined.Fabricator<$Schema> : $Schema extends Primitive.undefinable.Core ? Primitive.undefinable.Fabricator<$Schema> : $Schema extends Primitive.null.Core ? Primitive.null.Fabricator<$Schema> : $Schema extends Primitive.nullable.Core ? Primitive.nullable.Fabricator<$Schema> : $Schema extends Primitive.nullish.Core ? Primitive.nullish.Fabricator<$Schema> : $Schema extends {
|
|
142
|
-
readonly [Produces]?: unknown
|
|
143
|
-
} ? Fabricator<
|
|
152
|
+
readonly [Produces]?: Produced<unknown>;
|
|
153
|
+
} ? Fabricator<Fabricated<$Schema>> : Fabricator<unknown>;
|
|
@@ -52,12 +52,12 @@ import type { FabricatorTestContext, Integration } from "./Types";
|
|
|
52
52
|
* running providers outside the frame it opened, and raises
|
|
53
53
|
* `HarnessingProviderError` rather than quietly providing the base instance.
|
|
54
54
|
*
|
|
55
|
-
* Nothing here sets `clock`. A pinned `Date` (the recommended setup)
|
|
56
|
-
* wall-clock default are already concrete numbers by the time
|
|
57
|
-
* them, so they inherit through every `wrap` — the frame in
|
|
58
|
-
* salt varies per test, "now" does not. `clock: "derived"`
|
|
59
|
-
* purpose — that policy's documented meaning is that the salt
|
|
60
|
-
* reproducibility unit, so per-test clocks are the request honored,
|
|
61
|
-
* to override.
|
|
55
|
+
* Nothing here sets `clock`. A pinned `Date` or number (the recommended setup)
|
|
56
|
+
* and the wall-clock default are already concrete numbers by the time
|
|
57
|
+
* `overlay()` sees them, so they inherit through every `wrap` — the frame in
|
|
58
|
+
* effect's included: salt varies per test, "now" does not. `clock: "derived"`
|
|
59
|
+
* is left alone on purpose — that policy's documented meaning is that the salt
|
|
60
|
+
* _is_ the reproducibility unit, so per-test clocks are the request honored,
|
|
61
|
+
* not a bug to override.
|
|
62
62
|
*/
|
|
63
63
|
export declare function integration<$Registry extends PlainObject>(instance: Instance<$Registry>): Integration<FabricatorTestContext<$Registry>, Instance<$Registry>>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type FabricatorCache } from "../Fabricator/Fabricate";
|
|
1
2
|
import type { RandomSource } from "../Random/Types";
|
|
2
3
|
import type { PlainObject } from "../Utility/Types";
|
|
3
4
|
import type { Ancestry, Config, Instance, Overlay, Stack } from "./Types";
|
|
@@ -32,13 +33,15 @@ export declare const DEFAULT_COMBINATORIAL_LIMIT = 1024;
|
|
|
32
33
|
* inherited, or defaulted), so a bad limit fails at `fork()`/`initialize()`
|
|
33
34
|
* time rather than at first use. `clock` follows the given → inherited →
|
|
34
35
|
* default shape `algorithm` does, but stays _unresolved_ only for the explicit
|
|
35
|
-
* `"derived"` sentinel: an explicit `Date` and the unconfigured
|
|
36
|
-
* default are stored as epoch milliseconds (a stated instant,
|
|
37
|
-
* from then on
|
|
38
|
-
*
|
|
39
|
-
* `
|
|
40
|
-
* `
|
|
41
|
-
*
|
|
36
|
+
* `"derived"` sentinel: an explicit `Date` or number and the unconfigured
|
|
37
|
+
* wall-clock default are stored as epoch milliseconds (a stated instant,
|
|
38
|
+
* inherited as-is from then on; an explicit one is checked by `resolveInstant`,
|
|
39
|
+
* so a `fork()`/`wrap()` rejects a bad clock as eagerly as `initialize()`
|
|
40
|
+
* does), while `"derived"` is left as the sentinel rather than collapsed to a
|
|
41
|
+
* number, so `resolveClock` can re-derive it from whichever `salt` is actually
|
|
42
|
+
* in effect at read time. An omitted `clock` on a `fork`/`wrap` whose salt
|
|
43
|
+
* changed therefore keeps the parent's instant unless that parent was itself
|
|
44
|
+
* `"derived"`.
|
|
42
45
|
*/
|
|
43
46
|
export declare function overlay<$Registry extends PlainObject>(base: Partial<Config<PlainObject>>, over: Overlay<$Registry>): Config<$Registry>;
|
|
44
47
|
/**
|
|
@@ -59,7 +62,10 @@ export declare function overlay<$Registry extends PlainObject>(base: Partial<Con
|
|
|
59
62
|
*
|
|
60
63
|
* `parent` describes the instance this one is derived from, and its absence is
|
|
61
64
|
* the single marker of a root: `initialize` passes none, every `fork`/`wrap`
|
|
62
|
-
* passes the receiver's.
|
|
65
|
+
* passes the receiver's. Its three fields always travel together: `ancestry`
|
|
66
|
+
* and `root` place this instance in its lineage, and `fabricatorCache` is the
|
|
67
|
+
* lineage's one `fabricate` cache, created here at the root and handed to every
|
|
68
|
+
* descendant (see `Fabricator/Fabricate.ts` for why it is shared).
|
|
63
69
|
*
|
|
64
70
|
* A fresh token is minted either way, so no two instances share an identity,
|
|
65
71
|
* and the `ancestry` built here is _this_ instance's: the parent's plus one.
|
|
@@ -67,6 +73,7 @@ export declare function overlay<$Registry extends PlainObject>(base: Partial<Con
|
|
|
67
73
|
export declare function instantiate<$Registry extends PlainObject>(config: Config<$Registry>, stack: Stack, parent?: {
|
|
68
74
|
ancestry: Ancestry;
|
|
69
75
|
root: Instance<PlainObject>;
|
|
76
|
+
fabricatorCache: FabricatorCache;
|
|
70
77
|
}): {
|
|
71
78
|
instance: Instance<$Registry>;
|
|
72
79
|
source: RandomSource;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { RandomSource } from "../../Random/Types";
|
|
1
2
|
import type { Ancestry, Frame, Stack } from "../Types";
|
|
2
3
|
/**
|
|
3
4
|
* The frames in `frames` that `ancestry` may resolve against, outermost first —
|
|
@@ -24,3 +25,12 @@ export declare function toVisible(frames: ReadonlyArray<Frame>, ancestry: Ancest
|
|
|
24
25
|
* indexing `visible()` itself, so "innermost visible" has one definition.
|
|
25
26
|
*/
|
|
26
27
|
export declare function toInnermostFrame(stack: Stack, ancestry: Ancestry): Frame | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* The `RandomSource` a construction reached right now draws from: the innermost
|
|
30
|
+
* visible frame's, or `source` (the reader's own) outside any. One definition
|
|
31
|
+
* for every consumer that needs it — `resolveScope` takes its ordinal from it,
|
|
32
|
+
* `enumerables` derives its salts from it, and `fabricate` keys its cache by it
|
|
33
|
+
* — so the cache key and the source a cached build drew its ordinal from cannot
|
|
34
|
+
* disagree.
|
|
35
|
+
*/
|
|
36
|
+
export declare function toEffectiveSource(source: RandomSource, stack: Stack, ancestry: Ancestry): RandomSource;
|