@abide/harness 0.4.3 → 0.6.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.3",
3
+ "version": "0.6.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.136.1"
60
+ "@abide/abide": ">=0.142.0"
61
61
  },
62
62
  "peerDependenciesMeta": {
63
63
  "@playwright/test": {
@@ -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
 
@@ -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
+ }
@@ -1,4 +1,4 @@
1
- // Lane 3 of 3 — bytes, microtask turns and allocations, on bun.
1
+ // Lane 3 of 3 — bytes, microtask turns and what a run leaves reachable, on bun.
2
2
  // The lane that prices what ships and what the server spends getting it there.
3
3
  //
4
4
  // There is no `calls()` here. `bun:test` already ships one —
@@ -6,7 +6,7 @@
6
6
  // with `mockRestore` for the teardown — and a second spelling would be a rename with
7
7
  // a worse restore story.
8
8
 
9
- export { allocated } from './allocated.ts'
9
+ export { retainedPerRun } from './retainedPerRun.ts'
10
10
  export { bytes } from './bytes.ts'
11
11
  export { closures } from './closures.ts'
12
12
  export { retained } from './retained.ts'
@@ -1,5 +1,5 @@
1
1
  // Live objects by JSC type, and the change between two readings — what `retained()` and
2
- // `allocated()` both diff. Not on the lane's entry: each of the two says what its reading means.
2
+ // `retainedPerRun()` both diff. Not on the lane's entry: each of the two says what its reading means.
3
3
 
4
4
  import { fullGC, heapStats } from 'bun:jsc'
5
5
 
@@ -11,12 +11,12 @@ import { fullGC, heapStats } from 'bun:jsc'
11
11
  export function liveCounts(caller: string): Record<string, number> {
12
12
  // happy-dom's own implementation objects land in the same untyped `Object` bucket as the
13
13
  // framework's, so a total taken in a preloaded process is worthless for any op that touches
14
- // the DOM. `bunfig.toml` preloads a DOM into every `bun test` process, so the allocation gates
14
+ // the DOM. `bunfig.toml` preloads a DOM into every `bun test` process, so the reachability gates
15
15
  // get their own preload-free invocation and this converts the silent contamination into a
16
16
  // failure.
17
17
  if (typeof document !== 'undefined')
18
18
  throw new Error(
19
- `${caller}() ran in a process with a DOM. happy-dom buckets as \`Object\` exactly where the framework does, so the totals would move with the emulator. Run the allocation gates in a preload-free process.`,
19
+ `${caller}() ran in a process with a DOM. happy-dom buckets as \`Object\` exactly where the framework does, so the totals would move with the emulator. Run the reachability gates in a preload-free process.`,
20
20
  )
21
21
  fullGC()
22
22
  return heapStats().objectTypeCounts as Record<string, number>
@@ -9,8 +9,8 @@
9
9
  // collected it before the `after` read. So whether a gate observes what it gates
10
10
  // would depend on the optimizer's liveness analysis of a case body, invisible at the
11
11
  // call site and flipping when the body is edited for an unrelated reason. This
12
- // function answers the question that is stable; `allocated()` answers the other one
13
- // and is tagged so it cannot be gated on.
12
+ // function answers the question that is stable; `retainedPerRun()` answers it as a batch mean
13
+ // and is tagged so it cannot be gated on. Neither sees what an op allocated and dropped.
14
14
 
15
15
  import { countsMoved, liveCounts } from './liveCounts.ts'
16
16
 
@@ -0,0 +1,61 @@
1
+ // WHAT EACH RUN OF AN OP KEEPS, as a batch mean: the objects still reachable after a full
2
+ // collection, per run. NOT WHAT IT ALLOCATED — `heapStats()` counts survivors only, and JSC has no
3
+ // allocation counter, so an op that builds four objects and drops them reads ~0 here, and this was
4
+ // named `allocated` while it read that. Tagged, and the tag is the point: an exact integer is
5
+ // `retained()`'s to hand out, and a fractional reading like `Object:3.09` is a reachability
6
+ // artifact rather than a genuinely fractional quantity.
7
+ //
8
+ // What a fractional per-run count IS good for is the other question: a non-integer means what is
9
+ // kept is conditional or amortized — a rehash, a cache filling, a once-per-batch structure — and a
10
+ // mean over the whole batch hides which. So the batch is halved and the two halves are reported
11
+ // apart. Zero growth across them is the leak check.
12
+
13
+ import { countsMoved, liveCounts } from './liveCounts.ts'
14
+
15
+ const DEFAULT_REPS = 1000
16
+
17
+ /**
18
+ * Mean objects each rep leaves reachable, by JSC type over a batch, and how that mean grew between
19
+ * its halves.
20
+ *
21
+ * A batch mean, never exact — gate exact counts on `retained()`. Throws in a process with a DOM.
22
+ */
23
+ export function retainedPerRun(
24
+ body: () => unknown,
25
+ reps = DEFAULT_REPS,
26
+ ): {
27
+ /**
28
+ * Always `'batch mean'`.
29
+ *
30
+ * A literal, so this record can never be `toMatchObject`'d against an exact integer the way a
31
+ * `retained()` diff is.
32
+ */
33
+ tagged: 'batch mean'
34
+ reps: number
35
+ perRep: Record<string, number>
36
+ /**
37
+ * The last half's mean minus the first half's, per rep.
38
+ *
39
+ * A type that grows across the batch is retaining rather than allocating.
40
+ */
41
+ growth: Record<string, number>
42
+ } {
43
+ // EXACTLY `reps` RUNS, the second half taking an odd one — halved to equal halves, 401 ran 400.
44
+ const runs = Math.max(2, Math.floor(reps))
45
+ const firstHalf = Math.floor(runs / 2)
46
+ const secondHalf = runs - firstHalf
47
+ const start = liveCounts('retainedPerRun')
48
+ for (let rep = 0; rep < firstHalf; rep += 1) body()
49
+ const middle = liveCounts('retainedPerRun')
50
+ for (let rep = 0; rep < secondHalf; rep += 1) body()
51
+ const end = liveCounts('retainedPerRun')
52
+
53
+ const first = countsMoved(start, middle, firstHalf)
54
+ const second = countsMoved(middle, end, secondHalf)
55
+ return {
56
+ tagged: 'batch mean',
57
+ reps: runs,
58
+ perRep: countsMoved(start, end, runs),
59
+ growth: countsMoved(first, second, 1),
60
+ }
61
+ }
@@ -1,56 +0,0 @@
1
- // WHAT AN OP ALLOCATED, which `heapStats()` cannot see and this therefore does not
2
- // pretend to report exactly. A batch mean, tagged, and the tag is the point: an exact
3
- // integer is `retained()`'s to hand out, and a fractional reading like `Object:3.09`
4
- // is a reachability artifact rather than a genuinely fractional quantity.
5
- //
6
- // What a fractional per-rep count IS good for is the other question: a non-integer
7
- // means the allocation is conditional or amortized — a rehash, an IC transition, a
8
- // once-per-batch structure — and a mean over the whole batch hides which. So the
9
- // batch is halved and the two halves are reported apart. Zero growth across them is
10
- // the leak check.
11
-
12
- import { countsMoved, liveCounts } from './liveCounts.ts'
13
-
14
- const DEFAULT_REPS = 1000
15
-
16
- /**
17
- * Mean objects per rep by JSC type over a batch, and how that mean grew between its halves.
18
- *
19
- * A batch mean, never exact — gate exact counts on `retained()`. Throws in a process with a DOM.
20
- */
21
- export function allocated(
22
- body: () => unknown,
23
- reps = DEFAULT_REPS,
24
- ): {
25
- /**
26
- * Always `'batch mean'`.
27
- *
28
- * A literal, so this record can never be `toMatchObject`'d against an exact integer the way a
29
- * `retained()` diff is.
30
- */
31
- tagged: 'batch mean'
32
- reps: number
33
- perRep: Record<string, number>
34
- /**
35
- * The last half's mean minus the first half's, per rep.
36
- *
37
- * A type that grows across the batch is retaining rather than allocating.
38
- */
39
- growth: Record<string, number>
40
- } {
41
- const half = Math.max(1, Math.floor(reps / 2))
42
- const start = liveCounts('allocated')
43
- for (let rep = 0; rep < half; rep += 1) body()
44
- const middle = liveCounts('allocated')
45
- for (let rep = 0; rep < half; rep += 1) body()
46
- const end = liveCounts('allocated')
47
-
48
- const first = countsMoved(start, middle, half)
49
- const second = countsMoved(middle, end, half)
50
- return {
51
- tagged: 'batch mean',
52
- reps: half * 2,
53
- perRep: countsMoved(start, end, half * 2),
54
- growth: countsMoved(first, second, 1),
55
- }
56
- }