|
1 | 1 | // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
2 | 2 |
|
3 | 3 | import { describe, it, expect, vi } from 'vitest'; |
4 | | -import { SeedLoaderService } from './seed-loader.js'; |
| 4 | +import { SeedLoaderService, seedLoadPartialWrites } from './seed-loader.js'; |
| 5 | +import { seedAppliedLanded } from './seed-applied-landed.js'; |
5 | 6 | import type { IDataEngine, IMetadataService } from '@objectstack/spec/contracts'; |
6 | 7 | import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate } from '@objectstack/metadata-core'; |
7 | 8 |
|
@@ -300,3 +301,58 @@ describe('seed deferred back-fill failure is reported, not swallowed (framework# |
300 | 301 | expect(result.summary.totalErrored).toBe(0); |
301 | 302 | }); |
302 | 303 | }); |
| 304 | + |
| 305 | +/** |
| 306 | + * [#30429] A load that throws in PASS 2 — after every dataset finished — is |
| 307 | + * still a thrown load, and its partial-writes reading lists every dataset as |
| 308 | + * finished: no `failedObject`, and the receipt's `landed` (the same |
| 309 | + * `seedAppliedLanded` projection the publish catch uses) names every row the |
| 310 | + * load wrote. |
| 311 | + * |
| 312 | + * Pass 2 guards its own driver traffic (a failed back-fill write is counted, |
| 313 | + * a failed lookup probe is a miss), so the throw is staged at the one call in |
| 314 | + * it that is not guarded: the `logger.error` its back-fill failure reports |
| 315 | + * through — a log transport that fails while pass 2 reports. What is pinned is |
| 316 | + * the loader's state at that point, not the transport. |
| 317 | + */ |
| 318 | +describe('a load that throws in pass 2 reports every dataset as finished (#30429)', () => { |
| 319 | + it('finished lists both datasets with their store ids; no failed object; landed names every row', async () => { |
| 320 | + const { engine, store } = createFaithfulEngine(); |
| 321 | + const metadata = createMetadata(); |
| 322 | + const realUpdate = (engine.update as any).getMockImplementation(); |
| 323 | + (engine.update as any).mockImplementation(async (obj: string, data: any, opts: any) => { |
| 324 | + if (obj === 'audit_department') throw new Error('fetch failed'); |
| 325 | + return realUpdate(obj, data, opts); |
| 326 | + }); |
| 327 | + const logger = createLogger(); |
| 328 | + logger.error.mockImplementation(() => { throw new Error('the log transport is down'); }); |
| 329 | + |
| 330 | + const err = await new SeedLoaderService(engine, metadata, logger) |
| 331 | + .load({ seeds: SEEDS, config: CONFIG }) |
| 332 | + .then(() => undefined, (e: unknown) => e); |
| 333 | + |
| 334 | + expect((err as Error)?.message).toBe('the log transport is down'); |
| 335 | + // Positive control: the throw came from pass 2's back-fill report, after |
| 336 | + // both datasets wrote their row. |
| 337 | + expect((engine.update as any).mock.calls.some(([obj]: [string]) => obj === 'audit_department')).toBe(true); |
| 338 | + const deptIds = (store.audit_department ?? []).map((r) => String(r.id)); |
| 339 | + const workerIds = (store.audit_worker ?? []).map((r) => String(r.id)); |
| 340 | + expect(deptIds).toHaveLength(1); |
| 341 | + expect(workerIds).toHaveLength(1); |
| 342 | + const partial = seedLoadPartialWrites(err)!; |
| 343 | + expect(partial).toEqual({ |
| 344 | + inserted: 2, |
| 345 | + updated: 0, |
| 346 | + finished: [ |
| 347 | + { object: 'audit_department', landedIds: deptIds, insertedIds: deptIds }, |
| 348 | + { object: 'audit_worker', landedIds: workerIds, insertedIds: workerIds }, |
| 349 | + ], |
| 350 | + }); |
| 351 | + expect('failedObject' in partial).toBe(false); |
| 352 | + const list = (ids: string[]) => ({ ids, total: ids.length, truncated: false }); |
| 353 | + expect(seedAppliedLanded(partial.finished)).toEqual([ |
| 354 | + { object: 'audit_department', inserted: list(deptIds), matched: list([]) }, |
| 355 | + { object: 'audit_worker', inserted: list(workerIds), matched: list([]) }, |
| 356 | + ]); |
| 357 | + }); |
| 358 | +}); |
0 commit comments