@ultimat3/testing 30.8.0 → 31.0.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 (159) hide show
  1. package/CLAUDE.md +49 -62
  2. package/README.md +179 -39
  3. package/package.json +23 -17
  4. package/src/ai-conformance-answer.ts +67 -0
  5. package/src/ai-gateway-conformance.ts +105 -0
  6. package/src/ai-provider-conformance.ts +69 -0
  7. package/src/ai-provider-transport-conformance.ts +170 -0
  8. package/src/audit-sink-conformance.ts +204 -0
  9. package/src/auth-adapter-conformance-credentials.ts +124 -0
  10. package/src/auth-adapter-conformance-mfa.ts +70 -0
  11. package/src/auth-adapter-conformance-passkeys.ts +133 -0
  12. package/src/auth-adapter-conformance-sessions.ts +115 -0
  13. package/src/auth-adapter-conformance-support.ts +71 -0
  14. package/src/auth-adapter-conformance-users.ts +128 -0
  15. package/src/auth-adapter-conformance.ts +34 -0
  16. package/src/auth-limiter-factory-conformance.ts +224 -0
  17. package/src/auth-server-store-conformance-records.ts +69 -0
  18. package/src/auth-server-store-conformance.ts +208 -0
  19. package/src/cache-tier-conformance-checks.ts +142 -0
  20. package/src/cache-tier-conformance.ts +24 -0
  21. package/src/cassette-format.ts +8 -13
  22. package/src/cassette-record.ts +1 -1
  23. package/src/cdp-browser.ts +10 -11
  24. package/src/cdp-e2e-page.ts +2 -2
  25. package/src/cdp-e2e-session.ts +2 -2
  26. package/src/cdp-network-probe.ts +1 -1
  27. package/src/cdp-offline-script.ts +2 -2
  28. package/src/clock-conformance.ts +126 -0
  29. package/src/conformance.ts +239 -0
  30. package/src/e2e-browser-handle.ts +1 -2
  31. package/src/e2e-error-codes.ts +17 -16
  32. package/src/e2e-run.ts +4 -3
  33. package/src/embedder-conformance.ts +101 -0
  34. package/src/entity-conformance-aggregates.ts +210 -0
  35. package/src/entity-conformance-batches.ts +216 -0
  36. package/src/entity-conformance-bulk.ts +379 -0
  37. package/src/entity-conformance-checks.ts +27 -0
  38. package/src/entity-conformance-entities.ts +52 -0
  39. package/src/entity-conformance-locks.ts +286 -0
  40. package/src/entity-conformance-matrix.ts +397 -0
  41. package/src/entity-conformance-reads.ts +241 -0
  42. package/src/entity-conformance-support.ts +56 -0
  43. package/src/entity-conformance-tenancy.ts +296 -0
  44. package/src/entity-conformance-values.ts +305 -0
  45. package/src/entity-conformance-versions.ts +60 -0
  46. package/src/entity-conformance-writes.ts +348 -0
  47. package/src/entity-driver-conformance.ts +103 -0
  48. package/src/error-groups-store-conformance.ts +198 -0
  49. package/src/error-reporter-conformance.ts +119 -0
  50. package/src/errors.ts +41 -11
  51. package/src/event-bus-conformance.ts +161 -0
  52. package/src/exercise-drift-hint.ts +4 -1
  53. package/src/exercise-judge.ts +5 -3
  54. package/src/exercise-override-input.ts +22 -9
  55. package/src/exercise-override-line.ts +98 -0
  56. package/src/exercise-report.ts +30 -51
  57. package/src/exercise-sweeps.ts +3 -1
  58. package/src/exercise.ts +38 -19
  59. package/src/factories.ts +192 -179
  60. package/src/factory-api.ts +131 -0
  61. package/src/factory-associations.ts +67 -0
  62. package/src/factory-errors.ts +38 -0
  63. package/src/factory-lint.ts +92 -0
  64. package/src/factory-persist.ts +74 -16
  65. package/src/factory-pipeline.ts +191 -0
  66. package/src/factory-registry.ts +2 -1
  67. package/src/factory-types.ts +131 -0
  68. package/src/failure-store-conformance.ts +137 -0
  69. package/src/fixture-jobs.ts +50 -15
  70. package/src/fixture-mail.ts +7 -7
  71. package/src/fixture-seed.ts +111 -0
  72. package/src/fixture-statements.ts +68 -52
  73. package/src/framework-fixtures.ts +1 -1
  74. package/src/idempotency-store-conformance.ts +186 -0
  75. package/src/idempotency-store-replay-conformance.ts +140 -0
  76. package/src/idempotency-store-tx-conformance.ts +132 -0
  77. package/src/idempotency-store-world-conformance.ts +82 -0
  78. package/src/identity-provider-conformance.ts +144 -0
  79. package/src/image-transform-conformance.ts +89 -0
  80. package/src/index.ts +68 -157
  81. package/src/isr-store-conformance.ts +113 -0
  82. package/src/job-sim-effects.ts +19 -31
  83. package/src/job-sim-world.ts +6 -15
  84. package/src/lease-store-conformance.ts +195 -0
  85. package/src/mail-driver-conformance.ts +108 -0
  86. package/src/matcher-doubles.ts +104 -0
  87. package/src/matcher-stub.ts +78 -0
  88. package/src/matcher-surface.ts +21 -0
  89. package/src/matchers.ts +19 -1
  90. package/src/mcp-confirmation-store-conformance.ts +262 -0
  91. package/src/meter-store-conformance.ts +135 -0
  92. package/src/notify-digest-conformance.ts +133 -0
  93. package/src/notify-inbox-conformance.ts +130 -0
  94. package/src/notify-ledger-conformance.ts +134 -0
  95. package/src/notify-preference-conformance.ts +49 -0
  96. package/src/notify-stores-conformance.ts +73 -0
  97. package/src/outbox-store-conformance-claims.ts +112 -0
  98. package/src/outbox-store-conformance-subject.ts +65 -0
  99. package/src/outbox-store-conformance.ts +71 -0
  100. package/src/per-test-reset.ts +55 -20
  101. package/src/plan-oracle.ts +10 -3
  102. package/src/plan-sweep.ts +37 -12
  103. package/src/preload.ts +4 -2
  104. package/src/purge-conformance.ts +79 -0
  105. package/src/push-device-conformance.ts +124 -0
  106. package/src/push-subscription-conformance.ts +123 -0
  107. package/src/quiet-logs.ts +4 -4
  108. package/src/rate-limit-store-conformance.ts +178 -0
  109. package/src/registry-leak-error.ts +8 -1
  110. package/src/scale-sweep-run.ts +3 -1
  111. package/src/scrape-driver-conformance-frame-checks.ts +97 -0
  112. package/src/scrape-driver-conformance-page-checks.ts +114 -0
  113. package/src/scrape-driver-conformance-site.ts +84 -0
  114. package/src/scrape-driver-conformance.ts +34 -0
  115. package/src/sealed-dns-resolver.ts +56 -0
  116. package/src/sealed-dns.ts +63 -64
  117. package/src/sealed-network.ts +13 -12
  118. package/src/sealed-node-sockets.ts +63 -0
  119. package/src/sealed-sockets.ts +13 -8
  120. package/src/sealed-udp.ts +118 -0
  121. package/src/search-index-conformance-list.ts +76 -0
  122. package/src/search-index-conformance-support.ts +69 -0
  123. package/src/search-index-conformance-versions.ts +66 -0
  124. package/src/search-index-conformance.ts +126 -0
  125. package/src/secret-source-conformance.ts +91 -0
  126. package/src/seed-factories.ts +57 -0
  127. package/src/seed-writer.ts +8 -19
  128. package/src/semantic-cache-factory-conformance.ts +86 -0
  129. package/src/sms-transport-conformance.ts +102 -0
  130. package/src/step-store-conformance.ts +171 -0
  131. package/src/storage-conformance-read-checks.ts +132 -0
  132. package/src/storage-conformance-shape.ts +121 -0
  133. package/src/storage-conformance-stream-checks.ts +226 -0
  134. package/src/storage-conformance.ts +34 -0
  135. package/src/stub-action.ts +24 -0
  136. package/src/stub-errors.ts +86 -0
  137. package/src/stub-job.ts +30 -0
  138. package/src/stub-query.ts +25 -0
  139. package/src/stub-script.ts +76 -0
  140. package/src/stub-stack.ts +29 -0
  141. package/src/stub.ts +73 -0
  142. package/src/swap-adapter.ts +135 -0
  143. package/src/test-scope.ts +96 -0
  144. package/src/test-types.ts +1 -1
  145. package/src/totp-replay-guard-conformance.ts +76 -0
  146. package/src/transport-conformance.ts +179 -0
  147. package/src/upload-scanner-conformance.ts +112 -0
  148. package/src/vector-store-conformance-checks.ts +174 -0
  149. package/src/vector-store-conformance.ts +21 -0
  150. package/src/cdp-connection.ts +0 -291
  151. package/src/cdp-errors.ts +0 -179
  152. package/src/cdp-launch-attempt.ts +0 -224
  153. package/src/cdp-launch-candidates.ts +0 -62
  154. package/src/cdp-launch-profile.ts +0 -106
  155. package/src/cdp-launch-reap.ts +0 -68
  156. package/src/cdp-launch-wire.ts +0 -171
  157. package/src/cdp-launch.ts +0 -175
  158. package/src/cdp-pipe.ts +0 -77
  159. package/src/cdp-wire-watch.ts +0 -113
