|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + validateSeedReplaySafety, |
| 4 | + SEED_INSERT_MODE_DUPLICATES_ON_REPLAY, |
| 5 | +} from './validate-seed-replay-safety.js'; |
| 6 | + |
| 7 | +describe('validateSeedReplaySafety (framework#3434 replay-unsafe seed gate)', () => { |
| 8 | + it('passes a stack with no seed data', () => { |
| 9 | + expect(validateSeedReplaySafety({})).toHaveLength(0); |
| 10 | + expect(validateSeedReplaySafety({ data: [] })).toHaveLength(0); |
| 11 | + }); |
| 12 | + |
| 13 | + it('passes idempotent modes (ignore / upsert / update / replace) and the default (unset)', () => { |
| 14 | + const findings = validateSeedReplaySafety({ |
| 15 | + data: [ |
| 16 | + { object: 'a', mode: 'ignore', externalId: ['team', 'project'], records: [] }, |
| 17 | + { object: 'b', mode: 'upsert', externalId: 'code', records: [] }, |
| 18 | + { object: 'c', mode: 'update', records: [] }, |
| 19 | + { object: 'd', mode: 'replace', records: [] }, |
| 20 | + { object: 'e', records: [] }, // mode unset → defaults to upsert, safe |
| 21 | + ], |
| 22 | + }); |
| 23 | + expect(findings).toHaveLength(0); |
| 24 | + }); |
| 25 | + |
| 26 | + it("flags a mode: 'insert' seed with location + an actionable fix hint", () => { |
| 27 | + const findings = validateSeedReplaySafety({ |
| 28 | + data: [ |
| 29 | + { object: 'showcase_project_membership', mode: 'insert', records: [{ team: 'x', project: 'y' }] }, |
| 30 | + ], |
| 31 | + }); |
| 32 | + expect(findings).toHaveLength(1); |
| 33 | + expect(findings[0]).toMatchObject({ |
| 34 | + severity: 'warning', |
| 35 | + rule: SEED_INSERT_MODE_DUPLICATES_ON_REPLAY, |
| 36 | + path: 'data[0].mode', |
| 37 | + }); |
| 38 | + expect(findings[0].where).toContain('showcase_project_membership'); |
| 39 | + expect(findings[0].message).toContain('replay'); |
| 40 | + // Hint steers to the idempotent modes and the composite externalId the fix added. |
| 41 | + expect(findings[0].hint).toContain('ignore'); |
| 42 | + expect(findings[0].hint).toContain('upsert'); |
| 43 | + expect(findings[0].hint).toContain('externalId'); |
| 44 | + expect(findings[0].hint).toContain("['team', 'project']"); |
| 45 | + }); |
| 46 | + |
| 47 | + it('flags each insert seed and reports its index in the path; leaves safe seeds alone', () => { |
| 48 | + const findings = validateSeedReplaySafety({ |
| 49 | + data: [ |
| 50 | + { object: 'safe', mode: 'upsert', records: [] }, |
| 51 | + { object: 'bad_one', mode: 'insert', records: [] }, |
| 52 | + { object: 'also_safe', mode: 'ignore', records: [] }, |
| 53 | + { object: 'bad_two', mode: 'insert', records: [] }, |
| 54 | + ], |
| 55 | + }); |
| 56 | + expect(findings).toHaveLength(2); |
| 57 | + expect(findings.map((f) => f.path)).toEqual(['data[1].mode', 'data[3].mode']); |
| 58 | + expect(findings.map((f) => f.where)).toEqual(['seed "bad_one"', 'seed "bad_two"']); |
| 59 | + }); |
| 60 | + |
| 61 | + it('falls back to a data[i] location when a seed has no object name', () => { |
| 62 | + const findings = validateSeedReplaySafety({ data: [{ mode: 'insert', records: [] }] }); |
| 63 | + expect(findings).toHaveLength(1); |
| 64 | + expect(findings[0].where).toBe('data[0]'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('ignores non-object entries defensively', () => { |
| 68 | + const findings = validateSeedReplaySafety({ data: [null, 'insert', 42, { object: 'z', mode: 'insert', records: [] }] }); |
| 69 | + expect(findings).toHaveLength(1); |
| 70 | + expect(findings[0].where).toContain('z'); |
| 71 | + }); |
| 72 | +}); |
0 commit comments