@ultimat3/testing 30.9.0 → 31.1.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/CLAUDE.md +46 -55
- package/README.md +179 -39
- package/package.json +23 -17
- package/src/ai-conformance-answer.ts +67 -0
- package/src/ai-gateway-conformance.ts +105 -0
- package/src/ai-provider-conformance.ts +69 -0
- package/src/ai-provider-transport-conformance.ts +170 -0
- package/src/audit-sink-conformance.ts +204 -0
- package/src/auth-adapter-conformance-credentials.ts +124 -0
- package/src/auth-adapter-conformance-mfa.ts +70 -0
- package/src/auth-adapter-conformance-passkeys.ts +133 -0
- package/src/auth-adapter-conformance-sessions.ts +115 -0
- package/src/auth-adapter-conformance-support.ts +71 -0
- package/src/auth-adapter-conformance-users.ts +128 -0
- package/src/auth-adapter-conformance.ts +34 -0
- package/src/auth-limiter-factory-conformance.ts +224 -0
- package/src/auth-server-store-conformance-records.ts +69 -0
- package/src/auth-server-store-conformance.ts +208 -0
- package/src/cache-tier-conformance-checks.ts +142 -0
- package/src/cache-tier-conformance.ts +24 -0
- package/src/cassette-format.ts +8 -13
- package/src/cassette-record.ts +1 -1
- package/src/cdp-browser.ts +10 -11
- package/src/cdp-e2e-page.ts +2 -2
- package/src/cdp-e2e-session.ts +2 -2
- package/src/cdp-network-probe.ts +1 -1
- package/src/cdp-offline-script.ts +2 -2
- package/src/clock-conformance.ts +126 -0
- package/src/conformance.ts +239 -0
- package/src/e2e-browser-handle.ts +1 -2
- package/src/e2e-error-codes.ts +17 -16
- package/src/e2e-run.ts +4 -3
- package/src/embedder-conformance.ts +101 -0
- package/src/entity-conformance-aggregates.ts +210 -0
- package/src/entity-conformance-batches.ts +216 -0
- package/src/entity-conformance-bulk.ts +379 -0
- package/src/entity-conformance-checks.ts +27 -0
- package/src/entity-conformance-entities.ts +52 -0
- package/src/entity-conformance-locks.ts +286 -0
- package/src/entity-conformance-matrix.ts +397 -0
- package/src/entity-conformance-reads.ts +241 -0
- package/src/entity-conformance-support.ts +56 -0
- package/src/entity-conformance-tenancy.ts +296 -0
- package/src/entity-conformance-values.ts +305 -0
- package/src/entity-conformance-versions.ts +60 -0
- package/src/entity-conformance-writes.ts +348 -0
- package/src/entity-driver-conformance.ts +103 -0
- package/src/error-groups-store-conformance.ts +198 -0
- package/src/error-reporter-conformance.ts +119 -0
- package/src/errors.ts +20 -2
- package/src/event-bus-conformance.ts +161 -0
- package/src/exercise-drift-hint.ts +4 -1
- package/src/exercise-judge.ts +5 -3
- package/src/exercise-override-line.ts +98 -0
- package/src/exercise-report.ts +138 -115
- package/src/exercise-room-mail.ts +40 -0
- package/src/exercise.ts +33 -29
- package/src/factories.ts +192 -179
- package/src/factory-api.ts +131 -0
- package/src/factory-associations.ts +67 -0
- package/src/factory-errors.ts +38 -0
- package/src/factory-lint.ts +92 -0
- package/src/factory-persist.ts +74 -16
- package/src/factory-pipeline.ts +191 -0
- package/src/factory-registry.ts +2 -1
- package/src/factory-types.ts +131 -0
- package/src/failure-store-conformance.ts +137 -0
- package/src/fixture-jobs.ts +27 -11
- package/src/fixture-mail.ts +7 -7
- package/src/fixture-seed.ts +111 -0
- package/src/fixture-statements.ts +68 -52
- package/src/framework-fixtures.ts +1 -1
- package/src/idempotency-store-conformance.ts +186 -0
- package/src/idempotency-store-replay-conformance.ts +140 -0
- package/src/idempotency-store-tx-conformance.ts +132 -0
- package/src/idempotency-store-world-conformance.ts +82 -0
- package/src/identity-provider-conformance.ts +144 -0
- package/src/image-transform-conformance.ts +89 -0
- package/src/index.ts +68 -157
- package/src/isr-store-conformance.ts +113 -0
- package/src/job-sim-effects.ts +19 -31
- package/src/job-sim-world.ts +6 -15
- package/src/lease-store-conformance.ts +195 -0
- package/src/mail-driver-conformance.ts +108 -0
- package/src/matcher-doubles.ts +104 -0
- package/src/matcher-stub.ts +78 -0
- package/src/matcher-surface.ts +21 -0
- package/src/matchers.ts +19 -1
- package/src/mcp-confirmation-store-conformance.ts +262 -0
- package/src/meter-store-conformance.ts +135 -0
- package/src/notify-digest-conformance.ts +133 -0
- package/src/notify-inbox-conformance.ts +130 -0
- package/src/notify-ledger-conformance.ts +134 -0
- package/src/notify-preference-conformance.ts +49 -0
- package/src/notify-stores-conformance.ts +73 -0
- package/src/outbox-store-conformance-claims.ts +112 -0
- package/src/outbox-store-conformance-subject.ts +65 -0
- package/src/outbox-store-conformance.ts +71 -0
- package/src/per-test-reset.ts +55 -20
- package/src/preload.ts +4 -2
- package/src/purge-conformance.ts +79 -0
- package/src/push-device-conformance.ts +124 -0
- package/src/push-subscription-conformance.ts +123 -0
- package/src/quiet-logs.ts +4 -4
- package/src/rate-limit-store-conformance.ts +178 -0
- package/src/registry-leak-error.ts +8 -1
- package/src/scale-sweep-run.ts +6 -9
- package/src/scrape-driver-conformance-frame-checks.ts +97 -0
- package/src/scrape-driver-conformance-page-checks.ts +114 -0
- package/src/scrape-driver-conformance-site.ts +84 -0
- package/src/scrape-driver-conformance.ts +34 -0
- package/src/sealed-network.ts +20 -2
- package/src/search-index-conformance-list.ts +76 -0
- package/src/search-index-conformance-support.ts +69 -0
- package/src/search-index-conformance-versions.ts +66 -0
- package/src/search-index-conformance.ts +126 -0
- package/src/secret-source-conformance.ts +91 -0
- package/src/seed-factories.ts +57 -0
- package/src/seed-writer.ts +8 -19
- package/src/semantic-cache-factory-conformance.ts +86 -0
- package/src/sms-transport-conformance.ts +102 -0
- package/src/step-store-conformance.ts +171 -0
- package/src/storage-conformance-read-checks.ts +132 -0
- package/src/storage-conformance-shape.ts +121 -0
- package/src/storage-conformance-stream-checks.ts +226 -0
- package/src/storage-conformance.ts +34 -0
- package/src/stub-action.ts +24 -0
- package/src/stub-errors.ts +86 -0
- package/src/stub-job.ts +30 -0
- package/src/stub-query.ts +25 -0
- package/src/stub-script.ts +76 -0
- package/src/stub-stack.ts +29 -0
- package/src/stub.ts +73 -0
- package/src/swap-adapter.ts +135 -0
- package/src/test-scope.ts +96 -0
- package/src/test-types.ts +1 -1
- package/src/totp-replay-guard-conformance.ts +76 -0
- package/src/transport-conformance.ts +179 -0
- package/src/upload-scanner-conformance.ts +112 -0
- package/src/vector-store-conformance-checks.ts +174 -0
- package/src/vector-store-conformance.ts +21 -0
- package/src/cdp-connection.ts +0 -291
- package/src/cdp-errors.ts +0 -179
- package/src/cdp-launch-attempt.ts +0 -224
- package/src/cdp-launch-candidates.ts +0 -62
- package/src/cdp-launch-profile.ts +0 -106
- package/src/cdp-launch-reap.ts +0 -68
- package/src/cdp-launch-wire.ts +0 -171
- package/src/cdp-launch.ts +0 -175
- package/src/cdp-pipe.ts +0 -77
- package/src/cdp-wire-watch.ts +0 -113
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// The realtime `Transport` contract as shared examples: `behavesLike(transportConformance, () =>
|
|
2
|
+
// bus)`. The subject is a FRESH, open transport per test, closed by the suite. Delivery may be
|
|
3
|
+
// asynchronous (a real bus), so every "did not arrive" is proved by a later sentinel that did — one
|
|
4
|
+
// connection's messages arrive in order — never by waiting a while and calling silence an answer.
|
|
5
|
+
|
|
6
|
+
import { expect, test } from 'bun:test';
|
|
7
|
+
import { isUltimateError } from '@ultimat3/core';
|
|
8
|
+
import type { Transport } from '@ultimat3/realtime/adapter';
|
|
9
|
+
import type { SharedExamples } from './shared-examples';
|
|
10
|
+
import { sharedExamples } from './shared-examples';
|
|
11
|
+
|
|
12
|
+
export interface TransportCheck {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
run(transport: Transport): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Real milliseconds: a bus delivers on its own socket, whatever the preload froze. */
|
|
18
|
+
const POLL_MS = 5;
|
|
19
|
+
const POLLS = 400;
|
|
20
|
+
|
|
21
|
+
const arrived = async (done: () => boolean): Promise<void> => {
|
|
22
|
+
for (let poll = 0; poll < POLLS && !done(); poll += 1) await Bun.sleep(POLL_MS);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** A subject root no other check, file or process on a shared server publishes under. */
|
|
26
|
+
const rootOf = (): string => `xconf.${Bun.randomUUIDv7().replaceAll('-', '')}`;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Every subject a check composes, joined in one place. A `Transport` is BELOW `channel()` — that
|
|
30
|
+
* declaration maps onto these raw bus subjects — so the suite has no declaration to pass, only the
|
|
31
|
+
* one spelling rule: tokens joined by `.`, the wildcard a token of its own.
|
|
32
|
+
*/
|
|
33
|
+
const subjectOf = (...tokens: readonly string[]): string => tokens.join('.');
|
|
34
|
+
|
|
35
|
+
const codeOf = (value: unknown): string =>
|
|
36
|
+
isUltimateError(value) ? value.code : `not an UltimateError: ${String(value)}`;
|
|
37
|
+
|
|
38
|
+
const refusal = (pending: Promise<unknown>): Promise<unknown> =>
|
|
39
|
+
pending.then(
|
|
40
|
+
() => 'answered',
|
|
41
|
+
(error: unknown) => error,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
export const TRANSPORT_CHECKS: readonly TransportCheck[] = [
|
|
45
|
+
{
|
|
46
|
+
name: 'a publish reaches a subscriber on its exact subject, payload byte for byte',
|
|
47
|
+
async run(bus) {
|
|
48
|
+
const subject = `${rootOf()}.change`;
|
|
49
|
+
const payload = '{"id":"p1","title":"Grüße — 東京 🚀"}';
|
|
50
|
+
const seen: string[] = [];
|
|
51
|
+
await bus.subscribe(subject, (body, on) => seen.push(`${on}=${body}`));
|
|
52
|
+
await bus.publish(subject, payload);
|
|
53
|
+
await arrived(() => seen.length > 0);
|
|
54
|
+
expect(seen).toEqual([`${subject}=${payload}`]);
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: '`*` matches exactly one token and `>` one or more trailing ones',
|
|
59
|
+
async run(bus) {
|
|
60
|
+
const root = rootOf();
|
|
61
|
+
const [star, tail]: [string[], string[]] = [[], []];
|
|
62
|
+
await bus.subscribe(subjectOf(root, '*'), (_body, on) => star.push(on));
|
|
63
|
+
await bus.subscribe(subjectOf(root, '>'), (_body, on) => tail.push(on));
|
|
64
|
+
for (const leaf of ['a', 'a.b', 'end']) await bus.publish(subjectOf(root, leaf), leaf);
|
|
65
|
+
await bus.publish(root, 'the root itself matches neither');
|
|
66
|
+
await arrived(() => tail.includes(`${root}.end`) && star.includes(`${root}.end`));
|
|
67
|
+
expect([star, tail]).toEqual([
|
|
68
|
+
[`${root}.a`, `${root}.end`],
|
|
69
|
+
[`${root}.a`, `${root}.a.b`, `${root}.end`],
|
|
70
|
+
]);
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: 'a subscriber hears only the subjects it asked for',
|
|
75
|
+
async run(bus) {
|
|
76
|
+
const root = rootOf();
|
|
77
|
+
const seen: string[] = [];
|
|
78
|
+
await bus.subscribe(subjectOf(root, 'comments', '*'), (body) => seen.push(body));
|
|
79
|
+
await bus.publish(subjectOf(root, 'posts', 'org-1'), 'wrong entity');
|
|
80
|
+
await bus.publish(subjectOf(root, 'comments', 'org-1'), 'right entity');
|
|
81
|
+
await arrived(() => seen.length > 0);
|
|
82
|
+
expect(seen).toEqual(['right entity']);
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: 'every subscriber on one subject hears it, and an unsubscribed one stops hearing',
|
|
87
|
+
async run(bus) {
|
|
88
|
+
const subject = `${rootOf()}.fanout`;
|
|
89
|
+
const [kept, dropped]: [string[], string[]] = [[], []];
|
|
90
|
+
await bus.subscribe(subject, (body) => kept.push(body));
|
|
91
|
+
const leaving = await bus.subscribe(subject, (body) => dropped.push(body));
|
|
92
|
+
await bus.publish(subject, 'one');
|
|
93
|
+
await arrived(() => kept.length === 1 && dropped.length === 1);
|
|
94
|
+
leaving.unsubscribe();
|
|
95
|
+
await bus.publish(subject, 'two');
|
|
96
|
+
await arrived(() => kept.length === 2);
|
|
97
|
+
expect([kept, dropped]).toEqual([['one', 'two'], ['one']]);
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: 'a subscriber that throws does not cost the others their message',
|
|
102
|
+
async run(bus) {
|
|
103
|
+
const subject = `${rootOf()}.faulty`;
|
|
104
|
+
const seen: string[] = [];
|
|
105
|
+
// A listener that cannot parse what it was sent — the throw an app's subscriber really makes.
|
|
106
|
+
await bus.subscribe(subject, (body) => JSON.parse(body));
|
|
107
|
+
await bus.subscribe(subject, (body) => seen.push(body));
|
|
108
|
+
await bus.publish(subject, 'still delivered');
|
|
109
|
+
await bus.publish(subject, 'and the next one');
|
|
110
|
+
await arrived(() => seen.length === 2);
|
|
111
|
+
expect(seen).toEqual(['still delivered', 'and the next one']);
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
name: 'shared: put lists a member with its value; touch answers whether it was live',
|
|
116
|
+
async run(bus) {
|
|
117
|
+
const key = `${rootOf()}.presence`;
|
|
118
|
+
await bus.shared.put(key, 'm1', '{"actorId":"alice"}', 30_000);
|
|
119
|
+
await bus.shared.put(key, 'm2', '{"actorId":"bob"}', 30_000);
|
|
120
|
+
const listed = await bus.shared.entries(key);
|
|
121
|
+
expect(listed.map((entry) => [entry.member, entry.value]).sort()).toEqual([
|
|
122
|
+
['m1', '{"actorId":"alice"}'],
|
|
123
|
+
['m2', '{"actorId":"bob"}'],
|
|
124
|
+
]);
|
|
125
|
+
expect([
|
|
126
|
+
await bus.shared.touch(key, 'm1', 30_000),
|
|
127
|
+
await bus.shared.touch(key, 'ghost', 30_000),
|
|
128
|
+
]).toEqual([true, false]);
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: 'shared: drop removes one member, a dotted member id survives, an unknown key is empty',
|
|
133
|
+
async run(bus) {
|
|
134
|
+
const key = `${rootOf()}.room`;
|
|
135
|
+
await bus.shared.put(key, 'socket.7', 'dotted', 30_000);
|
|
136
|
+
await bus.shared.put(key, 'plain', 'kept', 30_000);
|
|
137
|
+
await bus.shared.drop(key, 'plain');
|
|
138
|
+
expect((await bus.shared.entries(key)).map((entry) => entry.member)).toEqual(['socket.7']);
|
|
139
|
+
expect(await bus.shared.entries(`${rootOf()}.nobody`)).toEqual([]);
|
|
140
|
+
await bus.shared.drop(key, 'socket.7');
|
|
141
|
+
expect(await bus.shared.entries(key)).toEqual([]);
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
name: 'onReconnect hands back an unsubscribe that can be called',
|
|
146
|
+
async run(bus) {
|
|
147
|
+
const stop = bus.onReconnect(() => undefined);
|
|
148
|
+
expect(typeof stop).toBe('function');
|
|
149
|
+
stop();
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
name: 'a closed transport refuses publish and subscribe with X_TRANSPORT_UNAVAILABLE',
|
|
154
|
+
async run(bus) {
|
|
155
|
+
const subject = `${rootOf()}.closed`;
|
|
156
|
+
await bus.close();
|
|
157
|
+
expect(codeOf(await refusal(bus.publish(subject, '{}')))).toBe('X_TRANSPORT_UNAVAILABLE');
|
|
158
|
+
expect(codeOf(await refusal(bus.subscribe(subject, () => undefined)))).toBe(
|
|
159
|
+
'X_TRANSPORT_UNAVAILABLE',
|
|
160
|
+
);
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
];
|
|
164
|
+
|
|
165
|
+
export const transportConformance: SharedExamples<Transport | Promise<Transport>> = sharedExamples(
|
|
166
|
+
'a realtime transport',
|
|
167
|
+
(subject) => {
|
|
168
|
+
for (const check of TRANSPORT_CHECKS) {
|
|
169
|
+
test(check.name, async () => {
|
|
170
|
+
const bus = await subject();
|
|
171
|
+
try {
|
|
172
|
+
await check.run(bus);
|
|
173
|
+
} finally {
|
|
174
|
+
await bus.close();
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
);
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// The `uploadScanner` port's contract as shared examples: `behavesLike(uploadScannerConformance, ()
|
|
2
|
+
// => scanner)`. Structural over `@ultimat3/storage`'s `UploadScanner`, as the storage suite is over
|
|
3
|
+
// its driver, so this package takes no dependency on storage. Bodies arrive as streams, cut small.
|
|
4
|
+
|
|
5
|
+
import { expect, test } from 'bun:test';
|
|
6
|
+
import type { SharedExamples } from './shared-examples';
|
|
7
|
+
import { sharedExamples } from './shared-examples';
|
|
8
|
+
|
|
9
|
+
/** `@ultimat3/storage`'s `ScanVerdict`, restated structurally. */
|
|
10
|
+
export type ScanVerdictLike =
|
|
11
|
+
| { readonly verdict: 'clean' }
|
|
12
|
+
| { readonly verdict: 'infected'; readonly signature: string }
|
|
13
|
+
| { readonly verdict: 'hold'; readonly reason: string };
|
|
14
|
+
|
|
15
|
+
/** `@ultimat3/storage`'s `UploadScanner`: a ClamAV adapter, a vendor API, the memory reference. */
|
|
16
|
+
export interface UploadScannerLike {
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly holds?: boolean | undefined;
|
|
19
|
+
scan(input: {
|
|
20
|
+
readonly key: string;
|
|
21
|
+
readonly pendingKey: string;
|
|
22
|
+
readonly size: number;
|
|
23
|
+
readonly contentType: string;
|
|
24
|
+
readonly body: ReadableStream<Uint8Array>;
|
|
25
|
+
}): Promise<ScanVerdictLike>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface UploadScannerCheck {
|
|
29
|
+
readonly name: string;
|
|
30
|
+
run(scanner: UploadScannerLike): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The EICAR test string, in two halves so this file is no detection itself. */
|
|
34
|
+
export const UPLOAD_SCANNER_EICAR = `${'X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR'}${'-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*'}`;
|
|
35
|
+
|
|
36
|
+
const encode = (text: string): Uint8Array => new TextEncoder().encode(text);
|
|
37
|
+
|
|
38
|
+
/** `bytes` as a stream of `size`-byte chunks — EICAR cut by the transport is still EICAR. */
|
|
39
|
+
function streamOf(bytes: Uint8Array, size: number): ReadableStream<Uint8Array> {
|
|
40
|
+
let at = 0;
|
|
41
|
+
return new ReadableStream({
|
|
42
|
+
pull(controller) {
|
|
43
|
+
if (at >= bytes.byteLength) return controller.close();
|
|
44
|
+
controller.enqueue(bytes.slice(at, at + size));
|
|
45
|
+
at += size;
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const scan = (scanner: UploadScannerLike, bytes: Uint8Array, chunk = 64 * 1024) =>
|
|
51
|
+
scanner.scan({
|
|
52
|
+
key: 'org/conformance/pending/scan/a1-u-1.txt',
|
|
53
|
+
pendingKey: 'org/conformance/pending/u-1.txt',
|
|
54
|
+
size: bytes.byteLength,
|
|
55
|
+
contentType: 'text/plain',
|
|
56
|
+
body: streamOf(bytes, chunk),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const PLAIN = encode('A plain note: nothing in here is a virus.\n');
|
|
60
|
+
|
|
61
|
+
export const UPLOAD_SCANNER_CHECKS: readonly UploadScannerCheck[] = [
|
|
62
|
+
{
|
|
63
|
+
name: 'has a name, for the refusal it ends up in',
|
|
64
|
+
async run(scanner) {
|
|
65
|
+
expect(scanner.name.length).toBeGreaterThan(0);
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: 'the EICAR test file is infected, with a non-empty signature',
|
|
70
|
+
async run(scanner) {
|
|
71
|
+
const verdict = await scan(scanner, encode(UPLOAD_SCANNER_EICAR));
|
|
72
|
+
expect(verdict.verdict).toBe('infected');
|
|
73
|
+
expect(verdict.verdict === 'infected' ? verdict.signature.length : 0).toBeGreaterThan(0);
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: 'the EICAR test file delivered in 7-byte chunks is still infected',
|
|
78
|
+
async run(scanner) {
|
|
79
|
+
expect((await scan(scanner, encode(UPLOAD_SCANNER_EICAR), 7)).verdict).toBe('infected');
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: 'a plain text file is clean',
|
|
84
|
+
async run(scanner) {
|
|
85
|
+
expect(await scan(scanner, PLAIN)).toEqual({ verdict: 'clean' });
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'an empty file is clean',
|
|
90
|
+
async run(scanner) {
|
|
91
|
+
expect(await scan(scanner, new Uint8Array())).toEqual({ verdict: 'clean' });
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
name: 'a verdict is about these bytes only: infected, then clean, from one scanner',
|
|
96
|
+
async run(scanner) {
|
|
97
|
+
expect((await scan(scanner, encode(UPLOAD_SCANNER_EICAR))).verdict).toBe('infected');
|
|
98
|
+
expect(await scan(scanner, PLAIN)).toEqual({ verdict: 'clean' });
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
export const uploadScannerConformance: SharedExamples<UploadScannerLike> = sharedExamples(
|
|
104
|
+
'an upload scanner',
|
|
105
|
+
(subject) => {
|
|
106
|
+
for (const check of UPLOAD_SCANNER_CHECKS) {
|
|
107
|
+
test(check.name, async () => {
|
|
108
|
+
await check.run(subject());
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
);
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// The checks behind `vectorStoreConformance`, one behaviour each, asserted through `VectorStore`
|
|
2
|
+
// alone. Every check binds its own fresh tenant with `scoped()`, so one table shared by the whole
|
|
3
|
+
// suite (pgvector) and one map (memory) both answer as if each check had a store of its own.
|
|
4
|
+
|
|
5
|
+
import { expect } from 'bun:test';
|
|
6
|
+
import type { SearchHit, VectorRecord, VectorStore } from '@ultimat3/ai';
|
|
7
|
+
import { isUltimateError } from '@ultimat3/core';
|
|
8
|
+
|
|
9
|
+
export interface VectorStoreCheck {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
run(store: VectorStore): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Every subject is built at this width: a pg table's column type is fixed by its DDL. */
|
|
15
|
+
export const VECTOR_CONFORMANCE_DIMENSION = 3;
|
|
16
|
+
|
|
17
|
+
const unit = (...values: number[]): Float32Array => {
|
|
18
|
+
const norm = Math.hypot(...values);
|
|
19
|
+
return Float32Array.from(values.map((value) => value / norm));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const NORTH = unit(1, 0, 0);
|
|
23
|
+
const CORPUS: readonly VectorRecord[] = [
|
|
24
|
+
{ id: 'a', vector: NORTH, text: 'alpha zirconium manual', metadata: { kind: 'guide' } },
|
|
25
|
+
{
|
|
26
|
+
id: 'b',
|
|
27
|
+
vector: unit(0.9, 0.1, 0),
|
|
28
|
+
text: 'beta guide about vectors',
|
|
29
|
+
metadata: { kind: 'guide' },
|
|
30
|
+
},
|
|
31
|
+
{ id: 'c', vector: unit(0, 1, 0), text: 'gamma error report', metadata: { kind: 'error' } },
|
|
32
|
+
{ id: 'd', vector: unit(0, 0, 1), text: 'delta billing summary' },
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
let minted = 0;
|
|
36
|
+
const tenantOf = (): string => {
|
|
37
|
+
minted += 1;
|
|
38
|
+
return `conformance-${Bun.randomUUIDv7()}-${minted.toString(36)}`;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** A fresh tenant's view of `store`, holding `CORPUS`. */
|
|
42
|
+
const seeded = async (store: VectorStore): Promise<VectorStore> => {
|
|
43
|
+
const view = store.scoped({ tenant: tenantOf() });
|
|
44
|
+
await view.upsert(CORPUS);
|
|
45
|
+
return view;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const ids = (hits: readonly SearchHit[]): readonly string[] => hits.map((hit) => hit.id);
|
|
49
|
+
const sortedIds = (hits: readonly SearchHit[]): readonly string[] => [...ids(hits)].sort();
|
|
50
|
+
|
|
51
|
+
const codeOf = async (run: () => unknown): Promise<string> => {
|
|
52
|
+
try {
|
|
53
|
+
await run();
|
|
54
|
+
return 'answered';
|
|
55
|
+
} catch (error) {
|
|
56
|
+
return isUltimateError(error) ? error.code : `not an UltimateError: ${String(error)}`;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const VECTOR_STORE_CHECKS: readonly VectorStoreCheck[] = [
|
|
61
|
+
{
|
|
62
|
+
name: 'search ranks the nearest vector first, stops at k, and carries text and metadata',
|
|
63
|
+
async run(store) {
|
|
64
|
+
expect(store.dimension).toBe(VECTOR_CONFORMANCE_DIMENSION);
|
|
65
|
+
const hits = await (await seeded(store)).search(NORTH, 2);
|
|
66
|
+
expect(ids(hits)).toEqual(['a', 'b']);
|
|
67
|
+
expect(hits[0]).toMatchObject({
|
|
68
|
+
text: 'alpha zirconium manual',
|
|
69
|
+
metadata: { kind: 'guide' },
|
|
70
|
+
});
|
|
71
|
+
expect(hits[0]?.score ?? 0).toBeGreaterThan(hits[1]?.score ?? 1);
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: 'a row with no metadata answers {}, and an upsert of a known id replaces it',
|
|
76
|
+
async run(store) {
|
|
77
|
+
const view = await seeded(store);
|
|
78
|
+
await view.upsert([{ id: 'd', vector: unit(0, 0, 1), text: 'delta rewritten' }]);
|
|
79
|
+
const hits = await view.search(unit(0, 0, 1), 1);
|
|
80
|
+
expect(hits).toMatchObject([{ id: 'd', text: 'delta rewritten', metadata: {} }]);
|
|
81
|
+
expect(await view.search(NORTH, 10)).toHaveLength(CORPUS.length);
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: 'a vector of the wrong width is X_VECTOR_DIM_MISMATCH on upsert, search and hybrid',
|
|
86
|
+
async run(store) {
|
|
87
|
+
const view = store.scoped({ tenant: tenantOf() });
|
|
88
|
+
const wide = new Float32Array(VECTOR_CONFORMANCE_DIMENSION + 1);
|
|
89
|
+
const record = { id: 'x', vector: wide, text: 'x' };
|
|
90
|
+
expect([
|
|
91
|
+
await codeOf(() => view.upsert([record])),
|
|
92
|
+
await codeOf(() => view.search(wide, 1)),
|
|
93
|
+
await codeOf(() => view.hybrid({ query: 'x', vector: wide, k: 1 })),
|
|
94
|
+
]).toEqual(['X_VECTOR_DIM_MISMATCH', 'X_VECTOR_DIM_MISMATCH', 'X_VECTOR_DIM_MISMATCH']);
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'a batch holding one wrong-width record is refused whole: none of it is written',
|
|
99
|
+
async run(store) {
|
|
100
|
+
const view = store.scoped({ tenant: tenantOf() });
|
|
101
|
+
const wide = new Float32Array(VECTOR_CONFORMANCE_DIMENSION + 1);
|
|
102
|
+
const batch = [...CORPUS, { id: 'x', vector: wide, text: 'x' }];
|
|
103
|
+
expect(await codeOf(() => view.upsert(batch))).toBe('X_VECTOR_DIM_MISMATCH');
|
|
104
|
+
expect(await view.search(NORTH, 10)).toEqual([]);
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
name: 'a metadata filter is an exact match, and a row missing the key is not a match',
|
|
109
|
+
async run(store) {
|
|
110
|
+
const view = await seeded(store);
|
|
111
|
+
expect(sortedIds(await view.search(NORTH, 10, { kind: 'guide' }))).toEqual(['a', 'b']);
|
|
112
|
+
expect(await view.search(NORTH, 10, { kind: 'guid' })).toEqual([]);
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: 'two tenants hold the same id as two rows, and neither sees nor deletes the other',
|
|
117
|
+
async run(store) {
|
|
118
|
+
const [one, two] = [await seeded(store), store.scoped({ tenant: tenantOf() })];
|
|
119
|
+
await two.upsert([{ id: 'a', vector: NORTH, text: 'the other tenant' }]);
|
|
120
|
+
await two.delete(['b']);
|
|
121
|
+
expect(ids(await two.search(NORTH, 10))).toEqual(['a']);
|
|
122
|
+
expect((await one.search(NORTH, 1))[0]?.text).toBe('alpha zirconium manual');
|
|
123
|
+
expect(await one.search(NORTH, 10)).toHaveLength(CORPUS.length);
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: 'a scope only tightens: another tenant is X_VECTOR_SCOPE_WIDENED, an allow-list hides',
|
|
128
|
+
async run(store) {
|
|
129
|
+
const view = await seeded(store);
|
|
130
|
+
expect(await codeOf(() => view.scoped({ tenant: tenantOf() }))).toBe(
|
|
131
|
+
'X_VECTOR_SCOPE_WIDENED',
|
|
132
|
+
);
|
|
133
|
+
const guides = view.scoped({ allow: { kind: ['guide', 'error'] } }).scoped({
|
|
134
|
+
allow: { kind: ['guide'] },
|
|
135
|
+
});
|
|
136
|
+
expect(sortedIds(await guides.search(NORTH, 10))).toEqual(['a', 'b']);
|
|
137
|
+
await guides.delete(['c', 'd']);
|
|
138
|
+
expect(await view.search(NORTH, 10)).toHaveLength(CORPUS.length);
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
name: 'prune deletes what the filter matches except the kept ids, and nothing else',
|
|
143
|
+
async run(store) {
|
|
144
|
+
const view = await seeded(store);
|
|
145
|
+
await view.prune({ kind: 'guide' }, ['b']);
|
|
146
|
+
expect(sortedIds(await view.search(NORTH, 10))).toEqual(['b', 'c', 'd']);
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
name: 'searchText ranks the document holding a rare exact term first; no match is []',
|
|
151
|
+
async run(store) {
|
|
152
|
+
const view = await seeded(store);
|
|
153
|
+
expect(ids(await view.searchText('zirconium', 3))[0]).toBe('a');
|
|
154
|
+
expect(await view.searchText('xylophonist', 3)).toEqual([]);
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
name: 'hybrid puts the row both rankings lead with first, and stops at k',
|
|
159
|
+
async run(store) {
|
|
160
|
+
const view = await seeded(store);
|
|
161
|
+
const hits = await view.hybrid({ query: 'zirconium', vector: NORTH, k: 2 });
|
|
162
|
+
expect(hits).toHaveLength(2);
|
|
163
|
+
expect(hits[0]?.id).toBe('a');
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
name: 'a k that is not a whole number is refused, never answered with []',
|
|
168
|
+
async run(store) {
|
|
169
|
+
const view = await seeded(store);
|
|
170
|
+
expect(await codeOf(() => view.search(NORTH, Number.NaN))).toBe('X_INVARIANT');
|
|
171
|
+
expect(await codeOf(() => view.searchText('zirconium', 1.5))).toBe('X_INVARIANT');
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// `@ultimat3/ai`'s `VectorStore` contract as shared examples: `behavesLike(vectorStoreConformance,
|
|
2
|
+
// () => store)` runs every check in `vector-store-conformance-checks.ts` against a store of your
|
|
3
|
+
// own — empty or shared, UNBOUND (each check binds its own tenant), `VECTOR_CONFORMANCE_DIMENSION`
|
|
4
|
+
// wide.
|
|
5
|
+
|
|
6
|
+
import { test } from 'bun:test';
|
|
7
|
+
import type { VectorStore } from '@ultimat3/ai';
|
|
8
|
+
import type { SharedExamples } from './shared-examples';
|
|
9
|
+
import { sharedExamples } from './shared-examples';
|
|
10
|
+
import { VECTOR_STORE_CHECKS } from './vector-store-conformance-checks';
|
|
11
|
+
|
|
12
|
+
export const vectorStoreConformance: SharedExamples<VectorStore> = sharedExamples(
|
|
13
|
+
'a vector store',
|
|
14
|
+
(subject) => {
|
|
15
|
+
for (const check of VECTOR_STORE_CHECKS) {
|
|
16
|
+
test(check.name, async () => {
|
|
17
|
+
await check.run(subject());
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
);
|