import { renderToStaticMarkup } from 'react-dom/server' // @ts-expect-error The test runner provides Node; the browser bundle intentionally omits @types/node. import { readFileSync } from 'node:fs' import { describe, expect, it, vi } from 'vitest' import { RegionCatalogView } from './QARegions' import { parseOrganization } from './qaOrg' const state = parseOrganization({ state: 'ready', directory_id: 'd', directory_source: 'weekly_report_snapshot', directory_synced_at: '2026-09-27T14:03:44Z', revision_id: 'r', updated_by: 'fixture-admin', updated_at: null, rules: [{ department_id: 'a', region: 'APAC' }], view: { departments: [ { id: 'a', parent_id: '', name: '', path: '示例公司/', region: 'APAC', explicit: true, inherited_from: '', }, ], regions: { 国内工业: 0, APAC: 1, EMEA: 0, America: 0 }, total_people: 2, unassigned_people: 0, conflicting_people: 1, missing_departments: [], }, }) const props = { state, preview: null, rules: state.rules, busy: false, error: '', notice: '', dirty: false, onPreview: vi.fn(), onSave: vi.fn(), onReload: vi.fn(), onRemoveMissing: vi.fn(), } describe('region catalog settings', () => { it('uses a region-name list without a second department mapping page', () => { const html = renderToStaticMarkup() expect(html).toContain('区域名称列表') expect(html).toContain('保存区域设置') expect(html).not.toContain('qa-region-table') expect(html).not.toContain('批量归属') expect(html).not.toContain('全选当前列表') }) it('shows direct binding counts and enters local confirmation before removing a bound region', () => { const regions = [ { id: 'APAC', name: 'Assigned' }, { id: 'spare', name: 'Spare' }, ] const html = renderToStaticMarkup() expect(html).toContain('删除区域 Spare') expect(html).toContain('直接绑定 1 个部门') expect(html).not.toMatch(/aria-label="删除区域 Assigned"[^>]*disabled=""/) expect(html).not.toContain('确认删除') }) it('shows editable saved region names but keeps department binding out of the settings list', () => { const custom = { ...state, regions: [{ id: 'APAC', name: '国外工业Amercia' }], view: { ...state.view, regions: { APAC: 1 } }, } const html = renderToStaticMarkup() for (const label of ['区域设置', '添加区域', '区域名称列表', '保存区域设置', '国外工业Amercia']) expect(html).toContain(label) for (const removed of ['一级部门', '二级部门', '批量归属', '全选当前列表', 'qa-region-table']) expect(html).not.toContain(removed) expect(html).not.toContain('将 示例公司/') }) it('shows real synchronization stages without inventing progress and protects dirty drafts', () => { const syncStatus = { configured: true, worker_ready: true, job: { id: 'j', state: 'running' as const, attempt: 1, max_attempts: 3, updated_at: '2026-10-17T08:00:00Z', error_code: '', result: null, }, } const html = renderToStaticMarkup() expect(html).toContain('正在读取并校验通讯录') expect(html).not.toMatch(/\d+%/) expect(html).toMatch(/disabled=""[^>]*>同步通讯录/) const dirty = renderToStaticMarkup( ) expect(dirty).toContain('未保存的区域修改') expect(dirty).toMatch(/disabled=""[^>]*>同步通讯录/) }) it('uses a responsive catalog list with wrapping warnings and no obsolete mapping-table styles', () => { const css = readFileSync(new URL('./qaRegions.css', import.meta.url), 'utf8') expect(css).toMatch(/\.qa-region-catalog-list/) expect(css).toMatch(/minmax\(0,\s*1fr\)/) expect(css).toMatch(/\.qa-region-row-actions/) expect(css).toMatch(/overflow-wrap:\s*anywhere/) expect(css).not.toContain('.qa-region-table') }) it('shows a dated historical roster, conflicts and no invented performance verdict', () => { const html = renderToStaticMarkup() expect(html).toContain('历史快照') expect(html).toContain('跨区域待核对:1') expect(html).not.toContain('配置区域归属不等于纳入绩效考核') expect(html).not.toContain('') expect(html).not.toContain('达标率') expect(html).toMatch(/disabled=""[^>]*>保存区域设置/) }) it('keeps optional preview visible and lets save invoke the same preview automatically', () => { const html = renderToStaticMarkup() expect(html).toContain('预览结果(未保存)') expect(html).not.toMatch(/disabled=""[^>]*>保存区域设置/) }) })