@ultimat3/testing 31.1.0 → 31.2.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/README.md +1 -0
- package/package.json +22 -22
- package/src/entity-conformance-writes.ts +26 -22
- package/src/factories.ts +19 -9
- package/src/factory-api.ts +12 -4
- package/src/factory-types.ts +31 -0
- package/src/index.ts +3 -0
package/README.md
CHANGED
|
@@ -383,6 +383,7 @@ postFactory.attributesFor(); // an action input: no i
|
|
|
383
383
|
| | |
|
|
384
384
|
|---|---|
|
|
385
385
|
| **strategies** | `build` (memory, never a database), `create` (writes, answers the row AS STORED: declared defaults, generated columns, timestamps), `attributesFor` (an input: no primary key, no `belongsTo` key, no transient), `buildList` / `createList` |
|
|
386
|
+
| **literals** | `defaults` is checked against the entity's columns as written: `status: 'open'` for an `enumerated(['open', 'closed'])` column compiles with no `as const`, `status: 'nope'` does not. Once checked, a default's literal widens (``title: `t${n}` `` → `string`), so `build({ title: 'x' })` compiles (`factories-literals.test.ts`) |
|
|
386
387
|
| **sequence** | `index` (`n` above), per factory, shared by every `with()` view and `extend()` child — ids never repeat |
|
|
387
388
|
| **trait** | a named partial (or `(n, ids, transient) => partial`). `with('a', 'b')` composes left to right; a call's override wins. An undeclared one fails at `with()` (`X_TEST_FACTORY_TRAIT_UNKNOWN`) |
|
|
388
389
|
| **transient** | declared defaults a call may override; read by `defaults`, traits and callbacks; never written |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/testing",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.2.0",
|
|
4
4
|
"description": "Test harness: cloned template DBs per worker, frozen clock, sealed network, 6 test types",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -47,26 +47,26 @@
|
|
|
47
47
|
"test": "bun test"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@ultimat3/action": "31.
|
|
51
|
-
"@ultimat3/ai": "31.
|
|
52
|
-
"@ultimat3/auth": "31.
|
|
53
|
-
"@ultimat3/cache": "31.
|
|
54
|
-
"@ultimat3/core": "31.
|
|
55
|
-
"@ultimat3/db": "31.
|
|
56
|
-
"@ultimat3/entity": "31.
|
|
57
|
-
"@ultimat3/http": "31.
|
|
58
|
-
"@ultimat3/i18n": "31.
|
|
59
|
-
"@ultimat3/jobs": "31.
|
|
60
|
-
"@ultimat3/mail": "31.
|
|
61
|
-
"@ultimat3/notify": "31.
|
|
62
|
-
"@ultimat3/policy": "31.
|
|
63
|
-
"@ultimat3/pwa": "31.
|
|
64
|
-
"@ultimat3/query": "31.
|
|
65
|
-
"@ultimat3/realtime": "31.
|
|
66
|
-
"@ultimat3/render": "31.
|
|
67
|
-
"@ultimat3/schema": "31.
|
|
68
|
-
"@ultimat3/scraping": "31.
|
|
69
|
-
"@ultimat3/seo": "31.
|
|
70
|
-
"@ultimat3/time": "31.
|
|
50
|
+
"@ultimat3/action": "31.2.0",
|
|
51
|
+
"@ultimat3/ai": "31.2.0",
|
|
52
|
+
"@ultimat3/auth": "31.2.0",
|
|
53
|
+
"@ultimat3/cache": "31.2.0",
|
|
54
|
+
"@ultimat3/core": "31.2.0",
|
|
55
|
+
"@ultimat3/db": "31.2.0",
|
|
56
|
+
"@ultimat3/entity": "31.2.0",
|
|
57
|
+
"@ultimat3/http": "31.2.0",
|
|
58
|
+
"@ultimat3/i18n": "31.2.0",
|
|
59
|
+
"@ultimat3/jobs": "31.2.0",
|
|
60
|
+
"@ultimat3/mail": "31.2.0",
|
|
61
|
+
"@ultimat3/notify": "31.2.0",
|
|
62
|
+
"@ultimat3/policy": "31.2.0",
|
|
63
|
+
"@ultimat3/pwa": "31.2.0",
|
|
64
|
+
"@ultimat3/query": "31.2.0",
|
|
65
|
+
"@ultimat3/realtime": "31.2.0",
|
|
66
|
+
"@ultimat3/render": "31.2.0",
|
|
67
|
+
"@ultimat3/schema": "31.2.0",
|
|
68
|
+
"@ultimat3/scraping": "31.2.0",
|
|
69
|
+
"@ultimat3/seo": "31.2.0",
|
|
70
|
+
"@ultimat3/time": "31.2.0"
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
// and a column stores only what Postgres stores. Lifted from `entity/src/write-parity.test.ts`.
|
|
4
4
|
|
|
5
5
|
import { expect } from 'bun:test';
|
|
6
|
+
import { isUltimateError } from '@ultimat3/core';
|
|
7
|
+
import { SQLSTATE, sqlState } from '@ultimat3/db';
|
|
6
8
|
import {
|
|
7
9
|
arrayOf,
|
|
8
10
|
bigint,
|
|
@@ -106,7 +108,19 @@ const docs = remember(
|
|
|
106
108
|
);
|
|
107
109
|
|
|
108
110
|
const ID = idAt(1);
|
|
109
|
-
|
|
111
|
+
/** A duplicate key: the framework's code AND Postgres' SQLSTATE, on every driver — a caller that
|
|
112
|
+
* branches on `sqlState(error)` (an upsert retry, a "taken" message) must not see Postgres only. */
|
|
113
|
+
const DUPLICATE = { code: 'X_DB_UNIQUE_VIOLATION', sqlState: SQLSTATE.uniqueViolation };
|
|
114
|
+
|
|
115
|
+
/** The refusal's code and SQLSTATE, or the value when nothing was refused. */
|
|
116
|
+
const duplicate = async (work: () => Promise<unknown>): Promise<unknown> => {
|
|
117
|
+
try {
|
|
118
|
+
return { ok: await work() };
|
|
119
|
+
} catch (error) {
|
|
120
|
+
return { code: isUltimateError(error) ? error.code : 'uncoded', sqlState: sqlState(error) };
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
110
124
|
const REFUSED = { code: 'X_INVARIANT_VIOLATED' };
|
|
111
125
|
|
|
112
126
|
const undefinedIsAbsent: readonly EntityDriverCheck[] = [
|
|
@@ -142,23 +156,19 @@ const undefinedIsAbsent: readonly EntityDriverCheck[] = [
|
|
|
142
156
|
const keysAreKept: readonly EntityDriverCheck[] = [
|
|
143
157
|
checkOver({ tags }, 'a duplicate primary key is refused, not a silent replace', async (db) => {
|
|
144
158
|
await db.tags.insert({ code: 'a', slug: 'one' });
|
|
145
|
-
expect(await
|
|
146
|
-
code: UNIQUE,
|
|
147
|
-
});
|
|
159
|
+
expect(await duplicate(() => db.tags.insert({ code: 'a', slug: 'two' }))).toEqual(DUPLICATE);
|
|
148
160
|
expect((await db.tags.all()).map((row) => row.slug)).toEqual(['one']);
|
|
149
161
|
}),
|
|
150
162
|
checkOver({ tags }, 'a duplicate unique column is refused', async (db) => {
|
|
151
163
|
await db.tags.insert({ code: 'a', slug: 'one' });
|
|
152
|
-
expect(await
|
|
153
|
-
code: UNIQUE,
|
|
154
|
-
});
|
|
164
|
+
expect(await duplicate(() => db.tags.insert({ code: 'b', slug: 'one' }))).toEqual(DUPLICATE);
|
|
155
165
|
}),
|
|
156
166
|
checkOver({ tags }, 'insertAll with a duplicate inside the batch stores nothing', async (db) => {
|
|
157
167
|
const batch = [
|
|
158
168
|
{ code: 'a', slug: 'one' },
|
|
159
169
|
{ code: 'a', slug: 'two' },
|
|
160
170
|
];
|
|
161
|
-
expect(await
|
|
171
|
+
expect(await duplicate(() => db.tags.insertAll(batch))).toEqual(DUPLICATE);
|
|
162
172
|
expect(await db.tags.all()).toHaveLength(0);
|
|
163
173
|
}),
|
|
164
174
|
checkOver({ tags }, 'a patch moving the primary key leaves one row, not two', async (db) => {
|
|
@@ -194,9 +204,9 @@ const keysAreKept: readonly EntityDriverCheck[] = [
|
|
|
194
204
|
async (db) => {
|
|
195
205
|
await db.notes.insert({ id: idAt(1), title: 'one', body: 'same' });
|
|
196
206
|
await db.notes.insert({ id: idAt(2), title: 'two', body: 'same' });
|
|
197
|
-
expect(
|
|
198
|
-
|
|
199
|
-
|
|
207
|
+
expect(
|
|
208
|
+
await duplicate(() => db.notes.updateWhere({ body: 'same' }, { id: idAt(3) })),
|
|
209
|
+
).toEqual(DUPLICATE);
|
|
200
210
|
const kept = (await db.notes.all()).map((row) => [row.id, row.title, row.body]).sort();
|
|
201
211
|
expect(kept).toEqual([
|
|
202
212
|
[idAt(1), 'one', 'same'],
|
|
@@ -211,10 +221,8 @@ const keysAreKept: readonly EntityDriverCheck[] = [
|
|
|
211
221
|
await db.countries.insert({ id: idAt(1), iso: 'AA', name: 'Same' });
|
|
212
222
|
await db.countries.insert({ id: idAt(2), iso: 'BB', name: 'Same' });
|
|
213
223
|
expect(
|
|
214
|
-
await
|
|
215
|
-
).toEqual(
|
|
216
|
-
code: UNIQUE,
|
|
217
|
-
});
|
|
224
|
+
await duplicate(() => db.countries.updateWhere({ name: 'Same' }, { iso: 'ZZ' })),
|
|
225
|
+
).toEqual(DUPLICATE);
|
|
218
226
|
const kept = (await db.countries.all()).map((row) => [row.id, row.iso]).sort();
|
|
219
227
|
expect(kept).toEqual([
|
|
220
228
|
[idAt(1), 'AA'],
|
|
@@ -224,9 +232,7 @@ const keysAreKept: readonly EntityDriverCheck[] = [
|
|
|
224
232
|
),
|
|
225
233
|
checkOver({ docs }, 'a unique declared as an invariant refuses a second row', async (db) => {
|
|
226
234
|
await db.docs.insert({ id: idAt(1), slug: 'one' });
|
|
227
|
-
expect(await
|
|
228
|
-
code: UNIQUE,
|
|
229
|
-
});
|
|
235
|
+
expect(await duplicate(() => db.docs.insert({ id: idAt(2), slug: 'one' }))).toEqual(DUPLICATE);
|
|
230
236
|
}),
|
|
231
237
|
checkOver(
|
|
232
238
|
{ docs },
|
|
@@ -234,14 +240,12 @@ const keysAreKept: readonly EntityDriverCheck[] = [
|
|
|
234
240
|
async (db) => {
|
|
235
241
|
await db.docs.insert({ id: idAt(1), slug: 'one' });
|
|
236
242
|
await db.docs.insert({ id: idAt(2), slug: 'two' });
|
|
237
|
-
expect(await
|
|
238
|
-
code: UNIQUE,
|
|
239
|
-
});
|
|
243
|
+
expect(await duplicate(() => db.docs.update(idAt(2), { slug: 'one' }))).toEqual(DUPLICATE);
|
|
240
244
|
const pair = [
|
|
241
245
|
{ id: idAt(3), slug: 'three' },
|
|
242
246
|
{ id: idAt(4), slug: 'three' },
|
|
243
247
|
];
|
|
244
|
-
expect(await
|
|
248
|
+
expect(await duplicate(() => db.docs.insertAll(pair))).toEqual(DUPLICATE);
|
|
245
249
|
expect((await db.docs.all()).map((row) => row.slug).sort()).toEqual(['one', 'two']);
|
|
246
250
|
},
|
|
247
251
|
),
|
package/src/factories.ts
CHANGED
|
@@ -10,10 +10,12 @@ import { persistRow } from './factory-persist';
|
|
|
10
10
|
import { attributesOf, type FactorySpec, rowSteps, type Step } from './factory-pipeline';
|
|
11
11
|
import type {
|
|
12
12
|
BelongsToMap,
|
|
13
|
+
DefaultsBound,
|
|
13
14
|
EntityLike,
|
|
14
15
|
FactoryIds,
|
|
15
16
|
HasManyMap,
|
|
16
17
|
None,
|
|
18
|
+
SettledAttrs,
|
|
17
19
|
StoredOf,
|
|
18
20
|
TraitMap,
|
|
19
21
|
} from './factory-types';
|
|
@@ -162,22 +164,30 @@ function extended(
|
|
|
162
164
|
}
|
|
163
165
|
|
|
164
166
|
export function defineFactory<
|
|
165
|
-
TAttrs extends
|
|
167
|
+
TAttrs extends DefaultsBound<TAttrs, E>,
|
|
166
168
|
E extends EntityLike = EntityLike,
|
|
167
169
|
TTransient extends object = None,
|
|
168
|
-
TTraits extends TraitMap<
|
|
169
|
-
TAttrs & StoredOf<E, TAttrs
|
|
170
|
+
TTraits extends TraitMap<
|
|
171
|
+
SettledAttrs<TAttrs> & StoredOf<E, SettledAttrs<TAttrs>>,
|
|
170
172
|
TTransient
|
|
171
|
-
>,
|
|
173
|
+
> = TraitMap<SettledAttrs<TAttrs> & StoredOf<E, SettledAttrs<TAttrs>>, TTransient>,
|
|
172
174
|
TBelongs extends BelongsToMap = None,
|
|
173
175
|
TMany extends HasManyMap = None,
|
|
174
176
|
>(
|
|
175
177
|
entity: E,
|
|
176
|
-
options: FactoryOptions<
|
|
178
|
+
options: FactoryOptions<
|
|
179
|
+
TAttrs,
|
|
180
|
+
TTraits,
|
|
181
|
+
TTransient,
|
|
182
|
+
TBelongs,
|
|
183
|
+
TMany,
|
|
184
|
+
StoredOf<E, SettledAttrs<TAttrs>>,
|
|
185
|
+
E
|
|
186
|
+
>,
|
|
177
187
|
): Factory<
|
|
178
|
-
TAttrs
|
|
188
|
+
SettledAttrs<TAttrs>,
|
|
179
189
|
Extract<keyof TTraits, string>,
|
|
180
|
-
StoredOf<E, TAttrs
|
|
190
|
+
StoredOf<E, SettledAttrs<TAttrs>>,
|
|
181
191
|
TTransient,
|
|
182
192
|
TBelongs,
|
|
183
193
|
TMany
|
|
@@ -211,9 +221,9 @@ export function defineFactory<
|
|
|
211
221
|
afterCreate: erased.afterCreate === undefined ? [] : [erased.afterCreate.bind(options)],
|
|
212
222
|
};
|
|
213
223
|
return view(spec, sequence, seed, []) as unknown as Factory<
|
|
214
|
-
TAttrs
|
|
224
|
+
SettledAttrs<TAttrs>,
|
|
215
225
|
Extract<keyof TTraits, string>,
|
|
216
|
-
StoredOf<E, TAttrs
|
|
226
|
+
StoredOf<E, SettledAttrs<TAttrs>>,
|
|
217
227
|
TTransient,
|
|
218
228
|
TBelongs,
|
|
219
229
|
TMany
|
package/src/factory-api.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
ForeignKeysOf,
|
|
13
13
|
HasManyMap,
|
|
14
14
|
None,
|
|
15
|
+
SettledAttrs,
|
|
15
16
|
TraitMap,
|
|
16
17
|
WriteBound,
|
|
17
18
|
} from './factory-types';
|
|
@@ -31,19 +32,26 @@ export interface FactoryOptions<
|
|
|
31
32
|
* Every column the factory owns; called once per row. `index` is the sequence. It must name each
|
|
32
33
|
* `belongsTo` key, so a key that is not a column is a compile error HERE: a check written as a
|
|
33
34
|
* constraint on `belongsTo` fixed the row type before this was read, and every row became `object`.
|
|
35
|
+
* `Readonly<TAttrs>` is the slot that keeps an enum literal literal (`DefaultsBound`).
|
|
34
36
|
*/
|
|
35
37
|
defaults(
|
|
36
38
|
index: number,
|
|
37
39
|
ids: FactoryIds,
|
|
38
40
|
transient: TTransient,
|
|
39
|
-
): TAttrs & WriteBound<E> & Readonly<Record<ForeignKeysOf<TBelongs>, unknown>>;
|
|
41
|
+
): TAttrs & Readonly<TAttrs> & WriteBound<E> & Readonly<Record<ForeignKeysOf<TBelongs>, unknown>>;
|
|
40
42
|
readonly traits?: TTraits;
|
|
41
43
|
/** Attributes a call may pass that are never written: read by `defaults`, traits and callbacks. */
|
|
42
44
|
readonly transient?: TTransient;
|
|
43
45
|
readonly belongsTo?: TBelongs;
|
|
44
46
|
readonly hasMany?: TMany;
|
|
45
|
-
/**
|
|
46
|
-
|
|
47
|
+
/**
|
|
48
|
+
* After the row is assembled, before any write — both strategies. The row is `build()`'s own
|
|
49
|
+
* type: a column holds its declared union, never the widened `SettledAttrs` one.
|
|
50
|
+
*/
|
|
51
|
+
afterBuild?(
|
|
52
|
+
row: NoInfer<SettledAttrs<TAttrs> & Partial<TRow>>,
|
|
53
|
+
context: FactoryContext<TTransient>,
|
|
54
|
+
): void;
|
|
47
55
|
/** After the row and its `hasMany` children are stored — `create()` only. */
|
|
48
56
|
afterCreate?(
|
|
49
57
|
row: NoInfer<Created<TRow, TMany>>,
|
|
@@ -68,7 +76,7 @@ export interface FactoryExtension<
|
|
|
68
76
|
readonly traits?: TTraits;
|
|
69
77
|
readonly transient?: Partial<TTransient>;
|
|
70
78
|
readonly hasMany?: TMoreMany;
|
|
71
|
-
afterBuild?(row: TAttrs
|
|
79
|
+
afterBuild?(row: TAttrs & Partial<TRow>, context: FactoryContext<TTransient>): void;
|
|
72
80
|
afterCreate?(
|
|
73
81
|
row: Created<TRow, TMany & TMoreMany>,
|
|
74
82
|
context: FactoryContext<TTransient>,
|
package/src/factory-types.ts
CHANGED
|
@@ -129,3 +129,34 @@ export type StoredOf<E, TAttrs> = E extends { readonly $row: infer R extends obj
|
|
|
129
129
|
export type WriteBound<E> = E extends { readonly $columns: infer C extends ColumnMap }
|
|
130
130
|
? Partial<Insertable<C>>
|
|
131
131
|
: object;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Any value at all (`{} | null | undefined` is `unknown`, so `Record<string, unknown>` fits), but
|
|
135
|
+
* spelled as a union so the checker sees it include `string` — `unknown` alone would not keep a literal.
|
|
136
|
+
*/
|
|
137
|
+
// biome-ignore lint/complexity/noBannedTypes: `{}` is what makes the union admit `unknown`.
|
|
138
|
+
export type AttrValue = string | number | boolean | bigint | symbol | {} | null | undefined;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The bound on what `defaults` returns. Its per-key `AttrValue` is what keeps `status: 'open'` a
|
|
142
|
+
* literal: TypeScript widens a literal unless the slot it lands in is a type variable whose
|
|
143
|
+
* constraint holds `string`, and `WriteBound<E>` is a conditional over an unfixed `E` that never
|
|
144
|
+
* is. Self-referential (`keyof A`) rather than a `Record`, so an `interface` still satisfies it.
|
|
145
|
+
*/
|
|
146
|
+
export type DefaultsBound<A, E> = { readonly [K in keyof A]: AttrValue } & WriteBound<E>;
|
|
147
|
+
|
|
148
|
+
type Widened<V> = V extends string
|
|
149
|
+
? string
|
|
150
|
+
: V extends number
|
|
151
|
+
? number
|
|
152
|
+
: V extends boolean
|
|
153
|
+
? boolean
|
|
154
|
+
: V extends bigint
|
|
155
|
+
? bigint
|
|
156
|
+
: V;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The literals `DefaultsBound` kept, widened back once checked: `title: \`t${n}\`` must not make
|
|
160
|
+
* `build({ title: 'x' })` a type error. A column's own union returns through `Partial<TRow>`.
|
|
161
|
+
*/
|
|
162
|
+
export type SettledAttrs<A> = { [K in keyof A]: Widened<A[K]> };
|
package/src/index.ts
CHANGED
|
@@ -171,10 +171,12 @@ export type { EntityRegistry, FactoryRegistry } from './factory-registry';
|
|
|
171
171
|
export { factoriesFor } from './factory-registry';
|
|
172
172
|
export type {
|
|
173
173
|
AttributesFor,
|
|
174
|
+
AttrValue,
|
|
174
175
|
BelongsTo,
|
|
175
176
|
BelongsToMap,
|
|
176
177
|
Built,
|
|
177
178
|
Created,
|
|
179
|
+
DefaultsBound,
|
|
178
180
|
EntityLike,
|
|
179
181
|
FactoryContext,
|
|
180
182
|
FactoryIds,
|
|
@@ -184,6 +186,7 @@ export type {
|
|
|
184
186
|
HasMany,
|
|
185
187
|
HasManyMap,
|
|
186
188
|
None,
|
|
189
|
+
SettledAttrs,
|
|
187
190
|
StoredOf,
|
|
188
191
|
Trait,
|
|
189
192
|
TraitMap,
|