@ultimat3/testing 31.9.0 → 31.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/testing",
3
- "version": "31.9.0",
3
+ "version": "31.10.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.9.0",
51
- "@ultimat3/ai": "31.9.0",
52
- "@ultimat3/auth": "31.9.0",
53
- "@ultimat3/cache": "31.9.0",
54
- "@ultimat3/core": "31.9.0",
55
- "@ultimat3/db": "31.9.0",
56
- "@ultimat3/entity": "31.9.0",
57
- "@ultimat3/http": "31.9.0",
58
- "@ultimat3/i18n": "31.9.0",
59
- "@ultimat3/jobs": "31.9.0",
60
- "@ultimat3/mail": "31.9.0",
61
- "@ultimat3/notify": "31.9.0",
62
- "@ultimat3/policy": "31.9.0",
63
- "@ultimat3/pwa": "31.9.0",
64
- "@ultimat3/query": "31.9.0",
65
- "@ultimat3/realtime": "31.9.0",
66
- "@ultimat3/render": "31.9.0",
67
- "@ultimat3/schema": "31.9.0",
68
- "@ultimat3/scraping": "31.9.0",
69
- "@ultimat3/seo": "31.9.0",
70
- "@ultimat3/time": "31.9.0"
50
+ "@ultimat3/action": "31.10.0",
51
+ "@ultimat3/ai": "31.10.0",
52
+ "@ultimat3/auth": "31.10.0",
53
+ "@ultimat3/cache": "31.10.0",
54
+ "@ultimat3/core": "31.10.0",
55
+ "@ultimat3/db": "31.10.0",
56
+ "@ultimat3/entity": "31.10.0",
57
+ "@ultimat3/http": "31.10.0",
58
+ "@ultimat3/i18n": "31.10.0",
59
+ "@ultimat3/jobs": "31.10.0",
60
+ "@ultimat3/mail": "31.10.0",
61
+ "@ultimat3/notify": "31.10.0",
62
+ "@ultimat3/policy": "31.10.0",
63
+ "@ultimat3/pwa": "31.10.0",
64
+ "@ultimat3/query": "31.10.0",
65
+ "@ultimat3/realtime": "31.10.0",
66
+ "@ultimat3/render": "31.10.0",
67
+ "@ultimat3/schema": "31.10.0",
68
+ "@ultimat3/scraping": "31.10.0",
69
+ "@ultimat3/seo": "31.10.0",
70
+ "@ultimat3/time": "31.10.0"
71
71
  }
72
72
  }
@@ -40,6 +40,25 @@ export const AUTH_ADAPTER_SESSION_CHECKS: readonly AuthAdapterCheck[] = [
40
40
  expect(await adapter.updateSession(idOf('nobody'), { lastSeenAt: at(1) })).toBeNull();
41
41
  },
42
42
  },
