import type { Pool } from 'pg'; import { describe, expect, it, vi } from 'vitest'; import { IngestionRepository } from './repository.ts'; import type { NormalizedMarketProduct } from './types.ts'; const product: NormalizedMarketProduct = { source: 'mvideo', sourceProductId: '400484046', sourceUrl: 'https://www.mvideo.ru/products/test-400484046', title: 'Verification Canonical Link 1', brand: 'Verification', model: 'Canonical Link 1', canonicalModel: 'Canonical Link 1', canonicalName: 'Verification Canonical Link 1', specs: { night_mode: false }, rawSpecs: [ { group: 'Verification', label: 'Night mode', value: 'нет', normalizedKey: 'night_mode', normalizedValue: false, }, ], images: [], }; describe('IngestionRepository', () => { it('links an exact canonical-name match and serializes source conflicts as JSONB', async () => { const conflictValues = [ { source: 'mvideo', value: false }, { source: 'yandex_market', value: true }, ]; const query = vi.fn(async (statement: string) => { if (statement.includes('select id\n from products')) { return { rows: [{ id: 'ym_5905353579' }], rowCount: 1 }; } if (statement.includes('insert into manufacturers')) { return { rows: [{ id: 'manufacturer-id' }], rowCount: 1 }; } if (statement.includes('select\n normalized_key')) { return { rows: [{ normalized_key: 'night_mode', values_json: conflictValues }], rowCount: 1, }; } return { rows: [], rowCount: 0 }; }); const repository = new IngestionRepository({ query } as unknown as Pool); const productId = await repository.upsertNormalizedProduct( product, 'stable-content-hash', '2026-07-30T23:00:00.000Z', ); const calls = query.mock.calls as unknown as Array<[string, unknown[] | undefined]>; const sourceCall = calls.find(([statement]) => statement.includes('insert into product_sources')); const conflictCall = calls.find(([statement]) => statement.includes('insert into moderation_conflicts')); const queueCalls = calls.filter(([statement]) => statement.includes('insert into moderation_queue')); expect(productId).toBe('ym_5905353579'); expect(sourceCall?.[1]?.[0]).toBe('ym_5905353579'); expect(JSON.parse(String(conflictCall?.[1]?.[2]))).toEqual(conflictValues); expect(queueCalls.at(-1)?.[1]).toEqual(['ym_5905353579', 'same_product']); }); });