import { describe, expect, it } from 'vitest'; import { ensureBranchCategoryCode, findBoundCodeForAliasKey, generateBranchCategoryCode, hasBranchCategoryPrefix, stripBranchCategoryPrefix, } from '../src/services/categoryCodePolicy'; describe('categoryCodePolicy', () => { it('strips and ensures BR prefix', () => { expect(stripBranchCategoryPrefix('VEG001')).toBe('VEG001'); expect(stripBranchCategoryPrefix('BR003_VEG001')).toBe('VEG001'); expect(ensureBranchCategoryCode('BR003', 'VEG001')).toBe('BR003_VEG001'); expect(ensureBranchCategoryCode('BR003', 'BR002_VEG001')).toBe('BR003_VEG001'); expect(ensureBranchCategoryCode('BR003', 'BR003_CAT1')).toBe('BR003_CAT1'); }); it('detects prefix', () => { expect(hasBranchCategoryPrefix('BR003_CAT1')).toBe(true); expect(hasBranchCategoryPrefix('BR003_CAT1', 'BR003')).toBe(true); expect(hasBranchCategoryPrefix('BR003_CAT1', 'BR002')).toBe(false); expect(hasBranchCategoryPrefix('CAT1')).toBe(false); }); it('generates branch-prefixed CAT codes', () => { const code = generateBranchCategoryCode('BR009'); expect(code.startsWith('BR009_CAT')).toBe(true); }); it('matches alias bare keys against prefixed bound codes', () => { const set = new Set(['BR003_VEG001', 'BR003_MEAT001']); expect(findBoundCodeForAliasKey(set, 'VEG001')).toBe('BR003_VEG001'); expect(findBoundCodeForAliasKey(set, 'FRUIT001')).toBeNull(); }); });