@ultimat3/testing 30.9.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 (148) hide show
  1. package/CLAUDE.md +46 -55
  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 +20 -2
  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-line.ts +98 -0
  55. package/src/exercise-report.ts +30 -51
  56. package/src/exercise.ts +12 -6
  57. package/src/factories.ts +192 -179
  58. package/src/factory-api.ts +131 -0
  59. package/src/factory-associations.ts +67 -0
  60. package/src/factory-errors.ts +38 -0
  61. package/src/factory-lint.ts +92 -0
  62. package/src/factory-persist.ts +74 -16
  63. package/src/factory-pipeline.ts +191 -0
  64. package/src/factory-registry.ts +2 -1
  65. package/src/factory-types.ts +131 -0
  66. package/src/failure-store-conformance.ts +137 -0
  67. package/src/fixture-jobs.ts +27 -11
  68. package/src/fixture-mail.ts +7 -7
  69. package/src/fixture-seed.ts +111 -0
  70. package/src/fixture-statements.ts +68 -52
  71. package/src/framework-fixtures.ts +1 -1
  72. package/src/idempotency-store-conformance.ts +186 -0
  73. package/src/idempotency-store-replay-conformance.ts +140 -0
  74. package/src/idempotency-store-tx-conformance.ts +132 -0
  75. package/src/idempotency-store-world-conformance.ts +82 -0
  76. package/src/identity-provider-conformance.ts +144 -0
  77. package/src/image-transform-conformance.ts +89 -0
  78. package/src/index.ts +68 -157
  79. package/src/isr-store-conformance.ts +113 -0
  80. package/src/job-sim-effects.ts +19 -31
  81. package/src/job-sim-world.ts +6 -15
  82. package/src/lease-store-conformance.ts +195 -0
  83. package/src/mail-driver-conformance.ts +108 -0
  84. package/src/matcher-doubles.ts +104 -0
  85. package/src/matcher-stub.ts +78 -0
  86. package/src/matcher-surface.ts +21 -0
  87. package/src/matchers.ts +19 -1
  88. package/src/mcp-confirmation-store-conformance.ts +262 -0
  89. package/src/meter-store-conformance.ts +135 -0
  90. package/src/notify-digest-conformance.ts +133 -0
  91. package/src/notify-inbox-conformance.ts +130 -0
  92. package/src/notify-ledger-conformance.ts +134 -0
  93. package/src/notify-preference-conformance.ts +49 -0
  94. package/src/notify-stores-conformance.ts +73 -0
  95. package/src/outbox-store-conformance-claims.ts +112 -0
  96. package/src/outbox-store-conformance-subject.ts +65 -0
  97. package/src/outbox-store-conformance.ts +71 -0
  98. package/src/per-test-reset.ts +55 -20
  99. package/src/preload.ts +4 -2
  100. package/src/purge-conformance.ts +79 -0
  101. package/src/push-device-conformance.ts +124 -0
  102. package/src/push-subscription-conformance.ts +123 -0
  103. package/src/quiet-logs.ts +4 -4
  104. package/src/rate-limit-store-conformance.ts +178 -0
  105. package/src/registry-leak-error.ts +8 -1
  106. package/src/scrape-driver-conformance-frame-checks.ts +97 -0
  107. package/src/scrape-driver-conformance-page-checks.ts +114 -0
  108. package/src/scrape-driver-conformance-site.ts +84 -0
  109. package/src/scrape-driver-conformance.ts +34 -0
  110. package/src/search-index-conformance-list.ts +76 -0
  111. package/src/search-index-conformance-support.ts +69 -0
  112. package/src/search-index-conformance-versions.ts +66 -0
  113. package/src/search-index-conformance.ts +126 -0
  114. package/src/secret-source-conformance.ts +91 -0
  115. package/src/seed-factories.ts +57 -0
  116. package/src/seed-writer.ts +8 -19
  117. package/src/semantic-cache-factory-conformance.ts +86 -0
  118. package/src/sms-transport-conformance.ts +102 -0
  119. package/src/step-store-conformance.ts +171 -0
  120. package/src/storage-conformance-read-checks.ts +132 -0
  121. package/src/storage-conformance-shape.ts +121 -0
  122. package/src/storage-conformance-stream-checks.ts +226 -0
  123. package/src/storage-conformance.ts +34 -0
  124. package/src/stub-action.ts +24 -0
  125. package/src/stub-errors.ts +86 -0
  126. package/src/stub-job.ts +30 -0
  127. package/src/stub-query.ts +25 -0
  128. package/src/stub-script.ts +76 -0
  129. package/src/stub-stack.ts +29 -0
  130. package/src/stub.ts +73 -0
  131. package/src/swap-adapter.ts +135 -0
  132. package/src/test-scope.ts +96 -0
  133. package/src/test-types.ts +1 -1
  134. package/src/totp-replay-guard-conformance.ts +76 -0
  135. package/src/transport-conformance.ts +179 -0
  136. package/src/upload-scanner-conformance.ts +112 -0
  137. package/src/vector-store-conformance-checks.ts +174 -0
  138. package/src/vector-store-conformance.ts +21 -0
  139. package/src/cdp-connection.ts +0 -291
  140. package/src/cdp-errors.ts +0 -179
  141. package/src/cdp-launch-attempt.ts +0 -224
  142. package/src/cdp-launch-candidates.ts +0 -62
  143. package/src/cdp-launch-profile.ts +0 -106
  144. package/src/cdp-launch-reap.ts +0 -68
  145. package/src/cdp-launch-wire.ts +0 -171
  146. package/src/cdp-launch.ts +0 -175
  147. package/src/cdp-pipe.ts +0 -77
  148. package/src/cdp-wire-watch.ts +0 -113