@@ -0,0 +1,178 @@
1
+ // The `RateLimitStore` contract of `@ultimat3/http` as shared examples, for a store of your own:
2
+ // `behavesLike(rateLimitStoreConformance, () => store)`. Asserted through the port only — the
3
+ // numbers every driver owes (`rateLimitDecision`'s maths), the read that writes nothing, the
4
+ // reset that touches one key, and the race a shared counter exists to win.
5
+
6
+ import { expect, test } from 'bun:test';
7
+ import type { Bucket, RateLimitStore } from '@ultimat3/http/adapter';
8
+ import type { SharedExamples } from './shared-examples';
9
+ import { sharedExamples } from './shared-examples';
10
+
11
+ export interface RateLimitStoreCheck {
12
+ readonly name: string;
13
+ run(store: RateLimitStore): Promise<void>;
14
+ }
15
+
16
+ /** Far from zero and from the host clock, so a store reading `Date.now()` instead of `nowMs` shows. */
17
+ const NOW = 1_700_000_000_000;
18
+ const FOUR: Bucket = { capacity: 4, refillPerSecond: 1 };
19
+ const HALVES: Bucket = { capacity: 3, refillPerSecond: 0.5 };
20
+ const RACERS = 16;
21
+
22
+ /** A key no other check, run or process shares — a shared table holds every run's buckets. */
23
+ const keyOf = (label: string): string => `conformance:${label}:${crypto.randomUUID()}`;
24
+
25
+ const spend = async (store: RateLimitStore, key: string, bucket: Bucket, n: number) => {
26
+ for (let index = 0; index < n; index += 1) await store.take(key, bucket, 1, NOW);
27
+ };
28
+
29
+ export const RATE_LIMIT_STORE_CHECKS: readonly RateLimitStoreCheck[] = [
30
+ {
31
+ name: 'declares where its counters live',
32
+ async run(store) {
33
+ expect(['process', 'shared']).toContain(store.scope);
34
+ },
35
+ },
36
+ {
37
+ name: 'a bucket of four admits four takes at one instant, then refuses and says when',
38
+ async run(store) {
39
+ const key = keyOf('burst');
40
+ const remaining: number[] = [];
41
+ for (let index = 0; index < 4; index += 1) {
42
+ const decision = await store.take(key, FOUR, 1, NOW);
43
+ expect([decision.allowed, decision.limit, decision.retryAfterSeconds]).toEqual([
44
+ true,
45
+ 4,
46
+ 0,
47
+ ]);
48
+ remaining.push(decision.remaining);
49
+ }
50
+ expect(remaining).toEqual([3, 2, 1, 0]);
51
+ expect(await store.take(key, FOUR, 1, NOW)).toEqual({
52
+ allowed: false,
53
+ limit: 4,
54
+ remaining: 0,
55
+ resetAtMs: NOW + 1_000,
56
+ retryAfterSeconds: 1,
57
+ });
58
+ },
59
+ },
60
+ {
61
+ name: 'tokens return at refillPerSecond and stop at capacity',
62
+ async run(store) {
63
+ const key = keyOf('refill');
64
+ await spend(store, key, FOUR, 4);
65
+ expect(await store.take(key, FOUR, 1, NOW + 1_000)).toMatchObject({
66
+ allowed: true,
67
+ remaining: 0,
68
+ });
69
+ // A minute is sixty tokens of refill against a bucket of four: capped, never banked.
70
+ expect(await store.take(key, FOUR, 1, NOW + 61_000)).toMatchObject({
71
+ allowed: true,
72
+ remaining: 3,
73
+ });
74
+ },
75
+ },
76
+ {
77
+ name: 'a cost above one spends that many, and a refused cost spends nothing',
78
+ async run(store) {
79
+ const key = keyOf('cost');
80
+ expect(await store.take(key, FOUR, 3, NOW)).toMatchObject({ allowed: true, remaining: 1 });
81
+ expect(await store.take(key, FOUR, 2, NOW)).toMatchObject({
82
+ allowed: false,
83
+ remaining: 1,
84
+ retryAfterSeconds: 1,
85
+ });
86
+ expect(await store.take(key, FOUR, 1, NOW)).toMatchObject({ allowed: true, remaining: 0 });
87
+ },
88
+ },
89
+ {
90
+ name: 'a fractional token survives between takes — half a token left is not none',
91
+ async run(store) {
92
+ const key = keyOf('half');
93
+ await spend(store, key, HALVES, 3);
94
+ const refused = await store.take(key, HALVES, 1, NOW + 1_000);
95
+ expect([refused.allowed, refused.retryAfterSeconds]).toEqual([false, 1]);
96
+ expect((await store.take(key, HALVES, 1, NOW + 2_000)).allowed).toBe(true);
97
+ },
98
+ },
99
+ {
100
+ name: 'a clock behind the stored instant refills nothing',
101
+ async run(store) {
102
+ const key = keyOf('skew');
103
+ for (let index = 0; index < 4; index += 1) await store.take(key, FOUR, 1, NOW + 10_000);
104
+ expect((await store.take(key, FOUR, 1, NOW)).allowed).toBe(false);
105
+ },
106
+ },
107
+ {
108
+ name: 'a five-per-week bucket is still spent a day and a fifth later',
109
+ async run(store) {
110
+ const key = keyOf('weekly');
111
+ const weekly: Bucket = { capacity: 5, refillPerSecond: 5 / (7 * 86_400) };
112
+ await spend(store, key, weekly, 5);
113
+ expect((await store.take(key, weekly, 1, NOW + 1.2 * 86_400_000)).allowed).toBe(false);
114
+ },
115
+ },
116
+ {
117
+ name: 'peek answers full for a key never taken, and refill-applied after a spend',
118
+ async run(store) {
119
+ const key = keyOf('peek');
120
+ expect(await store.peek(key, FOUR, NOW)).toEqual({ remaining: 4, retryAfterSeconds: 0 });
121
+ await spend(store, key, FOUR, 4);
122
+ expect(await store.peek(key, FOUR, NOW)).toEqual({ remaining: 0, retryAfterSeconds: 1 });
123
+ expect(await store.peek(key, FOUR, NOW + 2_500)).toEqual({
124
+ remaining: 2,
125
+ retryAfterSeconds: 0,
126
+ });
127
+ },
128
+ },
129
+ {
130
+ name: 'peek spends nothing: a full allowance is still there after many peeks',
131
+ async run(store) {
132
+ const key = keyOf('peek-free');
133
+ for (let index = 0; index < 6; index += 1) await store.peek(key, FOUR, NOW);
134
+ await store.take(key, FOUR, 1, NOW);
135
+ for (let index = 0; index < 6; index += 1) await store.peek(key, FOUR, NOW);
136
+ expect(await store.take(key, FOUR, 3, NOW)).toMatchObject({ allowed: true, remaining: 0 });
137
+ },
138
+ },
139
+ {
140
+ name: 'reset empties one key and leaves every other key spent',
141
+ async run(store) {
142
+ const [target, other] = [keyOf('reset'), keyOf('untouched')];
143
+ await spend(store, target, FOUR, 4);
144
+ await spend(store, other, FOUR, 4);
145
+ await store.reset(target);
146
+ expect((await store.take(target, FOUR, 1, NOW)).remaining).toBe(3);
147
+ // A reset that refilled a neighbour is one caller handing every other caller an allowance.
148
+ expect((await store.take(other, FOUR, 1, NOW)).allowed).toBe(false);
149
+ },
150
+ },
151
+ {
152
+ name: 'one key spent is no other key spent',
153
+ async run(store) {
154
+ const [first, second] = [keyOf('one'), keyOf('two')];
155
+ await spend(store, first, FOUR, 4);
156
+ expect((await store.take(second, FOUR, 1, NOW)).remaining).toBe(3);
157
+ },
158
+ },
159
+ {
160
+ name: `${RACERS} concurrent takes against a bucket of four admit exactly four`,
161
+ async run(store) {
162
+ const key = keyOf('race');
163
+ const decisions = await Promise.all(
164
+ Array.from({ length: RACERS }, () => store.take(key, FOUR, 1, NOW)),
165
+ );
166
+ expect(decisions.filter((decision) => decision.allowed)).toHaveLength(4);
167
+ },
168
+ },
169
+ ];
170
+
171
+ export const rateLimitStoreConformance: SharedExamples<RateLimitStore | Promise<RateLimitStore>> =
172
+ sharedExamples('a rate-limit store', (subject) => {
173
+ for (const check of RATE_LIMIT_STORE_CHECKS) {
174
+ test(check.name, async () => {
175
+ await check.run(await subject());
176
+ });
177
+ }
178
+ });
@@ -31,9 +31,16 @@ const FILE_PLACEHOLDER = '<the file the cause names>';
31
31
  */