43
+ {
44
+ // The one-round-trip read `authenticate` prefers. Optional on the seam, so an adapter without
45
+ // it passes here and `authenticate` falls back to the two reads this compares against.
46
+ name: 'getSessionWithUser answers exactly what getSession then findUserById answer',
47
+ async run({ adapter }) {
48
+ if (adapter.getSessionWithUser === undefined) return;
49
+ const owner = await seedUser(adapter, { orgId: crypto.randomUUID(), roles: ['editor'] });
50
+ await adapter.updateUser(owner, { disabledAt: at(5), scopes: ['tenancy:cross'] });
51
+ const row = session(owner);
52
+ await adapter.createSession(row);
53
+ const separately: unknown = {
54
+ session: await adapter.getSession(row.id),
55
+ user: await adapter.findUserById(owner),
56
+ };
57
+ expect<unknown>(await adapter.getSessionWithUser(row.id)).toEqual(separately);
58
+ expect((await adapter.getSessionWithUser(row.id))?.session).toEqual(row);
59
+ expect(await adapter.getSessionWithUser(idOf('nobody'))).toBeNull();
60
+ },
61
+ },
43
62
  {
44
63
  name: `${DELETERS} concurrent deletes of one session: exactly one answers true`,
45
64
  async run({ adapter }) {
@@ -5,16 +5,22 @@ import { AGGREGATE_CHECKS } from './entity-conformance-aggregates';
5
5
  import { BATCH_CHECKS } from './entity-conformance-batches';
6
6
  import { BULK_CHECKS } from './entity-conformance-bulk';
7
7
  import { CHAIN_WRITE_CHECKS } from './entity-conformance-chain-writes';
8
+ import { CONTAINS_CHECKS } from './entity-conformance-contains';
9
+ import { COUNTED_IN_CHECKS } from './entity-conformance-counted-in';
8
10
  import { HISTORY_CHECKS } from './entity-conformance-history';
11
+ import { LATEST_BY_CHECKS } from './entity-conformance-latest-by';
12
+ import { LOCK_KEYS_CHECKS } from './entity-conformance-lock-keys';
9
13
  import { LOCK_CHECKS } from './entity-conformance-locks';
10
14
  import { MATRIX_CHECKS } from './entity-conformance-matrix';
11
15
  import { MATRIX_WRITE_CHECKS } from './entity-conformance-matrix-writes';
16
+ import { MEASURE_CHECKS } from './entity-conformance-measures';
12
17
  import { PAGE_BYTE_CHECKS } from './entity-conformance-page-bytes';
13
18
  import { READ_CHECKS } from './entity-conformance-reads';
14
19
  import { TENANCY_CHECKS } from './entity-conformance-tenancy';
15
20
  import { UNRETURNED_CHECKS } from './entity-conformance-unreturned';
16
21
  import { UPDATE_MANY_CHECKS } from './entity-conformance-update-many';
17
22
  import { VALUE_CHECKS } from './entity-conformance-values';
23
+ import { VERSION_LOCK_CHECKS } from './entity-conformance-version-lock';
18
24
  import { VERSION_CHECKS } from './entity-conformance-versions';
19
25
  import { WRITE_CHECKS } from './entity-conformance-writes';
20
26
  import type { EntityDriverCheck } from './entity-driver-conformance';
@@ -29,11 +35,17 @@ export const ENTITY_DRIVER_CHECKS: readonly EntityDriverCheck[] = [
29
35
  ...UPDATE_MANY_CHECKS,
30
36
  ...UNRETURNED_CHECKS,
31
37
  ...AGGREGATE_CHECKS,
38
+ ...MEASURE_CHECKS,
39
+ ...CONTAINS_CHECKS,
32
40
  ...TENANCY_CHECKS,
33
41
  ...BATCH_CHECKS,
42
+ ...LATEST_BY_CHECKS,
34
43
  ...LOCK_CHECKS,
44
+ ...LOCK_KEYS_CHECKS,
35
45
  ...MATRIX_CHECKS,
36
46
  ...MATRIX_WRITE_CHECKS,
37
47
  ...VERSION_CHECKS,
48
+ ...VERSION_LOCK_CHECKS,
38
49
  ...HISTORY_CHECKS,
50
+ ...COUNTED_IN_CHECKS,
39
51
  ];
@@ -0,0 +1,145 @@
1
+ // Entity conformance · `contains` on a text column: a case-insensitive substring, over a table whose
2
+ // `gin_trgm_ops` index the migration builds (`pg_trgm` created first). `%`, `_` and `\` are found as
3
+ // themselves, never wildcards; a term that is no string matches nothing; a sealed column refuses it;
4
+ // and `contains` on an array column still means containment, decided by the column's kind.
5
+
6
+ import { expect } from 'bun:test';
7
+ import type { Database } from '@ultimat3/entity';
8
+ import { arrayOf, entity, text, uuid } from '@ultimat3/entity';
9
+ import { remember } from './entity-conformance-entities';
10
+ import { checkOver, codeOf, idAt, inOrg } from './entity-conformance-support';
11
+ import type { EntityDriverCheck } from './entity-driver-conformance';
12
+
13
+ const titles = remember(
14
+ entity('ec_tc_titles', {
15
+ columns: {
16
+ id: uuid().primaryKey(),
17
+ orgId: uuid().tenant(),
18
+ title: text({ max: 40 }),
19
+ note: text({ max: 40 }).nullable(),
20
+ secret: text({ max: 40 }).sealed(),
21
+ tags: arrayOf(text({ max: 10 })),
22
+ },
23
+ indexes: [
24
+ { on: ['title'], using: 'gin', operatorClass: 'gin_trgm_ops' },
25
+ { on: ['note'], using: 'gin', operatorClass: 'gin_trgm_ops' },
26
+ ],
27
+ }),
28
+ );
29
+
30
+ const SET = { titles };
31
+ type Db = Database<typeof SET>;
32
+ const ORG = idAt('a1');
33
+
34
+ const seed = async (db: Db, rows: readonly (readonly [string, string | null])[]) => {
35
+ await db.titles.insertAll(
36
+ rows.map(([title, note], index) => ({
37
+ id: idAt(`a${100 + index}`),
38
+ orgId: ORG,
39
+ title,
40
+ note,
41
+ secret: 's',
42
+ tags: [title.slice(0, 10)],
43
+ })),
44
+ );
45
+ };
46
+
47
+ /** The titles `contains` finds on `column`, sorted so no driver's order is asserted. */
48
+ const found = async (db: Db, column: 'title' | 'note', term: unknown) =>
49
+ (
50
+ await inOrg(ORG, () =>
51
+ db.titles
52
+ .where({ orgId: ORG })
53
+ .andWhere(column, 'contains', term as string)
54
+ .all(),
55
+ )
56
+ )
57
+ .map((row) => row.title)
58
+ .sort();
59
+
60
+ const checks: readonly EntityDriverCheck[] = [
61
+ checkOver(SET, 'text contains finds a substring in any ASCII case', async (db) => {
62
+ await seed(db, [
63
+ ['Hello World', null],
64
+ ['HELLO', null],
65
+ ['say hello!', null],
66
+ ['help', null],
67
+ ]);
68
+ expect([
69
+ await found(db, 'title', 'hello'),
70
+ await found(db, 'title', 'LLO w'),
71
+ await found(db, 'title', 'xyz'),
72
+ ]).toEqual([['HELLO', 'Hello World', 'say hello!'], ['Hello World'], []]);
73
+ }),
74
+ checkOver(
75
+ SET,
76
+ 'text contains folds a non-ASCII letter’s case as every driver does',
77
+ async (db) => {
78
+ await seed(db, [
79
+ ['École', null],
80
+ ['ÉCOLE', null],
81
+ ['STRASSE', null],
82
+ ]);
83
+ expect([await found(db, 'title', 'école'), await found(db, 'title', 'É')]).toEqual([
84
+ ['ÉCOLE', 'École'],
85
+ ['ÉCOLE', 'École'],
86
+ ]);
87
+ },
88
+ ),
89
+ checkOver(
90
+ SET,
91
+ 'text contains finds %, _ and \\ as themselves, never as wildcards',
92
+ async (db) => {
93
+ await seed(db, [
94
+ ['100%', null],
95
+ ['100 percent', null],
96
+ ['a_b', null],
97
+ ['axb', null],
98
+ ['back\\slash', null],
99
+ ['backslash', null],
100
+ ]);
101
+ expect([
102
+ await found(db, 'title', '%'),
103
+ await found(db, 'title', '0%'),
104
+ await found(db, 'title', '_'),
105
+ await found(db, 'title', 'a_'),
106
+ await found(db, 'title', '\\'),
107
+ await found(db, 'title', 'k\\s'),
108
+ ]).toEqual([['100%'], ['100%'], ['a_b'], ['a_b'], ['back\\slash'], ['back\\slash']]);
109
+ },
110
+ ),
111
+ checkOver(
112
+ SET,
113
+ 'text contains over a nullable column never matches NULL; an empty term matches every value',
114
+ async (db) => {
115
+ await seed(db, [
116
+ ['one', 'Draft'],
117
+ ['two', null],
118
+ ['three', ''],
119
+ ]);
120
+ expect([await found(db, 'note', 'dra'), await found(db, 'note', '')]).toEqual([
121
+ ['one'],
122
+ ['one', 'three'],
123
+ ]);
124
+ },
125
+ ),
126
+ checkOver(
127
+ SET,
128
+ 'contains on a sealed column is refused; on an array column it is still containment',
129
+ async (db) => {
130
+ await seed(db, [
131
+ ['alpha', null],
132
+ ['alphabet', null],
133
+ ]);
134
+ const sealed = () =>
135
+ inOrg(ORG, () => db.titles.andWhere('secret' as never, 'contains', 's' as never).all());
136
+ expect(await codeOf(sealed)).toBe('X_ENTITY_SEALED_PREDICATE');
137
+ const tagged = await inOrg(ORG, () =>
138
+ db.titles.andWhere('tags', 'contains', ['alpha']).all(),
139
+ );
140
+ expect(tagged.map((row) => row.title)).toEqual(['alpha']);
141
+ },
142
+ ),
143
+ ];
144
+
145
+ export const CONTAINS_CHECKS: readonly EntityDriverCheck[] = checks;
@@ -0,0 +1,229 @@
1
+ // Entity conformance · a `countedIn` counter is a write to its PARENT, and the row observer says so:
2
+ // a child insert, delete or re-parent reports `entity.row` for each parent whose count moved, its
3
+ // `before` and `after` the stored row; a bulk child write pinned to one parent reports that row, one
4
+ // spanning several a bulk count — never silence, and never a change a rolled-back write did not make.
5
+
6
+ import { expect } from 'bun:test';
7
+ import type { EntityRowEvent } from '@ultimat3/core';
8
+ import { isUltimateError } from '@ultimat3/core';
9
+ import type { Database } from '@ultimat3/entity';
10
+ import { entity, integer, text, uuid } from '@ultimat3/entity';
11
+ import { remember } from './entity-conformance-entities';
12
+ import { codeOf, handleOf, idAt } from './entity-conformance-support';
13
+ import { accountedFor, heard } from './entity-conformance-write-support';
14
+ import type { EntityDriverCheck, EntityDriverRun } from './entity-driver-conformance';
15
+
16
+ const posts = remember(
17
+ entity('ec_ci_posts', {
18
+ columns: { id: uuid().primaryKey(), title: text({ max: 20 }), likeCount: integer().default(0) },
19
+ }),
20
+ );
21
+
22
+ const likes = remember(
23
+ entity('ec_ci_likes', {
24
+ columns: {
25
+ id: uuid().primaryKey(),
26
+ postId: uuid().references(() => posts.id, { countedIn: () => posts.likeCount }),
27
+ reaction: text({ max: 20 }),
28
+ },
29
+ }),
30
+ );
31
+
32
+ const SET = { posts, likes };
33
+ type Db = Database<typeof SET>;
34
+ const PARENT = 'ec_ci_posts';
35
+ const [P1, P2] = [idAt('f1'), idAt('f2')];
36
+ const [L1, L2, L3] = [idAt('f11'), idAt('f12'), idAt('f13')];
37
+
38
+ const seedPosts = async (db: Db): Promise<void> => {
39
+ await db.posts.insertAll([
40
+ { id: P1, title: 'one', likeCount: 0 },
41
+ { id: P2, title: 'two', likeCount: 0 },
42
+ ]);
43
+ };
44
+
45
+ /** A row event as the three facts that matter: which parent, and its count before and after. */
46
+ const moved = (event: EntityRowEvent) => [
47
+ event.op,
48
+ event.after?.['id'] ?? event.before?.['id'],
49
+ event.before?.['likeCount'] ?? null,
50
+ event.after?.['likeCount'] ?? null,
51
+ ];
52
+
53
+ const stored = async (db: Db) =>
54
+ (await db.posts.orderBy('id').all()).map((row) => [row.id, row.likeCount]);
55
+
56
+ /**
57
+ * A check over a store that keeps the counter. One that does not (`defineDriver`: no lifecycle
58
+ * hook) refuses the counted child's repository outright, so there is no handle to build and no
59
+ * parent change to report: its claim is the refusal check below, never these.
60
+ */
61
+ const counted = (
62
+ name: string,
63
+ run: (db: Db, raw: EntityDriverRun) => Promise<void>,
64
+ needs: EntityDriverCheck['needs'] = [],
65
+ ): EntityDriverCheck => ({
66
+ name,
67
+ entities: Object.values(SET),
68
+ needs,
69
+ run: async (raw) => {
70
+ if (!raw.counters) return;
71
+ await run(handleOf(SET, raw.driver), raw);
72
+ },
73
+ });
74
+
75
+ /** `[code, capability]` a build refused with, or `'built'`. */
76
+ const builds = (build: () => unknown): unknown => {
77
+ try {
78
+ build();
79
+ return 'built';
80
+ } catch (error) {
81
+ return isUltimateError(error) ? [error.code, error.meta?.['capability']] : String(error);
82
+ }
83
+ };
84
+
85
+ const refusal: readonly EntityDriverCheck[] = [
86
+ {
87
+ name: 'a store that keeps no counter refuses the counted child, never a count left stale',
88
+ entities: Object.values(SET),
89
+ run: async (raw) => {
90
+ const child = builds(() => raw.driver.repo(likes));
91
+ // The parent declares no counter of its own: every store builds it.
92
+ expect([child, builds(() => raw.driver.repo(posts))]).toEqual([
93
+ raw.counters ? 'built' : ['X_DRIVER_UNSUPPORTED', 'countedIn'],
94
+ 'built',
95
+ ]);
96
+ },
97
+ },
98
+ ];
99
+
100
+ const rows: readonly EntityDriverCheck[] = [
101
+ counted(
102
+ 'a counted child insert and delete each report the parent row, its count before and after',
103
+ async (db) => {
104
+ await seedPosts(db);
105
+ const insert = await heard(PARENT, () =>
106
+ db.likes.insert({ id: L1, postId: P1, reaction: 'up' }),
107
+ );
108
+ const again = await heard(PARENT, () =>
109
+ db.likes.insert({ id: L2, postId: P1, reaction: 'up' }),
110
+ );
111
+ const remove = await heard(PARENT, () => db.likes.delete(L1));
112
+ expect([insert, again, remove].map((each) => each.heard.rows.map(moved))).toEqual([
113
+ [['update', P1, 0, 1]],
114
+ [['update', P1, 1, 2]],
115
+ [['update', P1, 2, 1]],
116
+ ]);
117
+ expect([insert, again, remove].map((each) => each.heard.bulk)).toEqual([[], [], []]);
118
+ expect(await stored(db)).toEqual([
119
+ [P1, 1],
120
+ [P2, 0],
121
+ ]);
122
+ },
123
+ ),
124
+ counted(
125
+ 'a re-parenting update reports both parents: the one it left and the one it joined',
126
+ async (db) => {
127
+ await seedPosts(db);
128
+ await db.likes.insert({ id: L1, postId: P1, reaction: 'up' });
129
+ const { heard: told } = await heard(PARENT, () => db.likes.update(L1, { postId: P2 }));
130
+ expect(told.rows.map(moved).sort((a, b) => String(a[1]).localeCompare(String(b[1])))).toEqual(
131
+ [
132
+ ['update', P1, 1, 0],
133
+ ['update', P2, 0, 1],
134
+ ],
135
+ );
136
+ expect(await stored(db)).toEqual([
137
+ [P1, 0],
138
+ [P2, 1],
139
+ ]);
140
+ },
141
+ ),
142
+ counted('an update leaving the counted key alone reports no parent change', async (db) => {
143
+ await seedPosts(db);
144
+ await db.likes.insert({ id: L1, postId: P1, reaction: 'up' });
145
+ const { heard: told } = await heard(PARENT, () => db.likes.update(L1, { reaction: 'down' }));
146
+ expect([told.rows.map(moved), told.bulk]).toEqual([[], []]);
147
+ }),
148
+ ];
149
+
150
+ const bulk: readonly EntityDriverCheck[] = [
151
+ counted(
152
+ 'a bulk child delete pinned to one parent reports that parent row, by filter or by chain',
153
+ async (db) => {
154
+ await seedPosts(db);
155
+ const three = () =>
156
+ db.likes.insertAll([
157
+ { id: L1, postId: P1, reaction: 'up' },
158
+ { id: L2, postId: P1, reaction: 'up' },
159
+ { id: L3, postId: P2, reaction: 'up' },
160
+ ]);
161
+ await three();
162
+ const filter = await heard(PARENT, () => db.likes.deleteWhere({ postId: P1 }));
163
+ await db.likes.deleteWhere({ postId: P2 });
164
+ await three();
165
+ const chain = await heard(PARENT, () =>
166
+ db.likes.where({ postId: P1, reaction: 'up' }).deleteAll(),
167
+ );
168
+ // Pinned, the parent is known and re-read; by how much is not, so `before` is unknown (null).
169
+ for (const [how, pinned] of [
170
+ ['deleteWhere', filter],
171
+ ['chain deleteAll', chain],
172
+ ] as const) {
173
+ expect([how, pinned.answer, pinned.heard.rows.map(moved), pinned.heard.bulk]).toEqual([
174
+ how,
175
+ 2,
176
+ [['update', P1, null, 0]],
177
+ [],
178
+ ]);
179
+ }
180
+ expect(await stored(db)).toEqual([
181
+ [P1, 0],
182
+ [P2, 1],
183
+ ]);
184
+ },
185
+ ),
186
+ counted(
187
+ 'a bulk child delete across parents reports a bulk count for the parent table, never silence',
188
+ async (db) => {
189
+ await seedPosts(db);
190
+ await db.likes.insertAll([
191
+ { id: L1, postId: P1, reaction: 'up' },
192
+ { id: L2, postId: P1, reaction: 'up' },
193
+ { id: L3, postId: P2, reaction: 'up' },
194
+ ]);
195
+ const spread = await heard(PARENT, () => db.likes.where({ reaction: 'up' }).deleteAll());
196
+ expect(spread.answer).toBe(3);
197
+ expect(spread.heard.bulk.map((each) => [each.entity, each.op])).toEqual([[PARENT, 'update']]);
198
+ // Two parents moved: a count a subscriber re-reads on may be wider, never narrower.
199
+ expect(accountedFor(spread.heard)).toBeGreaterThanOrEqual(2);
200
+ expect(await stored(db)).toEqual([
201
+ [P1, 0],
202
+ [P2, 0],
203
+ ]);
204
+ },
205
+ ),
206
+ counted(
207
+ 'a counted child write rolled back with its transaction reports no parent change',
208
+ async (db, { driver }) => {
209
+ await seedPosts(db);
210
+ await db.likes.insert({ id: L1, postId: P1, reaction: 'up' });
211
+ const { answer, heard: told } = await heard(PARENT, () =>
212
+ codeOf(() =>
213
+ driver.transactor().run(async () => {
214
+ await db.likes.insert({ id: L2, postId: P1, reaction: 'up' });
215
+ await db.likes.insert({ id: L1, postId: P2, reaction: 'dup' });
216
+ }),
217
+ ),
218
+ );
219
+ expect([answer, told.rows.map(moved), told.bulk]).toEqual(['X_DB_UNIQUE_VIOLATION', [], []]);
220
+ expect(await stored(db)).toEqual([
221
+ [P1, 1],
222
+ [P2, 0],
223
+ ]);
224
+ },
225
+ ['transactions'],
226
+ ),
227
+ ];
228
+
229
+ export const COUNTED_IN_CHECKS: readonly EntityDriverCheck[] = [...refusal, ...rows, ...bulk];