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