@moku-labs/game 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +5 -4
  2. package/dist/assets.d.mts +1 -1
  3. package/dist/{capture-fpd83Mi5.mjs → capture-BX5Gfoty.mjs} +1 -384
  4. package/dist/{commands-mzE9PX0H.d.mts → commands-C1kIfOG5.d.mts} +6 -4
  5. package/dist/control.d.mts +4 -3
  6. package/dist/control.mjs +2 -2
  7. package/dist/{define-BbgXv4cy.d.mts → define-BPpZcpxW.d.mts} +4 -3
  8. package/dist/headless-CvamCUcR.mjs +186 -0
  9. package/dist/headless-q98SwADR.d.mts +181 -0
  10. package/dist/index.d.mts +23 -22
  11. package/dist/index.mjs +17 -6
  12. package/dist/{input-CK4nxBO1.mjs → input-z_35Jwfn.mjs} +2 -2
  13. package/dist/inspect.d.mts +8 -6
  14. package/dist/inspect.mjs +2 -2
  15. package/dist/jsx-dev-runtime.d.mts +1 -1
  16. package/dist/jsx-runtime.d.mts +1 -1
  17. package/dist/project.d.mts +28 -28
  18. package/dist/project.mjs +7 -7
  19. package/dist/registry-DlpRCibU.mjs +384 -0
  20. package/dist/{run-Bcyg32M5.mjs → run-CUPSFM85.mjs} +4 -187
  21. package/dist/{runtime-HTlmRb81.d.mts → runtime-CvDZZg1S.d.mts} +1 -1
  22. package/dist/{sources-DVWJ8nfr.mjs → sources-BBUd15vQ.mjs} +1 -1
  23. package/dist/testing.d.mts +3 -328
  24. package/dist/testing.mjs +2 -2142
  25. package/dist/{types-BMEn0tSC.d.mts → types-BWzCSRM2.d.mts} +12 -2974
  26. package/dist/{types-CYYKjvLn.d.mts → types-CDSKa0vl.d.mts} +5 -182
  27. package/dist/{types-ClF8say0.d.mts → types-DF5kkW-C.d.mts} +5 -4
  28. package/dist/{types-BaUUcIaN.d.mts → types-DgdMLZnV.d.mts} +8 -7
  29. package/dist/{types-CXyipKhE.d.mts → types-DlgsfOmW.d.mts} +10 -9
  30. package/dist/types-ERb2Y9U5.d.mts +2967 -0
  31. package/dist/visual.d.mts +331 -0
  32. package/dist/visual.mjs +2142 -0
  33. package/llms.txt +6 -5
  34. package/package.json +7 -8
