const { findBestMaterialMatch, rankMaterialCandidates, scoreCandidate } = require('../src/services/smartPackageMaterialMatchService'); describe('smartPackageMaterialMatchService', () => { const catalog = [ { id: 10, sku: 'TAG-001', name: 'Taglægte 38x73 mm C24', unit: 'lbm', price: 14.5 }, { id: 11, sku: 'ISO-200', name: 'Mineraluld 200 mm', unit: 'm²', price: 135 }, { id: 12, sku: 'TAG-002', name: 'Taglægte 25x50 mm', unit: 'lbm', price: 9.5 } ]; test('prefers an exact master material SKU', () => { const match = findBestMaterialMatch({ name: 'Andet navn', itemCode: 'TAG-001' }, catalog); expect(match).toMatchObject({ status: 'matched_sku', score: 1 }); expect(match.material.id).toBe(10); }); test('matches a descriptive package line to the master material', () => { const match = findBestMaterialMatch({ name: 'Mineraluld 200mm', unit: 'm²' }, catalog, { minimumMargin: 0 }); expect(match.material.id).toBe(11); expect(match.status).toBe('matched_name'); }); test('matches Danish compound material names against catalog roots', () => { const line = { name: 'Gipsplader', unit: 'm²' }; const board = { name: 'RAW STANDARD GIPS AK 13 X 900 X 2400 MM', unit: 'PL' }; const tool = { name: 'BOSCH AKKUGIPSSKRUETRÆKKER GTB 18V', unit: 'STK' }; expect(rankMaterialCandidates(line.name, line.unit, [tool, board])[0]).toBe(board); }); test('keeps fuzzy roots suggestion-only instead of auto-matching imports', () => { const match = findBestMaterialMatch({ name: 'Gipsplader', unit: 'm²' }, [ { id: 700, name: 'RAW STANDARD GIPS AK 13 X 900 X 2400 MM', unit: 'PL' } ]); expect(match.material).toBeNull(); }); test('ranks sheet goods above unrelated products for an area-based board query', () => { const candidates = rankMaterialCandidates('Gipsplader', 'm²', [ { id: 1901, name: 'KOBLINGSDÅSE T/GIPS', unit: 'STK' }, { id: 700, name: 'RAW STANDARD GIPS AK 13 X 900 X 2400 MM', unit: 'PL' } ]); expect(candidates.map(item => item.id)).toEqual([700, 1901]); }); test('scores a dimensionally matching name above a generic alternative', () => { const line = { name: 'Taglægte 38x73 mm', unit: 'lbm' }; expect(scoreCandidate(line, catalog[0])).toBeGreaterThan(scoreCandidate(line, catalog[2])); }); });