@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.
- 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 +30 -51
- package/src/exercise.ts +12 -6
- 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/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/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,305 @@
|
|
|
1
|
+
// Entity conformance · what a VALUE means: the JSON `null` in a NOT NULL jsonb column, a money
|
|
2
|
+
// minor unit spelled as a `bigint`, a null test on a whole money property, and a value rule that
|
|
3
|
+
// refuses before the row lands. Lifted from `entity/src/{json-null,money-write,invariant}-parity`.
|
|
4
|
+
|
|
5
|
+
import { expect } from 'bun:test';
|
|
6
|
+
import { isUltimateError } from '@ultimat3/core';
|
|
7
|
+
import type { RowWrite } from '@ultimat3/entity';
|
|
8
|
+
import {
|
|
9
|
+
bigint,
|
|
10
|
+
decimal,
|
|
11
|
+
entity,
|
|
12
|
+
integer,
|
|
13
|
+
invariant,
|
|
14
|
+
json,
|
|
15
|
+
money,
|
|
16
|
+
t,
|
|
17
|
+
text,
|
|
18
|
+
url,
|
|
19
|
+
uuid,
|
|
20
|
+
} from '@ultimat3/entity';
|
|
21
|
+
import { remember } from './entity-conformance-entities';
|
|
22
|
+
import { checkOver, idAt, outcome } from './entity-conformance-support';
|
|
23
|
+
import type { EntityDriverCheck } from './entity-driver-conformance';
|
|
24
|
+
|
|
25
|
+
const shape = t.object({ a: t.number });
|
|
26
|
+
|
|
27
|
+
const events = remember(
|
|
28
|
+
entity('ec_v_events', {
|
|
29
|
+
columns: {
|
|
30
|
+
id: uuid().primaryKey(),
|
|
31
|
+
code: text({ max: 10 }).unique(),
|
|
32
|
+
/** NOT NULL in the table; `null` is a value the schema admits. */
|
|
33
|
+
payload: json(t.nullable(shape)),
|
|
34
|
+
/** Nullable in the table; `null` here is the column cleared. */
|
|
35
|
+
note: json(shape).nullable(),
|
|
36
|
+
},
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
/** NOT NULL, and the schema refuses `null`: the schema is what says so. */
|
|
41
|
+
const strict = remember(
|
|
42
|
+
entity('ec_v_strict', { columns: { id: uuid().primaryKey(), payload: json(shape) } }),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const AMOUNT = 129_900;
|
|
46
|
+
|
|
47
|
+
/** `.eq()` compares with `===`, the one operator that tells `129900n` from `129900`. */
|
|
48
|
+
const invoices = remember(
|
|
49
|
+
entity('ec_v_invoices', {
|
|
50
|
+
columns: { id: uuid().primaryKey(), reference: text({ max: 40 }), total: money() },
|
|
51
|
+
invariants: (c) => [invariant('ec_total_minor_is_the_amount', c.total.minor.eq(AMOUNT))],
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const priced = remember(
|
|
56
|
+
entity('ec_v_priced', { columns: { id: uuid().primaryKey(), price: money().nullable() } }),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const ledgers = remember(
|
|
60
|
+
entity('ec_v_ledgers', {
|
|
61
|
+
columns: {
|
|
62
|
+
id: uuid().primaryKey(),
|
|
63
|
+
total: bigint(),
|
|
64
|
+
rate: decimal({ precision: 8, scale: 2 }),
|
|
65
|
+
share: decimal({ precision: 2, scale: 2 }).nullable(),
|
|
66
|
+
code: text({ max: 20 }),
|
|
67
|
+
href: url().nullable(),
|
|
68
|
+
},
|
|
69
|
+
invariants: (c) => [
|
|
70
|
+
invariant('ec_total_non_negative', c.total.atLeast(0)),
|
|
71
|
+
invariant('ec_rate_fixed', c.rate.eq(1.5)),
|
|
72
|
+
invariant('ec_code_is_x', c.code.trimmed().eq('x')),
|
|
73
|
+
],
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const inOrder = (low: number, high: number): boolean => low <= high;
|
|
78
|
+
|
|
79
|
+
/** A rule only the app can judge: no CHECK exists, so the write is all that stands behind it. */
|
|
80
|
+
const ranges = remember(
|
|
81
|
+
entity('ec_v_ranges', {
|
|
82
|
+
columns: { id: uuid().primaryKey(), low: integer(), high: integer() },
|
|
83
|
+
invariants: (c) => [invariant('ec_low_first', c.satisfies(inOrder, ['low', 'high']))],
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const ID = idAt(1);
|
|
88
|
+
const REFUSED = { code: 'X_INVARIANT_VIOLATED' };
|
|
89
|
+
|
|
90
|
+
const jsonNull: readonly EntityDriverCheck[] = [
|
|
91
|
+
checkOver({ events }, 'a JSON null in a NOT NULL column is stored by every write', async (db) => {
|
|
92
|
+
await db.events.insert({ id: ID, code: 'a', payload: null });
|
|
93
|
+
expect(await db.events.where({ id: ID }).one()).toEqual({
|
|
94
|
+
id: ID,
|
|
95
|
+
code: 'a',
|
|
96
|
+
payload: null,
|
|
97
|
+
note: null,
|
|
98
|
+
});
|
|
99
|
+
await db.events.insert({ id: idAt(2), code: 'b', payload: { a: 1 } });
|
|
100
|
+
await db.events.update(idAt(2), { payload: null });
|
|
101
|
+
await db.events.insertAll([{ code: 'c', payload: null }]);
|
|
102
|
+
const rows = (await db.events.all()).map((row) => [row.code, row.payload]).sort();
|
|
103
|
+
expect(rows).toEqual([
|
|
104
|
+
['a', null],
|
|
105
|
+
['b', null],
|
|
106
|
+
['c', null],
|
|
107
|
+
]);
|
|
108
|
+
}),
|
|
109
|
+
checkOver(
|
|
110
|
+
{ events },
|
|
111
|
+
'upsertAll stores a JSON null, new row and collision alike',
|
|
112
|
+
async (db) => {
|
|
113
|
+
await db.events.upsertAll([{ code: 'b', payload: null }], { onConflict: ['code'] });
|
|
114
|
+
await db.events.insertAll([{ code: 'a', payload: { a: 1 } }]);
|
|
115
|
+
await db.events.upsertAll([{ code: 'a', payload: null }], { onConflict: ['code'] });
|
|
116
|
+
const rows = (await db.events.all()).map((row) => [row.code, row.payload]).sort();
|
|
117
|
+
expect(rows).toEqual([
|
|
118
|
+
['a', null],
|
|
119
|
+
['b', null],
|
|
120
|
+
]);
|
|
121
|
+
},
|
|
122
|
+
['upsert'],
|
|
123
|
+
),
|
|
124
|
+
checkOver({ events }, 'a null test matches no row: the column holds no SQL NULL', async (db) => {
|
|
125
|
+
await db.events.insert({ id: ID, code: 'a', payload: null });
|
|
126
|
+
expect([
|
|
127
|
+
(await db.events.where({ payload: null }).all()).length,
|
|
128
|
+
(await db.events.andWhere('payload', 'is-null', null).all()).length,
|
|
129
|
+
(await db.events.andWhere('payload', 'is-not-null', null).all()).length,
|
|
130
|
+
(await db.events.andWhere('payload', 'neq', null).all()).length,
|
|
131
|
+
(await db.events.andWhere('payload', 'in', [null]).all()).length,
|
|
132
|
+
(await db.events.andWhere('payload', 'contains', null).all()).length,
|
|
133
|
+
]).toEqual([0, 0, 1, 1, 0, 1]);
|
|
134
|
+
}),
|
|
135
|
+
checkOver({ strict }, 'a schema that refuses null refuses it, by the column', async (db) => {
|
|
136
|
+
const error: unknown = await db.strict.insert({ id: ID, payload: null as never }).then(
|
|
137
|
+
() => undefined,
|
|
138
|
+
(caught: unknown) => caught,
|
|
139
|
+
);
|
|
140
|
+
const said = isUltimateError(error) ? `${error.code} ${error.cause}` : 'not refused';
|
|
141
|
+
expect(said).toStartWith('X_INVARIANT_VIOLATED column.json:');
|
|
142
|
+
}),
|
|
143
|
+
checkOver(
|
|
144
|
+
{ events },
|
|
145
|
+
'a nullable json null is SQL NULL: cleared, and absent alike',
|
|
146
|
+
async (db) => {
|
|
147
|
+
await db.events.insert({ id: ID, code: 'a', payload: { a: 1 }, note: { a: 2 } });
|
|
148
|
+
await db.events.update(ID, { note: null });
|
|
149
|
+
await db.events.insert({ id: idAt(2), code: 'b', payload: { a: 1 } });
|
|
150
|
+
expect(await db.events.where({ id: ID }).one()).toEqual({
|
|
151
|
+
id: ID,
|
|
152
|
+
code: 'a',
|
|
153
|
+
payload: { a: 1 },
|
|
154
|
+
note: null,
|
|
155
|
+
});
|
|
156
|
+
expect((await db.events.where({ note: null }).all()).length).toBe(2);
|
|
157
|
+
},
|
|
158
|
+
),
|
|
159
|
+
];
|
|
160
|
+
|
|
161
|
+
/** A minor unit read straight off a `bigint` column, reaching a write with no conversion. */
|
|
162
|
+
const wide = (): RowWrite<typeof invoices.$row> => ({
|
|
163
|
+
id: ID,
|
|
164
|
+
reference: 'INV-1',
|
|
165
|
+
total: { minor: BigInt(AMOUNT), currency: 'EUR' },
|
|
166
|
+
});
|
|
167
|
+
const NARROW = { minor: AMOUNT, currency: 'EUR' };
|
|
168
|
+
|
|
169
|
+
const moneyWrites: readonly EntityDriverCheck[] = [
|
|
170
|
+
checkOver(
|
|
171
|
+
{ invoices },
|
|
172
|
+
'a bigint minor unit is narrowed before insert judges it',
|
|
173
|
+
async (_db, { driver }) => {
|
|
174
|
+
const written = wide();
|
|
175
|
+
const row = await driver.repo(invoices).insert(written);
|
|
176
|
+
expect(row.total).toEqual(NARROW);
|
|
177
|
+
expect(JSON.parse(JSON.stringify(row)).total).toEqual(NARROW);
|
|
178
|
+
// Narrowing copies; the row the caller built is theirs and is unchanged.
|
|
179
|
+
expect(written.total).toEqual({ minor: BigInt(AMOUNT), currency: 'EUR' });
|
|
180
|
+
},
|
|
181
|
+
),
|
|
182
|
+
checkOver(
|
|
183
|
+
{ invoices },
|
|
184
|
+
'insertAll judges the batch after narrowing it',
|
|
185
|
+
async (_db, { driver }) => {
|
|
186
|
+
const [row] = await driver.repo(invoices).insertAll([wide()]);
|
|
187
|
+
expect(row?.total).toEqual(NARROW);
|
|
188
|
+
},
|
|
189
|
+
),
|
|
190
|
+
checkOver(
|
|
191
|
+
{ invoices },
|
|
192
|
+
'upsertAll judges the incoming rows after narrowing them',
|
|
193
|
+
async (_db, { driver }) => {
|
|
194
|
+
const [row] = await driver.repo(invoices).upsertAll([wide()], { onConflict: ['id'] });
|
|
195
|
+
expect(row?.total).toEqual(NARROW);
|
|
196
|
+
},
|
|
197
|
+
['upsert'],
|
|
198
|
+
),
|
|
199
|
+
checkOver({ priced }, 'a null test on a whole money property reads the amount', async (db) => {
|
|
200
|
+
const [free, paid] = [idAt(11), idAt(12)];
|
|
201
|
+
await db.priced.insert({ id: free, price: null });
|
|
202
|
+
await db.priced.insert({ id: paid, price: { minor: 100, currency: 'EUR' } });
|
|
203
|
+
const ids = async (op: 'neq' | 'is-null' | 'is-not-null'): Promise<readonly string[]> =>
|
|
204
|
+
(await db.priced.andWhere('price', op, null).all()).map((row) => row.id);
|
|
205
|
+
expect((await db.priced.where({ price: null }).all()).map((row) => row.id)).toEqual([free]);
|
|
206
|
+
expect([await ids('neq'), await ids('is-null'), await ids('is-not-null')]).toEqual([
|
|
207
|
+
[paid],
|
|
208
|
+
[free],
|
|
209
|
+
[paid],
|
|
210
|
+
]);
|
|
211
|
+
const part = await db.priced.andWhere('price.minor' as never, 'is-null', null).all();
|
|
212
|
+
expect(part.map((row) => row.id)).toEqual([free]);
|
|
213
|
+
// An amount compared whole is refused, rows or none: no single column holds one.
|
|
214
|
+
const amount = { minor: 100, currency: 'EUR' };
|
|
215
|
+
expect(await outcome(() => db.priced.where({ price: amount }).all())).toEqual(REFUSED);
|
|
216
|
+
await db.priced.deleteWhere({ id: paid });
|
|
217
|
+
await db.priced.deleteWhere({ id: free });
|
|
218
|
+
expect(await outcome(() => db.priced.where({ price: amount }).all())).toEqual(REFUSED);
|
|
219
|
+
}),
|
|
220
|
+
];
|
|
221
|
+
|
|
222
|
+
const GOOD = { id: ID, total: '5', rate: '1.5', code: 'x' };
|
|
223
|
+
|
|
224
|
+
const valueRules: readonly EntityDriverCheck[] = [
|
|
225
|
+
checkOver(
|
|
226
|
+
{ ledgers },
|
|
227
|
+
"a numeric rule over bigint and decimal: '5' >= 0, '1.5' = 1.5",
|
|
228
|
+
async (db) => {
|
|
229
|
+
const stored = await db.ledgers.insert(GOOD);
|
|
230
|
+
expect([stored.total, stored.rate]).toEqual(['5', '1.50']);
|
|
231
|
+
const big = await db.ledgers.insert({ ...GOOD, id: idAt(2), total: '9007199254740993' });
|
|
232
|
+
expect(big.total).toBe('9007199254740993');
|
|
233
|
+
},
|
|
234
|
+
),
|
|
235
|
+
checkOver({ ledgers }, 'a row breaking a value rule is refused and not stored', async (db) => {
|
|
236
|
+
expect(await outcome(() => db.ledgers.insert({ ...GOOD, total: '-1' }))).toEqual(REFUSED);
|
|
237
|
+
expect(await outcome(() => db.ledgers.insert({ ...GOOD, rate: '1.6' }))).toEqual(REFUSED);
|
|
238
|
+
expect(await db.ledgers.all()).toHaveLength(0);
|
|
239
|
+
}),
|
|
240
|
+
checkOver({ ledgers }, 'trimmed() strips spaces only, as btrim() does', async (db) => {
|
|
241
|
+
expect(
|
|
242
|
+
await outcome(async () => (await db.ledgers.insert({ ...GOOD, code: ' x ' })).code),
|
|
243
|
+
).toEqual({ ok: ' x ' });
|
|
244
|
+
for (const [index, code] of ['\tx', 'x\n'].entries()) {
|
|
245
|
+
expect(
|
|
246
|
+
await outcome(() => db.ledgers.insert({ ...GOOD, id: idAt(index + 2), code })),
|
|
247
|
+
).toEqual(REFUSED);
|
|
248
|
+
}
|
|
249
|
+
}),
|
|
250
|
+
checkOver({ ledgers }, 'int8 ends where Postgres ends it; numeric(2,2) holds 0.5', async (db) => {
|
|
251
|
+
const top = await db.ledgers.insert({ ...GOOD, total: '9223372036854775807' });
|
|
252
|
+
expect(top.total).toBe('9223372036854775807');
|
|
253
|
+
const past = { ...GOOD, id: idAt(2), total: '9223372036854775808' };
|
|
254
|
+
expect(await outcome(() => db.ledgers.insert(past))).toEqual(REFUSED);
|
|
255
|
+
expect((await db.ledgers.insert({ ...GOOD, id: idAt(3), share: '0.5' })).share).toBe('0.50');
|
|
256
|
+
}),
|
|
257
|
+
checkOver({ ledgers }, 'url() refuses what is no url before any CHECK can', async (db) => {
|
|
258
|
+
for (const href of [' https://a.b', 'ht\ttps://a.b', 'https:/a.b']) {
|
|
259
|
+
expect([href, await outcome(() => db.ledgers.insert({ ...GOOD, href }))]).toEqual([
|
|
260
|
+
href,
|
|
261
|
+
REFUSED,
|
|
262
|
+
]);
|
|
263
|
+
}
|
|
264
|
+
}),
|
|
265
|
+
checkOver({ ranges }, 'a refused app-only rule leaves the row as it was', async (db) => {
|
|
266
|
+
await db.ranges.insert({ id: ID, low: 1, high: 5 });
|
|
267
|
+
expect(await outcome(() => db.ranges.update(ID, { low: 9 }))).toEqual(REFUSED);
|
|
268
|
+
expect(await db.ranges.where({ id: ID }).one()).toEqual({ id: ID, low: 1, high: 5 });
|
|
269
|
+
}),
|
|
270
|
+
checkOver({ ranges }, 'a refused updateWhere leaves every matched row as it was', async (db) => {
|
|
271
|
+
// The lower id takes `high: 20`, so it holds; the row after it does not. A driver writing row
|
|
272
|
+
// by row stores the first before the second refuses — the half-written state this catches.
|
|
273
|
+
await db.ranges.insert({ id: ID, low: 1, high: 20 });
|
|
274
|
+
await db.ranges.insert({ id: idAt(2), low: 1, high: 5 });
|
|
275
|
+
expect(await outcome(() => db.ranges.updateWhere({ low: 1 }, { low: 9 }))).toEqual(REFUSED);
|
|
276
|
+
expect((await db.ranges.orderBy('high').all()).map((row) => row.low)).toEqual([1, 1]);
|
|
277
|
+
}),
|
|
278
|
+
checkOver({ ranges }, 'a write that holds still lands', async (db) => {
|
|
279
|
+
await db.ranges.insert({ id: ID, low: 1, high: 5 });
|
|
280
|
+
await db.ranges.update(ID, { low: 4 });
|
|
281
|
+
expect(await db.ranges.updateWhere({ low: 4 }, { high: 9 })).toBe(1);
|
|
282
|
+
expect(await db.ranges.where({ id: ID }).one()).toEqual({ id: ID, low: 4, high: 9 });
|
|
283
|
+
}),
|
|
284
|
+
];
|
|
285
|
+
|
|
286
|
+
const malformedIds: readonly EntityDriverCheck[] = [
|
|
287
|
+
checkOver(
|
|
288
|
+
{ priced },
|
|
289
|
+
'a malformed uuid is refused before the store, by every write',
|
|
290
|
+
async (_db, { driver }) => {
|
|
291
|
+
const repo = driver.repo(priced);
|
|
292
|
+
const bad = '00000000-0000-7000-8000-0000000000p0';
|
|
293
|
+
expect(await outcome(() => repo.insert({ id: bad, price: null }))).toEqual(REFUSED);
|
|
294
|
+
expect(await outcome(() => repo.insertAll([{ id: bad, price: null }]))).toEqual(REFUSED);
|
|
295
|
+
expect(await repo.count({})).toBe(0);
|
|
296
|
+
},
|
|
297
|
+
),
|
|
298
|
+
];
|
|
299
|
+
|
|
300
|
+
export const VALUE_CHECKS: readonly EntityDriverCheck[] = [
|
|
301
|
+
...jsonNull,
|
|
302
|
+
...moneyWrites,
|
|
303
|
+
...valueRules,
|
|
304
|
+
...malformedIds,
|
|
305
|
+
];
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Entity conformance · a searchable entity's VERSION: every write stores its `onUpdateNow()` column
|
|
2
|
+
// strictly past the stored one, inside one millisecond too — an update, a filtered update and an
|
|
3
|
+
// upsert's collision. An external search index keeps only newer, so an equal version is a lost
|
|
4
|
+
// write. A store's own `upsert` merges the collision itself: `advanceVersion` is its half.
|
|
5
|
+
|
|
6
|
+
import { expect } from 'bun:test';
|
|
7
|
+
import type { Clock } from '@ultimat3/core';
|
|
8
|
+
import { ctxOf, runWithContext } from '@ultimat3/core';
|
|
9
|
+
import { entity, 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 docs = remember(
|
|
15
|
+
entity('ec_v_docs', {
|
|
16
|
+
columns: {
|
|
17
|
+
id: uuid().primaryKey(),
|
|
18
|
+
code: text({ max: 20 }).unique(),
|
|
19
|
+
title: text({ max: 40 }).searchable(),
|
|
20
|
+
updatedAt: timestamp().defaultNow().onUpdateNow(),
|
|
21
|
+
},
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const AT = new Date('2026-11-10T10:00:00.000Z');
|
|
26
|
+
|
|
27
|
+
/** A clock that never moves: every write of the check lands in one millisecond. */
|
|
28
|
+
const stopped: Clock = { now: () => new Date(AT), monotonic: () => AT.getTime() };
|
|
29
|
+
const inOneMs = <T>(work: () => Promise<T>): Promise<T> =>
|
|
30
|
+
runWithContext(ctxOf({ clock: stopped }), work);
|
|
31
|
+
|
|
32
|
+
const ROW = { id: idAt(901), code: 'a', title: 'one' };
|
|
33
|
+
|
|
34
|
+
export const VERSION_CHECKS: readonly EntityDriverCheck[] = [
|
|
35
|
+
checkOver({ docs }, "a searchable entity's update in the same ms is a newer version", (db) =>
|
|
36
|
+
inOneMs(async () => {
|
|
37
|
+
const first = await db.docs.insert(ROW);
|
|
38
|
+
const second = await db.docs.update(ROW.id, { title: 'two' });
|
|
39
|
+
const third = await db.docs.updateWhere({ code: 'a' }, { title: 'three' });
|
|
40
|
+
const stored = await db.docs.where({ code: 'a' }).one();
|
|
41
|
+
expect(third).toBe(1);
|
|
42
|
+
expect(second.updatedAt.getTime()).toBeGreaterThan(first.updatedAt.getTime());
|
|
43
|
+
expect(stored?.updatedAt.getTime()).toBeGreaterThan(second.updatedAt.getTime());
|
|
44
|
+
}),
|
|
45
|
+
),
|
|
46
|
+
checkOver(
|
|
47
|
+
{ docs },
|
|
48
|
+
"a searchable entity's upsert collision in the same ms is a newer version",
|
|
49
|
+
(db) =>
|
|
50
|
+
inOneMs(async () => {
|
|
51
|
+
const first = await db.docs.insert(ROW);
|
|
52
|
+
const [second] = await db.docs.upsertAll([{ ...ROW, title: 'two' }], {
|
|
53
|
+
onConflict: ['code'],
|
|
54
|
+
});
|
|
55
|
+
expect(second?.title).toBe('two');
|
|
56
|
+
expect(second?.updatedAt.getTime()).toBeGreaterThan(first.updatedAt.getTime());
|
|
57
|
+
}),
|
|
58
|
+
['upsert'],
|
|
59
|
+
),
|
|
60
|
+
];
|