import { it, expect, vi } from 'vitest' import { commitOrganization, parseOrganization, bindOrganizationDepartments } from './qaOrg' const raw = { state: 'ready', directory_id: 'fixture-dir', directory_source: 'weekly_report_snapshot', directory_synced_at: '2026-10-18T00:00:00Z', revision_id: 'old', updated_by: 'fixture-admin', updated_at: null, regions: [{ id: 'new-region', name: '业务区域' }], rules: [{ department_id: 'root', region: 'new-region' }], view: { departments: [ { id: 'root', parent_id: '', name: 'Fixture', path: 'Fixture', region: 'new-region', explicit: true, inherited_from: '', }, ], regions: { 'new-region': 1 }, total_people: 1, unassigned_people: 0, conflicting_people: 0, missing_departments: [], }, } const response = (v: unknown) => ({ ok: true, status: 200, json: async () => ({ data: v }) }) it('binds selected current department IDs in place and verifies the persisted version', async () => { const fetch = vi .fn() .mockResolvedValueOnce(response(raw)) .mockResolvedValueOnce(response(raw)) .mockResolvedValueOnce(response({ ...raw, revision_id: 'new' })) .mockResolvedValueOnce(response({ ...raw, revision_id: 'new' })) vi.stubGlobal('fetch', fetch) try { const saved = await bindOrganizationDepartments( { directory_id: 'fixture-dir', revision_id: 'old' }, ['root'], 'new-region' ) expect(saved.revision_id).toBe('new') expect(fetch.mock.calls.map((c) => c[1]?.method)).toEqual(['GET', 'POST', 'PUT', 'GET']) expect(JSON.parse(fetch.mock.calls[2][1].body).rules).toEqual(raw.rules) } finally { vi.unstubAllGlobals() } }) it('refuses binding a stale directory/revision or a missing department before any write', async () => { for (const [revision, ids] of [ ['stale', ['root']], ['old', ['missing']], ] as const) { const fetch = vi.fn().mockResolvedValue(response(raw)) vi.stubGlobal('fetch', fetch) try { await expect( bindOrganizationDepartments({ directory_id: 'fixture-dir', revision_id: revision }, [...ids], 'new-region') ).rejects.toThrow() expect(fetch).toHaveBeenCalledTimes(1) } finally { vi.unstubAllGlobals() } } }) it('previews and persists the same versioned catalog/rules before independent exact readback', async () => { const state = parseOrganization(raw), receipt = { ...raw, revision_id: 'new' } const fetch = vi .fn() .mockResolvedValueOnce(response(raw)) .mockResolvedValueOnce(response(receipt)) .mockResolvedValueOnce(response(receipt)) vi.stubGlobal('fetch', fetch) try { expect((await commitOrganization(state, state.regions!, state.rules)).revision_id).toBe('new') expect(fetch.mock.calls.map((c) => c[0])).toEqual([ '/api/v1/qa-ops/organization/preview', '/api/v1/qa-ops/organization/regions', '/api/v1/qa-ops/organization', ]) for (const call of fetch.mock.calls.slice(0, 2)) { const input = JSON.parse(call[1].body) expect(input).toMatchObject({ directory_id: 'fixture-dir', base_revision: 'old', regions: raw.regions, rules: raw.rules, }) } } finally { vi.unstubAllGlobals() } }) it('does not claim a successful save when another revision wins before readback', async () => { vi.stubGlobal( 'fetch', vi .fn() .mockResolvedValueOnce(response(raw)) .mockResolvedValueOnce(response({ ...raw, revision_id: 'new' })) .mockResolvedValueOnce(response({ ...raw, revision_id: 'other' })) ) try { const state = parseOrganization(raw) await expect(commitOrganization(state, state.regions!, state.rules)).rejects.toThrow('保存后配置') } finally { vi.unstubAllGlobals() } })