|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { SEED_APPLIED_LANDED_IDS_CAP, SeedAppliedLandedSchema } from '@objectstack/spec/api'; |
| 5 | +import { seedAppliedLanded, type SeedLandingResult } from './seed-applied-landed.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * [#30348] `seedAppliedLanded` — the one projection both `seedApplied` |
| 9 | + * producers fold the loader's per-dataset results through, pinned on its own, |
| 10 | + * without an engine: the junk it skips, the empty answer, the cap boundary, and |
| 11 | + * the cross-dataset merge that keeps a row this batch created out of |
| 12 | + * `matched`. The publish doors' pins (`@objectstack/objectql`'s |
| 13 | + * `publish-seed-receipt-landed-ids.test.ts`) read the same answer off a real |
| 14 | + * engine; these pin the cases a door cannot stage cleanly. |
| 15 | + */ |
| 16 | + |
| 17 | +const ids = (prefix: string, n: number) => Array.from({ length: n }, (_, i) => `${prefix}${i}`); |
| 18 | +const list = (xs: string[], total = xs.length) => ({ ids: xs, total, truncated: total > xs.length }); |
| 19 | +const NONE = list([]); |
| 20 | + |
| 21 | +describe('seedAppliedLanded (#30348)', () => { |
| 22 | + it('no results: an empty list, never undefined', () => { |
| 23 | + expect(seedAppliedLanded([])).toEqual([]); |
| 24 | + }); |
| 25 | + |
| 26 | + it('skips junk entries — a null, a missing or non-string object — and keeps the real ones', () => { |
| 27 | + const junk = [ |
| 28 | + null, |
| 29 | + undefined, |
| 30 | + {}, |
| 31 | + { object: 42, landedIds: ['x'] }, |
| 32 | + { landedIds: ['y'], insertedIds: ['y'] }, |
| 33 | + { object: 'account', landedIds: ['a1'], insertedIds: ['a1'] }, |
| 34 | + ] as unknown as SeedLandingResult[]; |
| 35 | + |
| 36 | + expect(seedAppliedLanded(junk)).toEqual([{ object: 'account', inserted: list(['a1']), matched: NONE }]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('a dataset that landed nothing still reports its object, with two empty lists', () => { |
| 40 | + expect(seedAppliedLanded([{ object: 'account', landedIds: [], insertedIds: [] }])) |
| 41 | + .toEqual([{ object: 'account', inserted: NONE, matched: NONE }]); |
| 42 | + // A result predating `insertedIds` (both keys absent) reads the same. |
| 43 | + expect(seedAppliedLanded([{ object: 'account' }])).toEqual([{ object: 'account', inserted: NONE, matched: NONE }]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('splits one dataset: inserted ids under `inserted`, every other landed id under `matched`', () => { |
| 47 | + expect(seedAppliedLanded([{ object: 'account', landedIds: ['c9', 'a1', 'a2'], insertedIds: ['a1', 'a2'] }])) |
| 48 | + .toEqual([{ object: 'account', inserted: list(['a1', 'a2']), matched: list(['c9']) }]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('the cap: exactly 200 is carried whole and not truncated; 201 carries the first 200 and says so', () => { |
| 52 | + expect(SEED_APPLIED_LANDED_IDS_CAP).toBe(200); |
| 53 | + const exact = ids('r', SEED_APPLIED_LANDED_IDS_CAP); |
| 54 | + const over = ids('r', SEED_APPLIED_LANDED_IDS_CAP + 1); |
| 55 | + |
| 56 | + const [atCap] = seedAppliedLanded([{ object: 'account', landedIds: exact, insertedIds: exact }]); |
| 57 | + expect(atCap.inserted).toEqual({ ids: exact, total: 200, truncated: false }); |
| 58 | + |
| 59 | + const [cut] = seedAppliedLanded([{ object: 'account', landedIds: over, insertedIds: [] }]); |
| 60 | + expect(cut.matched).toEqual({ ids: over.slice(0, 200), total: 201, truncated: true }); |
| 61 | + expect(cut.inserted).toEqual(NONE); |
| 62 | + |
| 63 | + // What the projection emits is what the spec schema carries. |
| 64 | + expect(SeedAppliedLandedSchema.parse(cut)).toEqual(cut); |
| 65 | + expect(SeedAppliedLandedSchema.parse(atCap)).toEqual(atCap); |
| 66 | + }); |
| 67 | + |
| 68 | + it('the cap is per list: 201 inserted and 201 matched cut each list on its own', () => { |
| 69 | + const ins = ids('i', 201); |
| 70 | + const mat = ids('m', 201); |
| 71 | + const [entry] = seedAppliedLanded([{ object: 'account', landedIds: [...ins, ...mat], insertedIds: ins }]); |
| 72 | + expect(entry.inserted).toEqual({ ids: ins.slice(0, 200), total: 201, truncated: true }); |
| 73 | + expect(entry.matched).toEqual({ ids: mat.slice(0, 200), total: 201, truncated: true }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('two datasets of one object: a row the first inserted and the second matched stays `inserted` only', () => { |
| 77 | + const results: SeedLandingResult[] = [ |
| 78 | + { object: 'account', landedIds: ['a1', 'a2'], insertedIds: ['a1', 'a2'] }, |
| 79 | + // Its key matched `a1`, the row the first dataset just created, and a customer row `c9`. |
| 80 | + { object: 'account', landedIds: ['a1', 'c9', 'a3'], insertedIds: ['a3'] }, |
| 81 | + ]; |
| 82 | + |
| 83 | + expect(seedAppliedLanded(results)).toEqual([ |
| 84 | + { object: 'account', inserted: list(['a1', 'a2', 'a3']), matched: list(['c9']) }, |
| 85 | + ]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('a row an EARLIER dataset matched and a later one inserted is `inserted`, never both', () => { |
| 89 | + // The rule is order-free: membership in any dataset's `insertedIds` decides. |
| 90 | + const [entry] = seedAppliedLanded([ |
| 91 | + { object: 'account', landedIds: ['x'], insertedIds: [] }, |
| 92 | + { object: 'account', landedIds: ['x'], insertedIds: ['x'] }, |
| 93 | + ]); |
| 94 | + expect(entry).toEqual({ object: 'account', inserted: list(['x']), matched: NONE }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('objects keep the order they first loaded in; ids dedupe, first occurrence kept', () => { |
| 98 | + const out = seedAppliedLanded([ |
| 99 | + { object: 'contact', landedIds: ['k1'], insertedIds: ['k1'] }, |
| 100 | + { object: 'account', landedIds: ['a1', 'a1'], insertedIds: ['a1', 'a1'] }, |
| 101 | + { object: 'contact', landedIds: ['k2', 'k1'], insertedIds: ['k2'] }, |
| 102 | + ]); |
| 103 | + expect(out.map((e) => e.object)).toEqual(['contact', 'account']); |
| 104 | + expect(out[0]).toEqual({ object: 'contact', inserted: list(['k1', 'k2']), matched: NONE }); |
| 105 | + expect(out[1]).toEqual({ object: 'account', inserted: list(['a1']), matched: NONE }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments