@abide/harness 0.4.2 → 0.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abide/harness",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "Ratios against hand-written code, counts of the work an op did, and gates whose revert can be run — three measurement lanes for anything that renders, on bun",
6
6
  "license": "MIT",
@@ -57,7 +57,7 @@
57
57
  },
58
58
  "peerDependencies": {
59
59
  "@playwright/test": "^1.64.0",
60
- "@abide/abide": ">=0.130.0"
60
+ "@abide/abide": ">=0.140.0"
61
61
  },
62
62
  "peerDependenciesMeta": {
63
63
  "@playwright/test": {
@@ -8,17 +8,32 @@
8
8
  // spec loads in both engine projects at once, and two workers writing one file is a
9
9
  // reader that can catch it half-written.
10
10
 
11
+ import type { Page } from '@playwright/test'
12
+
11
13
  const ENTRY = new URL('../src/measure/injectable.ts', import.meta.url).pathname
12
14
 
13
- /** Builds the measure lane's injectable and returns its source. */
14
- export async function buildInjectable(): Promise<string> {
15
+ /** Builds the measure lane's injectable — or another entry's, the same way — and returns its source. */
16
+ export async function buildInjectable(entry = ENTRY): Promise<string> {
15
17
  const built = await Bun.build({
16
- entrypoints: [ENTRY],
18
+ entrypoints: [entry],
17
19
  format: 'iife',
18
20
  target: 'browser',
19
21
  minify: false,
20
22
  })
21
23
  if (!built.success)
22
- throw new AggregateError(built.logs, 'measure did not build')
24
+ throw new AggregateError(built.logs, `${entry} did not build`)
23
25
  return (built.outputs[0] as Blob).text()
24
26
  }
27
+
28
+ /**
29
+ * Opens a blank page `page` runs `source` in first.
30
+ *
31
+ * NAVIGATED, NOT JUST SET: `setContent` on a page still on its initial `about:blank` reruns no init
32
+ * script — nothing navigated — so the injection would be silently absent, and every counter read
33
+ * undefined: the gate failing on the instrument rather than on the claim.
34
+ */
35
+ export async function openInjected(page: Page, source: string): Promise<void> {
36
+ await page.addInitScript({ content: source })
37
+ await page.goto('about:blank')
38
+ await page.setContent('<!doctype html><html><body></body></html>')
39
+ }
@@ -1,7 +1,7 @@
1
1
  // THE BUILT-IN ADAPTER FOR AN ABIDE APP, and the one entry of this package allowed to know abide.
2
2
  // Every lane measures anything; this is what makes the general instrument read like abide's own:
3
- // `measure` with abide's three counters on the record by name, and abide's `mount` beside it, so an
4
- // app's test imports both from one place.
3
+ // `measure` with abide's three counters on the record by name, and abide's `mount` and `settled`
4
+ // beside it, so an app's test imports all three from one place.
5
5
  //
6
6
  // ABIDE IS NOT IMPORTED TO COUNT. abide publishes its counters on the global every lane reads
7
7
  // (`WORK.ts`, 43.21) and this reads them back by name, so the counting stays in the lane and the
@@ -13,7 +13,7 @@ import {
13
13
  type Work,
14
14
  } from '../measure/index.ts'
15
15
 
16
- export { type Mounted, mount } from '@abide/abide/test'
16
+ export { type Mounted, mount, settled } from '@abide/abide/test'
17
17
 
18
18
  const FIELDS = ['wakes', 'bindingRuns', 'descents'] as const
19
19
 
@@ -0,0 +1,41 @@
1
+ // A CEILING, NOT A COUNT. An exact count of what a subject did internally — abide's `wakes` — is a
2
+ // claim about the subject's implementation, and an app that pins one breaks on every release that
3
+ // moves it without the app's work moving at all. The subject's own gates stay exact, since that
4
+ // number is the one they exist to hold; an app holds the number it can be blamed for, as a most.
5
+ //
6
+ // A plain throw rather than `expect`, so the failure reads from the line making the claim: the stack
7
+ // is cut at this function, and every field over its ceiling is named with what it reported, which
8
+ // is also what a `gate`'s `worth` looks for in the failure (43.9).
9
+
10
+ /**
11
+ * Throws naming every field of `record` over its ceiling in `ceilings`, with the value it reported.
12
+ *
13
+ * A ceiling on a field the record carries as absent (`null`, 43.19) throws too: nothing was
14
+ * published to compare.
15
+ */
16
+ export function atMost<Counted extends object>(
17
+ record: Counted,
18
+ ceilings: {
19
+ [
20
+ Field in keyof Counted as Counted[Field] extends number | null
21
+ ? Field
22
+ : never
23
+ ]?: number
24
+ },
25
+ ): void {
26
+ const over: string[] = []
27
+ for (const field of Object.keys(ceilings)) {
28
+ const ceiling = (ceilings as Record<string, number>)[field] as number
29
+ const reported = (record as Record<string, unknown>)[field]
30
+ if (typeof reported !== 'number')
31
+ over.push(
32
+ `${field} has a ceiling of ${ceiling} and the record carries it as ${reported === null ? 'absent' : String(reported)}`,
33
+ )
34
+ else if (reported > ceiling)
35
+ over.push(`${field}: ${reported}, over its ceiling of ${ceiling}`)
36
+ }
37
+ if (over.length === 0) return
38
+ const failure = new Error(`atMost: ${over.join('; ')}`)
39
+ Error.captureStackTrace(failure, atMost)
40
+ throw failure
41
+ }
package/src/gate/index.ts CHANGED
@@ -27,6 +27,7 @@
27
27
 
28
28
  import { test } from 'bun:test'
29
29
 
30
+ export { atMost } from './atMost.ts'
30
31
  export { inAFreshProcess } from './inAFreshProcess.ts'
31
32
  export { spawned } from './spawned.ts'
32
33
 
@@ -140,7 +140,7 @@ const DECLARED: [interfaceName: string, member: string, role: Role][] = [
140
140
  ['Document', 'write', 'refused'],
141
141
  ]
142
142
 
143
- /** A fresh `Work` with every counted row at 0 and the three published rows at `null`. */
143
+ /** A fresh `Work` with every counted row at 0 and nothing published. */
144
144
  export function blankWork(): Work {
145
145
  // Fixed shape at construction — every field initialized, `undefined` included —
146
146
  // because this object is read on every counted call.
@@ -36,11 +36,138 @@ for (const name of WIRE) native[name] = globalThis[name]
36
36
  // would fetch them from a host nothing answers, in whichever test file happened to parse one.
37
37
  // happy-dom fetches neither unless told to — a stylesheet needs `enableCSSFileLoading`, a script
38
38
  // `enableJavaScriptEvaluation` — so what is set here is that a load it skipped fires `load`, as a
39
- // fetched one would, rather than `error` and a logged `NotSupportedError`.
39
+ // fetched one would, rather than `error` and a logged `NotSupportedError`. 43.39 — an iframe's page
40
+ // it WOULD fetch, and does not.
40
41
  GlobalRegistrator.register({
41
42
  url: 'http://localhost/',
42
- settings: { handleDisabledFileLoadingAsSuccess: true },
43
+ settings: {
44
+ handleDisabledFileLoadingAsSuccess: true,
45
+ disableIframePageLoading: true,
46
+ },
43
47
  })
48
+ const DomBlob = globalThis.Blob as unknown as abstract new (
49
+ ...args: never[]
50
+ ) => object
51
+ const DomFile = globalThis.File as unknown as typeof DomBlob
52
+ const DomFormData = globalThis.FormData as unknown as new (
53
+ form?: unknown,
54
+ submitter?: unknown,
55
+ ) => Iterable<[string, unknown]>
44
56
  for (const name of WIRE) Object.assign(globalThis, { [name]: native[name] })
57
+ bridgeFiles()
45
58
  install()
46
59
  installPopover()
60
+
61
+ /**
62
+ * 43.37, 43.38 — ONE `File` FOR BOTH HALVES. The wire's are bun's (43.28), and happy-dom takes only
63
+ * its own: `DataTransfer`, `FileReader` and `URL.createObjectURL` check `instanceof` against its
64
+ * classes and then read the bytes off a symbol it keeps them under, and bun's `FormData` ignores
65
+ * the form it is handed. So the globals become bun's classes, subclassed to keep a copy of the bytes
66
+ * under happy-dom's symbol, and happy-dom's own `instanceof` is widened to a runtime `Blob` that
67
+ * carries one. A `Blob` built from a part only the runtime can read (`Bun.file`) carries none, and
68
+ * stays the wire's alone rather than reading as empty in the DOM.
69
+ */
70
+ function bridgeFiles(): void {
71
+ const RuntimeBlob = native.Blob as typeof Blob
72
+ const RuntimeFile = native.File as typeof File
73
+ const RuntimeFormData = native.FormData as typeof FormData
74
+ const BYTES = Object.getOwnPropertySymbols(
75
+ Reflect.construct(DomBlob, []),
76
+ ).find((symbol) => symbol.description === 'buffer') as symbol
77
+ const bytesOf = (parts: readonly unknown[]): Buffer | undefined => {
78
+ const buffers: Buffer[] = []
79
+ for (const part of parts) {
80
+ if (part instanceof RuntimeBlob || part instanceof DomBlob) {
81
+ const held = (part as Record<symbol, Buffer | undefined>)[BYTES]
82
+ if (held === undefined) return undefined
83
+ buffers.push(held)
84
+ } else if (part instanceof ArrayBuffer)
85
+ buffers.push(Buffer.from(new Uint8Array(part)))
86
+ else if (ArrayBuffer.isView(part))
87
+ buffers.push(
88
+ Buffer.from(
89
+ new Uint8Array(
90
+ part.buffer,
91
+ part.byteOffset,
92
+ part.byteLength,
93
+ ),
94
+ ),
95
+ )
96
+ else buffers.push(Buffer.from(String(part)))
97
+ }
98
+ return Buffer.concat(buffers)
99
+ }
100
+ // `instanceof` answers as the runtime's own does: a `File` bun's `formData()` parsed is not one of
101
+ // these, and a check against the global must still take it.
102
+ class BridgedBlob extends RuntimeBlob {
103
+ static override [Symbol.hasInstance](candidate: unknown): boolean {
104
+ return candidate instanceof RuntimeBlob
105
+ }
106
+ constructor(parts: BlobPart[] = [], options?: BlobPropertyBag) {
107
+ const listed = [...parts]
108
+ super(listed, options)
109
+ ;(this as Record<symbol, unknown>)[BYTES] = bytesOf(listed)
110
+ }
111
+ }
112
+ class BridgedFile extends RuntimeFile {
113
+ static override [Symbol.hasInstance](candidate: unknown): boolean {
114
+ return candidate instanceof RuntimeFile
115
+ }
116
+ constructor(
117
+ parts: BlobPart[],
118
+ name: string,
119
+ options?: FilePropertyBag,
120
+ ) {
121
+ const listed = [...parts]
122
+ super(listed, name, options)
123
+ ;(this as Record<symbol, unknown>)[BYTES] = bytesOf(listed)
124
+ }
125
+ }
126
+ // A form's entries come off happy-dom's own `FormData`, and the one it makes for an empty file
127
+ // input is a happy-dom `File` the wire cannot send — so that one is copied into a bridged one.
128
+ class BridgedFormData extends RuntimeFormData {
129
+ static override [Symbol.hasInstance](candidate: unknown): boolean {
130
+ return candidate instanceof RuntimeFormData
131
+ }
132
+ constructor(form?: HTMLFormElement, submitter?: HTMLElement | null) {
133
+ super()
134
+ if (form === undefined || form === null) return
135
+ for (const [name, value] of new DomFormData(form, submitter)) {
136
+ if (typeof value === 'string' || value instanceof RuntimeBlob)
137
+ this.append(name, value as string)
138
+ else {
139
+ const file = value as File & Record<symbol, Buffer>
140
+ const bytes = file[BYTES] as Buffer
141
+ this.append(
142
+ name,
143
+ new BridgedFile([bytes as BlobPart], file.name, {
144
+ type: file.type,
145
+ lastModified: file.lastModified,
146
+ }),
147
+ )
148
+ }
149
+ }
150
+ }
151
+ }
152
+ const ordinary = Function.prototype[Symbol.hasInstance]
153
+ for (const [domClass, runtimeClass] of [
154
+ [DomBlob, RuntimeBlob],
155
+ [DomFile, RuntimeFile],
156
+ ] as const)
157
+ Object.defineProperty(domClass, Symbol.hasInstance, {
158
+ value(this: unknown, candidate: unknown): boolean {
159
+ return (
160
+ ordinary.call(this, candidate) ||
161
+ (candidate instanceof runtimeClass &&
162
+ (candidate as unknown as Record<symbol, unknown>)[
163
+ BYTES
164
+ ] !== undefined)
165
+ )
166
+ },
167
+ })
168
+ Object.assign(globalThis, {
169
+ Blob: BridgedBlob,
170
+ File: BridgedFile,
171
+ FormData: BridgedFormData,
172
+ })
173
+ }