@earnestjs/model 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/LICENSE.LESSER +165 -0
- package/README.md +298 -2
- package/dist/breadth.d.ts +125 -0
- package/dist/breadth.js +150 -0
- package/dist/declarations.d.ts +201 -0
- package/dist/declarations.js +482 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +7 -0
- package/dist/m.d.ts +10 -0
- package/dist/m.js +3 -0
- package/dist/model.d.ts +297 -0
- package/dist/model.js +262 -0
- package/dist/replay.d.ts +41 -0
- package/dist/replay.js +99 -0
- package/dist/runs.d.ts +41 -0
- package/dist/runs.js +325 -0
- package/dist/search.d.ts +130 -0
- package/dist/search.js +484 -0
- package/dist/values.d.ts +296 -0
- package/dist/values.js +695 -0
- package/package.json +51 -4
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import { LockValue, MapValue, ModelValue, OwnedLockValue, QueueValue, REQUEST_STAGES, RequestValue, RetryValue, SeqValue, SetValue, TIMER_STAGES, TimerValue, frozen, isJsonList, isJsonObject, isRecord, toJson, } from "./values.js";
|
|
2
|
+
/** The most values `Declaration.values` will list before it throws instead. */
|
|
3
|
+
export const MOST_VALUES_LISTED = 1_000_000;
|
|
4
|
+
const shown = (value) => value === undefined || typeof value === "function" || typeof value === "symbol"
|
|
5
|
+
? String(value)
|
|
6
|
+
: JSON.stringify(isRecord(value) || value instanceof ModelValue ? toJson(value) : value);
|
|
7
|
+
const unreadable = (kind, json) => new Error(`${JSON.stringify(json)} can't be read as a ${kind}`);
|
|
8
|
+
const optionalOf = (inner, declared) => ({
|
|
9
|
+
kind: "optional",
|
|
10
|
+
source: `${inner.source}.optional()`,
|
|
11
|
+
count: inner.count + 1,
|
|
12
|
+
libraryKind: inner.libraryKind,
|
|
13
|
+
inner: declared,
|
|
14
|
+
rest: null,
|
|
15
|
+
values: () => [null, ...inner.values()],
|
|
16
|
+
has: (value) => value === null || inner.has(value),
|
|
17
|
+
adopt: (value) => (value === null ? null : inner.adopt(value)),
|
|
18
|
+
decode: (json) => (json === null ? null : inner.decode(json)),
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* The finite set of values a field, an input or a collection member can take. Build one with
|
|
22
|
+
* `m.boolean()`, `m.int(lo, hi)` and the other builders; it is a plain value, so one declaration
|
|
23
|
+
* can be shared by several fields and models. `.optional()` and `.initial(value)` give back a
|
|
24
|
+
* new declaration and leave this one as it was.
|
|
25
|
+
*/
|
|
26
|
+
export class Declaration {
|
|
27
|
+
#shape;
|
|
28
|
+
#initial;
|
|
29
|
+
/** Model files use the `m` builders instead of calling this. */
|
|
30
|
+
constructor(shape, initial = null) {
|
|
31
|
+
this.#shape = shape;
|
|
32
|
+
this.#initial = initial;
|
|
33
|
+
}
|
|
34
|
+
/** What was declared, in words: `"enum"`, `"number"`, `"set"` and so on. */
|
|
35
|
+
get kind() {
|
|
36
|
+
return this.#shape.kind;
|
|
37
|
+
}
|
|
38
|
+
/** How many values the declaration has. Very large counts are approximate. */
|
|
39
|
+
get count() {
|
|
40
|
+
return this.#shape.count;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Where the values are not plain JavaScript, the kind of the first part that holds objects of
|
|
44
|
+
* this library, such as `SetValue`: `"set"` for `m.set(item)`, also inside `.optional()` or a
|
|
45
|
+
* record. `null` when every value is a boolean, a number, a text, `null` or a record of those,
|
|
46
|
+
* values that any JavaScript code can hold, copy and compare by content.
|
|
47
|
+
*/
|
|
48
|
+
get libraryKind() {
|
|
49
|
+
return this.#shape.libraryKind;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The `m` expression that declares the same values, as a model file writes it, such as
|
|
53
|
+
* `m.int(0, 3).optional()` or `m.record({ seat: m.enum(["A", "B"]) })`. It leaves out
|
|
54
|
+
* `.initial`. Code that writes model files puts it after `m.model`'s field names.
|
|
55
|
+
*/
|
|
56
|
+
get source() {
|
|
57
|
+
return this.#shape.source;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* For a declaration made with `m.record`, the declaration of each field, as given to
|
|
61
|
+
* `m.record`; `null` for every other kind, an optional record included. Code that checks a
|
|
62
|
+
* value field by field reads it, as `@earnestjs/xstate` does to let an optional field inside a
|
|
63
|
+
* record be missing.
|
|
64
|
+
*/
|
|
65
|
+
get fields() {
|
|
66
|
+
return this.#shape.fields ?? null;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* For a declaration made with `.optional()`, the same declaration without `null` and without
|
|
70
|
+
* `.initial`; `null` for every other kind. Code that checks a value against the declaration
|
|
71
|
+
* step by step reads it, as `fields` is read for a record.
|
|
72
|
+
*/
|
|
73
|
+
get inner() {
|
|
74
|
+
return this.#shape.inner ?? null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Every value, in the order the checker tries them: `false` before `true`, numbers upward,
|
|
78
|
+
* enum values as listed, `null` before the values of an optional, a record's fields in the
|
|
79
|
+
* order declared with the first one changing slowest. Throws when there are more than
|
|
80
|
+
* `MOST_VALUES_LISTED`.
|
|
81
|
+
*/
|
|
82
|
+
values() {
|
|
83
|
+
if (this.#shape.count > MOST_VALUES_LISTED) {
|
|
84
|
+
throw new Error(`this ${this.kind} has ${this.#shape.count} values, too many to list; declare a smaller one`);
|
|
85
|
+
}
|
|
86
|
+
return this.#shape.values();
|
|
87
|
+
}
|
|
88
|
+
/** Whether `value` is one of the declared values. Any value may be asked about. */
|
|
89
|
+
has(value) {
|
|
90
|
+
return this.#shape.has(value);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The values a field declared this way starts at: the `.initial` value when there is one;
|
|
94
|
+
* otherwise the empty, free, idle or zero value of a collection, lock, timer, request or
|
|
95
|
+
* retry; otherwise every value.
|
|
96
|
+
*/
|
|
97
|
+
starts() {
|
|
98
|
+
if (this.#initial !== null) {
|
|
99
|
+
return [this.#initial.value];
|
|
100
|
+
}
|
|
101
|
+
return this.#shape.rest === null ? this.values() : [this.#shape.rest.value];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Reads a value from the form `earnest verify --json` prints. The result may lie outside the
|
|
105
|
+
* declaration, as a value on an out-of-range path does, so check it with `has`. Throws when
|
|
106
|
+
* the JSON is of another kind altogether, such as text where a set is declared.
|
|
107
|
+
*/
|
|
108
|
+
fromJson(json) {
|
|
109
|
+
return this.#shape.decode(json);
|
|
110
|
+
}
|
|
111
|
+
/** The same declaration, starting at `value`. Throws when `value` is not a declared value. */
|
|
112
|
+
initial(value) {
|
|
113
|
+
const adopted = frozen(this.#shape.adopt(value));
|
|
114
|
+
if (!this.#shape.has(adopted)) {
|
|
115
|
+
throw new Error(`${shown(adopted)} is not a value of this ${this.kind}`);
|
|
116
|
+
}
|
|
117
|
+
return new Declaration(this.#shape, { value: adopted });
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The same declaration with `null` as one more value. Without `.initial` it then starts at
|
|
121
|
+
* every value, `null` included. Throws for a declaration that is already optional and for a
|
|
122
|
+
* lock with an owner, whose free state is already `null`.
|
|
123
|
+
*/
|
|
124
|
+
optional() {
|
|
125
|
+
if (this.kind === "optional" || this.kind === "owned lock") {
|
|
126
|
+
throw new Error(`an ${this.kind} can't be made optional: it already has a null value`);
|
|
127
|
+
}
|
|
128
|
+
return new Declaration(optionalOf(this.#shape, new Declaration(this.#shape)), this.#initial);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The value a field declared as `declared` starts at when it is given `value` as `.initial`
|
|
133
|
+
* would be. Throws when `value` is not a value of the declaration.
|
|
134
|
+
*/
|
|
135
|
+
export const startingAt = (declared, value) => {
|
|
136
|
+
const given = declared;
|
|
137
|
+
return given.initial(value).starts()[0];
|
|
138
|
+
};
|
|
139
|
+
const combinationCount = (entries) => entries.reduce((total, [, declared]) => total * declared.count, 1);
|
|
140
|
+
/**
|
|
141
|
+
* Every row of values the declarations in `entries` give, in the order the checker tries an
|
|
142
|
+
* action's inputs: the first name varies fastest.
|
|
143
|
+
*/
|
|
144
|
+
export const rowsOf = (entries) => entries.reduceRight((rest, [name, declared]) => rest.flatMap((later) => declared.values().map((value) => ({ [name]: value, ...later }))), [{}]);
|
|
145
|
+
/**
|
|
146
|
+
* Every combination of one value for each of `fields`, in the order `explore` tries an action's
|
|
147
|
+
* inputs: the first field varies fastest, each through its values in the order of `values()`.
|
|
148
|
+
* Gives one empty row when there are no fields. Throws, before it lists any, when there are more
|
|
149
|
+
* than `MOST_VALUES_LISTED` combinations.
|
|
150
|
+
*/
|
|
151
|
+
export const combinationsOf = (fields) => {
|
|
152
|
+
const entries = Object.entries(fields);
|
|
153
|
+
const count = combinationCount(entries);
|
|
154
|
+
if (count > MOST_VALUES_LISTED) {
|
|
155
|
+
throw new Error(`these fields have ${count} combinations of values, too many to list; declare fewer values`);
|
|
156
|
+
}
|
|
157
|
+
return rowsOf(entries);
|
|
158
|
+
};
|
|
159
|
+
const plain = (kind, json) => {
|
|
160
|
+
if (typeof json === "object") {
|
|
161
|
+
throw unreadable(kind, json);
|
|
162
|
+
}
|
|
163
|
+
return json;
|
|
164
|
+
};
|
|
165
|
+
const same = (value) => value;
|
|
166
|
+
/** Declares a field that is `true` or `false`. */
|
|
167
|
+
export const booleanOf = () => new Declaration({
|
|
168
|
+
kind: "boolean",
|
|
169
|
+
source: "m.boolean()",
|
|
170
|
+
libraryKind: null,
|
|
171
|
+
count: 2,
|
|
172
|
+
rest: null,
|
|
173
|
+
values: () => [false, true],
|
|
174
|
+
has: (value) => typeof value === "boolean",
|
|
175
|
+
adopt: same,
|
|
176
|
+
decode: (json) => plain("boolean", json),
|
|
177
|
+
});
|
|
178
|
+
/**
|
|
179
|
+
* Declares a whole number from `lo` to `hi`, both included. Arithmetic that leaves the range is
|
|
180
|
+
* not an error in the action: the checker reports the state as out of range. Throws when a bound
|
|
181
|
+
* is not a whole number or `lo` is above `hi`.
|
|
182
|
+
*/
|
|
183
|
+
export const int = (lo, hi) => {
|
|
184
|
+
if (!Number.isSafeInteger(lo) || !Number.isSafeInteger(hi) || lo > hi) {
|
|
185
|
+
throw new Error(`m.int(${lo}, ${hi}) needs two whole numbers, the lower one first`);
|
|
186
|
+
}
|
|
187
|
+
return new Declaration({
|
|
188
|
+
kind: "number",
|
|
189
|
+
source: `m.int(${lo}, ${hi})`,
|
|
190
|
+
libraryKind: null,
|
|
191
|
+
count: hi - lo + 1,
|
|
192
|
+
rest: null,
|
|
193
|
+
values: () => Array.from({ length: hi - lo + 1 }, (_, position) => lo + position),
|
|
194
|
+
has: (value) => typeof value === "number" && Number.isInteger(value) && lo <= value && value <= hi,
|
|
195
|
+
adopt: same,
|
|
196
|
+
decode: (json) => plain("number", json),
|
|
197
|
+
});
|
|
198
|
+
};
|
|
199
|
+
const choice = (builder, options) => {
|
|
200
|
+
const listed = [...options];
|
|
201
|
+
if (listed.length === 0 || new Set(listed).size !== listed.length) {
|
|
202
|
+
throw new Error(`${builder} needs a list of one or more different values`);
|
|
203
|
+
}
|
|
204
|
+
if (new Set(listed.map((option) => typeof option)).size !== 1) {
|
|
205
|
+
throw new Error(`${builder} needs values that are all text, all numbers or all booleans`);
|
|
206
|
+
}
|
|
207
|
+
if (listed.some((option) => typeof option === "number" && !Number.isSafeInteger(option))) {
|
|
208
|
+
throw new Error(`${builder} needs whole numbers that JavaScript holds exactly`);
|
|
209
|
+
}
|
|
210
|
+
return new Declaration({
|
|
211
|
+
kind: "enum",
|
|
212
|
+
source: `${builder}([${listed.map((option) => JSON.stringify(option)).join(", ")}])`,
|
|
213
|
+
libraryKind: null,
|
|
214
|
+
count: listed.length,
|
|
215
|
+
rest: null,
|
|
216
|
+
values: () => listed,
|
|
217
|
+
has: (value) => listed.some((option) => option === value),
|
|
218
|
+
adopt: same,
|
|
219
|
+
decode: (json) => plain("enum", json),
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
/** Declares a field that is one of the listed texts. Its type is the union of those literals. */
|
|
223
|
+
export const enumOf = (options) => choice("m.enum", options);
|
|
224
|
+
/**
|
|
225
|
+
* Declares a field that is one of the listed values, which are all text, all numbers or all
|
|
226
|
+
* booleans. Its type is the union of those literals.
|
|
227
|
+
*/
|
|
228
|
+
export const oneOf = (options) => choice("m.oneOf", options);
|
|
229
|
+
const propertyName = (name) => /^[A-Za-z_$][\w$]*$/.test(name) ? name : JSON.stringify(name);
|
|
230
|
+
const product = (prefixes, options) => prefixes.flatMap((prefix) => options.map((option) => [...prefix, option]));
|
|
231
|
+
/**
|
|
232
|
+
* Declares a record: an object with exactly the given fields. Read a field with `r.field` and
|
|
233
|
+
* change one with `{ ...r, field: value }`. Compare records field by field: `===` on two records
|
|
234
|
+
* compares the objects, not their content.
|
|
235
|
+
*/
|
|
236
|
+
export const record = (fields) => {
|
|
237
|
+
const entries = Object.entries(fields);
|
|
238
|
+
const has = (value) => isRecord(value) &&
|
|
239
|
+
Object.keys(value).length === entries.length &&
|
|
240
|
+
entries.every(([name, declared]) => declared.has(value[name]));
|
|
241
|
+
return new Declaration({
|
|
242
|
+
kind: "record",
|
|
243
|
+
source: `m.record({${entries.length === 0 ? "" : ` ${entries.map(([name, declared]) => `${propertyName(name)}: ${declared.source}`).join(", ")} `}})`,
|
|
244
|
+
count: combinationCount(entries),
|
|
245
|
+
fields: Object.freeze({ ...fields }),
|
|
246
|
+
libraryKind: entries.find(([, declared]) => declared.libraryKind !== null)?.[1].libraryKind ?? null,
|
|
247
|
+
rest: null,
|
|
248
|
+
values: () => entries
|
|
249
|
+
.reduce((partial, [name, declared]) => partial.flatMap((prefix) => declared.values().map((value) => ({ ...prefix, [name]: value }))), [{}])
|
|
250
|
+
.map(frozen)
|
|
251
|
+
.filter(has),
|
|
252
|
+
has,
|
|
253
|
+
adopt: same,
|
|
254
|
+
decode: (json) => {
|
|
255
|
+
if (!isJsonObject(json) || Object.keys(json).length !== entries.length) {
|
|
256
|
+
throw unreadable(`record with the fields ${entries.map(([name]) => name).join(", ")}`, json);
|
|
257
|
+
}
|
|
258
|
+
return Object.fromEntries(entries.map(([name, declared]) => {
|
|
259
|
+
const found = json[name];
|
|
260
|
+
if (found === undefined) {
|
|
261
|
+
throw unreadable(`record with the field ${name}`, json);
|
|
262
|
+
}
|
|
263
|
+
return [name, declared.fromJson(found)];
|
|
264
|
+
}));
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
};
|
|
268
|
+
const listOf = (kind, json) => {
|
|
269
|
+
if (!isJsonList(json)) {
|
|
270
|
+
throw unreadable(kind, json);
|
|
271
|
+
}
|
|
272
|
+
return json;
|
|
273
|
+
};
|
|
274
|
+
const subsets = (items) => items.reduce((smaller, item) => [...smaller, ...smaller.map((subset) => subset.add(item))], [SetValue.of([])]);
|
|
275
|
+
const mapsOver = (keys, values) => keys.reduce((smaller, key) => [
|
|
276
|
+
...smaller,
|
|
277
|
+
...smaller.flatMap((known) => values.map((value) => known.set(key, value))),
|
|
278
|
+
], [MapValue.of([])]);
|
|
279
|
+
/** Declares a set of values of `item`. Without `.initial` it starts empty. */
|
|
280
|
+
export const set = (item) => new Declaration({
|
|
281
|
+
kind: "set",
|
|
282
|
+
source: `m.set(${item.source})`,
|
|
283
|
+
libraryKind: "set",
|
|
284
|
+
count: 2 ** item.count,
|
|
285
|
+
rest: { value: SetValue.of([]) },
|
|
286
|
+
values: () => subsets(item.values()),
|
|
287
|
+
has: (value) => value instanceof SetValue && value.every((each) => item.has(each)),
|
|
288
|
+
adopt: same,
|
|
289
|
+
decode: (json) => SetValue.of(listOf("set", json).map((each) => item.fromJson(each))),
|
|
290
|
+
});
|
|
291
|
+
const holdable = (builder, inside) => {
|
|
292
|
+
if (inside.kind === "owned lock") {
|
|
293
|
+
throw new Error(`${builder} can't hold a lock with an owner: a free lock is already null, so reading it could not be told from reading nothing`);
|
|
294
|
+
}
|
|
295
|
+
return inside;
|
|
296
|
+
};
|
|
297
|
+
/**
|
|
298
|
+
* Declares a map from values of `key` to values of `value`. Without `.initial` it starts empty.
|
|
299
|
+
* Throws when `value` is a lock with an owner.
|
|
300
|
+
*/
|
|
301
|
+
export const map = (key, valueDeclared) => {
|
|
302
|
+
const value = holdable("m.map", valueDeclared);
|
|
303
|
+
return new Declaration({
|
|
304
|
+
kind: "map",
|
|
305
|
+
source: `m.map(${key.source}, ${value.source})`,
|
|
306
|
+
libraryKind: "map",
|
|
307
|
+
count: (value.count + 1) ** key.count,
|
|
308
|
+
rest: { value: MapValue.of([]) },
|
|
309
|
+
values: () => mapsOver(key.values(), value.values()),
|
|
310
|
+
has: (found) => found instanceof MapValue &&
|
|
311
|
+
[...found].every(([each, held]) => key.has(each) && value.has(held)),
|
|
312
|
+
adopt: same,
|
|
313
|
+
decode: (json) => MapValue.of(listOf("map", json).map((pair) => {
|
|
314
|
+
const [each, held, ...rest] = listOf("map entry", pair);
|
|
315
|
+
if (each === undefined || held === undefined || rest.length > 0) {
|
|
316
|
+
throw unreadable("map entry", pair);
|
|
317
|
+
}
|
|
318
|
+
return [key.fromJson(each), value.fromJson(held)];
|
|
319
|
+
})),
|
|
320
|
+
});
|
|
321
|
+
};
|
|
322
|
+
const lists = (items, longest) => {
|
|
323
|
+
const all = [[]];
|
|
324
|
+
let last = [[]];
|
|
325
|
+
for (let length = 0; length < longest; length += 1) {
|
|
326
|
+
last = product(last, items);
|
|
327
|
+
all.push(...last);
|
|
328
|
+
}
|
|
329
|
+
return all;
|
|
330
|
+
};
|
|
331
|
+
const listCount = (each, longest) => Array.from({ length: longest + 1 }, (_, length) => each ** length).reduce((total, ofLength) => total + ofLength, 0);
|
|
332
|
+
const wholeLimit = (builder, limit) => {
|
|
333
|
+
if (!Number.isSafeInteger(limit) || limit < 0) {
|
|
334
|
+
throw new Error(`${builder} needs a whole number that is not negative, not ${limit}`);
|
|
335
|
+
}
|
|
336
|
+
return limit;
|
|
337
|
+
};
|
|
338
|
+
/**
|
|
339
|
+
* Declares a sequence of at most `max` values of `item`. Without `.initial` it starts empty.
|
|
340
|
+
* Pushing past `max` is not an error in the action: the checker reports the state as out of
|
|
341
|
+
* range. Throws when `item` is a lock with an owner.
|
|
342
|
+
*/
|
|
343
|
+
export const seq = (itemDeclared, limit) => {
|
|
344
|
+
const item = holdable("m.seq", itemDeclared);
|
|
345
|
+
const max = wholeLimit("m.seq's max", limit.max);
|
|
346
|
+
return new Declaration({
|
|
347
|
+
kind: "sequence",
|
|
348
|
+
source: `m.seq(${item.source}, { max: ${max} })`,
|
|
349
|
+
libraryKind: "sequence",
|
|
350
|
+
count: listCount(item.count, max),
|
|
351
|
+
rest: { value: SeqValue.of([]) },
|
|
352
|
+
values: () => lists(item.values(), max).map((items) => SeqValue.of(items)),
|
|
353
|
+
has: (value) => value instanceof SeqValue && value.size <= max && value.every((each) => item.has(each)),
|
|
354
|
+
adopt: same,
|
|
355
|
+
decode: (json) => SeqValue.of(listOf("sequence", json).map((each) => item.fromJson(each))),
|
|
356
|
+
});
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* Declares a first-in, first-out queue of at most `capacity` values of `item`. Without
|
|
360
|
+
* `.initial` it starts empty; `.initial(m.seqOf(a, b))` starts it with `a` at the head. Sending
|
|
361
|
+
* to a full queue is not an error in the action: the checker reports the state as out of range.
|
|
362
|
+
* Throws when `item` is a lock with an owner.
|
|
363
|
+
*/
|
|
364
|
+
export const queue = (itemDeclared, limit) => {
|
|
365
|
+
const item = holdable("m.queue", itemDeclared);
|
|
366
|
+
const capacity = wholeLimit("m.queue's capacity", limit.capacity);
|
|
367
|
+
return new Declaration({
|
|
368
|
+
kind: "queue",
|
|
369
|
+
source: `m.queue(${item.source}, { capacity: ${capacity} })`,
|
|
370
|
+
libraryKind: "queue",
|
|
371
|
+
count: listCount(item.count, capacity),
|
|
372
|
+
rest: { value: QueueValue.of(capacity, []) },
|
|
373
|
+
values: () => lists(item.values(), capacity).map((items) => QueueValue.of(capacity, items)),
|
|
374
|
+
has: (value) => value instanceof QueueValue &&
|
|
375
|
+
value.capacity === capacity &&
|
|
376
|
+
value.size <= capacity &&
|
|
377
|
+
value.every((each) => item.has(each)),
|
|
378
|
+
adopt: (value) => QueueValue.of(capacity, value),
|
|
379
|
+
decode: (json) => QueueValue.of(capacity, listOf("queue", json).map((each) => item.fromJson(each))),
|
|
380
|
+
});
|
|
381
|
+
};
|
|
382
|
+
const plainLock = () => new Declaration({
|
|
383
|
+
kind: "lock",
|
|
384
|
+
source: "m.lock()",
|
|
385
|
+
libraryKind: "lock",
|
|
386
|
+
count: 2,
|
|
387
|
+
rest: { value: LockValue.of(false) },
|
|
388
|
+
values: () => [LockValue.of(false), LockValue.of(true)],
|
|
389
|
+
has: (value) => value instanceof LockValue,
|
|
390
|
+
adopt: same,
|
|
391
|
+
decode: (json) => {
|
|
392
|
+
if (typeof json !== "boolean") {
|
|
393
|
+
throw unreadable("lock", json);
|
|
394
|
+
}
|
|
395
|
+
return LockValue.of(json);
|
|
396
|
+
},
|
|
397
|
+
});
|
|
398
|
+
const ownedLock = (owner) => {
|
|
399
|
+
if (owner.kind === "optional") {
|
|
400
|
+
throw new Error("a free lock already has no owner, so the owner can't be optional");
|
|
401
|
+
}
|
|
402
|
+
return new Declaration({
|
|
403
|
+
kind: "owned lock",
|
|
404
|
+
source: `m.lock(${owner.source})`,
|
|
405
|
+
libraryKind: "owned lock",
|
|
406
|
+
count: owner.count + 1,
|
|
407
|
+
rest: { value: OwnedLockValue.of(null) },
|
|
408
|
+
values: () => [
|
|
409
|
+
OwnedLockValue.of(null),
|
|
410
|
+
...owner.values().map((each) => OwnedLockValue.of(each)),
|
|
411
|
+
],
|
|
412
|
+
has: (value) => value instanceof OwnedLockValue && (value.holder === null || owner.has(value.holder)),
|
|
413
|
+
adopt: same,
|
|
414
|
+
decode: (json) => OwnedLockValue.of(json === null ? null : owner.fromJson(json)),
|
|
415
|
+
});
|
|
416
|
+
};
|
|
417
|
+
export function lock(owner) {
|
|
418
|
+
return owner === undefined ? plainLock() : ownedLock(owner);
|
|
419
|
+
}
|
|
420
|
+
const staged = (kind, stages, at, has) => {
|
|
421
|
+
const values = stages.map(at);
|
|
422
|
+
return new Declaration({
|
|
423
|
+
kind,
|
|
424
|
+
source: `m.${kind}()`,
|
|
425
|
+
libraryKind: kind,
|
|
426
|
+
count: values.length,
|
|
427
|
+
rest: values[0] === undefined ? null : { value: values[0] },
|
|
428
|
+
values: () => values,
|
|
429
|
+
has,
|
|
430
|
+
adopt: same,
|
|
431
|
+
decode: (json) => {
|
|
432
|
+
const stage = stages.find((each) => each === json);
|
|
433
|
+
if (stage === undefined) {
|
|
434
|
+
throw unreadable(kind, json);
|
|
435
|
+
}
|
|
436
|
+
return at(stage);
|
|
437
|
+
},
|
|
438
|
+
});
|
|
439
|
+
};
|
|
440
|
+
/** Declares a timer that is idle, running or fired. Without `.initial` it starts idle. */
|
|
441
|
+
export const timer = () => staged("timer", TIMER_STAGES, TimerValue.at, (value) => value instanceof TimerValue);
|
|
442
|
+
/**
|
|
443
|
+
* Declares an async request that is idle, pending, succeeded or failed. Without `.initial` it
|
|
444
|
+
* starts idle.
|
|
445
|
+
*/
|
|
446
|
+
export const request = () => staged("request", REQUEST_STAGES, RequestValue.at, (value) => value instanceof RequestValue);
|
|
447
|
+
/**
|
|
448
|
+
* Declares a count of attempts from zero to `max`. Without `.initial` it starts at zero.
|
|
449
|
+
* Counting past `max` is not an error in the action: the checker reports the state as out of
|
|
450
|
+
* range, so guard `again()` with `canRetry`.
|
|
451
|
+
*/
|
|
452
|
+
export const retry = (limit) => {
|
|
453
|
+
const max = wholeLimit("m.retry's max", limit.max);
|
|
454
|
+
return new Declaration({
|
|
455
|
+
kind: "retry",
|
|
456
|
+
source: `m.retry({ max: ${max} })`,
|
|
457
|
+
libraryKind: "retry",
|
|
458
|
+
count: max + 1,
|
|
459
|
+
rest: { value: RetryValue.of(max, 0) },
|
|
460
|
+
values: () => Array.from({ length: max + 1 }, (_, attempts) => RetryValue.of(max, attempts)),
|
|
461
|
+
has: (value) => value instanceof RetryValue && value.max === max && value.attempts <= max,
|
|
462
|
+
adopt: same,
|
|
463
|
+
decode: (json) => {
|
|
464
|
+
if (typeof json !== "number") {
|
|
465
|
+
throw unreadable("retry", json);
|
|
466
|
+
}
|
|
467
|
+
return RetryValue.of(max, json);
|
|
468
|
+
},
|
|
469
|
+
});
|
|
470
|
+
};
|
|
471
|
+
const itemsOf = (over) => over instanceof Declaration ? over.values() : [...over];
|
|
472
|
+
/**
|
|
473
|
+
* Whether `test` is true for every value of a declaration, or for every member of a set,
|
|
474
|
+
* sequence or queue. Over a map it is given `[key, value]` pairs. True when there is nothing to
|
|
475
|
+
* test.
|
|
476
|
+
*/
|
|
477
|
+
export const forall = (over, test) => itemsOf(over).every((item) => test(item));
|
|
478
|
+
/**
|
|
479
|
+
* Whether `test` is true for some value of a declaration, or for some member of a set, sequence
|
|
480
|
+
* or queue. Over a map it is given `[key, value]` pairs. False when there is nothing to test.
|
|
481
|
+
*/
|
|
482
|
+
export const exists = (over, test) => itemsOf(over).some((item) => test(item));
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * as m from "./m.ts";
|
|
2
|
+
export { Declaration, MOST_VALUES_LISTED, combinationsOf } from "./declarations.ts";
|
|
3
|
+
export type { Fields, StateOf, ValueOf } from "./declarations.ts";
|
|
4
|
+
export { Model } from "./model.ts";
|
|
5
|
+
export type { Action, ActionOf, ActionRunner, ActionSpec, AnyModel, End, EndRunner, Eventually, EventuallyRunner, EventuallySpec, NoInput, Rule, RuleRunner, Runners, Setup, SetupRunner, Step as WitnessStep, View, ViewSpec, Witness, WitnessRunner, WitnessSpec, } from "./model.ts";
|
|
6
|
+
export { replay } from "./replay.ts";
|
|
7
|
+
export type { ReplayOptions, ReplayPath, ReplayStep, StateJson } from "./replay.ts";
|
|
8
|
+
export { DEFAULT_MAX_STATES, search } from "./breadth.ts";
|
|
9
|
+
export type { SearchFinding, SearchFindingKind, SearchInput, SearchMove, SearchResult, SearchRule, SearchRuleResult, Status, Trail, TrailStep, Verdict, } from "./breadth.ts";
|
|
10
|
+
export { explore } from "./search.ts";
|
|
11
|
+
export type { Change, Exploration, ExploreOptions, Exports, Finding, FindingKind, Named, Path, RuleResult, Step, } from "./search.ts";
|
|
12
|
+
export { LockValue, MapValue, ModelValue, OwnedLockValue, QueueValue, RequestValue, RetryValue, SeqValue, SetValue, TimerValue, compareText, } from "./values.ts";
|
|
13
|
+
export type { Json, RequestStage, Row, TimerStage } from "./values.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * as m from "./m.js";
|
|
2
|
+
export { Declaration, MOST_VALUES_LISTED, combinationsOf } from "./declarations.js";
|
|
3
|
+
export { Model } from "./model.js";
|
|
4
|
+
export { replay } from "./replay.js";
|
|
5
|
+
export { DEFAULT_MAX_STATES, search } from "./breadth.js";
|
|
6
|
+
export { explore } from "./search.js";
|
|
7
|
+
export { LockValue, MapValue, ModelValue, OwnedLockValue, QueueValue, RequestValue, RetryValue, SeqValue, SetValue, TimerValue, compareText, } from "./values.js";
|
package/dist/m.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Declaration, Fields, StateOf } from "./declarations.ts";
|
|
2
|
+
import type { Model } from "./model.ts";
|
|
3
|
+
export { booleanOf as boolean, enumOf as enum, exists, forall, int, lock, map, oneOf, queue, record, request, retry, seq, set, timer, } from "./declarations.ts";
|
|
4
|
+
export { action, end, eventually, model, rule, setup, step, view, witness } from "./model.ts";
|
|
5
|
+
export { div, mapOf, rem, seqOf, setOf, some } from "./values.ts";
|
|
6
|
+
/**
|
|
7
|
+
* The type a model's state has, or the type of a declaration's values:
|
|
8
|
+
* `type State = m.Infer<typeof Cart>` and `type Press = m.Infer<typeof Press>`.
|
|
9
|
+
*/
|
|
10
|
+
export type Infer<Declared> = Declared extends Model<infer F extends Fields> ? StateOf<F> : Declared extends Declaration<infer T, infer _In> ? T : never;
|
package/dist/m.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { booleanOf as boolean, enumOf as enum, exists, forall, int, lock, map, oneOf, queue, record, request, retry, seq, set, timer, } from "./declarations.js";
|
|
2
|
+
export { action, end, eventually, model, rule, setup, step, view, witness } from "./model.js";
|
|
3
|
+
export { div, mapOf, rem, seqOf, setOf, some } from "./values.js";
|