@typeonce/ritmo-devtools 0.24.0 → 0.26.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 (53) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +197 -1
  3. package/dist/Fuzz.d.ts +38 -6
  4. package/dist/Fuzz.d.ts.map +1 -1
  5. package/dist/Fuzz.js +37 -11
  6. package/dist/Fuzz.js.map +1 -1
  7. package/dist/Replay.d.ts +1 -1
  8. package/dist/cli.js +171 -3
  9. package/dist/cli.js.map +1 -1
  10. package/dist/internal/dependencyScan.js +1 -1
  11. package/dist/internal/dependencyScan.js.map +1 -1
  12. package/dist/internal/fuzzNetwork.d.ts +15 -2
  13. package/dist/internal/fuzzNetwork.d.ts.map +1 -1
  14. package/dist/internal/fuzzNetwork.js +30 -7
  15. package/dist/internal/fuzzNetwork.js.map +1 -1
  16. package/dist/internal/keyboardPage.d.ts +29 -0
  17. package/dist/internal/keyboardPage.d.ts.map +1 -0
  18. package/dist/internal/keyboardPage.js +199 -0
  19. package/dist/internal/keyboardPage.js.map +1 -0
  20. package/dist/internal/keyboardWalk.d.ts +91 -0
  21. package/dist/internal/keyboardWalk.d.ts.map +1 -0
  22. package/dist/internal/keyboardWalk.js +373 -0
  23. package/dist/internal/keyboardWalk.js.map +1 -0
  24. package/dist/internal/layoutPage.d.ts +35 -0
  25. package/dist/internal/layoutPage.d.ts.map +1 -0
  26. package/dist/internal/layoutPage.js +535 -0
  27. package/dist/internal/layoutPage.js.map +1 -0
  28. package/dist/internal/layoutWalk.d.ts +84 -0
  29. package/dist/internal/layoutWalk.d.ts.map +1 -0
  30. package/dist/internal/layoutWalk.js +252 -0
  31. package/dist/internal/layoutWalk.js.map +1 -0
  32. package/dist/internal/pageOpen.d.ts +86 -0
  33. package/dist/internal/pageOpen.d.ts.map +1 -0
  34. package/dist/internal/pageOpen.js +166 -0
  35. package/dist/internal/pageOpen.js.map +1 -0
  36. package/dist/internal/route.d.ts +94 -0
  37. package/dist/internal/route.d.ts.map +1 -0
  38. package/dist/internal/route.js +230 -0
  39. package/dist/internal/route.js.map +1 -0
  40. package/dist/internal/version.d.ts +1 -1
  41. package/dist/internal/version.js +1 -1
  42. package/package.json +3 -3
  43. package/src/Fuzz.ts +62 -11
  44. package/src/cli.ts +165 -3
  45. package/src/internal/dependencyScan.ts +1 -1
  46. package/src/internal/fuzzNetwork.ts +33 -7
  47. package/src/internal/keyboardPage.ts +198 -0
  48. package/src/internal/keyboardWalk.ts +474 -0
  49. package/src/internal/layoutPage.ts +534 -0
  50. package/src/internal/layoutWalk.ts +365 -0
  51. package/src/internal/pageOpen.ts +225 -0
  52. package/src/internal/route.ts +276 -0
  53. package/src/internal/version.ts +1 -1
