deckhearth/test/lib/card-import-reconcile-submissions.test.js
Randall Stillwell ef08398149 Auto-link pending scan submissions after catalog sync imports.
When a set lands via runCatalogSync, match pending card_submissions by set/name/number to catalog rows and approve them with promoted_card_id instead of leaving them in the admin queue.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-28 09:46:11 -05:00

88 lines
2.2 KiB
JavaScript

import { describe, expect, it } from 'vitest';
import {
normalizeCardNumber,
pickCatalogCardForPayload,
submissionPayloadMatchesSet,
} from '../../lib/card-import/reconcile-submissions.js';
describe('normalizeCardNumber', () => {
it('treats leading-zero numerators as equivalent', () => {
expect(normalizeCardNumber('015/208')).toBe('15/208');
expect(normalizeCardNumber('15/208')).toBe('15/208');
});
});
describe('submissionPayloadMatchesSet', () => {
it('matches by set code or set name for the imported game', () => {
const imported = { game: 'pokemon', setCode: 'sv10', setName: 'Perfect Order' };
expect(
submissionPayloadMatchesSet(
{ name: 'Seel', setCode: 'sv10', game: 'Pokemon' },
imported
)
).toBe(true);
expect(
submissionPayloadMatchesSet(
{ name: 'Seel', set: 'Perfect Order', game: 'pokemon' },
imported
)
).toBe(true);
expect(
submissionPayloadMatchesSet(
{ name: 'Seel', setCode: 'sv9', game: 'Pokemon' },
imported
)
).toBe(false);
expect(
submissionPayloadMatchesSet({ name: 'Bolt', setCode: 'fin', game: 'MTG' }, {
game: 'pokemon',
setCode: 'sv10',
setName: 'Perfect Order',
})
).toBe(false);
});
});
describe('pickCatalogCardForPayload', () => {
const catalogRows = [
{ id: 1, card_number: '015/208' },
{ id: 2, card_number: '016/208' },
];
it('matches a specific printing when the collector number aligns', () => {
expect(
pickCatalogCardForPayload(catalogRows, {
name: 'Seel',
cardNumber: '15/208',
})
).toBe(1);
});
it('returns null when the collector number is ambiguous or missing among siblings', () => {
expect(
pickCatalogCardForPayload(catalogRows, {
name: 'Seel',
cardNumber: '99/208',
})
).toBeNull();
expect(
pickCatalogCardForPayload(catalogRows, {
name: 'Seel',
})
).toBeNull();
});
it('matches name-only payloads when exactly one catalog row exists', () => {
expect(
pickCatalogCardForPayload([{ id: 42, card_number: '1/100' }], {
name: 'Pikachu',
})
).toBe(42);
});
});