|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { SeedLoaderService } from './seed-loader.js'; |
| 5 | +import type { IDataEngine, IMetadataService } from '@objectstack/spec/contracts'; |
| 6 | +import { SeedLoadResultSchema } from '@objectstack/spec/data'; |
| 7 | +import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate } from '@objectstack/metadata-core'; |
| 8 | + |
| 9 | +/** |
| 10 | + * #23354 — `SeedLoadResult.landedIds`: the rows each dataset landed on. |
| 11 | + * |
| 12 | + * The seed-ownership claim (ADR-0138 D1) may hand to the platform admin only |
| 13 | + * the unowned rows the SEED wrote: a guest-written row and a public form's |
| 14 | + * intake keep no owner, and no stored column tells them from a seed row. The |
| 15 | + * loader is the one place that knows which rows its records resolved to — it |
| 16 | + * matches natural keys, resolves references to the target's id first, and |
| 17 | + * re-identifies rows per organization — so it publishes the answer instead of |
| 18 | + * a reader re-deriving it from the dataset. |
| 19 | + * |
| 20 | + * What the claim depends on, pinned here: |
| 21 | + * |
| 22 | + * - the insert path reports the ids it wrote; |
| 23 | + * - a REPLAY reports the rows an earlier load wrote, on the skip path and on |
| 24 | + * the update path alike — that is what keeps the claim correct on every |
| 25 | + * later boot; |
| 26 | + * - a dataset keyed on a REFERENCE reports the matched row: the stored key is |
| 27 | + * the resolved id, never the dataset's own value, which is exactly what a |
| 28 | + * reader copying the dataset would get wrong; |
| 29 | + * - a row the load did not land (a refused insert) and a dry run report none. |
| 30 | + * |
| 31 | + * The engine is a faithful in-memory double (`where` filters, ids minted on |
| 32 | + * insert), the same shape `seed-loader-replay.test.ts` drives; the loader is |
| 33 | + * the real one. |
| 34 | + */ |
| 35 | + |
| 36 | +function createLogger() { |
| 37 | + return { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }; |
| 38 | +} |
| 39 | + |
| 40 | +function createFaithfulEngine(opts: { refuseInsert?: (object: string, row: any) => boolean } = {}) { |
| 41 | + const store: Record<string, any[]> = {}; |
| 42 | + let idCounter = 0; |
| 43 | + const mint = (object: string, d: any) => { |
| 44 | + if (opts.refuseInsert?.(object, d)) { |
| 45 | + throw Object.assign(new Error('Validation failed: name is reserved'), { code: 'VALIDATION_FAILED', status: 400 }); |
| 46 | + } |
| 47 | + return { id: `gen-${++idCounter}`, ...d }; |
| 48 | + }; |
| 49 | + |
| 50 | + const engine = { |
| 51 | + find: vi.fn(async (objectName: string, query?: any) => { |
| 52 | + let records = store[objectName] || []; |
| 53 | + if (query?.where) { |
| 54 | + records = records.filter((r) => |
| 55 | + Object.entries(query.where).every(([k, v]) => { |
| 56 | + if (k.startsWith('$')) throw new Error(`fake driver: unsupported operator ${k}`); |
| 57 | + return r[k] === v; |
| 58 | + }), |
| 59 | + ); |
| 60 | + } |
| 61 | + if (typeof query?.limit === 'number') records = records.slice(0, query.limit); |
| 62 | + return records; |
| 63 | + }), |
| 64 | + findOne: vi.fn(async (objectName: string, query?: any) => { |
| 65 | + assertEngineFindOnePredicate(objectName, query); |
| 66 | + const rows = await (engine.find as any)(objectName, { ...query, limit: 1 }); |
| 67 | + return rows[0] ?? null; |
| 68 | + }), |
| 69 | + insert: vi.fn(async (objectName: string, data: any) => { |
| 70 | + if (!store[objectName]) store[objectName] = []; |
| 71 | + if (Array.isArray(data)) { |
| 72 | + const records = data.map((d) => mint(objectName, d)); |
| 73 | + store[objectName].push(...records); |
| 74 | + return records; |
| 75 | + } |
| 76 | + const record = mint(objectName, data); |
| 77 | + store[objectName].push(record); |
| 78 | + return record; |
| 79 | + }), |
| 80 | + update: vi.fn(async (objectName: string, data: any) => { |
| 81 | + assertEngineUpdateDispatch(data, undefined); |
| 82 | + const records = store[objectName] || []; |
| 83 | + const idx = records.findIndex((r) => r.id === data.id); |
| 84 | + if (idx >= 0) { |
| 85 | + records[idx] = { ...records[idx], ...data }; |
| 86 | + return records[idx]; |
| 87 | + } |
| 88 | + return data; |
| 89 | + }), |
| 90 | + delete: vi.fn(async (_objectName: string, options?: any) => { |
| 91 | + assertEngineDeleteDispatch(options); |
| 92 | + return { deleted: 1 }; |
| 93 | + }), |
| 94 | + count: vi.fn(async (objectName: string) => (store[objectName] || []).length), |
| 95 | + aggregate: vi.fn(async () => []), |
| 96 | + } as unknown as IDataEngine; |
| 97 | + |
| 98 | + return { engine, store }; |
| 99 | +} |
| 100 | + |
| 101 | +function createMetadata(): IMetadataService { |
| 102 | + const objects: Record<string, any> = { |
| 103 | + lid_team: { name: 'lid_team', fields: { name: { type: 'text' } } }, |
| 104 | + lid_case: { name: 'lid_case', fields: { name: { type: 'text' }, status: { type: 'text' } } }, |
| 105 | + // A junction keyed on its REFERENCE: the stored `team` is the resolved |
| 106 | + // team id, never the dataset's natural key. |
| 107 | + lid_membership: { |
| 108 | + name: 'lid_membership', |
| 109 | + fields: { |
| 110 | + team: { type: 'lookup', reference: 'lid_team', required: true }, |
| 111 | + role: { type: 'text' }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }; |
| 115 | + return { |
| 116 | + getObject: vi.fn(async (name: string) => objects[name]), |
| 117 | + listObjects: vi.fn(async () => Object.values(objects)), |
| 118 | + register: vi.fn(async () => {}), |
| 119 | + get: vi.fn(async (_t: string, name: string) => objects[name]), |
| 120 | + list: vi.fn(async () => []), |
| 121 | + unregister: vi.fn(async () => {}), |
| 122 | + exists: vi.fn(async () => false), |
| 123 | + listNames: vi.fn(async () => []), |
| 124 | + } as unknown as IMetadataService; |
| 125 | +} |
| 126 | + |
| 127 | +// The AppPlugin inline-seed config: upsert, multi-pass. |
| 128 | +const CONFIG = { |
| 129 | + dryRun: false, |
| 130 | + haltOnError: false, |
| 131 | + multiPass: true, |
| 132 | + defaultMode: 'upsert', |
| 133 | + batchSize: 1000, |
| 134 | + transaction: false, |
| 135 | +} as any; |
| 136 | + |
| 137 | +const SEEDS = [ |
| 138 | + { |
| 139 | + object: 'lid_team', |
| 140 | + externalId: 'name', |
| 141 | + mode: 'upsert', |
| 142 | + records: [{ name: 'Experience' }, { name: 'Platform' }], |
| 143 | + }, |
| 144 | + { |
| 145 | + object: 'lid_case', |
| 146 | + externalId: 'name', |
| 147 | + mode: 'upsert', |
| 148 | + records: [ |
| 149 | + { name: 'CASE-1', status: 'open' }, |
| 150 | + { name: 'CASE-2', status: 'open' }, |
| 151 | + ], |
| 152 | + }, |
| 153 | + { |
| 154 | + object: 'lid_membership', |
| 155 | + externalId: 'team', |
| 156 | + mode: 'upsert', |
| 157 | + records: [{ team: 'Experience', role: 'lead' }], |
| 158 | + }, |
| 159 | +] as any[]; |
| 160 | + |
| 161 | +function landed(result: { results: Array<{ object: string; landedIds: string[] }> }, object: string): string[] { |
| 162 | + return result.results.filter((r) => r.object === object).flatMap((r) => r.landedIds); |
| 163 | +} |
| 164 | + |
| 165 | +const idsOf = (rows: any[] | undefined) => (rows ?? []).map((r) => r.id); |
| 166 | + |
| 167 | +describe('SeedLoadResult.landedIds — the rows a seed landed on (#23354)', () => { |
| 168 | + it('the insert path reports the ids it wrote, per dataset, in record order', async () => { |
| 169 | + const { engine, store } = createFaithfulEngine(); |
| 170 | + const result = await new SeedLoaderService(engine, createMetadata(), createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 171 | + |
| 172 | + expect(result.success).toBe(true); |
| 173 | + expect(landed(result, 'lid_team')).toEqual(idsOf(store.lid_team)); |
| 174 | + expect(landed(result, 'lid_case')).toEqual(idsOf(store.lid_case)); |
| 175 | + expect(landed(result, 'lid_membership')).toEqual(idsOf(store.lid_membership)); |
| 176 | + expect(landed(result, 'lid_case')).toHaveLength(2); |
| 177 | + // The published shape is the spec's: it parses, and the field survives. |
| 178 | + for (const r of result.results) expect(SeedLoadResultSchema.parse(r).landedIds).toEqual(r.landedIds); |
| 179 | + }); |
| 180 | + |
| 181 | + it('a replay of an unchanged seed reports the rows the FIRST load wrote (skip path)', async () => { |
| 182 | + const { engine, store } = createFaithfulEngine(); |
| 183 | + const metadata = createMetadata(); |
| 184 | + await new SeedLoaderService(engine, metadata, createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 185 | + const firstIds = { team: idsOf(store.lid_team), cases: idsOf(store.lid_case), membership: idsOf(store.lid_membership) }; |
| 186 | + |
| 187 | + const replay = await new SeedLoaderService(engine, metadata, createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 188 | + |
| 189 | + // Positive control: this really was the skip path, nothing written. |
| 190 | + expect(replay.summary.totalInserted).toBe(0); |
| 191 | + expect(replay.summary.totalUpdated).toBe(0); |
| 192 | + expect(replay.summary.totalSkipped).toBe(5); |
| 193 | + expect(landed(replay, 'lid_team')).toEqual(firstIds.team); |
| 194 | + expect(landed(replay, 'lid_case')).toEqual(firstIds.cases); |
| 195 | + expect(landed(replay, 'lid_membership')).toEqual(firstIds.membership); |
| 196 | + }); |
| 197 | + |
| 198 | + it('a replay that updates the rows reports them too (update path)', async () => { |
| 199 | + const { engine, store } = createFaithfulEngine(); |
| 200 | + const metadata = createMetadata(); |
| 201 | + await new SeedLoaderService(engine, metadata, createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 202 | + for (const row of store.lid_case) row.status = 'edited at runtime'; |
| 203 | + |
| 204 | + const replay = await new SeedLoaderService(engine, metadata, createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 205 | + |
| 206 | + expect(replay.results.find((r) => r.object === 'lid_case')!.updated).toBe(2); |
| 207 | + expect(landed(replay, 'lid_case')).toEqual(idsOf(store.lid_case)); |
| 208 | + }); |
| 209 | + |
| 210 | + it('a dataset keyed on a reference reports the matched row, whose stored key is the resolved id', async () => { |
| 211 | + const { engine, store } = createFaithfulEngine(); |
| 212 | + const metadata = createMetadata(); |
| 213 | + await new SeedLoaderService(engine, metadata, createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 214 | + const experienceId = store.lid_team.find((r) => r.name === 'Experience')!.id; |
| 215 | + const membership = store.lid_membership[0]; |
| 216 | + // The trap a reader copying the dataset falls into: the stored key is NOT |
| 217 | + // the dataset's value. |
| 218 | + expect(membership.team).toBe(experienceId); |
| 219 | + expect(membership.team).not.toBe('Experience'); |
| 220 | + |
| 221 | + const replay = await new SeedLoaderService(engine, metadata, createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 222 | + |
| 223 | + expect(replay.results.find((r) => r.object === 'lid_membership')!.skipped).toBe(1); |
| 224 | + expect(landed(replay, 'lid_membership')).toEqual([membership.id]); |
| 225 | + }); |
| 226 | + |
| 227 | + it('a row that is not in the seed is never reported, even when it shares the object', async () => { |
| 228 | + const { engine, store } = createFaithfulEngine(); |
| 229 | + const metadata = createMetadata(); |
| 230 | + await new SeedLoaderService(engine, metadata, createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 231 | + // Written by someone else between boots — a guest, a form door, a user. |
| 232 | + store.lid_case.push({ id: 'guest-row', name: 'Guest enquiry', status: 'new' }); |
| 233 | + |
| 234 | + const replay = await new SeedLoaderService(engine, metadata, createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 235 | + |
| 236 | + expect(landed(replay, 'lid_case')).not.toContain('guest-row'); |
| 237 | + expect(landed(replay, 'lid_case')).toHaveLength(2); |
| 238 | + }); |
| 239 | + |
| 240 | + it('a refused insert lands no row, so it reports no id', async () => { |
| 241 | + const { engine, store } = createFaithfulEngine({ refuseInsert: (o, row) => o === 'lid_case' && row.name === 'CASE-2' }); |
| 242 | + const result = await new SeedLoaderService(engine, createMetadata(), createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 243 | + |
| 244 | + expect(result.results.find((r) => r.object === 'lid_case')!.errored).toBe(1); |
| 245 | + expect(store.lid_case.map((r) => r.name)).toEqual(['CASE-1']); |
| 246 | + expect(landed(result, 'lid_case')).toEqual(idsOf(store.lid_case)); |
| 247 | + }); |
| 248 | + |
| 249 | + it('two records that resolve to one row report it once', async () => { |
| 250 | + const { engine, store } = createFaithfulEngine(); |
| 251 | + const metadata = createMetadata(); |
| 252 | + await new SeedLoaderService(engine, metadata, createLogger()).load({ seeds: SEEDS, config: CONFIG }); |
| 253 | + const doubled = [{ |
| 254 | + object: 'lid_case', |
| 255 | + externalId: 'name', |
| 256 | + mode: 'upsert', |
| 257 | + records: [{ name: 'CASE-1', status: 'open' }, { name: 'CASE-1', status: 'open' }], |
| 258 | + }] as any[]; |
| 259 | + |
| 260 | + const replay = await new SeedLoaderService(engine, metadata, createLogger()).load({ seeds: doubled, config: CONFIG }); |
| 261 | + |
| 262 | + const case1 = store.lid_case.find((r) => r.name === 'CASE-1')!.id; |
| 263 | + expect(landed(replay, 'lid_case')).toEqual([case1]); |
| 264 | + }); |
| 265 | + |
| 266 | + it('a dry run writes nothing and reports nothing', async () => { |
| 267 | + const { engine, store } = createFaithfulEngine(); |
| 268 | + const result = await new SeedLoaderService(engine, createMetadata(), createLogger()).load({ |
| 269 | + seeds: SEEDS, |
| 270 | + config: { ...CONFIG, dryRun: true }, |
| 271 | + }); |
| 272 | + |
| 273 | + // Positive control: the dry run did count the would-be inserts. |
| 274 | + expect(result.summary.totalInserted).toBe(5); |
| 275 | + expect(store.lid_case).toBeUndefined(); |
| 276 | + for (const r of result.results) expect(r.landedIds).toEqual([]); |
| 277 | + }); |
| 278 | +}); |
0 commit comments