@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,101 @@
|
|
|
1
|
+
// The `embedder` port's contract as shared examples: `behavesLike(embedderConformance, () => yours)`.
|
|
2
|
+
// What `@ultimat3/ai`'s semantic cache and vector store assume of an `Embedder` and never re-check
|
|
3
|
+
// per call: one vector per text, in order, of the declared dimension, finite, with a direction.
|
|
4
|
+
|
|
5
|
+
import { expect, test } from 'bun:test';
|
|
6
|
+
import type { Embedder } from '@ultimat3/ai/adapter';
|
|
7
|
+
import type { SharedExamples } from './shared-examples';
|
|
8
|
+
import { sharedExamples } from './shared-examples';
|
|
9
|
+
|
|
10
|
+
export interface EmbedderCheck {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
run(embedder: Embedder): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Near-equal, not equal: a remote model may differ in the last bits between two calls. */
|
|
16
|
+
const SAME_DIRECTION = 0.999;
|
|
17
|
+
|
|
18
|
+
const TEXTS = ['list my orders', 'the invoice for March', 'reset my password'] as const;
|
|
19
|
+
|
|
20
|
+
function cosine(a: Float32Array, b: Float32Array): number {
|
|
21
|
+
let dot = 0;
|
|
22
|
+
let normA = 0;
|
|
23
|
+
let normB = 0;
|
|
24
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
25
|
+
const x = a[i] ?? 0;
|
|
26
|
+
const y = b[i] ?? 0;
|
|
27
|
+
dot += x * y;
|
|
28
|
+
normA += x * x;
|
|
29
|
+
normB += y * y;
|
|
30
|
+
}
|
|
31
|
+
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const EMBEDDER_CHECKS: readonly EmbedderCheck[] = [
|
|
35
|
+
{
|
|
36
|
+
name: 'it names itself and declares a whole, positive dimension',
|
|
37
|
+
async run(embedder) {
|
|
38
|
+
expect(embedder.name.length).toBeGreaterThan(0);
|
|
39
|
+
expect(Number.isSafeInteger(embedder.dimension) && embedder.dimension > 0).toBe(true);
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'one vector per text, of the declared dimension, every component finite',
|
|
44
|
+
async run(embedder) {
|
|
45
|
+
const vectors = await embedder.embed(TEXTS);
|
|
46
|
+
expect(vectors.length).toBe(TEXTS.length);
|
|
47
|
+
for (const vector of vectors) {
|
|
48
|
+
expect(vector).toBeInstanceOf(Float32Array);
|
|
49
|
+
expect(vector.length).toBe(embedder.dimension);
|
|
50
|
+
expect(vector.every((component) => Number.isFinite(component))).toBe(true);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'an empty batch answers no vectors, never one',
|
|
56
|
+
async run(embedder) {
|
|
57
|
+
expect((await embedder.embed([])).length).toBe(0);
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: 'a text has a direction: no all-zero vector, which every cosine answers NaN against',
|
|
62
|
+
async run(embedder) {
|
|
63
|
+
for (const vector of await embedder.embed(TEXTS)) {
|
|
64
|
+
expect(vector.some((component) => component !== 0)).toBe(true);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: 'the batch keeps the order the texts arrived in',
|
|
70
|
+
async run(embedder) {
|
|
71
|
+
// Against each text alone: a one-text batch has no order to get wrong.
|
|
72
|
+
const batch = await embedder.embed(TEXTS);
|
|
73
|
+
for (let at = 0; at < TEXTS.length; at += 1) {
|
|
74
|
+
const [alone] = await embedder.embed([TEXTS[at] ?? '']);
|
|
75
|
+
const inBatch = batch[at];
|
|
76
|
+
if (alone === undefined || inBatch === undefined) return expect.unreachable();
|
|
77
|
+
expect(cosine(inBatch, alone)).toBeGreaterThan(SAME_DIRECTION);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: 'the same text twice is the same direction: a cache keyed on it can hit',
|
|
83
|
+
async run(embedder) {
|
|
84
|
+
const [first] = await embedder.embed([TEXTS[0]]);
|
|
85
|
+
const [again] = await embedder.embed([TEXTS[0]]);
|
|
86
|
+
if (first === undefined || again === undefined) return expect.unreachable();
|
|
87
|
+
expect(cosine(first, again)).toBeGreaterThan(SAME_DIRECTION);
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
export const embedderConformance: SharedExamples<Embedder> = sharedExamples(
|
|
93
|
+
'an embedder',
|
|
94
|
+
(subject) => {
|
|
95
|
+
for (const check of EMBEDDER_CHECKS) {
|
|
96
|
+
test(check.name, async () => {
|
|
97
|
+
await check.run(subject());
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
);
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// Entity conformance · AGGREGATES: a grouped count in one meaning (scope, soft delete, a bound that
|
|
2
|
+
// refuses rather than truncates, a page that never narrows it), the refusals every driver shares,
|
|
3
|
+
// and min/max/sum answering exact values. Lifted from `entity/src/{count-by,aggregate-time,write}-parity`.
|
|
4
|
+
|
|
5
|
+
import { expect } from 'bun:test';
|
|
6
|
+
import { isUltimateError } from '@ultimat3/core';
|
|
7
|
+
import { decimal, entity, integer, json, money, t, text, timestamp, uuid } from '@ultimat3/entity';
|
|
8
|
+
import { remember } from './entity-conformance-entities';
|
|
9
|
+
import { checkOver, idAt } from './entity-conformance-support';
|
|
10
|
+
import type { EntityDriverCheck } from './entity-driver-conformance';
|
|
11
|
+
|
|
12
|
+
/** `MAX_GROUPS` in `entity/src/count-by.ts`: a breakdown past it is refused, never cut. */
|
|
13
|
+
const MAX_GROUPS = 1000;
|
|
14
|
+
|
|
15
|
+
/** A `jsonb` column: refused as a group key. */
|
|
16
|
+
const jsonb = () => json(t.object({}));
|
|
17
|
+
|
|
18
|
+
const likes = remember(
|
|
19
|
+
entity('ec_a_likes', {
|
|
20
|
+
columns: {
|
|
21
|
+
id: uuid().primaryKey(),
|
|
22
|
+
orgId: uuid().tenant(),
|
|
23
|
+
postId: uuid().nullable(),
|
|
24
|
+
reaction: text({ max: 20 }),
|
|
25
|
+
count: integer(),
|
|
26
|
+
payload: jsonb(),
|
|
27
|
+
total: money(),
|
|
28
|
+
createdAt: timestamp().defaultNow(),
|
|
29
|
+
deletedAt: timestamp().nullable(),
|
|
30
|
+
},
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
type Like = typeof likes.$row;
|
|
34
|
+
|
|
35
|
+
/** Every column a `Map` cannot be keyed by — the one shape whose refusal names no column. */
|
|
36
|
+
const ungroupable = remember(
|
|
37
|
+
entity('ec_a_ungroupable', {
|
|
38
|
+
columns: { at: timestamp().primaryKey(), payload: jsonb(), total: money() },
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const events = remember(
|
|
43
|
+
entity('ec_a_events', { columns: { id: uuid().primaryKey(), at: timestamp() } }),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const rates = remember(
|
|
47
|
+
entity('ec_a_rates', {
|
|
48
|
+
columns: { id: uuid().primaryKey(), rate: decimal({ precision: 8, scale: 2 }) },
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const ORG = idAt('a1');
|
|
53
|
+
const OTHER_ORG = idAt('a2');
|
|
54
|
+
const [POST_A, POST_B, POST_C] = [idAt('aaa'), idAt('bbb'), idAt('ccc')];
|
|
55
|
+
|
|
56
|
+
const like = (index: number, over: Partial<Like> = {}): Like => ({
|
|
57
|
+
id: idAt(String(index)),
|
|
58
|
+
orgId: ORG,
|
|
59
|
+
postId: POST_A,
|
|
60
|
+
reaction: 'like',
|
|
61
|
+
count: 1,
|
|
62
|
+
payload: {},
|
|
63
|
+
total: { minor: 0, currency: 'EUR' },
|
|
64
|
+
createdAt: new Date('2026-01-02T03:04:05.000Z'),
|
|
65
|
+
deletedAt: null,
|
|
66
|
+
...over,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
/** Three on A, two each on B, C and none; then a stamped row and another tenant's, both on A. */
|
|
70
|
+
const SEED: readonly Like[] = [
|
|
71
|
+
...[POST_A, POST_A, POST_A, POST_B, POST_B, POST_C, POST_C, null, null].map((postId, index) =>
|
|
72
|
+
like(index, { postId }),
|
|
73
|
+
),
|
|
74
|
+
like(90, { deletedAt: new Date('2026-02-02T00:00:00.000Z') }),
|
|
75
|
+
like(91, { orgId: OTHER_ORG }),
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
const NEEDS_COUNT_BY = ['aggregate:countBy'] as const;
|
|
79
|
+
|
|
80
|
+
/** What a refusal said, as `[code, cause, fix]` — or `['resolved']` when nothing was refused. */
|
|
81
|
+
type Said = readonly [code: string, cause?: string, fix?: string];
|
|
82
|
+
|
|
83
|
+
const refusal = async (work: () => Promise<unknown>): Promise<Said> => {
|
|
84
|
+
try {
|
|
85
|
+
await work();
|
|
86
|
+
return ['resolved'];
|
|
87
|
+
} catch (error) {
|
|
88
|
+
return isUltimateError(error) ? [error.code, error.cause, error.fix] : [String(error)];
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const countBy: readonly EntityDriverCheck[] = [
|
|
93
|
+
checkOver(
|
|
94
|
+
{ likes },
|
|
95
|
+
'countBy groups the visible rows of one tenant, in value order, absent never zero',
|
|
96
|
+
async (_db, { driver }) => {
|
|
97
|
+
const repo = driver.repo(likes);
|
|
98
|
+
await repo.insertAll(SEED);
|
|
99
|
+
expect([...(await repo.countBy('postId', { orgId: ORG }))]).toEqual([
|
|
100
|
+
[POST_A, 3],
|
|
101
|
+
[POST_B, 2],
|
|
102
|
+
[POST_C, 2],
|
|
103
|
+
[null, 2],
|
|
104
|
+
]);
|
|
105
|
+
expect((await repo.countBy('postId', { orgId: ORG, includeDeleted: true })).get(POST_A)).toBe(
|
|
106
|
+
4,
|
|
107
|
+
);
|
|
108
|
+
expect([...(await repo.countBy('postId', { orgId: OTHER_ORG }))]).toEqual([[POST_A, 1]]);
|
|
109
|
+
const where = [{ column: 'postId', op: 'in' as const, value: [POST_B, POST_C] }];
|
|
110
|
+
expect([...(await repo.countBy('postId', { orgId: ORG, where }))]).toEqual([
|
|
111
|
+
[POST_B, 2],
|
|
112
|
+
[POST_C, 2],
|
|
113
|
+
]);
|
|
114
|
+
const none = [{ column: 'reaction', op: 'eq' as const, value: 'nope' }];
|
|
115
|
+
const empty = await repo.countBy('postId', { orgId: ORG, where: none });
|
|
116
|
+
expect([[...empty], empty.get(POST_A)]).toEqual([[], undefined]);
|
|
117
|
+
},
|
|
118
|
+
NEEDS_COUNT_BY,
|
|
119
|
+
),
|
|
120
|
+
checkOver(
|
|
121
|
+
{ likes },
|
|
122
|
+
'a page bounds a page, never a count: limit and cursor reach no aggregate',
|
|
123
|
+
async (_db, { driver }) => {
|
|
124
|
+
const repo = driver.repo(likes);
|
|
125
|
+
await repo.insertAll(SEED);
|
|
126
|
+
const args = { orgId: ORG } as const;
|
|
127
|
+
const { nextCursor } = await repo.findMany({ ...args, limit: 2 });
|
|
128
|
+
expect(nextCursor).not.toBeNull();
|
|
129
|
+
const paged = { ...args, limit: 2, cursor: nextCursor };
|
|
130
|
+
expect([...(await repo.countBy('postId', paged))]).toEqual([
|
|
131
|
+
...(await repo.countBy('postId', args)),
|
|
132
|
+
]);
|
|
133
|
+
expect(await repo.count(paged)).toBe(await repo.count(args));
|
|
134
|
+
},
|
|
135
|
+
NEEDS_COUNT_BY,
|
|
136
|
+
),
|
|
137
|
+
checkOver(
|
|
138
|
+
{ likes },
|
|
139
|
+
`one group past ${MAX_GROUPS} is refused, exactly ${MAX_GROUPS} resolves whole`,
|
|
140
|
+
async (_db, { driver }) => {
|
|
141
|
+
const repo = driver.repo(likes);
|
|
142
|
+
const many = Array.from({ length: MAX_GROUPS + 1 }, (_, index) =>
|
|
143
|
+
like(index, { postId: idAt(5000 + index) }),
|
|
144
|
+
);
|
|
145
|
+
await repo.insertAll(many.slice(0, MAX_GROUPS));
|
|
146
|
+
expect((await repo.countBy('postId', { orgId: ORG })).size).toBe(MAX_GROUPS);
|
|
147
|
+
await repo.insert(many[MAX_GROUPS] as Like);
|
|
148
|
+
expect((await refusal(() => repo.countBy('postId', { orgId: ORG })))[0]).toBe(
|
|
149
|
+
'X_INVARIANT_VIOLATED',
|
|
150
|
+
);
|
|
151
|
+
},
|
|
152
|
+
NEEDS_COUNT_BY,
|
|
153
|
+
),
|
|
154
|
+
checkOver(
|
|
155
|
+
{ likes, ungroupable },
|
|
156
|
+
'an ungroupable column is refused before the scope, naming a column that would work',
|
|
157
|
+
async (_db, { driver }) => {
|
|
158
|
+
const repo = driver.repo(likes);
|
|
159
|
+
const causes: readonly (readonly [string, string])[] = [
|
|
160
|
+
['secret', 'no column "secret" on ec_a_likes'],
|
|
161
|
+
['createdAt', 'a timestamptz column is not a key a map can be looked up by'],
|
|
162
|
+
['payload', 'a jsonb column is not a key a map can be looked up by'],
|
|
163
|
+
['total', 'a money column is not a key a map can be looked up by'],
|
|
164
|
+
];
|
|
165
|
+
for (const [property, cause] of causes) {
|
|
166
|
+
const refused = await refusal(() => repo.countBy(property, { orgId: ORG }));
|
|
167
|
+
expect(refused[0]).toBe('X_INVARIANT_VIOLATED');
|
|
168
|
+
expect(refused[1]).toContain(`ec_a_likes.countBy('${property}'): ${cause}`);
|
|
169
|
+
}
|
|
170
|
+
// No scope at all: the column is judged first, so the author is told about the column.
|
|
171
|
+
const unscoped = await refusal(() => repo.countBy('createdAt'));
|
|
172
|
+
expect(unscoped[2]).toBe(
|
|
173
|
+
"ec_a_likes.countBy('id') # group by one of: id, orgId, postId, reaction, count",
|
|
174
|
+
);
|
|
175
|
+
const nothing = await refusal(() => driver.repo(ungroupable).countBy('at'));
|
|
176
|
+
expect(nothing[2]).toBe(
|
|
177
|
+
'x entities show ec_a_ungroupable --json # this entity declares no column a count can be keyed by',
|
|
178
|
+
);
|
|
179
|
+
},
|
|
180
|
+
NEEDS_COUNT_BY,
|
|
181
|
+
),
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
const EARLIEST = new Date('0099-06-01T00:00:00.000Z');
|
|
185
|
+
const LATEST = new Date('2026-09-23T10:15:30.123Z');
|
|
186
|
+
const AT = [EARLIEST, new Date('1900-01-01T12:00:00.000Z'), LATEST];
|
|
187
|
+
|
|
188
|
+
const extremes: readonly EntityDriverCheck[] = [
|
|
189
|
+
checkOver(
|
|
190
|
+
{ events },
|
|
191
|
+
'min/max over a timestamptz answer the exact instants, year 0099 included',
|
|
192
|
+
async (db) => {
|
|
193
|
+
for (const [index, at] of AT.entries()) await db.events.insert({ id: idAt(index + 1), at });
|
|
194
|
+
expect([await db.events.min('at'), await db.events.max('at')]).toEqual([EARLIEST, LATEST]);
|
|
195
|
+
},
|
|
196
|
+
['aggregate:min', 'aggregate:max'],
|
|
197
|
+
),
|
|
198
|
+
checkOver(
|
|
199
|
+
{ rates },
|
|
200
|
+
'sum and max over a decimal answer canonical decimal text',
|
|
201
|
+
async (db) => {
|
|
202
|
+
await db.rates.insert({ id: idAt(1), rate: '007.5' });
|
|
203
|
+
await db.rates.insert({ id: idAt(2), rate: '2.5' });
|
|
204
|
+
expect([await db.rates.sum('rate'), await db.rates.max('rate')]).toEqual(['10.00', '7.50']);
|
|
205
|
+
},
|
|
206
|
+
['aggregate:sum', 'aggregate:max'],
|
|
207
|
+
),
|
|
208
|
+
];
|
|
209
|
+
|
|
210
|
+
export const AGGREGATE_CHECKS: readonly EntityDriverCheck[] = [...countBy, ...extremes];
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// Entity conformance · walking and joining: `inBatches(size)` cuts one seed into the same batches
|
|
2
|
+
// and its cursor resumes at the same row, a numeric key found under any spelling, and `preload`
|
|
3
|
+
// attaching the same rows — `null` for a key that resolves to nothing, `[]` for an empty hasMany.
|
|
4
|
+
// Lifted from `entity/src/{batch,preload}-parity.test.ts`.
|
|
5
|
+
|
|
6
|
+
import { expect } from 'bun:test';
|
|
7
|
+
import { ctxOf, runWithContext } from '@ultimat3/core';
|
|
8
|
+
import type { BatchIterator } from '@ultimat3/entity';
|
|
9
|
+
import { bigint, decimal, entity, integer, text, timestamp, uuid } from '@ultimat3/entity';
|
|
10
|
+
import { remember } from './entity-conformance-entities';
|
|
11
|
+
import { checkOver, idAt } from './entity-conformance-support';
|
|
12
|
+
import type { EntityDriverCheck } from './entity-driver-conformance';
|
|
13
|
+
|
|
14
|
+
const walked = remember(
|
|
15
|
+
entity('ec_p_walked', {
|
|
16
|
+
columns: { id: uuid().primaryKey(), orgId: uuid().tenant(), title: text({ max: 80 }) },
|
|
17
|
+
}),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const bigs = remember(
|
|
21
|
+
entity('ec_p_bigs', { columns: { id: bigint().primaryKey(), label: text({ max: 8 }) } }),
|
|
22
|
+
);
|
|
23
|
+
const ints = remember(
|
|
24
|
+
entity('ec_p_ints', { columns: { id: integer().primaryKey(), label: text({ max: 8 }) } }),
|
|
25
|
+
);
|
|
26
|
+
const decimals = remember(
|
|
27
|
+
entity('ec_p_decimals', { columns: { id: decimal().primaryKey(), label: text({ max: 8 }) } }),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const orgs = remember(
|
|
31
|
+
entity('ec_p_orgs', { columns: { id: uuid().primaryKey(), slug: text({ max: 40 }) } }),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const members = remember(
|
|
35
|
+
entity('ec_p_members', {
|
|
36
|
+
columns: {
|
|
37
|
+
id: uuid().primaryKey(),
|
|
38
|
+
orgId: uuid()
|
|
39
|
+
.references(() => orgs.id)
|
|
40
|
+
.tenant(),
|
|
41
|
+
email: text({ max: 120 }),
|
|
42
|
+
deletedAt: timestamp().nullable(),
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const posts = remember(
|
|
48
|
+
entity('ec_p_posts', {
|
|
49
|
+
columns: {
|
|
50
|
+
id: uuid().primaryKey(),
|
|
51
|
+
orgId: uuid()
|
|
52
|
+
.references(() => orgs.id)
|
|
53
|
+
.tenant(),
|
|
54
|
+
authorId: uuid().references(() => members.id),
|
|
55
|
+
reviewerId: uuid()
|
|
56
|
+
.references(() => members.id)
|
|
57
|
+
.nullable(),
|
|
58
|
+
title: text({ max: 120 }),
|
|
59
|
+
},
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const ORG = idAt(1);
|
|
64
|
+
const SEED = Array.from({ length: 6 }, (_, index) => ({
|
|
65
|
+
id: idAt(200 + index),
|
|
66
|
+
orgId: ORG,
|
|
67
|
+
title: `Post ${index}`,
|
|
68
|
+
}));
|
|
69
|
+
const ids = (batches: readonly (readonly { readonly id: string }[])[]) =>
|
|
70
|
+
batches.flat().map((row) => row.id);
|
|
71
|
+
|
|
72
|
+
const drain = async <Row>(batches: BatchIterator<Row>): Promise<Row[][]> => {
|
|
73
|
+
const seen: Row[][] = [];
|
|
74
|
+
for await (const batch of batches) seen.push([...batch]);
|
|
75
|
+
return seen;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** A point read batches only inside a request's context: `coalesce.ts` keys on it. */
|
|
79
|
+
const inRequest = <T>(read: () => Promise<T>): Promise<T> => runWithContext(ctxOf({}), read);
|
|
80
|
+
|
|
81
|
+
const batches: readonly EntityDriverCheck[] = [
|
|
82
|
+
checkOver(
|
|
83
|
+
{ walked },
|
|
84
|
+
'inBatches cuts one seed into the same batches, in key order',
|
|
85
|
+
async (db) => {
|
|
86
|
+
await db.walked.insertAll(SEED);
|
|
87
|
+
const even = await drain(db.walked.where({ orgId: ORG }).inBatches(2));
|
|
88
|
+
const uneven = await drain(db.walked.where({ orgId: ORG }).inBatches(4));
|
|
89
|
+
expect([even.map((batch) => batch.length), uneven.map((batch) => batch.length)]).toEqual([
|
|
90
|
+
[2, 2, 2],
|
|
91
|
+
[4, 2],
|
|
92
|
+
]);
|
|
93
|
+
expect([ids(even), ids(uneven)]).toEqual([
|
|
94
|
+
SEED.map((row) => row.id),
|
|
95
|
+
SEED.map((row) => row.id),
|
|
96
|
+
]);
|
|
97
|
+
},
|
|
98
|
+
),
|
|
99
|
+
checkOver(
|
|
100
|
+
{ walked },
|
|
101
|
+
'a batch cursor resumes at the next row: nothing repeated, nothing skipped',
|
|
102
|
+
async (db) => {
|
|
103
|
+
await db.walked.insertAll(SEED);
|
|
104
|
+
const walk = db.walked.where({ orgId: ORG }).inBatches(2);
|
|
105
|
+
const first: { id: string }[] = [];
|
|
106
|
+
for await (const batch of walk) {
|
|
107
|
+
first.push(...batch);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
const rest = await drain(db.walked.where({ orgId: ORG }).after(walk.cursor).inBatches(2));
|
|
111
|
+
expect([...first.map((row) => row.id), ...ids(rest)]).toEqual(SEED.map((row) => row.id));
|
|
112
|
+
},
|
|
113
|
+
),
|
|
114
|
+
checkOver(
|
|
115
|
+
{ bigs, decimals, ints },
|
|
116
|
+
'a numeric key is found under any spelling, alone or batched in one tick',
|
|
117
|
+
async (db, { driver }) => {
|
|
118
|
+
await db.bigs.insert({ id: '7', label: 'big' });
|
|
119
|
+
await db.decimals.insert({ id: '2.5', label: 'dec' });
|
|
120
|
+
await db.ints.insert({ id: 7, label: 'int' });
|
|
121
|
+
const [big, dec, int] = [driver.repo(bigs), driver.repo(decimals), driver.repo(ints)];
|
|
122
|
+
const label = async (read: () => Promise<{ readonly label: string } | null>) =>
|
|
123
|
+
(await inRequest(read))?.label;
|
|
124
|
+
for (const id of ['007', '+7', '7'])
|
|
125
|
+
expect([id, await label(() => big.findById(id))]).toEqual([id, 'big']);
|
|
126
|
+
for (const id of ['2.5', '2.50', '+02.500'])
|
|
127
|
+
expect([id, await label(() => dec.findById(id))]).toEqual([id, 'dec']);
|
|
128
|
+
const batched = await inRequest(() =>
|
|
129
|
+
Promise.all([big.findById('007'), big.findById('+7'), big.findById('7')]),
|
|
130
|
+
);
|
|
131
|
+
expect(batched.map((row) => row?.label)).toEqual(['big', 'big', 'big']);
|
|
132
|
+
// An integer key spelled as text is the number the column parses it as.
|
|
133
|
+
expect(await label(() => int.findById('007' as never))).toBe('int');
|
|
134
|
+
},
|
|
135
|
+
),
|
|
136
|
+
];
|
|
137
|
+
|
|
138
|
+
const [ANA, BEN, GONE] = [idAt(10), idAt(11), idAt(12)];
|
|
139
|
+
const [P1, P2, P3] = [idAt(20), idAt(21), idAt(22)];
|
|
140
|
+
/** A `hasMany` is named for the entity the rows come from, so both inbound keys take the long form. */
|
|
141
|
+
const [BY_AUTHOR, BY_REVIEWER] = ['ec_p_postsByAuthor', 'ec_p_postsByReviewer'];
|
|
142
|
+
const field = (row: unknown, key: string): unknown => (row as Record<string, unknown>)[key];
|
|
143
|
+
const idsIn = (rows: unknown): unknown =>
|
|
144
|
+
(rows as readonly unknown[]).map((row) => field(row, 'id'));
|
|
145
|
+
const SET = { orgs, members, posts };
|
|
146
|
+
|
|
147
|
+
const preloads: readonly EntityDriverCheck[] = [
|
|
148
|
+
checkOver(
|
|
149
|
+
SET,
|
|
150
|
+
'belongsTo attaches the row, and null for a key that resolves to nothing',
|
|
151
|
+
async (db) => {
|
|
152
|
+
await db.orgs.insert({ id: ORG, slug: 'ours' });
|
|
153
|
+
for (const id of [ANA, BEN, GONE])
|
|
154
|
+
await db.members.insert({ id, orgId: ORG, email: `${id}@example.com` });
|
|
155
|
+
await db.posts.insert({ id: P1, orgId: ORG, authorId: ANA, title: 'First' });
|
|
156
|
+
await db.posts.insert({
|
|
157
|
+
id: P2,
|
|
158
|
+
orgId: ORG,
|
|
159
|
+
authorId: ANA,
|
|
160
|
+
reviewerId: BEN,
|
|
161
|
+
title: 'Second',
|
|
162
|
+
});
|
|
163
|
+
await db.posts.insert({
|
|
164
|
+
id: P3,
|
|
165
|
+
orgId: ORG,
|
|
166
|
+
authorId: BEN,
|
|
167
|
+
reviewerId: GONE,
|
|
168
|
+
title: 'Third',
|
|
169
|
+
});
|
|
170
|
+
// Soft-deleted: a key every store holds, that a read no longer resolves.
|
|
171
|
+
await db.members.delete(GONE, { orgId: ORG });
|
|
172
|
+
const rows = await db.posts
|
|
173
|
+
.where({ orgId: ORG })
|
|
174
|
+
.orderBy('id')
|
|
175
|
+
.preload('author')
|
|
176
|
+
.preload('reviewer')
|
|
177
|
+
.all();
|
|
178
|
+
expect(field(rows[0], 'author')).toMatchObject({ id: ANA });
|
|
179
|
+
expect(Array.isArray(field(rows[0], 'author'))).toBe(false);
|
|
180
|
+
expect([
|
|
181
|
+
field(rows[0], 'reviewer'),
|
|
182
|
+
field(rows[2], 'reviewer'),
|
|
183
|
+
'reviewer' in (rows[2] as object),
|
|
184
|
+
]).toEqual([null, null, true]);
|
|
185
|
+
expect(field(rows[1], 'reviewer')).toMatchObject({ id: BEN });
|
|
186
|
+
const narrowed = await db.posts
|
|
187
|
+
.where({ orgId: ORG })
|
|
188
|
+
.orderBy('id')
|
|
189
|
+
.select({ id: true, title: true })
|
|
190
|
+
.preload('author')
|
|
191
|
+
.all();
|
|
192
|
+
expect(Object.keys(narrowed[0] as object).sort()).toEqual(['author', 'id', 'title']);
|
|
193
|
+
},
|
|
194
|
+
),
|
|
195
|
+
checkOver(SET, 'hasMany attaches an array in order, empty as [], never merged', async (db) => {
|
|
196
|
+
await db.orgs.insert({ id: ORG, slug: 'ours' });
|
|
197
|
+
for (const id of [ANA, BEN])
|
|
198
|
+
await db.members.insert({ id, orgId: ORG, email: `${id}@example.com` });
|
|
199
|
+
await db.posts.insert({ id: P1, orgId: ORG, authorId: ANA, title: 'First' });
|
|
200
|
+
await db.posts.insert({ id: P2, orgId: ORG, authorId: ANA, reviewerId: BEN, title: 'Second' });
|
|
201
|
+
await db.posts.insert({ id: P3, orgId: ORG, authorId: BEN, title: 'Third' });
|
|
202
|
+
const rows = await db.members
|
|
203
|
+
.where({ orgId: ORG })
|
|
204
|
+
.orderBy('id')
|
|
205
|
+
.preload(BY_AUTHOR)
|
|
206
|
+
.preload(BY_REVIEWER)
|
|
207
|
+
.all();
|
|
208
|
+
expect([idsIn(field(rows[0], BY_AUTHOR)), field(rows[0], BY_REVIEWER)]).toEqual([[P1, P2], []]);
|
|
209
|
+
expect([idsIn(field(rows[1], BY_AUTHOR)), idsIn(field(rows[1], BY_REVIEWER))]).toEqual([
|
|
210
|
+
[P3],
|
|
211
|
+
[P2],
|
|
212
|
+
]);
|
|
213
|
+
}),
|
|
214
|
+
];
|
|
215
|
+
|
|
216
|
+
export const BATCH_CHECKS: readonly EntityDriverCheck[] = [...batches, ...preloads];
|