@moku-labs/game 0.0.1 → 0.0.2
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/README.md +424 -37
- package/dist/assets.d.mts +161 -0
- package/dist/assets.mjs +4729 -0
- package/dist/component-DGg5DqKK.mjs +44 -0
- package/dist/control.d.mts +162 -0
- package/dist/control.mjs +758 -0
- package/dist/define-sFoO3y6X.d.mts +263 -0
- package/dist/headless-CvamCUcR.mjs +186 -0
- package/dist/headless-KaSWcd0s.d.mts +181 -0
- package/dist/index.d.mts +1204 -50
- package/dist/index.mjs +20978 -2010
- package/dist/inspect.d.mts +109 -0
- package/dist/inspect.mjs +519 -0
- package/dist/jsx-dev-runtime.d.mts +2 -0
- package/dist/jsx-dev-runtime.mjs +2 -0
- package/dist/jsx-runtime.d.mts +2 -0
- package/dist/jsx-runtime.mjs +2 -0
- package/dist/memory-CvgdnsQO.mjs +259 -0
- package/dist/registry-DlpRCibU.mjs +384 -0
- package/dist/runtime-DRlwxkIv.mjs +182 -0
- package/dist/runtime-DiOTkDZz.d.mts +77 -0
- package/dist/session-DmAxY6Ll.mjs +145 -0
- package/dist/testing.d.mts +23 -111
- package/dist/testing.mjs +12 -289
- package/dist/types-BxkNNYul.d.mts +2840 -0
- package/dist/types-CTPS9GBu.d.mts +742 -0
- package/dist/types-DD-QrG_z.d.mts +588 -0
- package/dist/types-DWILGrPn.d.mts +622 -0
- package/dist/types-DYLnSgMI.d.mts +570 -0
- package/dist/types-JNc_UQBo.d.mts +2896 -0
- package/dist/types-yg_ywtT-.d.mts +1859 -0
- package/dist/visual-BDUHSRvf.mjs +734 -0
- package/package.json +26 -4
- package/dist/registry-DWV5C0Mf.mjs +0 -666
- package/dist/types-BfsmUzLC.d.mts +0 -1908
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { K as Snapshot, L as Json } from "./types-JNc_UQBo.mjs";
|
|
2
|
+
import { t as HeadlessApp } from "./headless-KaSWcd0s.mjs";
|
|
3
|
+
import { Log } from "@moku-labs/common/browser";
|
|
4
|
+
|
|
5
|
+
//#region src/plugins/flow/doors/types.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* The kinds an input field can take. `json` is any JSON value the command checks itself.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* // The schema of game.step: the frame count, and the frame length in milliseconds.
|
|
12
|
+
* const kinds: InputKind[] = ["number", "number"];
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
type InputKind = "string" | "number" | "boolean" | "json";
|
|
16
|
+
/**
|
|
17
|
+
* The input of a source or a command: field name to kind. A kind ending in `?` is optional, so
|
|
18
|
+
* "key or target" is two optional fields and the command checks that one is given.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* // game.restore takes exactly one of the two.
|
|
23
|
+
* const input: InputSchema = { bookmark: "json?", repro: "json?" };
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
type InputSchema = Readonly<Record<string, InputKind | `${InputKind}?`>>;
|
|
27
|
+
/** The TypeScript type of each input kind. */
|
|
28
|
+
type KindTypes = {
|
|
29
|
+
string: string;
|
|
30
|
+
number: number;
|
|
31
|
+
boolean: boolean;
|
|
32
|
+
json: Json;
|
|
33
|
+
};
|
|
34
|
+
/** The value type of one field kind, optional or not. */
|
|
35
|
+
type ValueOf<Kind> = Kind extends `${infer Base extends InputKind}?` ? KindTypes[Base] : Kind extends InputKind ? KindTypes[Kind] : never;
|
|
36
|
+
/** The fields of a schema whose kind has no `?`. */
|
|
37
|
+
type RequiredKeys<S extends InputSchema> = { [K in keyof S]: S[K] extends InputKind ? K : never }[keyof S];
|
|
38
|
+
/** Shows an intersection as one object type. */
|
|
39
|
+
type Flat<T> = { [K in keyof T]: T[K] };
|
|
40
|
+
/**
|
|
41
|
+
* The input value a schema describes. A `?` field may be left out or be `undefined`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* type Step = InputOf<{ frames: "number"; deltaMs: "number?" }>; // { frames: number; deltaMs?: number | undefined }
|
|
46
|
+
* const step: Step = { frames: 3 };
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
type InputOf<S extends InputSchema> = Flat<{ -readonly [K in RequiredKeys<S>]: ValueOf<S[K]> } & { -readonly [K in Exclude<keyof S, RequiredKeys<S>>]?: ValueOf<S[K]> | undefined }>;
|
|
50
|
+
/**
|
|
51
|
+
* The input argument of `read` and `run`: optional when every field of the schema is optional,
|
|
52
|
+
* required otherwise.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* type History = InputArguments<{ last: "number?" }>; // [input?: { last?: number | undefined }]
|
|
57
|
+
* type Rect = InputArguments<{ key: "string" }>; // [input: { key: string }]
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
type InputArguments<S extends InputSchema> = Partial<InputOf<S>> extends InputOf<S> ? [input?: InputOf<S>] : [input: InputOf<S>];
|
|
61
|
+
/**
|
|
62
|
+
* The input argument of `watch`: `undefined` is allowed when every field is optional.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* type Position = WatchInput<{}>; // {} | undefined
|
|
67
|
+
* type Rect = WatchInput<{ key: "string" }>; // { key: string }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
type WatchInput<S extends InputSchema> = Partial<InputOf<S>> extends InputOf<S> ? InputOf<S> | undefined : InputOf<S>;
|
|
71
|
+
/**
|
|
72
|
+
* When `watch` reads a source again: every frame, when the committed model changed, or when
|
|
73
|
+
* the graph moved (an edge, a gate, a mode).
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* // game.position moves only with the graph; game.render changes every frame.
|
|
78
|
+
* const changes: Changes = "edge";
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
type Changes = "frame" | "commit" | "edge";
|
|
82
|
+
/**
|
|
83
|
+
* What a command does to the game. `route` goes through the graph and keeps the session clean;
|
|
84
|
+
* `cheat` and `raw` taint it and are journaled.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* // game.restore replaces the whole state: the session is not a played one any more.
|
|
89
|
+
* const effect: Effect = "raw";
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
type Effect = "read" | "route" | "cosmetic" | "cheat" | "raw";
|
|
93
|
+
/**
|
|
94
|
+
* A read-only view on a running game: an id, a title, the input it takes, when it changes and
|
|
95
|
+
* how to read it. `App` is what the reader needs: the headless app by default, a screen source
|
|
96
|
+
* asks for the screen plugins.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* // A game's .dev module: the coins a panel shows, re-read on every commit.
|
|
101
|
+
* const coins = defineSource({
|
|
102
|
+
* id: "timber.coins",
|
|
103
|
+
* title: "Coins",
|
|
104
|
+
* input: {},
|
|
105
|
+
* changes: "commit",
|
|
106
|
+
* read: (app: HeadlessApp & { model: ModelApi }) => app.model.store.snapshot().player
|
|
107
|
+
* });
|
|
108
|
+
* read(app, coins); // { coins: 120, ... }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
type Source<S extends InputSchema, O, App = HeadlessApp> = {
|
|
112
|
+
readonly id: string;
|
|
113
|
+
readonly title: string;
|
|
114
|
+
readonly input: S;
|
|
115
|
+
readonly changes: Changes;
|
|
116
|
+
readonly read: (app: App, input: InputOf<S>) => O;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* A dev-only action on a running game: an id, a title, the input it takes, its effect and how to
|
|
120
|
+
* run it. The body starts with the inline dev guard, so a production build drops it.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* // A game's .dev module: jump to a level through the graph, the session stays clean.
|
|
125
|
+
* const jumpToLevel = defineCommand({
|
|
126
|
+
* id: "timber.jumpToLevel",
|
|
127
|
+
* title: "Go to level",
|
|
128
|
+
* input: { level: "number" },
|
|
129
|
+
* effect: "route",
|
|
130
|
+
* run: (app, { level }) => {
|
|
131
|
+
* if (typeof __MOKU_GAME_DEV__ === "undefined" || !__MOKU_GAME_DEV__) throw controlRefused();
|
|
132
|
+
* return app.flow.walk([{ at: "home", intent: "play", payload: { level } }]);
|
|
133
|
+
* }
|
|
134
|
+
* });
|
|
135
|
+
* (await run(app, jumpToLevel, { level: 3 })).state.path; // "board/awaitIntent"
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
type Command<S extends InputSchema, O, App = HeadlessApp> = {
|
|
139
|
+
readonly id: string;
|
|
140
|
+
readonly title: string;
|
|
141
|
+
readonly input: S;
|
|
142
|
+
readonly effect: Effect;
|
|
143
|
+
readonly run: (app: App, input: InputOf<S>) => O | Promise<O>;
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Where the game stands after a command: the graph path, the frame and whether the session
|
|
147
|
+
* was tainted by a cheat or a raw write.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* const envelope: Envelope = { path: "board/awaitIntent", frame: 1840, tainted: false };
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
type Envelope = {
|
|
155
|
+
readonly path: string;
|
|
156
|
+
readonly frame: number;
|
|
157
|
+
readonly tainted: boolean;
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* What `run` resolves with: the command's value and the envelope read after it.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* // A tap on Play while the home screen rests.
|
|
165
|
+
* const ran: Ran<boolean> = { value: true, state: { path: "board/awaitIntent", frame: 312, tainted: false } };
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
type Ran<O> = {
|
|
169
|
+
readonly value: O;
|
|
170
|
+
readonly state: Envelope;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* One journaled cheat or raw command: its id, the input it got and the frame it ran on.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* const entry: CheatEntry = { id: "game.restore", input: { bookmark: { path: "home" } }, frame: 96 };
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
type CheatEntry = {
|
|
181
|
+
readonly id: string;
|
|
182
|
+
readonly input: Readonly<Record<string, Json | undefined>>;
|
|
183
|
+
readonly frame: number;
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* What `watch` needs of an app: the frame loop, the graph and the committed model it compares.
|
|
187
|
+
* Every app with the default plugins fits.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* const app: WatchApp = createApp({ pluginConfigs: { flow: { mainFlow } } });
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
type WatchApp = HeadlessApp & {
|
|
195
|
+
readonly model: {
|
|
196
|
+
readonly store: {
|
|
197
|
+
snapshot(): Snapshot;
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* What the base commands need of an app: the headless app plus the log, where every command
|
|
203
|
+
* leaves a `moku:dev` entry. Every app with the default plugins fits.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* const app: ControlApp = createApp({ pluginConfigs: { flow: { mainFlow } } });
|
|
208
|
+
* app.log.trace().at(-1)?.event; // "moku:dev" after a command ran
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
type ControlApp = HeadlessApp & {
|
|
212
|
+
readonly log: Log.LogApi;
|
|
213
|
+
};
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region src/plugins/flow/doors/define.d.ts
|
|
216
|
+
/**
|
|
217
|
+
* Declares a source: a read-only view the editor lists, reads and watches. The types flow from
|
|
218
|
+
* the object: the input from `input`, the app from the `read` parameter, the output from its
|
|
219
|
+
* result.
|
|
220
|
+
*
|
|
221
|
+
* @param source - The descriptor.
|
|
222
|
+
* @returns The same descriptor, frozen.
|
|
223
|
+
* @throws {Error} When the id is not a dotted name such as `"game.position"`.
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* // A game's .dev module: the orders on the board, re-read after every edge.
|
|
227
|
+
* export const orders = defineSource({
|
|
228
|
+
* id: "timber.orders",
|
|
229
|
+
* title: "Orders",
|
|
230
|
+
* input: {},
|
|
231
|
+
* changes: "edge",
|
|
232
|
+
* read: app => app.flow.state().path
|
|
233
|
+
* });
|
|
234
|
+
* read(app, orders); // "board/awaitIntent"
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
declare function defineSource<S extends InputSchema, O, App = HeadlessApp>(source: Source<S, O, App>): Source<S, O, App>;
|
|
238
|
+
/**
|
|
239
|
+
* Declares a command: a dev-only action the editor lists and runs. Its body starts with the
|
|
240
|
+
* inline dev guard, so a production build drops it.
|
|
241
|
+
*
|
|
242
|
+
* @param command - The descriptor.
|
|
243
|
+
* @returns The same descriptor, frozen.
|
|
244
|
+
* @throws {Error} When the id is not a dotted name such as `"game.answer"`.
|
|
245
|
+
* @example
|
|
246
|
+
* ```ts
|
|
247
|
+
* // A game's .dev module: open the shop from anywhere, through the graph.
|
|
248
|
+
* export const openShop = defineCommand({
|
|
249
|
+
* id: "timber.openShop",
|
|
250
|
+
* title: "Open the shop",
|
|
251
|
+
* input: {},
|
|
252
|
+
* effect: "route",
|
|
253
|
+
* run: app => {
|
|
254
|
+
* if (typeof __MOKU_GAME_DEV__ === "undefined" || !__MOKU_GAME_DEV__) throw controlRefused();
|
|
255
|
+
* return app.flow.walk([{ at: "home", intent: "shop" }]);
|
|
256
|
+
* }
|
|
257
|
+
* });
|
|
258
|
+
* (await run(app, openShop)).state.path; // "shop"
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
declare function defineCommand<S extends InputSchema, O, App = HeadlessApp>(command: Command<S, O, App>): Command<S, O, App>;
|
|
262
|
+
//#endregion
|
|
263
|
+
export { ControlApp as a, InputSchema as c, WatchApp as d, WatchInput as f, Command as i, Ran as l, defineSource as n, InputArguments as o, CheatEntry as r, InputOf as s, defineCommand as t, Source as u };
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { a as graphHash } from "./registry-DlpRCibU.mjs";
|
|
2
|
+
//#region src/plugins/flow/headless.ts
|
|
3
|
+
const noPayload = null;
|
|
4
|
+
/** Seed of a repro that pins no rng state, the seed `saveOf` writes. */
|
|
5
|
+
const defaultSeed = 1;
|
|
6
|
+
/**
|
|
7
|
+
* Turns whatever `run()` rejected with into an error that can be thrown again.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The rejection value.
|
|
10
|
+
* @returns The value itself when it is an error, a wrapped one otherwise.
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* asError("offline").message; // "[game] The headless run failed.\n offline."
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
function asError(value) {
|
|
17
|
+
if (value instanceof Error) return value;
|
|
18
|
+
return /* @__PURE__ */ new Error(`[game] The headless run failed.\n ${String(value)}.`);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Starts the one loop unless the app's own `onStart` already did, and keeps a fatal error until
|
|
22
|
+
* someone asks for it. `run()` rejects only on a fatal error, and nobody awaits it here.
|
|
23
|
+
*
|
|
24
|
+
* @param app - The started app.
|
|
25
|
+
* @returns The holder of the fatal error.
|
|
26
|
+
*/
|
|
27
|
+
function startLoop(app) {
|
|
28
|
+
const fatal = { error: void 0 };
|
|
29
|
+
if (app.flow.state().running) return fatal;
|
|
30
|
+
app.flow.run().catch((error) => {
|
|
31
|
+
fatal.error = error;
|
|
32
|
+
});
|
|
33
|
+
return fatal;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Waits until the loop rests for the first time, or ends. An empty route walks nowhere: it only
|
|
37
|
+
* waits for the gate the loop opens at the node it enters, so a start that is a chain of transit
|
|
38
|
+
* nodes is played out before anything reads the game.
|
|
39
|
+
*
|
|
40
|
+
* @param app - The started app whose loop is running.
|
|
41
|
+
* @param fatal - Holder of the fatal error of `run()`.
|
|
42
|
+
* @returns Resolves once the loop rests at its first rest node.
|
|
43
|
+
* @throws {Error} When the loop failed fatally instead of reaching a rest node.
|
|
44
|
+
*/
|
|
45
|
+
async function settleAtFirstRest(app, fatal) {
|
|
46
|
+
await app.flow.walk([]).catch((error) => {
|
|
47
|
+
throw asError(fatal.error ?? error);
|
|
48
|
+
});
|
|
49
|
+
if (fatal.error !== void 0) throw asError(fatal.error);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Starts an app headless: sets flow mode `"fast"`, awaits `app.start()`, starts `flow.run()`
|
|
53
|
+
* unless the app's own `onStart` already did, and waits until the loop rests for the first time.
|
|
54
|
+
* `game.state().path` therefore names the first rest node of the graph as soon as the call
|
|
55
|
+
* resolves. A fatal error of the loop rejects this call, and a later one is re-thrown by `walk`
|
|
56
|
+
* and by `stop`, so a headless test never loses it.
|
|
57
|
+
*
|
|
58
|
+
* @param app - An app that is not started yet.
|
|
59
|
+
* @returns The game: walk it, answer it, read it, stop it.
|
|
60
|
+
* @throws {Error} When the loop failed fatally before it reached its first rest node.
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* // A test starts the merge game without a screen. The call resolves at the first rest node.
|
|
64
|
+
* const game = await createHeadless(app);
|
|
65
|
+
*
|
|
66
|
+
* game.state().path; // "home": the transit node "boot" was played out
|
|
67
|
+
* game.state().mode; // "fast"
|
|
68
|
+
* await game.stop();
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
async function createHeadless(app) {
|
|
72
|
+
app.flow.setMode("fast");
|
|
73
|
+
await app.start();
|
|
74
|
+
const fatal = startLoop(app);
|
|
75
|
+
await settleAtFirstRest(app, fatal);
|
|
76
|
+
return {
|
|
77
|
+
walk: async (route) => {
|
|
78
|
+
const state = await app.flow.walk(route).catch((error) => {
|
|
79
|
+
throw asError(fatal.error ?? error);
|
|
80
|
+
});
|
|
81
|
+
if (fatal.error !== void 0) throw asError(fatal.error);
|
|
82
|
+
return state;
|
|
83
|
+
},
|
|
84
|
+
answer: (answer) => app.flow.gate.answer(answer),
|
|
85
|
+
state: () => app.flow.state(),
|
|
86
|
+
history: () => app.flow.history(),
|
|
87
|
+
stop: async () => {
|
|
88
|
+
await app.stop();
|
|
89
|
+
if (fatal.error !== void 0) throw asError(fatal.error);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Reads the rest node a repro starts at: its checkpoint, or the start of the main flow.
|
|
95
|
+
*
|
|
96
|
+
* @param app - The started app.
|
|
97
|
+
* @param repro - Starting state, optional checkpoint and the route.
|
|
98
|
+
* @returns The path of the node the repro enters.
|
|
99
|
+
* @throws {Error} When the graph has no main flow to start from.
|
|
100
|
+
*/
|
|
101
|
+
function reproPath(app, repro) {
|
|
102
|
+
if (repro.checkpoint !== void 0) return repro.checkpoint;
|
|
103
|
+
const graph = app.flow.describe();
|
|
104
|
+
const start = graph.flows[graph.main]?.start;
|
|
105
|
+
if (start === void 0) throw new Error(`[game] runRepro() found no flow "${graph.main}" to start from.\n Name the checkpoint the repro was taken at.`);
|
|
106
|
+
return start;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Builds the bookmark a repro is entered with: its state, its checkpoint and the hash of the
|
|
110
|
+
* graph that runs now, so a checkpoint is accepted and any other rest node is checked. The route
|
|
111
|
+
* is not part of it: the caller walks it after the restore.
|
|
112
|
+
*
|
|
113
|
+
* @param app - The started app.
|
|
114
|
+
* @param repro - Starting state, optional checkpoint and the route.
|
|
115
|
+
* @returns The bookmark to restore.
|
|
116
|
+
* @throws {Error} When the repro names no checkpoint and the graph has no main flow.
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* // The game.restore command loads a bug report taken at the "home" checkpoint.
|
|
120
|
+
* const bookmark = reproBookmark(app, { player: { coins: 7 }, checkpoint: "home", route: [] });
|
|
121
|
+
* bookmark.path; // "home"
|
|
122
|
+
* bookmark.rng; // { seed: 1, streams: {} }: the repro pinned no rng
|
|
123
|
+
* await app.flow.restore(bookmark);
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
function reproBookmark(app, repro) {
|
|
127
|
+
return {
|
|
128
|
+
path: reproPath(app, repro),
|
|
129
|
+
input: noPayload,
|
|
130
|
+
player: repro.player,
|
|
131
|
+
session: repro.session ?? {},
|
|
132
|
+
rng: repro.rng ?? {
|
|
133
|
+
seed: defaultSeed,
|
|
134
|
+
streams: {}
|
|
135
|
+
},
|
|
136
|
+
graph: graphHash(app.flow.describe())
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Plays a repro: starts the app headless, restores the bookmark built from its state and
|
|
141
|
+
* checkpoint, then walks its route. The app keeps running, so the caller can read more than the
|
|
142
|
+
* result and stops it itself.
|
|
143
|
+
*
|
|
144
|
+
* @param app - An app that is not started yet.
|
|
145
|
+
* @param repro - Starting state, optional checkpoint and the route.
|
|
146
|
+
* @returns The path the run ended at, and the committed state.
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* // A bug report on the dice game: "reset keeps my coins". Replay it from the "home" checkpoint.
|
|
150
|
+
* const reset = { at: "home", intent: "reset" };
|
|
151
|
+
* const result = await runRepro(app, { player: { coins: 7 }, checkpoint: "home", route: [reset] });
|
|
152
|
+
*
|
|
153
|
+
* result.player; // { coins: 0 }; result.path is ["home"]
|
|
154
|
+
* await app.stop(); // the app keeps running after the run
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
async function runRepro(app, repro) {
|
|
158
|
+
const game = await createHeadless(app);
|
|
159
|
+
await app.flow.restore(reproBookmark(app, repro));
|
|
160
|
+
const state = await game.walk(repro.route);
|
|
161
|
+
const ended = app.flow.bookmark();
|
|
162
|
+
return {
|
|
163
|
+
path: state.stack.map((frame) => frame.node),
|
|
164
|
+
player: ended.player,
|
|
165
|
+
session: ended.session
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Steps the frame loop by hand: `count` calls of `app.time.step(deltaMs)`. A headless game has no
|
|
170
|
+
* frame source, so this is the only thing that moves the frame phases.
|
|
171
|
+
*
|
|
172
|
+
* @param app - A started app.
|
|
173
|
+
* @param count - Number of frames.
|
|
174
|
+
* @param deltaMs - Milliseconds per frame.
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* // A headless game has no frame source: the test moves the six frame phases by hand.
|
|
178
|
+
* stepFrames(app, 2, 16);
|
|
179
|
+
* app.time.snapshot(); // frame: 2, delta: 16, elapsed: 32
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
function stepFrames(app, count, deltaMs) {
|
|
183
|
+
for (let frame = 0; frame < count; frame += 1) app.time.step(deltaMs);
|
|
184
|
+
}
|
|
185
|
+
//#endregion
|
|
186
|
+
export { stepFrames as i, reproBookmark as n, runRepro as r, createHeadless as t };
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { L as Json, O as Answer, S as RouteStep, Y as RngState, b as JournalEntry, k as Api, t as Api$1, y as FlowState } from "./types-JNc_UQBo.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/plugins/flow/headless.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The part of an app the headless helpers use. Structural on purpose: any app created with the
|
|
6
|
+
* default plugins fits.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const app: HeadlessApp = createApp({ pluginConfigs: { flow: { mainFlow } } });
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
type HeadlessApp = {
|
|
14
|
+
start(): Promise<void>;
|
|
15
|
+
stop(): Promise<void>;
|
|
16
|
+
time: Api;
|
|
17
|
+
flow: Api$1;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A game played without a screen: answers go through the gate, effects resolve instantly.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* // A test of the dice game: two rolls, no screen, no waiting.
|
|
25
|
+
* const game = await createHeadless(app);
|
|
26
|
+
* const state = await game.walk([{ at: "home", intent: "roll" }, { at: "home", intent: "roll" }]);
|
|
27
|
+
*
|
|
28
|
+
* state.path; // "home"
|
|
29
|
+
* await game.stop();
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
type HeadlessGame = {
|
|
33
|
+
/**
|
|
34
|
+
* Walks a route through the running loop.
|
|
35
|
+
*
|
|
36
|
+
* @param route - The player's answers and substituted sub-flow results, in order.
|
|
37
|
+
* @returns The state the walk ended in.
|
|
38
|
+
* @throws {Error} When a step is never reached, or the loop failed fatally.
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* // Open the board and tap the generator twice. Every node's logic runs for real.
|
|
42
|
+
* const tap = { at: "board/awaitIntent", intent: "tap", payload: { generatorId: "sawmill" } };
|
|
43
|
+
* const state = await game.walk([{ at: "home", intent: "play" }, tap, tap]);
|
|
44
|
+
*
|
|
45
|
+
* state.path; // "board/awaitIntent"
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
walk(route: readonly RouteStep[]): Promise<FlowState>;
|
|
49
|
+
/**
|
|
50
|
+
* Gives one answer to the gate, the way a tap does.
|
|
51
|
+
*
|
|
52
|
+
* @param answer - Intent and optional payload.
|
|
53
|
+
* @returns Whether the gate took it.
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* // One tap without a route. The test then waits for the loop by itself.
|
|
57
|
+
* game.answer({ intent: "play" }); // true: "home" rests and lists "play"
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
answer(answer: Answer): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Reads where the graph stands.
|
|
63
|
+
*
|
|
64
|
+
* @returns Whether it runs, the path, the stack, what it waits for and the mode.
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* // Right after createHeadless: the graph rests at its first rest node.
|
|
68
|
+
* game.state().path; // "home"
|
|
69
|
+
* game.state().pending; // { gate: ["play"] }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
state(): FlowState;
|
|
73
|
+
/**
|
|
74
|
+
* Reads the edges taken since the last checkpoint.
|
|
75
|
+
*
|
|
76
|
+
* @returns A copy of the journal.
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* // After a merge onto an empty cell: the last edge says why the move was refused.
|
|
80
|
+
* const last = game.history().at(-1);
|
|
81
|
+
* // last?.path: "board/merge", last?.outcome: "rejected", last?.payload: { reason: "empty" }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
history(): readonly JournalEntry[];
|
|
85
|
+
/**
|
|
86
|
+
* Stops the app, then re-throws a fatal error of the loop.
|
|
87
|
+
*
|
|
88
|
+
* @throws {Error} When the loop failed fatally.
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* // The last line of a headless test. A fatal error of the loop fails the test here.
|
|
92
|
+
* await game.stop();
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
stop(): Promise<void>;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* A reproducible run: a starting state, an optional checkpoint and the route played from it.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* const repro: Repro = { player: { coins: 0 }, checkpoint: "home", route: [{ at: "home", intent: "play" }] };
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
type Repro = {
|
|
106
|
+
player: Json;
|
|
107
|
+
session?: Json;
|
|
108
|
+
rng?: RngState;
|
|
109
|
+
checkpoint?: string;
|
|
110
|
+
route: RouteStep[];
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* What a repro run ends with: the path where the loop rests and the committed state.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* const result: ReproResult = { path: ["home"], player: { coins: 3 }, session: { visits: 1 } };
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
type ReproResult = {
|
|
121
|
+
path: string[];
|
|
122
|
+
player: Json;
|
|
123
|
+
session: Json;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Starts an app headless: sets flow mode `"fast"`, awaits `app.start()`, starts `flow.run()`
|
|
127
|
+
* unless the app's own `onStart` already did, and waits until the loop rests for the first time.
|
|
128
|
+
* `game.state().path` therefore names the first rest node of the graph as soon as the call
|
|
129
|
+
* resolves. A fatal error of the loop rejects this call, and a later one is re-thrown by `walk`
|
|
130
|
+
* and by `stop`, so a headless test never loses it.
|
|
131
|
+
*
|
|
132
|
+
* @param app - An app that is not started yet.
|
|
133
|
+
* @returns The game: walk it, answer it, read it, stop it.
|
|
134
|
+
* @throws {Error} When the loop failed fatally before it reached its first rest node.
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* // A test starts the merge game without a screen. The call resolves at the first rest node.
|
|
138
|
+
* const game = await createHeadless(app);
|
|
139
|
+
*
|
|
140
|
+
* game.state().path; // "home": the transit node "boot" was played out
|
|
141
|
+
* game.state().mode; // "fast"
|
|
142
|
+
* await game.stop();
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
declare function createHeadless(app: HeadlessApp): Promise<HeadlessGame>;
|
|
146
|
+
/**
|
|
147
|
+
* Plays a repro: starts the app headless, restores the bookmark built from its state and
|
|
148
|
+
* checkpoint, then walks its route. The app keeps running, so the caller can read more than the
|
|
149
|
+
* result and stops it itself.
|
|
150
|
+
*
|
|
151
|
+
* @param app - An app that is not started yet.
|
|
152
|
+
* @param repro - Starting state, optional checkpoint and the route.
|
|
153
|
+
* @returns The path the run ended at, and the committed state.
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* // A bug report on the dice game: "reset keeps my coins". Replay it from the "home" checkpoint.
|
|
157
|
+
* const reset = { at: "home", intent: "reset" };
|
|
158
|
+
* const result = await runRepro(app, { player: { coins: 7 }, checkpoint: "home", route: [reset] });
|
|
159
|
+
*
|
|
160
|
+
* result.player; // { coins: 0 }; result.path is ["home"]
|
|
161
|
+
* await app.stop(); // the app keeps running after the run
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
declare function runRepro(app: HeadlessApp, repro: Repro): Promise<ReproResult>;
|
|
165
|
+
/**
|
|
166
|
+
* Steps the frame loop by hand: `count` calls of `app.time.step(deltaMs)`. A headless game has no
|
|
167
|
+
* frame source, so this is the only thing that moves the frame phases.
|
|
168
|
+
*
|
|
169
|
+
* @param app - A started app.
|
|
170
|
+
* @param count - Number of frames.
|
|
171
|
+
* @param deltaMs - Milliseconds per frame.
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* // A headless game has no frame source: the test moves the six frame phases by hand.
|
|
175
|
+
* stepFrames(app, 2, 16);
|
|
176
|
+
* app.time.snapshot(); // frame: 2, delta: 16, elapsed: 32
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
declare function stepFrames(app: HeadlessApp, count: number, deltaMs: number): void;
|
|
180
|
+
//#endregion
|
|
181
|
+
export { createHeadless as a, ReproResult as i, HeadlessGame as n, runRepro as o, Repro as r, stepFrames as s, HeadlessApp as t };
|