-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathseed-loader-replay.test.ts
More file actions
318 lines (284 loc) · 12.6 KB
/
Copy pathseed-loader-replay.test.ts
File metadata and controls
318 lines (284 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, vi } from 'vitest';
import { SeedLoaderService } from './seed-loader';
import type { IDataEngine, IMetadataService } from '@objectstack/spec/contracts';
/**
* Replay regression: seeds with lookup natural keys must survive a dev-server
* restart (the upsert UPDATE path), not just first boot (the INSERT path).
*
* Third-party eval 2026-07-17 (15.1.x): every restart, the seed replay's
* update path wrote `candidate = NULL` for records whose lookup was authored
* as a natural key — only a NOT NULL column stopped silent data corruption.
*
* Unlike seed-loader.test.ts's engine mock (whose find() ignores `where` and
* returns the whole table — masking exactly this bug), this mock filters
* `where` faithfully like a real engine.
*/
function createLogger() {
return { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() };
}
function createFaithfulEngine(): { engine: IDataEngine; store: Record<string, any[]> } {
const store: Record<string, any[]> = {};
let idCounter = 0;
const engine = {
find: vi.fn(async (objectName: string, query?: any) => {
let records = store[objectName] || [];
if (query?.where) {
records = records.filter((r) =>
Object.entries(query.where).every(([k, v]) => r[k] === v),
);
}
if (typeof query?.limit === 'number') {
records = records.slice(0, query.limit);
}
return records;
}),
findOne: vi.fn(async (objectName: string, query?: any) => {
const rows = await (engine.find as any)(objectName, { ...query, limit: 1 });
return rows[0] ?? null;
}),
insert: vi.fn(async (objectName: string, data: any) => {
if (!store[objectName]) store[objectName] = [];
if (Array.isArray(data)) {
const records = data.map((d) => ({ id: `gen-${++idCounter}`, ...d }));
store[objectName].push(...records);
return records;
}
const record = { id: `gen-${++idCounter}`, ...data };
store[objectName].push(record);
return record;
}),
update: vi.fn(async (objectName: string, data: any) => {
const records = store[objectName] || [];
const idx = records.findIndex((r) => r.id === data.id);
if (idx >= 0) {
records[idx] = { ...records[idx], ...data };
return records[idx];
}
return data;
}),
delete: vi.fn(async () => ({ deleted: 1 })),
count: vi.fn(async (objectName: string) => (store[objectName] || []).length),
aggregate: vi.fn(async () => []),
} as unknown as IDataEngine;
return { engine, store };
}
function createMetadata(): IMetadataService {
const objects: Record<string, any> = {
my_app_candidate: {
name: 'my_app_candidate',
fields: {
name: { type: 'text' },
email: { type: 'text' },
},
},
my_app_interview: {
name: 'my_app_interview',
fields: {
name: { type: 'text' },
candidate: { type: 'lookup', reference: 'my_app_candidate', required: true },
stage: { type: 'text' },
},
},
};
return {
getObject: vi.fn(async (name: string) => objects[name]),
listObjects: vi.fn(async () => Object.values(objects)),
register: vi.fn(async () => {}),
get: vi.fn(async (_t: string, name: string) => objects[name]),
list: vi.fn(async () => []),
unregister: vi.fn(async () => {}),
exists: vi.fn(async () => false),
listNames: vi.fn(async () => []),
} as unknown as IMetadataService;
}
// Mirrors the AppPlugin inline-seed config (defaultMode upsert, multiPass on).
const CONFIG = {
dryRun: false,
haltOnError: false,
multiPass: true,
defaultMode: 'upsert',
batchSize: 1000,
transaction: false,
} as any;
// Parent's natural key is a non-`name` externalId (email) — the exact shape
// from the eval repro (interview.candidate: 'alice@example.com').
const SEEDS = [
{
object: 'my_app_candidate',
externalId: 'email',
mode: 'upsert',
env: ['prod', 'dev', 'test'],
records: [
{ name: 'Alice Smith', email: 'alice@example.com' },
{ name: 'Bob Jones', email: 'bob@example.com' },
],
},
{
object: 'my_app_interview',
externalId: 'name',
mode: 'upsert',
env: ['prod', 'dev', 'test'],
records: [
{ name: 'iv-alice-1', candidate: 'alice@example.com', stage: 'screening' },
{ name: 'iv-bob-1', candidate: 'bob@example.com', stage: 'onsite' },
],
},
] as any[];
/** No interview update may ever carry a NULL or unresolved (email-string) candidate. */
function corruptInterviewUpdates(engine: IDataEngine) {
return (engine.update as any).mock.calls.filter(
([obj, data]: [string, any]) =>
obj === 'my_app_interview' &&
'candidate' in data &&
(data.candidate == null || String(data.candidate).includes('@')),
);
}
describe('seed replay (restart) — lookup natural keys on the update path', () => {
it('keeps lookups pointing at the right parent after a second load (no NULL overwrite)', async () => {
const { engine, store } = createFaithfulEngine();
const metadata = createMetadata();
// Boot #1 — fresh DB, insert path.
const first = await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: SEEDS,
config: CONFIG,
});
expect(first.success).toBe(true);
const aliceId = store.my_app_candidate.find((r) => r.email === 'alice@example.com')!.id;
const bobId = store.my_app_candidate.find((r) => r.email === 'bob@example.com')!.id;
expect(store.my_app_interview.find((r) => r.name === 'iv-alice-1')!.candidate).toBe(aliceId);
expect(store.my_app_interview.find((r) => r.name === 'iv-bob-1')!.candidate).toBe(bobId);
// Boot #2 — same DB, new loader instance (dev-server restart): upsert
// matches existing rows and takes the UPDATE path.
const second = await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: SEEDS,
config: CONFIG,
});
// The replay must not corrupt the association: still the right parent id,
// never NULL (a nullable column would silently lose the link; a NOT NULL
// column turns every replayed record into a constraint error).
expect(store.my_app_interview.find((r) => r.name === 'iv-alice-1')!.candidate).toBe(aliceId);
expect(store.my_app_interview.find((r) => r.name === 'iv-bob-1')!.candidate).toBe(bobId);
expect(corruptInterviewUpdates(engine)).toEqual([]);
expect(second.success).toBe(true);
expect(second.summary.totalErrored).toBe(0);
});
it('replaying an unchanged seed is a no-op (skip, no update churn)', async () => {
const { engine, store } = createFaithfulEngine();
const metadata = createMetadata();
await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: SEEDS,
config: CONFIG,
});
(engine.update as any).mockClear();
const before = structuredClone(store);
const replay = await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: SEEDS,
config: CONFIG,
});
// Nothing the seed declares changed → no update at all: updated_at stays
// put, lifecycle validation (e.g. a state_machine rule) never re-fires.
expect(engine.update).not.toHaveBeenCalled();
expect(replay.summary.totalUpdated).toBe(0);
expect(replay.summary.totalSkipped).toBe(4);
expect(replay.summary.totalErrored).toBe(0);
expect(store).toEqual(before);
});
it('a rejected parent update (state-machine style) must not cascade into NULLed child lookups', async () => {
const { engine, store } = createFaithfulEngine();
const metadata = createMetadata();
await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: SEEDS,
config: CONFIG,
});
const aliceId = store.my_app_candidate.find((r) => r.email === 'alice@example.com')!.id;
const bobId = store.my_app_candidate.find((r) => r.email === 'bob@example.com')!.id;
// A user edited rows at runtime (the eval scenario: kanban drags changed
// candidate stages, someone rescheduled an interview) — so the replay's
// updates are NOT no-ops — and the object now vetoes the transition back
// (my-app's `Invalid stage transition.`).
for (const r of store.my_app_candidate) r.name = r.name + ' (edited)';
for (const r of store.my_app_interview) r.stage = 'edited';
const realUpdate = (engine.update as any).getMockImplementation();
(engine.update as any).mockImplementation(async (objectName: string, data: any) => {
if (objectName === 'my_app_candidate') throw new Error('Invalid stage transition.');
return realUpdate(objectName, data);
});
const replay = await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: SEEDS,
config: CONFIG,
});
// The candidate updates legitimately failed (reported), but the interview
// records still resolved their parents and re-applied the seed values.
expect(replay.summary.totalErrored).toBe(2);
expect(store.my_app_interview.find((r) => r.name === 'iv-alice-1')!.candidate).toBe(aliceId);
expect(store.my_app_interview.find((r) => r.name === 'iv-bob-1')!.candidate).toBe(bobId);
expect(store.my_app_interview.every((r) => r.stage !== 'edited')).toBe(true);
expect(corruptInterviewUpdates(engine)).toEqual([]);
});
it('resolves a lookup from the DB by the target dataset externalId when the parent dataset is absent', async () => {
const { engine, store } = createFaithfulEngine();
const metadata = createMetadata();
// Parents already in the DB (seeded earlier / another load).
await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: SEEDS,
config: CONFIG,
});
const aliceId = store.my_app_candidate.find((r) => r.email === 'alice@example.com')!.id;
// A later load carries ONLY the child dataset, so the in-memory
// insertedRecords map has no candidate entries — resolution must fall
// back to the DB and query the candidate dataset's externalId (email),
// not the hardcoded 'name' column.
const childOnly = await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: [
{ ...SEEDS[0] }, // candidate dataset present but EMPTY → only its externalId declaration matters
{
object: 'my_app_interview',
externalId: 'name',
mode: 'upsert',
env: ['prod', 'dev', 'test'],
records: [{ name: 'iv-alice-2', candidate: 'alice@example.com', stage: 'onsite' }],
},
].map((s, idx) => (idx === 0 ? { ...s, records: [] } : s)) as any,
config: CONFIG,
});
expect(childOnly.summary.totalErrored).toBe(0);
expect(store.my_app_interview.find((r) => r.name === 'iv-alice-2')!.candidate).toBe(aliceId);
});
it('an unresolvable lookup never reaches the row: deferred pass leaves the column alone, single-pass drops the record', async () => {
const { engine, store } = createFaithfulEngine();
const metadata = createMetadata();
await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: SEEDS,
config: CONFIG,
});
const aliceId = store.my_app_candidate.find((r) => r.email === 'alice@example.com')!.id;
// Poison the seed: alice's interview now references a candidate that does
// not exist anywhere. Change another field so the update is not a no-op.
const poisoned = structuredClone(SEEDS);
poisoned[1].records[0].candidate = 'ghost@example.com';
poisoned[1].records[0].stage = 'changed';
// multiPass: the update must NOT touch the candidate column (pass 2 also
// fails → reported), preserving the existing association.
const deferred = await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: poisoned,
config: CONFIG,
});
expect(deferred.success).toBe(false);
expect(store.my_app_interview.find((r) => r.name === 'iv-alice-1')!.candidate).toBe(aliceId);
expect(corruptInterviewUpdates(engine)).toEqual([]);
// single-pass: the whole record is dropped (reported + counted), rather
// than written with a corrupted reference.
(engine.update as any).mockClear();
const singlePass = await new SeedLoaderService(engine, metadata, createLogger()).load({
seeds: poisoned,
config: { ...CONFIG, multiPass: false },
});
expect(singlePass.success).toBe(false);
expect(singlePass.summary.totalErrored).toBeGreaterThanOrEqual(1);
expect(
(engine.update as any).mock.calls.filter(([obj, data]: [string, any]) => obj === 'my_app_interview' && data.name === 'iv-alice-1'),
).toEqual([]);
expect(store.my_app_interview.find((r) => r.name === 'iv-alice-1')!.candidate).toBe(aliceId);
});
});