import { describe, expect, it, vi } from 'vitest' import { fetchOrganization, parseOrganization, changeRegionRule } from './qaOrg' export const directoryFixture = { state: 'ready', directory_id: 'dir-fixture', directory_source: 'weekly_report_snapshot', directory_synced_at: '2026-09-27T14:03:44Z', revision_id: '', updated_by: '', updated_at: null, rules: [], view: { departments: [ { id: 'root', parent_id: '', name: '示例公司', path: '示例公司', region: '', explicit: false, inherited_from: '', }, ], regions: { 国内工业: 0, APAC: 0, EMEA: 0, America: 0 }, total_people: 1, unassigned_people: 1, conflicting_people: 0, missing_departments: [], }, } describe('department region configuration', () => { it('uses configurable stable region IDs rather than a four-name whitelist', () => { const catalog = [ { id: 'education', name: '国内教育' }, { id: 'west', name: '国外工业Amercia' }, ] const state = parseOrganization({ ...directoryFixture, regions: catalog, rules: [{ department_id: 'root', region: 'education' }], view: { ...directoryFixture.view, departments: [{ ...directoryFixture.view.departments[0], region: 'education', explicit: true }], regions: { education: 1, west: 0 }, unassigned_people: 0, }, }) expect(state.regions).toEqual(catalog) expect(state.rules[0].region).toBe('education') expect(state.view.regions.education).toBe(1) }) it('rejects undeclared region counts rather than silently dropping them', () => { expect(() => parseOrganization({ ...directoryFixture, view: { ...directoryFixture.view, regions: { ...directoryFixture.view.regions, undeclared: 0 } }, }) ).toThrow() }) it('parses an honest dated snapshot without exposing employee identities', () => { const state = parseOrganization(directoryFixture) expect(state.directory_source).toBe('weekly_report_snapshot') expect(state.view.unassigned_people).toBe(1) expect(() => parseOrganization({ ...directoryFixture, view: { ...directoryFixture.view, total_people: 0 } }) ).toThrow() expect(() => parseOrganization({ ...directoryFixture, view: { ...directoryFixture.view, departments: [{}, {}] } }) ).toThrow() }) it('changes only a direct stable ID rule, leaving sibling and child overrides alone', () => { const rules = [ { department_id: 'parent', region: '国内工业' }, { department_id: 'child', region: 'APAC' }, ] expect(changeRegionRule(rules, 'parent', 'EMEA', true)).toEqual([ { department_id: 'child', region: 'APAC' }, { department_id: 'parent', region: 'EMEA' }, ]) expect(changeRegionRule(rules, 'parent', '国内工业', false)).toEqual([{ department_id: 'child', region: 'APAC' }]) }) it('surfaces store errors rather than displaying an empty directory', async () => { vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue({ ok: false, status: 503, json: async () => ({ error: '通讯录不可用' }) }) ) await expect(fetchOrganization()).rejects.toThrow('通讯录不可用') vi.unstubAllGlobals() }) })