@@ -0,0 +1,331 @@
1
+ import { F as Api, Z as Api$1 } from "./types-ERb2Y9U5.mjs";
2
+ import { k as Api$3, m as Api$2 } from "./types-BWzCSRM2.mjs";
3
+ import { r as Repro } from "./headless-q98SwADR.mjs";
4
+ import { a as InputOf, r as ControlApp } from "./types-CDSKa0vl.mjs";
5
+ import { H as InputApi } from "./types-DgdMLZnV.mjs";
6
+ import { b as AnimApi, v as UiApi } from "./types-DlgsfOmW.mjs";
7
+ import { t as commands } from "./commands-C1kIfOG5.mjs";
8
+
9
+ //#region src/visual/types.d.ts
10
+ /** The base commands of the `/control` door, keyed by their short names. */
11
+ type Catalogue = typeof commands;
12
+ /**
13
+ * The commands a step may name: the catalogue of `/control` minus `bookmark` (it only reads),
14
+ * `capture` (the runner captures by itself) and `debug` (debug drawing would enter a baseline). A
15
+ * command the catalogue gains is a step with no change to the runner.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * const name: StepCommand = "tap"; // also "answer", "walk", "step", "reducedMotion", ...
20
+ * ```
21
+ */
22
+ type StepCommand = Exclude<keyof Catalogue, "bookmark" | "capture" | "debug">;
23
+ /**
24
+ * Where a visual test starts: what `game.restore` loads, a repro without its route. The player
25
+ * is required; the session, the rng and the checkpoint are optional, as in a repro.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * // A save with a plank ready for the first order, entered at Home.
30
+ * const start: VisualStart = { player: { coins: 40, orders: [] }, checkpoint: "home" };
31
+ * ```
32
+ */
33
+ type VisualStart = Omit<Repro, "route">;
34
+ /**
35
+ * One step of a visual test: a `/control` command by its short name with that command's input,
36
+ * typed from the catalogue, or a checkpoint by its name.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * // Tap Play, answer the order on the board, then compare the screen with its baseline.
41
+ * const steps: VisualStep[] = [
42
+ * { tap: { key: "play" } },
43
+ * { answer: { intent: "deliver", payload: { orderId: "o1" } } },
44
+ * { checkpoint: "open" }
45
+ * ];
46
+ * ```
47
+ */
48
+ type VisualStep = {
49
+ checkpoint: string;
50
+ } | { [K in StepCommand]: { [P in K]: InputOf<Catalogue[K]["input"]> } }[StepCommand];
51
+ /**
52
+ * A visual test as `defineVisualTest` answers it: its name, where it starts, its steps, and
53
+ * `webgl`, true when it also runs in the WebGL leg. Frozen data; nothing runs at definition.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * const test: VisualTest = defineVisualTest("reward-popup", {
58
+ * start: { player: { coins: 40 }, checkpoint: "home" },
59
+ * steps: [{ tap: { key: "play" } }, { checkpoint: "board" }]
60
+ * });
61
+ * test.name; // "reward-popup"
62
+ * test.webgl; // false
63
+ * Object.isFrozen(test.steps); // true
64
+ * ```
65
+ */
66
+ type VisualTest = {
67
+ readonly name: string;
68
+ readonly start: Readonly<VisualStart>;
69
+ readonly steps: readonly VisualStep[];
70
+ readonly webgl: boolean;
71
+ };
72
+ /**
73
+ * What the runner needs of an app: the control app, the plugins the base commands act on and
74
+ * the plugins the readers read. Every app composed with the screen set fits.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * const app: VisualApp = createApp({ plugins: [...screen, boardFeature], pluginConfigs });
79
+ * ```
80
+ */
81
+ type VisualApp = ControlApp & {
82
+ readonly anim: AnimApi;
83
+ readonly ui: UiApi;
84
+ readonly renderer: Api$2;
85
+ readonly world: Api$3;
86
+ readonly model: Api;
87
+ readonly input: InputApi;
88
+ readonly lifecycle: Api$1;
89
+ };
90
+ /**
91
+ * The dev page the browser leg opens: its URL, the viewport width in CSS px (390 by default), the
92
+ * device scale (2 by default) and which Chrome to launch. The page height follows the inert frame
93
+ * of the renderer: 520 for a 390 wide portrait game at the default 4:3. Without `browser` the leg
94
+ * launches Playwright's own Chromium, and the system Chrome when that one is not installed.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * // The fixture's dev server, a 390 px wide phone at 2x, the system Chrome.
99
+ * const page: VisualPage = { url: "http://localhost:3000/", browser: { channel: "chrome" } };
100
+ * ```
101
+ */
102
+ type VisualPage = {
103
+ url: string;
104
+ width?: number;
105
+ deviceScaleFactor?: number;
106
+ browser?: {
107
+ channel?: string;
108
+ executablePath?: string;
109
+ };
110
+ };
111
+ /**
112
+ * How the runner builds a game: a fresh app per test, not started, with the screen set
113
+ * composed; the renderer stays inert in Bun. `page` is the dev page of the pixel leg.
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * // tests/visual/run.ts of a game.
118
+ * const setup: VisualSetup = { app: () => createScreenGame().app, page: { url: "http://localhost:3000/" } };
119
+ * ```
120
+ */
121
+ type VisualSetup = {
122
+ app: () => VisualApp;
123
+ page?: VisualPage;
124
+ };
125
+ /**
126
+ * How far a pixel may differ: `threshold` is the largest channel delta that still counts as the
127
+ * same pixel, `ratio` the share of differing pixels a checkpoint tolerates.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * const tolerance: VisualTolerance = { ratio: 0.001, threshold: 24 }; // the defaults
132
+ * ```
133
+ */
134
+ type VisualTolerance = {
135
+ ratio: number;
136
+ threshold: number;
137
+ };
138
+ /**
139
+ * The backend the browser leg draws with: `"webgpu"`, or `"webgl"` for the WebGL leg.
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * const renderer: VisualRenderer = "webgl"; // what --webgl asks for
144
+ * ```
145
+ */
146
+ type VisualRenderer = "webgpu" | "webgl";
147
+ /**
148
+ * The options of a run. Each one left out comes from the command line (`argv`), then from its
149
+ * default: `dir` `"tests/visual"`, `update` false, `pixels` true with a `page` on a Mac,
150
+ * `renderer` `"webgpu"`, `settleFrames` 600, `tolerance` `{ ratio: 0.001, threshold: 24 }`,
151
+ * `argv` `process.argv.slice(2)`. An option given here wins over a flag. `renderer: "webgl"`
152
+ * runs only the tests with `webgl: true`, on the page with `?renderer=webgl`, and compares their
153
+ * pictures with `screen.webgl.webp`.
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * // The same as `bun tests/visual/run.ts --only reward-popup --no-pixels`.
158
+ * const options: VisualOptions = { only: ["reward-popup"], pixels: false };
159
+ * // The same as `bun tests/visual/run.ts --webgl`.
160
+ * const webgl: VisualOptions = { renderer: "webgl" };
161
+ * ```
162
+ */
163
+ type VisualOptions = {
164
+ dir?: string;
165
+ update?: boolean;
166
+ pixels?: boolean;
167
+ renderer?: VisualRenderer;
168
+ only?: readonly string[];
169
+ settleFrames?: number;
170
+ tolerance?: VisualTolerance;
171
+ argv?: readonly string[];
172
+ };
173
+ /**
174
+ * What a comparison with a baseline file found: the same content, a different one, or no
175
+ * baseline (or `--update`), so the file was written.
176
+ *
177
+ * @example
178
+ * ```ts
179
+ * const outcome: Outcome = "written"; // the first run of a new checkpoint
180
+ * ```
181
+ */
182
+ type Outcome = "same" | "different" | "written";
183
+ /**
184
+ * The outcome of one checkpoint. `first` is the JSON path of the first difference, the state's
185
+ * before the describe's. `pixels` is `"skipped"` when the pixel leg did not run; `pixelRatio`
186
+ * and `verdict` come with a pixel difference.
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * // The player's coins differ from the baseline; the layout is the same.
191
+ * const result: CheckpointResult = {
192
+ * name: "claimed", state: "different", describe: "same", pixels: "skipped", first: "player.coins"
193
+ * };
194
+ * ```
195
+ */
196
+ type CheckpointResult = {
197
+ name: string;
198
+ state: Outcome;
199
+ describe: Outcome;
200
+ pixels: Outcome | "skipped";
201
+ pixelRatio?: number;
202
+ first?: string;
203
+ verdict?: "rendering" | "behaviour";
204
+ };
205
+ /**
206
+ * The outcome of one visual test: every checkpoint it reached, and the error that ended it early.
207
+ *
208
+ * @example
209
+ * ```ts
210
+ * const result: VisualTestResult = {
211
+ * name: "reward-popup",
212
+ * checkpoints: [{ name: "open", state: "same", describe: "same", pixels: "skipped" }]
213
+ * };
214
+ * ```
215
+ */
216
+ type VisualTestResult = {
217
+ name: string;
218
+ checkpoints: readonly CheckpointResult[];
219
+ error?: string;
220
+ };
221
+ /**
222
+ * What `runVisualTests` answers: `ok` is true when no test failed and no outcome is
223
+ * `"different"`.
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * const report: VisualReport = await runVisualTests(setup, [rewardPopup], { argv: [] });
228
+ * process.exitCode = report.ok ? 0 : 1;
229
+ * ```
230
+ */
231
+ type VisualReport = {
232
+ ok: boolean;
233
+ tests: readonly VisualTestResult[];
234
+ };
235
+ /** What the command line asked for: the flags it carried, nothing else. */
236
+ type VisualFlags = {
237
+ dir?: string;
238
+ update?: boolean;
239
+ pixels?: boolean;
240
+ renderer?: VisualRenderer;
241
+ only?: readonly string[];
242
+ };
243
+ //#endregion
244
+ //#region src/visual/define.d.ts
245
+ /**
246
+ * Defines a visual test: where it starts, what `game.restore` loads, and its steps. A step is a
247
+ * `/control` command by its short name with that command's input, or `{ checkpoint: name }`,
248
+ * where the runner saves `state.json` and `describe.json` next to the test. `walk` runs fast: a
249
+ * popup its last node opens never mounts, so the step before a checkpoint that shows a node's
250
+ * result is `answer` or `tap`. With `webgl: true` the test also runs in the WebGL leg
251
+ * (`--webgl`), whose pictures go to `screen.webgl.webp`: set it on a screen with a custom filter.
252
+ * The answer is a frozen copy; nothing runs here.
253
+ *
254
+ * @param name - The test name, also its folder: lowercase letters, digits and dashes.
255
+ * @param test - Where it starts, its steps and its WebGL leg.
256
+ * @param test.start - The player, and optionally the session, the rng and the checkpoint.
257
+ * @param test.steps - The commands and the checkpoints, in order.
258
+ * @param test.webgl - True to run the test in the WebGL leg too; false by default.
259
+ * @returns The test, frozen.
260
+ * @throws {Error} When the name or a checkpoint name is not a file name, a checkpoint name is
261
+ * used twice, or a step is not one step command.
262
+ * @example
263
+ * ```ts
264
+ * // tests/visual/home.visual.ts: Home with the glowing Play plank, in both legs.
265
+ * const home = defineVisualTest("home", { start: atHome, steps: [{ checkpoint: "rest" }], webgl: true });
266
+ * home.webgl; // true
267
+ * ```
268
+ */
269
+ declare function defineVisualTest(name: string, test: {
270
+ start: VisualStart;
271
+ steps: readonly VisualStep[];
272
+ webgl?: boolean;
273
+ }): VisualTest;
274
+ //#endregion
275
+ //#region src/visual/run.d.ts
276
+ /**
277
+ * Reads the flags of the visual tests from a command line: `--update`, `--no-pixels`, `--webgl`,
278
+ * `--only <name>` (repeatable) and `--dir <path>`. An argument it does not know is left to the
279
+ * script that got it, so a test runner's own arguments pass through.
280
+ *
281
+ * @param argv - The arguments after the script name.
282
+ * @returns The flags that were given, and nothing else.
283
+ * @throws {Error} When `--only` or `--dir` has no value.
284
+ * @example
285
+ * ```ts
286
+ * // `bun tests/visual/run.ts --update --only reward-popup`
287
+ * parseVisualArgv(["--update", "--only", "reward-popup"]); // { update: true, only: ["reward-popup"] }
288
+ * parseVisualArgv(["--no-pixels", "--dir", "shots"]); // { pixels: false, dir: "shots" }
289
+ * parseVisualArgv(["--webgl"]); // { renderer: "webgl" }
290
+ * ```
291
+ */
292
+ declare function parseVisualArgv(argv: readonly string[]): VisualFlags;
293
+ /**
294
+ * Runs visual tests: the headless leg over every selected test, then the browser leg over the
295
+ * same tests when `pixels` is on (by default only with a `page` on a Mac). With `renderer:
296
+ * "webgl"` (`--webgl`) only the tests with `webgl: true` run, the page opens with
297
+ * `?renderer=webgl` and its pictures go to `screen.webgl.webp`; `state.json` and `describe.json`
298
+ * are shared with the WebGPU leg and compared as always. A checkpoint saves
299
+ * `state.json` and `describe.json` in `<dir>/<test>/<checkpoint>/`, and the browser leg
300
+ * `screen.webp`, lossless: a missing file is written, `--update` rewrites every file of the tests
301
+ * run, a JSON file is compared exactly. The browser leg opens the dev page in Chrome with WebGPU
302
+ * through `playwright-core`, pauses its game, plays each test there on stepped frames of 1000/60
303
+ * ms, so two runs see the same game time, checks the page's state against `state.json` and
304
+ * compares the picture in the page: a pixel differs above `tolerance.threshold`, and the screen
305
+ * differs when more than `tolerance.ratio` of its pixels do; `screen.actual.webp` and
306
+ * `screen.diff.webp` are written beside the baseline then. A pixel difference over the same state
307
+ * and describe is a rendering regression (`verdict: "rendering"`). One line per checkpoint goes
308
+ * to the branded console; the script sets the exit code from `ok`.
309
+ *
310
+ * @param setup - How to build a game: a fresh, unstarted app per test, and the dev page.
311
+ * @param tests - Every visual test of the game.
312
+ * @param options - Overrides of the flags of `argv` and of the defaults.
313
+ * @returns The report: `ok` when no test failed and nothing is `"different"`.
314
+ * @throws {Error} When a test is not well formed, two tests share a name, `only` names no test,
315
+ * a flag misses its value, or the browser leg fails as a whole: no `page`, no
316
+ * `playwright-core`, no Chrome, or a page that does not open, expose `game` and `doors`, start,
317
+ * draw with the requested backend or stop loading. In the WebGL leg, also when `only` names a
318
+ * test without `webgl: true`.
319
+ * @example
320
+ * ```ts
321
+ * // tests/visual/run.ts of a game, `bun tests/visual/run.ts --update` on a Mac
322
+ * const report = await runVisualTests(
323
+ * { app: () => createScreenGame().app, page: { url: "http://localhost:3000/" } },
324
+ * [rewardPopup]
325
+ * );
326
+ * report.tests[0]?.checkpoints[0]; // { name: "open", state: "written", describe: "written", pixels: "written" }
327
+ * ```
328
+ */
329
+ declare function runVisualTests(setup: VisualSetup, tests: readonly VisualTest[], options?: VisualOptions): Promise<VisualReport>;
330
+ //#endregion
331
+ export { type CheckpointResult, type VisualApp, type VisualOptions, type VisualPage, type VisualRenderer, type VisualReport, type VisualSetup, type VisualStart, type VisualStep, type VisualTest, type VisualTestResult, type VisualTolerance, defineVisualTest, parseVisualArgv, runVisualTests };