@@ -0,0 +1,276 @@
1
+ /**
2
+ * The route file `ritmo-devtools keyboard` and `ritmo-devtools layout` both
3
+ * read: the screens of a game's page, how to reach each one, and what each
4
+ * walk needs to know about them. Each walk ignores the fields that belong to
5
+ * the other (`noPress` is the keyboard walk's; `viewports`, `minTarget`, and
6
+ * `allow` are the layout audit's), so one route serves both.
7
+ *
8
+ * @internal
9
+ */
10
+ import * as Result from "@typeonce/ritmo/Result"
11
+
12
+ /** A control a route names: by accessible name, and by role when given. */
13
+ export interface ControlName {
14
+ readonly name: string
15
+ readonly role?: string
16
+ }
17
+
18
+ /** One thing a route does to reach a screen. */
19
+ export type Step =
20
+ | { readonly press: string }
21
+ | { readonly type: string }
22
+ | { readonly click: { readonly role: string; readonly name: string } }
23
+ | { readonly waitFor: { readonly role: string; readonly name: string } }
24
+ | { readonly wait: number }
25
+
26
+ /** The layout audit's checks that a route may allow, by selector. */
27
+ export type LayoutCheckId =
28
+ | "overlap"
29
+ | "cut-text"
30
+ | "off-screen"
31
+ | "safe-area"
32
+ | "small-target"
33
+ | "layout-shift"
34
+ | "truncated-text"
35
+ | "scroll-edge"
36
+
37
+ export const layoutChecks: ReadonlyArray<LayoutCheckId> = ["overlap", "cut-text", "off-screen", "safe-area", "small-target", "layout-shift", "truncated-text", "scroll-edge"]
38
+
39
+ const isLayoutCheck = (value: unknown): value is LayoutCheckId => typeof value === "string" && (layoutChecks as ReadonlyArray<string>).includes(value)
40
+
41
+ /**
42
+ * A finding the game means: the layout audit does not report a finding of
43
+ * `check` whose element (or the other element of an overlap) matches
44
+ * `selector` or is inside an element that does.
45
+ */
46
+ export interface Allow {
47
+ readonly check: LayoutCheckId
48
+ readonly selector: string
49
+ }
50
+
51
+ /** One screen of the walk: where it starts, how to reach it, what not to press, and what is meant. */
52
+ export interface Screen {
53
+ readonly name: string
54
+ /** Appended to the URL: `/`, `?map=8`, `#settings`. */
55
+ readonly path: string
56
+ readonly steps: ReadonlyArray<Step>
57
+ /** `true` presses nothing on this screen (no `focus-dropped`); a list skips those controls. */
58
+ readonly noPress: true | ReadonlyArray<ControlName>
59
+ /** Layout findings the game means on this screen (with the route's own `allow`). */
60
+ readonly allow: ReadonlyArray<Allow>
61
+ }
62
+
63
+ /** Space the device keeps for itself on each side (a notch, a home bar), in CSS pixels. */
64
+ export interface Insets {
65
+ readonly top: number
66
+ readonly right: number
67
+ readonly bottom: number
68
+ readonly left: number
69
+ }
70
+
71
+ /** A screen size the layout audit measures every screen at. */
72
+ export interface Viewport {
73
+ readonly name: string
74
+ readonly width: number
75
+ readonly height: number
76
+ /** A touch screen (a phone or a tablet): `hasTouch` and a mobile viewport. */
77
+ readonly touch: boolean
78
+ readonly insets: Insets
79
+ }
80
+
81
+ export interface Route {
82
+ readonly screens: ReadonlyArray<Screen>
83
+ /** The sizes the layout audit measures at (`defaultViewports` when the route names none). */
84
+ readonly viewports: ReadonlyArray<Viewport>
85
+ /** The smallest tap area, in CSS pixels, either side (44 by default). */
86
+ readonly minTarget: number
87
+ /** Layout findings the game means on every screen. */
88
+ readonly allow: ReadonlyArray<Allow>
89
+ }
90
+
91
+ const noInsets: Insets = { top: 0, right: 0, bottom: 0, left: 0 }
92
+
93
+ /**
94
+ * A small phone and a phone with a notch, held sideways and upright, and a
95
+ * desktop window. The notch insets are an iPhone's (the camera on one side
96
+ * when sideways, at the top when upright, and the home bar at the bottom).
97
+ */
98
+ export const defaultViewports: ReadonlyArray<Viewport> = [
99
+ { name: "small-phone-landscape", width: 667, height: 375, touch: true, insets: noInsets },
100
+ { name: "phone-landscape", width: 844, height: 390, touch: true, insets: { top: 0, right: 47, bottom: 21, left: 47 } },
101
+ { name: "phone-portrait", width: 390, height: 844, touch: true, insets: { top: 47, right: 0, bottom: 34, left: 0 } },
102
+ { name: "desktop", width: 1280, height: 800, touch: false, insets: noInsets }
103
+ ]
104
+
105
+ /** The route of a game that writes none: the start page, as one screen. */
106
+ export const startRoute: Route = {
107
+ screens: [{ name: "start", path: "", steps: [], noPress: [], allow: [] }],
108
+ viewports: defaultViewports,
109
+ minTarget: 44,
110
+ allow: []
111
+ }
112
+
113
+ const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null && !Array.isArray(value)
114
+
115
+ const onlyKeys = (value: Record<string, unknown>, keys: ReadonlyArray<string>, at: string): string | undefined => {
116
+ const extra = Object.keys(value).find((key) => !keys.includes(key))
117
+ return extra === undefined ? undefined : `${at}.${extra} is not a field (expected ${keys.join(", ")})`
118
+ }
119
+
120
+ const nonEmpty = (value: unknown): value is string => typeof value === "string" && value.trim() !== ""
121
+
122
+ const wholeIn = (value: unknown, least: number, most: number): value is number =>
123
+ typeof value === "number" && Number.isInteger(value) && value >= least && value <= most
124
+
125
+ const parseControlName = (value: unknown, at: string): Result.Result<ControlName, string> => {
126
+ if (!isRecord(value)) return Result.failure(`${at} must be an object { "name": string, "role"?: string }`)
127
+ const extra = onlyKeys(value, ["name", "role"], at)
128
+ if (extra !== undefined) return Result.failure(extra)
129
+ if (!nonEmpty(value["name"])) return Result.failure(`${at}.name must be a non-empty string: the control's accessible name`)
130
+ if (value["role"] !== undefined && !nonEmpty(value["role"])) return Result.failure(`${at}.role must be a non-empty string when given: an ARIA role such as "button"`)
131
+ return Result.success(value["role"] === undefined ? { name: value["name"] } : { name: value["name"], role: value["role"] })
132
+ }
133
+
134
+ const stepKinds = `{ "press": "Escape" }, { "type": "Ada" }, { "click": { "role": "button", "name": "Play" } }, { "waitFor": { "role": "button", "name": "Play" } }, { "wait": 500 }`
135
+
136
+ const parseStep = (value: unknown, at: string): Result.Result<Step, string> => {
137
+ if (!isRecord(value) || Object.keys(value).length !== 1) return Result.failure(`${at} must be one of ${stepKinds}`)
138
+ if ("press" in value) {
139
+ return nonEmpty(value["press"]) ? Result.success({ press: value["press"] }) : Result.failure(`${at}.press must be a key name, as Playwright's keyboard.press takes it ("Enter", "Escape", "a")`)
140
+ }
141
+ if ("type" in value) {
142
+ return typeof value["type"] === "string" && value["type"] !== "" ? Result.success({ type: value["type"] }) : Result.failure(`${at}.type must be the text to type into the focused field ("Ada")`)
143
+ }
144
+ if ("wait" in value) {
145
+ const wait = value["wait"]
146
+ return wholeIn(wait, 0, 60_000) ? Result.success({ wait }) : Result.failure(`${at}.wait must be a whole number of milliseconds from 0 to 60000`)
147
+ }
148
+ for (const kind of ["click", "waitFor"] as const) {
149
+ if (!(kind in value)) continue
150
+ const named = value[kind]
151
+ if (!isRecord(named)) return Result.failure(`${at}.${kind} must be an object { "role": string, "name": string }`)
152
+ const extra = onlyKeys(named, ["role", "name"], `${at}.${kind}`)
153
+ if (extra !== undefined) return Result.failure(extra)
154
+ if (!nonEmpty(named["role"])) return Result.failure(`${at}.${kind}.role must be a non-empty string: an ARIA role such as "button"`)
155
+ if (!nonEmpty(named["name"])) return Result.failure(`${at}.${kind}.name must be a non-empty string: the control's accessible name`)
156
+ const control = { role: named["role"], name: named["name"] }
157
+ return Result.success(kind === "click" ? { click: control } : { waitFor: control })
158
+ }
159
+ return Result.failure(`${at} must be one of ${stepKinds}; ${Object.keys(value)[0]} is not a step`)
160
+ }
161
+
162
+ const parseAllow = (value: unknown, at: string): Result.Result<ReadonlyArray<Allow>, string> => {
163
+ const raw = value ?? []
164
+ if (!Array.isArray(raw)) return Result.failure(`${at} must be an array of { "check", "selector" }`)
165
+ const allow: Array<Allow> = []
166
+ for (const [index, entry] of raw.entries()) {
167
+ const where = `${at}[${index}]`
168
+ if (!isRecord(entry)) return Result.failure(`${where} must be an object { "check": string, "selector": string }`)
169
+ const extra = onlyKeys(entry, ["check", "selector"], where)
170
+ if (extra !== undefined) return Result.failure(extra)
171
+ if (!isLayoutCheck(entry["check"])) return Result.failure(`${where}.check must be one of ${layoutChecks.join(", ")}`)
172
+ if (!nonEmpty(entry["selector"])) return Result.failure(`${where}.selector must be a CSS selector of the element the finding names, such as ".card .badge"`)
173
+ allow.push({ check: entry["check"], selector: entry["selector"] })
174
+ }
175
+ return Result.success(allow)
176
+ }
177
+
178
+ const parseScreen = (value: unknown, at: string): Result.Result<Screen, string> => {
179
+ if (!isRecord(value)) return Result.failure(`${at} must be an object { "name", "path"?, "steps"?, "noPress"?, "allow"? }`)
180
+ const extra = onlyKeys(value, ["name", "path", "steps", "noPress", "allow"], at)
181
+ if (extra !== undefined) return Result.failure(extra)
182
+ if (!nonEmpty(value["name"])) return Result.failure(`${at}.name must be a non-empty string`)
183
+ const path = value["path"] ?? ""
184
+ if (typeof path !== "string") return Result.failure(`${at}.path must be a string appended to the URL ("/", "?map=8")`)
185
+ const rawSteps = value["steps"] ?? []
186
+ if (!Array.isArray(rawSteps)) return Result.failure(`${at}.steps must be an array`)
187
+ const steps: Array<Step> = []
188
+ for (const [index, raw] of rawSteps.entries()) {
189
+ const step = parseStep(raw, `${at}.steps[${index}]`)
190
+ if (!step.ok) return step
191
+ steps.push(step.value)
192
+ }
193
+ const allow = parseAllow(value["allow"], `${at}.allow`)
194
+ if (!allow.ok) return allow
195
+ const rawNoPress = value["noPress"] ?? []
196
+ if (rawNoPress === true) return Result.success({ name: value["name"], path, steps, noPress: true, allow: allow.value })
197
+ if (!Array.isArray(rawNoPress)) return Result.failure(`${at}.noPress must be true (press nothing on this screen) or an array of { "name", "role"? }`)
198
+ const noPress: Array<ControlName> = []
199
+ for (const [index, raw] of rawNoPress.entries()) {
200
+ const control = parseControlName(raw, `${at}.noPress[${index}]`)
201
+ if (!control.ok) return control
202
+ noPress.push(control.value)
203
+ }
204
+ return Result.success({ name: value["name"], path, steps, noPress, allow: allow.value })
205
+ }
206
+
207
+ const parseViewport = (value: unknown, at: string): Result.Result<Viewport, string> => {
208
+ if (!isRecord(value)) return Result.failure(`${at} must be an object { "name", "width", "height", "touch"?, "insets"? }`)
209
+ const extra = onlyKeys(value, ["name", "width", "height", "touch", "insets"], at)
210
+ if (extra !== undefined) return Result.failure(extra)
211
+ if (!nonEmpty(value["name"])) return Result.failure(`${at}.name must be a non-empty string`)
212
+ if (!wholeIn(value["width"], 200, 4000)) return Result.failure(`${at}.width must be a whole number of CSS pixels from 200 to 4000`)
213
+ if (!wholeIn(value["height"], 200, 4000)) return Result.failure(`${at}.height must be a whole number of CSS pixels from 200 to 4000`)
214
+ const touch = value["touch"] ?? false
215
+ if (typeof touch !== "boolean") return Result.failure(`${at}.touch must be true (a phone or tablet) or false`)
216
+ const rawInsets = value["insets"] ?? noInsets
217
+ if (!isRecord(rawInsets)) return Result.failure(`${at}.insets must be an object { "top", "right", "bottom", "left" } of CSS pixels`)
218
+ const insetsExtra = onlyKeys(rawInsets, ["top", "right", "bottom", "left"], `${at}.insets`)
219
+ if (insetsExtra !== undefined) return Result.failure(insetsExtra)
220
+ const sides = ["top", "right", "bottom", "left"] as const
221
+ for (const side of sides) {
222
+ if (!wholeIn(rawInsets[side] ?? 0, 0, 200)) return Result.failure(`${at}.insets.${side} must be a whole number of CSS pixels from 0 to 200`)
223
+ }
224
+ const inset = (side: (typeof sides)[number]): number => {
225
+ const given = rawInsets[side]
226
+ return typeof given === "number" ? given : 0
227
+ }
228
+ return Result.success({
229
+ name: value["name"],
230
+ width: value["width"],
231
+ height: value["height"],
232
+ touch,
233
+ insets: { top: inset("top"), right: inset("right"), bottom: inset("bottom"), left: inset("left") }
234
+ })
235
+ }
236
+
237
+ /** Validates a route file's JSON; the error names the field that is wrong. */
238
+ export const parseRoute = (value: unknown): Result.Result<Route, string> => {
239
+ if (!isRecord(value)) return Result.failure(`the route must be an object { "screens": [...] }`)
240
+ const extra = onlyKeys(value, ["screens", "viewports", "minTarget", "allow"], "route")
241
+ if (extra !== undefined) return Result.failure(extra)
242
+ const raw = value["screens"]
243
+ if (!Array.isArray(raw) || raw.length === 0) return Result.failure(`route.screens must be a non-empty array`)
244
+ const screens: Array<Screen> = []
245
+ for (const [index, entry] of raw.entries()) {
246
+ const screen = parseScreen(entry, `route.screens[${index}]`)
247
+ if (!screen.ok) return screen
248
+ if (screens.some((other) => other.name === screen.value.name)) return Result.failure(`route.screens[${index}].name "${screen.value.name}" is already the name of another screen`)
249
+ screens.push(screen.value)
250
+ }
251
+ let viewports = defaultViewports
252
+ if (value["viewports"] !== undefined) {
253
+ const rawViewports = value["viewports"]
254
+ if (!Array.isArray(rawViewports) || rawViewports.length === 0) return Result.failure(`route.viewports must be a non-empty array of { "name", "width", "height", "touch"?, "insets"? }`)
255
+ const parsed: Array<Viewport> = []
256
+ for (const [index, entry] of rawViewports.entries()) {
257
+ const viewport = parseViewport(entry, `route.viewports[${index}]`)
258
+ if (!viewport.ok) return viewport
259
+ if (parsed.some((other) => other.name === viewport.value.name)) return Result.failure(`route.viewports[${index}].name "${viewport.value.name}" is already the name of another viewport`)
260
+ parsed.push(viewport.value)
261
+ }
262
+ viewports = parsed
263
+ }
264
+ const minTarget = value["minTarget"] ?? 44
265
+ if (!wholeIn(minTarget, 1, 200)) return Result.failure(`route.minTarget must be a whole number of CSS pixels from 1 to 200 (the smallest tap area either side)`)
266
+ const allow = parseAllow(value["allow"], "route.allow")
267
+ if (!allow.ok) return allow
268
+ return Result.success({ screens, viewports, minTarget, allow: allow.value })
269
+ }
270
+
271
+ /** Joins a screen's path to the base URL, keeping the base's own path. */
272
+ export const screenUrl = (base: string, path: string): string => {
273
+ if (path === "") return base
274
+ if (path.startsWith("?") || path.startsWith("#")) return base.replace(/[?#].*$/, "") + path
275
+ return new URL(path, base).href
276
+ }
@@ -3,4 +3,4 @@
3
3
  * `package.json` by `scripts/sync-version.ts` (run by \`pnpm version-packages\`)
4
4
  * and checked by a test.
5
5
  */
6
- export const libraryVersion = "0.24.0"
6
+ export const libraryVersion = "0.26.0"