32
32
  const DOUBLE_QUOTE_LIVE = /[$`!]/;
33
33
 
34
+ /**
35
+ * A relative path gets `./`: to `bun test` a bare one is a SUBSTRING filter, so
36
+ * `packages/cli/x.test.ts` also ran `examples/dummy/packages/cli/x.test.ts` under the wrong config.
37
+ */
38
+ const anchored = (file: string): string =>
39
+ file.startsWith('/') || file.startsWith('./') || file.startsWith('../') ? file : `./${file}`;
40
+
34
41
  const rerunArg = (file: string): string =>
35
42
  isFixShellSafe(file) || !DOUBLE_QUOTE_LIVE.test(file)
36
- ? renderFixLiteral(file, FILE_PLACEHOLDER)
43
+ ? renderFixLiteral(anchored(file), FILE_PLACEHOLDER)
37
44
  : `'${FILE_PLACEHOLDER}'`;
38
45
 
39
46
  /** Every leaked file as `bun test` arguments — one screen, so the fix-shell-arg guard can see it. */
@@ -59,6 +59,9 @@ async function callAt(
59
59
  options: ScaleSweepOptions,
60
60
  self: boolean,
61
61
  ): Promise<Call> {
62
+ // Before the setup: an override's input binds on the queue this call runs on, so a job it
63
+ // enqueues is there when the call looks it up by id.
64
+ await using jobs = await testJobs();
62
65
  let prepared: Awaited<ReturnType<ScaleSetup>>;
63
66
  try {
64
67
  prepared = await options.setup(subject, graph, tenant, self);
@@ -68,7 +71,6 @@ async function callAt(
68
71
  return { failed: `its exercise.ts input threw: ${renderThrowable(error)}` };
69
72
  }
70
73
  if (prepared === undefined) return { failed: 'no actor its policy permits' };
71
- await using jobs = await testJobs();
72
74
  options.room.begin();
73
75
  try {
74
76
  const answer = await subject.call(prepared.input, prepared.actor, tenant, jobs);
@@ -0,0 +1,97 @@
1
+ // The frame and keyboard half of the `ScrapeDriver` contract. A frame target is built by spreading
2
+ // the parent's, so a verb nobody overrode acts on the PARENT document — an iframe'd SSO login
3
+ // typing a password into the page around it. Lifted from
4
+ // `scraping/src/driver-parity-{frames,keys}.test.ts`.
5
+
6
+ import { expect } from 'bun:test';
7
+ import type { ScrapeDriverCheck } from './scrape-driver-conformance-page-checks';
8
+ import { codeOf, SCRAPE_CONFORMANCE_URLS } from './scrape-driver-conformance-site';
9
+
10
+ export const SCRAPE_FRAME_CHECKS: readonly ScrapeDriverCheck[] = [
11
+ {
12
+ name: 'a frame reads its OWN document, through values and query alike',
13
+ async run({ page }) {
14
+ await page.goto(SCRAPE_CONFORMANCE_URLS.login);
15
+ expect((await page.frame('idp').values('#q'))[0]?.value).toBe('frame-value');
16
+ expect((await page.frame('idp').query('#q'))[0]?.value).toBe('frame-value');
17
+ expect((await page.query('#q'))[0]?.value).toBe('page-value');
18
+ },
19
+ },
20
+ {
21
+ name: 'fill inside a frame REPLACES the frame field and leaves the parent untouched',
22
+ async run({ page }) {
23
+ await page.goto(SCRAPE_CONFORMANCE_URLS.login);
24
+ await page.frame('idp').fill('#q', 'typed');
25
+ expect((await page.frame('idp').values('#q'))[0]?.value).toBe('typed');
26
+ expect((await page.frame('idp').query('#q'))[0]?.value).toBe('typed');
27
+ expect((await page.values('#q'))[0]?.value).toBe('page-value');
28
+ },
29
+ },
30
+ {
31
+ name: 'type inside a frame appends to the frame value, never the parent`s',
32
+ async run({ page }) {
33
+ await page.goto(SCRAPE_CONFORMANCE_URLS.login);
34
+ await page.frame('idp').type('#q', '-more');
35
+ expect((await page.frame('idp').values('#q'))[0]?.value).toBe('frame-value-more');
36
+ expect((await page.values('#q'))[0]?.value).toBe('page-value');
37
+ },
38
+ },
39
+ {
40
+ name: 'select inside a frame sets the frame control, never the parent`s',
41
+ async run({ page }) {
42
+ await page.goto(SCRAPE_CONFORMANCE_URLS.login);
43
+ await page.frame('idp').select('#s', ['f1']);
44
+ expect((await page.frame('idp').values('#s'))[0]?.value).toBe('f1');
45
+ expect((await page.values('#s'))[0]?.value).toBe('');
46
+ },
47
+ },
48
+ {
49
+ name: 'a click inside a frame leaves the PARENT where it was',
50
+ async run({ page }) {
51
+ await page.goto(SCRAPE_CONFORMANCE_URLS.login);
52
+ await page.frame('idp').click('#go');
53
+ expect(page.url()).toBe(SCRAPE_CONFORMANCE_URLS.login);
54
+ expect((await page.values('#q'))[0]?.value).toBe('page-value');
55
+ },
56
+ },
57
+ {
58
+ name: 'focus resolves on a present control and is X_SCRAPE_SELECTOR_MISSING on a missing one',
59
+ async run({ page }) {
60
+ await page.goto(SCRAPE_CONFORMANCE_URLS.search);
61
+ expect(await page.focus('#q')).toBeUndefined();
62
+ expect(await codeOf(() => page.focus('#nothing', { timeout: 50 }))).toBe(
63
+ 'X_SCRAPE_SELECTOR_MISSING',
64
+ );
65
+ },
66
+ },
67
+ {
68
+ name: "a well-formed chord resolves; a bad one is X_SCRAPE_KEY_INVALID — 'Ctrl' is no modifier",
69
+ async run({ page }) {
70
+ await page.goto(SCRAPE_CONFORMANCE_URLS.search);
71
+ expect(await codeOf(() => page.press('Meta+K'))).toBeUndefined();
72
+ expect(await codeOf(() => page.press('Enter'))).toBeUndefined();
73
+ expect(await codeOf(() => page.press('Ctrl+K'))).toBe('X_SCRAPE_KEY_INVALID');
74
+ expect(await codeOf(() => page.press('Shift+'))).toBe('X_SCRAPE_KEY_INVALID');
75
+ },
76
+ },
77
+ {
78
+ name: 'accessibility() answers the computed role and name, or is refused X_NOT_IMPLEMENTED',
79
+ async run({ page }) {
80
+ await page.goto(SCRAPE_CONFORMANCE_URLS.search);
81
+ // Screened before any driver is asked, whichever kind this is.
82
+ expect(await codeOf(() => page.accessibility('#go', { max: Number.NaN }))).toBe(
83
+ 'X_INVARIANT',
84
+ );
85
+ expect(await codeOf(() => page.accessibility('#go', { max: 0 }))).toBe('X_INVARIANT');
86
+ const refused = await codeOf(() => page.accessibility('#go'));
87
+ if (refused !== undefined) {
88
+ // An offline driver refuses rather than reading `role=` off the markup and inventing one.
89
+ expect(refused).toBe('X_NOT_IMPLEMENTED');
90
+ return;
91
+ }
92
+ expect(await page.accessibility('#go')).toMatchObject([
93
+ { role: 'button', name: 'Run search' },
94
+ ]);
95
+ },
96
+ },
97
+ ];
@@ -0,0 +1,114 @@
1
+ // The page half of the `ScrapeDriver` contract: the read vocabulary, clicks, the allow list on both
2
+ // legs, and the members an offline driver must REFUSE by name rather than fake. Lifted from
3
+ // `scraping/src/driver-parity.test.ts`, where each held only for the three drivers that file named.
4
+
5
+ import { expect } from 'bun:test';
6
+ import type { ScrapeSession } from '@ultimat3/scraping';
7
+ import { codeOf, SCRAPE_CONFORMANCE_URLS } from './scrape-driver-conformance-site';
8
+
9
+ export interface ScrapeDriverCheck {
10
+ readonly name: string;
11
+ run(session: ScrapeSession): Promise<void>;
12
+ }
13
+
14
+ export const SCRAPE_PAGE_CHECKS: readonly ScrapeDriverCheck[] = [
15
+ {
16
+ name: 'text, values and count read the document',
17
+ async run({ page }) {
18
+ await page.goto(SCRAPE_CONFORMANCE_URLS.orders);
19
+ expect(await page.text('#title')).toBe('Orders');
20
+ expect(await page.count('.row')).toBe(2);
21
+ const rows = await page.values('.row');
22
+ expect(rows.map((row) => [row.text, row.attrs['data-id']])).toEqual([
23
+ ['One', '1'],
24
+ ['Two', '2'],
25
+ ]);
26
+ },
27
+ },
28
+ {
29
+ name: 'query answers every match with visible and enabled; no match is [], never a refusal',
30
+ async run({ page }) {
31
+ await page.goto(SCRAPE_CONFORMANCE_URLS.orders);
32
+ expect((await page.query('.row')).map((row) => row.attrs['data-id'])).toEqual(['1', '2']);
33
+ expect((await page.query('#pay'))[0]?.enabled).toBe(false);
34
+ expect((await page.query('#hidden'))[0]?.visible).toBe(false);
35
+ expect((await page.query('#title'))[0]?.visible).toBe(true);
36
+ expect(await page.query('#nothing')).toEqual([]);
37
+ },
38
+ },
39
+ {
40
+ name: 'a click that navigates lands on the next page, and url() follows it',
41
+ async run({ page }) {
42
+ await page.goto(SCRAPE_CONFORMANCE_URLS.orders);
43
+ expect(page.url()).toBe(SCRAPE_CONFORMANCE_URLS.orders);
44
+ // Read again after an operation that navigates nothing: a url() refreshed only by a wait.
45
+ expect(await page.text('#title')).toBe('Orders');
46
+ expect(page.url()).toBe(SCRAPE_CONFORMANCE_URLS.orders);
47
+ await page.click('#next');
48
+ expect([page.url(), await page.text('#title')]).toEqual([
49
+ SCRAPE_CONFORMANCE_URLS.second,
50
+ 'Page two',
51
+ ]);
52
+ },
53
+ },
54
+ {
55
+ name: 'a multi-match selector clicks the FIRST match',
56
+ async run({ page }) {
57
+ await page.goto(SCRAPE_CONFORMANCE_URLS.orders);
58
+ // The second `.go` points at a page the site does not have.
59
+ await page.click('.go');
60
+ expect(await page.text('#title')).toBe('Page two');
61
+ },
62
+ },
63
+ {
64
+ name: 'a disabled control is X_SCRAPE_NOT_ACTIONABLE; a missing one X_SCRAPE_SELECTOR_MISSING',
65
+ async run({ page }) {
66
+ await page.goto(SCRAPE_CONFORMANCE_URLS.orders);
67
+ expect(await codeOf(() => page.click('#pay', { timeout: 50 }))).toBe(
68
+ 'X_SCRAPE_NOT_ACTIONABLE',
69
+ );
70
+ expect(await codeOf(() => page.waitFor('#nothing', { timeout: 50 }))).toBe(
71
+ 'X_SCRAPE_SELECTOR_MISSING',
72
+ );
73
+ },
74
+ },
75
+ {
76
+ name: 'a host the allow list does not name is X_SCRAPE_HOST_BLOCKED on BOTH legs',
77
+ async run({ page, http }) {
78
+ expect(await codeOf(() => page.goto('https://evil.test/'))).toBe('X_SCRAPE_HOST_BLOCKED');
79
+ expect(await codeOf(() => http.request('https://evil.test/api'))).toBe(
80
+ 'X_SCRAPE_HOST_BLOCKED',
81
+ );
82
+ },
83
+ },
84
+ {
85
+ name: 'the HTTP leg answers the site`s endpoint in the same session as the page',
86
+ async run({ page, http }) {
87
+ await page.goto(SCRAPE_CONFORMANCE_URLS.orders);
88
+ const response = await http.request(SCRAPE_CONFORMANCE_URLS.api);
89
+ expect(response.status).toBe(200);
90
+ expect(await response.json()).toEqual({ orders: [{ id: 1 }] });
91
+ },
92
+ },
93
+ {
94
+ name: 'pageErrors() answers, empty on a page that threw nothing',
95
+ async run({ page }) {
96
+ await page.goto(SCRAPE_CONFORMANCE_URLS.orders);
97
+ expect([page.pageErrors(), page.pageErrorsDropped()]).toEqual([[], 0]);
98
+ },
99
+ },
100
+ {
101
+ name: 'offline(true) cuts the browser off or is refused X_NOT_IMPLEMENTED — never a no-op',
102
+ async run({ page }) {
103
+ await page.goto(SCRAPE_CONFORMANCE_URLS.orders);
104
+ const refused = await codeOf(() => page.offline(true));
105
+ if (refused !== undefined) {
106
+ expect(refused).toBe('X_NOT_IMPLEMENTED');
107
+ return;
108
+ }
109
+ // Set: then the browser must actually fail a navigation, or a test proved nothing offline.
110
+ expect(await codeOf(() => page.goto(SCRAPE_CONFORMANCE_URLS.second))).not.toBeUndefined();
111
+ await page.offline(false);
112
+ },
113
+ },
114
+ ];
@@ -0,0 +1,84 @@
1
+ // The one small site every `ScrapeDriver` check reads: pages, an iframe'd login whose frame
2
+ // carries the SAME ids as its parent (the only shape in which a frame verb aimed at the parent is
3
+ // visible), and one JSON endpoint behind them. A subject's driver must serve exactly these — an
4
+ // offline driver from recordings of them, a real browser from a server answering them.
5
+
6
+ import type { Logger } from '@ultimat3/core';
7
+ import { frozenClock, structuredLogger } from '@ultimat3/core';
8
+ import type { HttpRecording, PageRecording, ScrapeDriver, ScrapeSession } from '@ultimat3/scraping';
9
+
10
+ export const SCRAPE_CONFORMANCE_HOST = 'shop.test';
11
+ const ORIGIN = `https://${SCRAPE_CONFORMANCE_HOST}`;
12
+
13
+ export const SCRAPE_CONFORMANCE_URLS = {
14
+ orders: `${ORIGIN}/orders`,
15
+ second: `${ORIGIN}/orders/2`,
16
+ login: `${ORIGIN}/login`,
17
+ search: `${ORIGIN}/search`,
18
+ api: `${ORIGIN}/api/orders?page=1`,
19
+ } as const;
20
+
21
+ const ORDERS = `<html><body>
22
+ <h1 id="title">Orders</h1>
23
+ <ul><li class="row" data-id="1">One</li><li class="row" data-id="2">Two</li></ul>
24
+ <a id="next" href="/orders/2">Next</a>
25
+ <a class="go" href="/orders/2">Go first</a>
26
+ <a class="go" href="/orders/nowhere">Go second</a>
27
+ <button id="pay" disabled>Pay</button>
28
+ <p id="hidden" style="display:none">gone</p>
29
+ <img src="https://tracker.test/pixel.gif">
30
+ </body></html>`;
31
+
32
+ const LOGIN = `<html><body>
33
+ <input id="q" name="q" value="page-value">
34
+ <select id="s"><option value="p1">P1</option></select>
35
+ <a id="go" href="/orders/2">Leave</a>
36
+ <iframe name="idp" src="/idp"></iframe>
37
+ </body></html>`;
38
+
39
+ /** `#go` here does NOT navigate: a frame click resolved against the parent moves the whole page. */
40
+ const IDP =
41
+ '<input id="q" name="q" value="frame-value"><select id="s"><option value="f1">F1</option></select><button id="go">Stay</button>';
42
+
43
+ const SEARCH = `<html><body>
44
+ <input id="q" name="q" placeholder="Search">
45
+ <button id="go" aria-label="Run search">Go</button>
46
+ </body></html>`;
47
+
48
+ export const SCRAPE_CONFORMANCE_PAGES: readonly PageRecording[] = [
49
+ { url: SCRAPE_CONFORMANCE_URLS.orders, html: ORDERS },
50
+ {
51
+ url: SCRAPE_CONFORMANCE_URLS.second,
52
+ html: '<html><body><h1 id="title">Page two</h1></body></html>',
53
+ },
54
+ { url: SCRAPE_CONFORMANCE_URLS.login, html: LOGIN, frames: { idp: IDP } },
55
+ { url: SCRAPE_CONFORMANCE_URLS.search, html: SEARCH },
56
+ ];
57
+
58
+ export const SCRAPE_CONFORMANCE_HTTP: readonly HttpRecording[] = [
59
+ { url: SCRAPE_CONFORMANCE_URLS.api, method: 'GET', status: 200, body: '{"orders":[{"id":1}]}' },
60
+ ];
61
+
62
+ const silent: Logger = structuredLogger({ writer: () => undefined });
63
+
64
+ /** A session on `driver` the way a scrape opens one: this site allowed, images blocked. */
65
+ export const openOn = (driver: ScrapeDriver): Promise<ScrapeSession> =>
66
+ driver.open({
67
+ name: 'conformance',
68
+ logger: silent,
69
+ rules: { allowHosts: [SCRAPE_CONFORMANCE_HOST], block: ['image'] },
70
+ clock: frozenClock(),
71
+ timeoutMs: 5_000,
72
+ });
73
+
74
+ /** The code a call failed with, or `undefined` when it answered. */
75
+ export async function codeOf(run: () => Promise<unknown>): Promise<string | undefined> {
76
+ try {
77
+ await run();
78
+ return undefined;
79
+ } catch (thrown) {
80
+ const code: unknown =
81
+ typeof thrown === 'object' && thrown !== null ? Reflect.get(thrown, 'code') : undefined;
82
+ return typeof code === 'string' ? code : `uncoded: ${String(thrown)}`;
83
+ }
84
+ }
@@ -0,0 +1,34 @@
1
+ // The `scrapeDriver` port's contract as shared examples: `behavesLike(scrapeDriverConformance, ()
2
+ // => driver)` — a driver serving `SCRAPE_CONFORMANCE_PAGES` and `SCRAPE_CONFORMANCE_HTTP`. Each
3
+ // check opens its own session the way a scrape does, and closes it whatever happens. Where drivers
4
+ // genuinely cannot agree, a check accepts the answer OR the coded refusal.
5
+
6
+ import { test } from 'bun:test';
7
+ import type { ScrapeDriver } from '@ultimat3/scraping';
8
+ import { SCRAPE_FRAME_CHECKS } from './scrape-driver-conformance-frame-checks';
9
+ import type { ScrapeDriverCheck } from './scrape-driver-conformance-page-checks';
10
+ import { SCRAPE_PAGE_CHECKS } from './scrape-driver-conformance-page-checks';
11
+ import { openOn } from './scrape-driver-conformance-site';
12
+ import type { SharedExamples } from './shared-examples';
13
+ import { sharedExamples } from './shared-examples';
14
+
15
+ export const SCRAPE_DRIVER_CHECKS: readonly ScrapeDriverCheck[] = [
16
+ ...SCRAPE_PAGE_CHECKS,
17
+ ...SCRAPE_FRAME_CHECKS,
18
+ ];
19
+
20
+ export const scrapeDriverConformance: SharedExamples<ScrapeDriver> = sharedExamples(
21
+ 'a scrape driver',
22
+ (subject) => {
23
+ for (const check of SCRAPE_DRIVER_CHECKS) {
24
+ test(check.name, async () => {
25
+ const session = await openOn(subject());
26
+ try {
27
+ await check.run(session);
28
+ } finally {
29
+ await session.close();
30
+ }
31
+ });
32
+ }
33
+ },
34
+ );
@@ -0,0 +1,56 @@
1
+ // The `Resolver` half of the sealed resolver: an instance asks its own c-ares channel, never
2
+ // `Bun.dns`, so each class is sealed on its prototype (a named import shares it). A sealed instance
3
+ // answers exactly what the module function of the same name answers — that one is sealed at the root.
4
+
5
+ // why: `node:dns`'s two `Resolver` classes are what an app's own resolver instance is built from.
6
+ import nodeDns from 'node:dns';
7
+ import type { DnsGate } from './sealed-dns';
8
+
9
+ /** Each `Resolver` method and the query its arguments ask; `resolve` names its record type. */
10
+ const RESOLVER_QUERIES: Readonly<Record<string, (args: readonly unknown[]) => string>> = {
11
+ resolve: (args) => (typeof args[1] === 'string' ? args[1].toUpperCase() : 'A'),
12
+ resolve4: () => 'A',
13
+ resolve6: () => 'AAAA',
14
+ resolveAny: () => 'ANY',
15
+ resolveCaa: () => 'CAA',
16
+ resolveCname: () => 'CNAME',
17
+ resolveMx: () => 'MX',
18
+ resolveNaptr: () => 'NAPTR',
19
+ resolveNs: () => 'NS',
20
+ resolvePtr: () => 'PTR',
21
+ resolveSoa: () => 'SOA',
22
+ resolveSrv: () => 'SRV',
23
+ resolveTxt: () => 'TXT',
24
+ reverse: () => 'REVERSE',
25
+ };
26
+
27
+ type Method = (this: unknown, ...args: unknown[]) => unknown;
28
+
29
+ /** Seal one class's prototype, answering through `moduleApi`; returns the uninstaller. */
30
+ const sealPrototype = (prototype: object, moduleApi: object, gate: DnsGate): (() => void) => {
31
+ const originals = new Map<string, Method>();
32
+ for (const [name, queryOf] of Object.entries(RESOLVER_QUERIES)) {
33
+ const original = Reflect.get(prototype, name) as Method;
34
+ const viaModule = Reflect.get(moduleApi, name) as Method;
35
+ originals.set(name, original);
36
+ // A `function`, not an arrow: the real method needs the instance its own servers live on.
37
+ Reflect.set(prototype, name, function sealed(this: unknown, ...args: unknown[]): unknown {
38
+ const verdict = gate(String(args[0]), queryOf(args));
39
+ if ('real' in verdict) return Reflect.apply(original, this, args);
40
+ return Reflect.apply(viaModule, moduleApi, args);
41
+ });
42
+ }
43
+ return () => {
44
+ for (const [name, original] of originals) Reflect.set(prototype, name, original);
45
+ };
46
+ };
47
+
48
+ /** Seal both `Resolver` classes — callback and promise — and return the one uninstaller. */
49
+ export function installResolverSeal(gate: DnsGate): () => void {
50
+ const restoreCallback = sealPrototype(nodeDns.Resolver.prototype, nodeDns, gate);
51
+ const restorePromise = sealPrototype(nodeDns.promises.Resolver.prototype, nodeDns.promises, gate);
52
+ return () => {
53
+ restoreCallback();
54
+ restorePromise();
55
+ };
56
+ }