import { describe, expect, it } from 'vitest'; import { computeDiff } from '../src/lib/marketplace/diff.ts'; const fromInfo = { semver: '1.0.0', versionId: 'v1' }; const toInfo = { semver: '1.1.0', versionId: 'v2' }; function fullCard(ord: number, hash: string, front = `Q${ord}`, back = `A${ord}`) { return { contentHash: hash, type: 'basic', fields: { front, back }, ord }; } describe('computeDiff', () => { it('classifies all-unchanged when nothing moved', () => { const diff = computeDiff({ from: [ { contentHash: 'h1', ord: 0 }, { contentHash: 'h2', ord: 1 }, ], to: [fullCard(0, 'h1'), fullCard(1, 'h2')], fromInfo, toInfo, }); expect(diff.unchanged).toHaveLength(2); expect(diff.added).toHaveLength(0); expect(diff.changed).toHaveLength(0); expect(diff.removed).toHaveLength(0); }); it('detects added when a brand-new card appears', () => { const diff = computeDiff({ from: [{ contentHash: 'h1', ord: 0 }], to: [fullCard(0, 'h1'), fullCard(1, 'h2')], fromInfo, toInfo, }); expect(diff.added).toHaveLength(1); expect(diff.added[0].contentHash).toBe('h2'); expect(diff.unchanged).toHaveLength(1); }); it('detects removed when a card vanishes (and ord is unique)', () => { const diff = computeDiff({ from: [ { contentHash: 'h1', ord: 0 }, { contentHash: 'h2', ord: 1 }, ], to: [fullCard(0, 'h1')], fromInfo, toInfo, }); expect(diff.removed).toHaveLength(1); expect(diff.removed[0].contentHash).toBe('h2'); }); it('detects changed when same ord has different hash', () => { const diff = computeDiff({ from: [{ contentHash: 'h1', ord: 0 }], to: [fullCard(0, 'h1-tweaked', 'Q0', 'A0-edited')], fromInfo, toInfo, }); expect(diff.changed).toHaveLength(1); expect(diff.changed[0].previous.contentHash).toBe('h1'); expect(diff.changed[0].next.contentHash).toBe('h1-tweaked'); expect(diff.added).toHaveLength(0); expect(diff.removed).toHaveLength(0); }); it('mixed: 1 unchanged, 1 changed, 1 added, 1 removed', () => { const diff = computeDiff({ from: [ { contentHash: 'h-stay', ord: 0 }, { contentHash: 'h-old', ord: 1 }, { contentHash: 'h-bye', ord: 2 }, ], to: [ fullCard(0, 'h-stay'), fullCard(1, 'h-new', 'replaced', 'card'), fullCard(3, 'h-fresh', 'new', 'card'), ], fromInfo, toInfo, }); expect(diff.unchanged.map((c) => c.contentHash)).toEqual(['h-stay']); expect(diff.changed).toHaveLength(1); expect(diff.changed[0].previous.contentHash).toBe('h-old'); expect(diff.changed[0].next.contentHash).toBe('h-new'); expect(diff.added).toHaveLength(1); expect(diff.added[0].contentHash).toBe('h-fresh'); expect(diff.removed).toHaveLength(1); expect(diff.removed[0].contentHash).toBe('h-bye'); }); it('returns the version-info verbatim', () => { const diff = computeDiff({ from: [], to: [], fromInfo, toInfo, }); expect(diff.from).toEqual(fromInfo); expect(diff.to).toEqual(toInfo); }); });