@@ -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,76 @@
1
+ // The search index's `list` contract — what `reindex`'s sweep reads to find documents whose row is
2
+ // gone: every live document of one tenant, paged without a repeat or a gap, never a removed id and
3
+ // never another tenant's; every tenant's when the query names none.
4
+
5
+ import { expect } from 'bun:test';
6
+ import type { SearchListEntry, SearchListQuery, SearchScope } from '@ultimat3/entity/adapter';
7
+ import type { SearchIndexCheck, SearchIndexSubject } from './search-index-conformance-support';
8
+ import {
9
+ searchDoc as doc,
10
+ putSearchDocs,
11
+ SEARCH_TENANT_A,
12
+ SEARCH_TENANT_B,
13
+ searchDocId,
14
+ } from './search-index-conformance-support';
15
+
16
+ /** Every page of `query`, followed to its end; a page past the entries there are is a failure. */
17
+ export const listAll = async (
18
+ subject: SearchIndexSubject,
19
+ query: Omit<SearchListQuery, 'after' | 'entity'>,
20
+ ): Promise<readonly SearchListEntry[]> => {
21
+ const entries: SearchListEntry[] = [];
22
+ let after: string | null = null;
23
+ for (let pages = 0; pages < 20; pages += 1) {
24
+ const page = await subject.index.list({ ...query, entity: subject.entity, after });
25
+ expect(page.entries.length).toBeLessThanOrEqual(query.limit);
26
+ entries.push(...page.entries);
27
+ if (page.next === null) return entries;
28
+ after = page.next;
29
+ }
30
+ return expect.unreachable(`list never ended: ${entries.length} entries in 20 pages`);
31
+ };
32
+
33
+ const ids = (entries: readonly SearchListEntry[]) => entries.map((entry) => entry.id).sort();
34
+
35
+ export const SEARCH_INDEX_LIST_CHECKS: readonly SearchIndexCheck[] = [
36
+ {
37
+ name: "list pages one tenant's live documents: each once, none removed, none of another tenant",
38
+ async run(subject) {
39
+ await putSearchDocs(subject, [
40
+ doc(subject.entity, 1, 'zebra'),
41
+ doc(subject.entity, 2, 'quokka'),
42
+ doc(subject.entity, 3, 'lantern'),
43
+ doc(subject.entity, 4, 'harbour', { tenant: SEARCH_TENANT_B }),
44
+ ]);
45
+ const own: SearchScope = { entity: subject.entity, tenant: SEARCH_TENANT_A };
46
+ await subject.drop(own, [searchDocId(2)]);
47
+ await subject.index.remove(own, [searchDocId(2)], 2);
48
+ const listed = await listAll(subject, { tenant: SEARCH_TENANT_A, limit: 1 });
49
+ expect(ids(listed)).toEqual([searchDocId(1), searchDocId(3)]);
50
+ expect(listed.length).toBe(2);
51
+ for (const entry of listed) {
52
+ expect(entry.tenant).toBe(SEARCH_TENANT_A);
53
+ expect(Number.isFinite(entry.version)).toBe(true);
54
+ }
55
+ const other = await listAll(subject, { tenant: SEARCH_TENANT_B, limit: 10 });
56
+ expect(other.map((entry) => [entry.tenant, entry.id])).toEqual([
57
+ [SEARCH_TENANT_B, searchDocId(4)],
58
+ ]);
59
+ },
60
+ },
61
+ {
62
+ name: "list naming no tenant pages every tenant's documents, each with its own tenant",
63
+ async run(subject) {
64
+ await putSearchDocs(subject, [
65
+ doc(subject.entity, 1, 'zebra'),
66
+ doc(subject.entity, 2, 'zebra', { tenant: SEARCH_TENANT_B }),
67
+ ]);
68
+ const every = subject.everyTenant ?? ((read) => read());
69
+ const listed = await every(() => listAll(subject, { limit: 1 }));
70
+ expect(listed.map((entry) => [entry.tenant, entry.id]).sort()).toEqual([
71
+ [SEARCH_TENANT_A, searchDocId(1)],
72
+ [SEARCH_TENANT_B, searchDocId(2)],
73
+ ]);
74
+ },
75
+ },
76
+ ];
@@ -0,0 +1,69 @@
1
+ // What every search-index check shares: the subject, the suite's ids and tenants, one document
2
+ // builder (version 1 unless a check says otherwise), stored-then-indexed, and the sorted hit ids.
3
+
4
+ import { finiteOption } from '@ultimat3/core';
5
+ import type { SearchDocument, SearchIndex, SearchScope } from '@ultimat3/entity/adapter';
6
+
7
+ export interface SearchIndexSubject {
8
+ readonly index: SearchIndex;
9
+ /** A searchable entity with `title` and `body` searchable columns and a tenant column. */
10
+ readonly entity: string;
11
+ /** Writes (upserts) each document's source row; a no-op for an index holding its own copy. */
12
+ store(documents: readonly SearchDocument[]): Promise<void>;
13
+ /** Deletes the source rows of `ids` under `scope`; a no-op for an index holding its own copy. */
14
+ drop(scope: SearchScope, ids: readonly string[]): Promise<void>;
15
+ /**
16
+ * Runs a read that names no tenant — `list` across every tenant, as `reindex` sweeps. Absent, it
17
+ * runs as is; the built-in Postgres index needs the `crossTenant` capability around it.
18
+ */
19
+ everyTenant?<T>(read: () => Promise<T>): Promise<T>;
20
+ }
21
+
22
+ export interface SearchIndexCheck {
23
+ readonly name: string;
24
+ run(subject: SearchIndexSubject): Promise<void>;
25
+ }
26
+
27
+ export const SEARCH_TENANT_A = '00000000-0000-7000-8000-00000000a0a0';
28
+ export const SEARCH_TENANT_B = '00000000-0000-7000-8000-00000000b0b0';
29
+
30
+ /** A uuid-shaped id, so a uuid-keyed table holds the suite's rows. */
31
+ export const searchDocId = (n: number): string =>
32
+ `00000000-0000-7000-8000-${String(n).padStart(12, '0')}`;
33
+
34
+ export interface SearchDocOptions {
35
+ readonly body?: string;
36
+ readonly tenant?: string;
37
+ /** The row's version (its `onUpdateNow()` instant, epoch ms). Default 1. */
38
+ readonly version?: number;
39
+ }
40
+
41
+ /** One suite document: tenant A, version 1, unless the check says otherwise. */
42
+ export const searchDoc = (
43
+ entity: string,
44
+ n: number,
45
+ title: string,
46
+ options: SearchDocOptions = {},
47
+ ): SearchDocument => ({
48
+ entity,
49
+ tenant: options.tenant ?? SEARCH_TENANT_A,
50
+ id: searchDocId(n),
51
+ version: finiteOption('searchDoc', 'version', options.version ?? 1),
52
+ fields: options.body === undefined ? { title } : { title, body: options.body },
53
+ });
54
+
55
+ /** Stored then indexed — the order a write hook and `reindex` both keep. */
56
+ export const putSearchDocs = async (
57
+ subject: SearchIndexSubject,
58
+ documents: readonly SearchDocument[],
59
+ ) => {
60
+ await subject.store(documents);
61
+ await subject.index.index(documents);
62
+ };
63
+
64
+ export const searchIds = async (
65
+ subject: SearchIndexSubject,
66
+ term: string,
67
+ tenant = SEARCH_TENANT_A,
68
+ ) =>
69
+ (await subject.index.query({ entity: subject.entity, tenant, term })).map((hit) => hit.id).sort();
@@ -0,0 +1,66 @@
1
+ // The search index's ordering contract: keep only NEWER. A `reindex` batch that read a row at v1
2
+ // may call `index` after the write path indexed v2 or removed the row; the index must answer v2, or
3
+ // nothing. Each stale call is `index` ALONE, as when a batch's read raced a write, so the built-in
4
+ // Postgres index passes by construction.
5
+
6
+ // An EQUAL version is a replay: the writer, not the index, keeps versions strictly increasing per
7
+ // row (`@ultimat3/entity`'s `advanceVersion`). A `committed` document — the write path's — replaces
8
+ // a tombstone whatever its version: a key deleted and written again is live.
9
+
10
+ import { expect } from 'bun:test';
11
+ import type { SearchScope } from '@ultimat3/entity/adapter';
12
+ import type { SearchIndexCheck } from './search-index-conformance-support';
13
+ import {
14
+ searchDoc as doc,
15
+ putSearchDocs,
16
+ SEARCH_TENANT_A,
17
+ searchDocId,
18
+ searchIds,
19
+ } from './search-index-conformance-support';
20
+
21
+ export const SEARCH_INDEX_VERSION_CHECKS: readonly SearchIndexCheck[] = [
22
+ {
23
+ name: 'a committed document replaces a newer tombstone: a key deleted and written again is found',
24
+ async run(subject) {
25
+ await putSearchDocs(subject, [doc(subject.entity, 1, 'zebra', { version: 10 })]);
26
+ const own: SearchScope = { entity: subject.entity, tenant: SEARCH_TENANT_A };
27
+ await subject.drop(own, [searchDocId(1)]);
28
+ await subject.index.remove(own, [searchDocId(1)], 30);
29
+ const again = doc(subject.entity, 1, 'quokka', { version: 20 });
30
+ await subject.store([again]);
31
+ await subject.index.index([again], { committed: true });
32
+ expect(await searchIds(subject, 'quokka')).toEqual([searchDocId(1)]);
33
+ },
34
+ },
35
+ {
36
+ name: 'an older version indexed after a newer one leaves the newer one',
37
+ async run(subject) {
38
+ await putSearchDocs(subject, [doc(subject.entity, 1, 'lantern', { version: 20 })]);
39
+ await subject.index.index([doc(subject.entity, 1, 'zebra', { version: 10 })]);
40
+ expect(await searchIds(subject, 'zebra')).toEqual([]);
41
+ expect(await searchIds(subject, 'lantern')).toEqual([searchDocId(1)]);
42
+ },
43
+ },
44
+ {
45
+ name: 'a version indexed after the id was removed at a later version stays removed',
46
+ async run(subject) {
47
+ await putSearchDocs(subject, [doc(subject.entity, 1, 'zebra', { version: 10 })]);
48
+ const own: SearchScope = { entity: subject.entity, tenant: SEARCH_TENANT_A };
49
+ await subject.drop(own, [searchDocId(1)]);
50
+ await subject.index.remove(own, [searchDocId(1)], 30);
51
+ await subject.index.index([doc(subject.entity, 1, 'zebra', { version: 20 })]);
52
+ expect(await searchIds(subject, 'zebra')).toEqual([]);
53
+ },
54
+ },
55
+ {
56
+ name: 'an id written again after its removal, at a newer version, is found',
57
+ async run(subject) {
58
+ await putSearchDocs(subject, [doc(subject.entity, 1, 'zebra', { version: 10 })]);
59
+ const own: SearchScope = { entity: subject.entity, tenant: SEARCH_TENANT_A };
60
+ await subject.drop(own, [searchDocId(1)]);
61
+ await subject.index.remove(own, [searchDocId(1)], 30);
62
+ await putSearchDocs(subject, [doc(subject.entity, 1, 'quokka', { version: 40 })]);
63
+ expect(await searchIds(subject, 'quokka')).toEqual([searchDocId(1)]);
64
+ },
65
+ },
66
+ ];
@@ -0,0 +1,126 @@
1
+ // The `searchIndex` port's contract as shared examples: `behavesLike(searchIndexConformance, () =>
2
+ // ({ index, entity, store, drop }))`. `store`/`drop` write the SOURCE rows (the built-in Postgres
3
+ // index reads its table; an external one ignores them), then the suite calls `index`/`remove` as
4
+ // `reindex` and a write hook do. Words are chosen no English stemmer rewrites, so every index agrees.
5
+
6
+ import { expect, test } from 'bun:test';
7
+ import type { SearchScope } from '@ultimat3/entity/adapter';
8
+ import { SEARCH_INDEX_LIST_CHECKS } from './search-index-conformance-list';
9
+ import type { SearchIndexCheck, SearchIndexSubject } from './search-index-conformance-support';
10
+ import {
11
+ searchDoc as doc,
12
+ putSearchDocs,
13
+ SEARCH_TENANT_A,
14
+ SEARCH_TENANT_B,
15
+ searchDocId,
16
+ searchIds,
17
+ } from './search-index-conformance-support';
18
+ import { SEARCH_INDEX_VERSION_CHECKS } from './search-index-conformance-versions';
19
+ import type { SharedExamples } from './shared-examples';
20
+ import { sharedExamples } from './shared-examples';
21
+
22
+ export const SEARCH_INDEX_CHECKS: readonly SearchIndexCheck[] = [
23
+ {
24
+ name: 'an indexed document is found by a word of any field, whatever its case',
25
+ async run(subject) {
26
+ expect(subject.index.name.length).toBeGreaterThan(0);
27
+ await putSearchDocs(subject, [
28
+ doc(subject.entity, 1, 'Zebra crossing', { body: 'a lantern harbour' }),
29
+ ]);
30
+ expect(await searchIds(subject, 'zebra')).toEqual([searchDocId(1)]);
31
+ expect(await searchIds(subject, 'HARBOUR')).toEqual([searchDocId(1)]);
32
+ },
33
+ },
34
+ {
35
+ name: 'every word of the term must match',
36
+ async run(subject) {
37
+ await putSearchDocs(subject, [
38
+ doc(subject.entity, 1, 'zebra quokka'),
39
+ doc(subject.entity, 2, 'zebra lantern'),
40
+ ]);
41
+ expect(await searchIds(subject, 'zebra quokka')).toEqual([searchDocId(1)]);
42
+ expect(await searchIds(subject, 'zebra')).toEqual([searchDocId(1), searchDocId(2)]);
43
+ },
44
+ },
45
+ {
46
+ name: 'no match, an empty term and bare punctuation answer no hits, never an error',
47
+ async run(subject) {
48
+ await putSearchDocs(subject, [doc(subject.entity, 1, 'zebra')]);
49
+ for (const term of ['walrus', '', ' ', '|||', '(((']) {
50
+ expect(await searchIds(subject, term), term).toEqual([]);
51
+ }
52
+ },
53
+ },
54
+ {
55
+ name: "a tenant is never answered another tenant's document",
56
+ async run(subject) {
57
+ await putSearchDocs(subject, [
58
+ doc(subject.entity, 1, 'zebra'),
59
+ doc(subject.entity, 2, 'zebra', { tenant: SEARCH_TENANT_B }),
60
+ ]);
61
+ expect(await searchIds(subject, 'zebra')).toEqual([searchDocId(1)]);
62
+ expect(await searchIds(subject, 'zebra', SEARCH_TENANT_B)).toEqual([searchDocId(2)]);
63
+ },
64
+ },
65
+ {
66
+ name: 'indexing a newer version of an id replaces its text, and it is one hit',
67
+ async run(subject) {
68
+ await putSearchDocs(subject, [doc(subject.entity, 1, 'zebra')]);
69
+ await putSearchDocs(subject, [doc(subject.entity, 1, 'quokka zebra', { version: 2 })]);
70
+ expect(await searchIds(subject, 'zebra')).toEqual([searchDocId(1)]);
71
+ await putSearchDocs(subject, [doc(subject.entity, 1, 'lantern', { version: 3 })]);
72
+ expect(await searchIds(subject, 'zebra')).toEqual([]);
73
+ expect(await searchIds(subject, 'lantern')).toEqual([searchDocId(1)]);
74
+ },
75
+ },
76
+ {
77
+ name: "a removed id is gone, and a removal under another tenant leaves this one's",
78
+ async run(subject) {
79
+ await putSearchDocs(subject, [
80
+ doc(subject.entity, 1, 'zebra'),
81
+ doc(subject.entity, 2, 'zebra'),
82
+ ]);
83
+ const other: SearchScope = { entity: subject.entity, tenant: SEARCH_TENANT_B };
84
+ await subject.drop(other, [searchDocId(1)]);
85
+ await subject.index.remove(other, [searchDocId(1)], 2);
86
+ expect(await searchIds(subject, 'zebra')).toEqual([searchDocId(1), searchDocId(2)]);
87
+ const own: SearchScope = { entity: subject.entity, tenant: SEARCH_TENANT_A };
88
+ await subject.drop(own, [searchDocId(1)]);
89
+ await subject.index.remove(own, [searchDocId(1)], 2);
90
+ expect(await searchIds(subject, 'zebra')).toEqual([searchDocId(2)]);
91
+ },
92
+ },
93
+ {
94
+ name: 'the limit bounds the hits, each id at most once',
95
+ async run(subject) {
96
+ await putSearchDocs(
97
+ subject,
98
+ [1, 2, 3].map((n) => doc(subject.entity, n, `zebra ${'quokka '.repeat(n)}`)),
99
+ );
100
+ const hits = await subject.index.query({
101
+ entity: subject.entity,
102
+ tenant: SEARCH_TENANT_A,
103
+ term: 'zebra',
104
+ limit: 2,
105
+ });
106
+ expect(hits.length).toBe(2);
107
+ expect(new Set(hits.map((hit) => hit.id)).size).toBe(2);
108
+ },
109
+ },
110
+ ];
111
+
112
+ export const SEARCH_INDEX_ALL_CHECKS: readonly SearchIndexCheck[] = [
113
+ ...SEARCH_INDEX_CHECKS,
114
+ ...SEARCH_INDEX_VERSION_CHECKS,
115
+ ...SEARCH_INDEX_LIST_CHECKS,
116
+ ];
117
+
118
+ export const searchIndexConformance: SharedExamples<
119
+ SearchIndexSubject | Promise<SearchIndexSubject>
120
+ > = sharedExamples('a search index', (subject) => {
121
+ for (const check of SEARCH_INDEX_ALL_CHECKS) {
122
+ test(check.name, async () => {
123
+ await check.run(await subject());
124
+ });
125
+ }
126
+ });