import { it, expect } from 'vitest' import { renderToStaticMarkup } from 'react-dom/server' import { DepartmentStatisticsView, QARegionalStatistics } from './QADepartmentStatistics' import { parseDepartmentStatistics } from './qaDepartmentStatistics' import { departmentStatisticsFixture } from './qaDepartmentStatistics.fixture' it('offers all, natural-month and natural-week scopes and never switches the selected-department action to a second mapping page', () => { const html = renderToStaticMarkup() for (const label of ['按累计统计', '按自然月统计', '按自然周统计', '绑定选中部门']) expect(html).toContain(label) for (const label of ['按部门', '按区域', '按人']) expect(html).toContain('>' + label + '<') expect(html).toContain('aria-label="分析维度"') expect(html).not.toContain('设置选中部门的区域') expect(html).not.toContain('返回部门统计') }) it('renders six columns with total people separate from visitors and no fabricated zero ratio', () => { const data = parseDepartmentStatistics(departmentStatisticsFixture, { period: 'all' }).snapshot! const html = renderToStaticMarkup() const headers = [...html.matchAll(/]*>([^<]+)<\/th>/g)].map((m) => m[1]) expect(headers).toEqual(['部门 / 人员', '总人数', '访问人数', '访问比例', '访问次数', '最近提问时间']) const root = html.match(/]*data-department-id="root"[^>]*>([\s\S]*?)<\/tr>/)![1] const cells = [...root.matchAll(/]*>([\s\S]*?)<\/td>/g)].map((m) => m[1]) expect(cells).toHaveLength(6) expect(cells[1]).toBe('3') expect(cells[2]).toBe('2') expect(html).not.toContain('qa-department-roster') expect(html).toContain('66.7%') expect(html).toContain('50.0%') expect(html).not.toContain('覆盖服务') expect(html).toContain('data-ratio="unknown"') expect(html).not.toContain('NaN') }) it('shows direct people beneath each expanded department with a multi-department badge and readable department paths', () => { const data = parseDepartmentStatistics(departmentStatisticsFixture, { period: 'all' }).snapshot! const html = renderToStaticMarkup() expect(html.match(/data-person-key=/g)).toHaveLength(4) expect(html.match(/>Fixture AlphaFixture Beta兼属 2 部门]*data-person-key="[^"]+"[^>]*>([\s\S]*?)<\/tr>/g)] expect(people).toHaveLength(4) for (const person of people) { const cells = [...person[1].matchAll(/]*>([\s\S]*?)<\/td>/g)].map((m) => m[1]) expect(cells).toHaveLength(6) expect(cells.slice(1, 4)).toEqual(['—', '—', '—']) expect(person[1]).toContain('人员行不单独计算部门总人数') } }) it('hides direct people together with collapsed departments without changing branch metrics', () => { const data = parseDepartmentStatistics(departmentStatisticsFixture, { period: 'all' }).snapshot! const html = renderToStaticMarkup() expect(html).not.toContain('data-person-key=') expect(html).toContain('data-total-people="3"') }) it('defaults to first-level departments and keeps deeper leaves manually expandable without changing metrics', () => { const data = parseDepartmentStatistics(departmentStatisticsFixture, { period: 'all' }).snapshot! const child = data.rows.find((d) => d.id === 'child')! data.rows.push({ ...child, id: 'deep', parent_id: 'child', name: 'Deep Fixture' }) for (const p of data.people) p.department_ids = p.department_ids.map((id) => (id === 'child' ? 'deep' : id)) const initial = renderToStaticMarkup() expect(initial).toContain('data-department-id="root"') expect(initial).toContain('data-department-id="child"') expect(initial).not.toContain('data-department-id="deep"') expect(initial).not.toContain('data-parent-department="deep"') const parentOpened = renderToStaticMarkup() expect(parentOpened).toContain('展开部门 Deep Fixture') expect(parentOpened).not.toContain('data-parent-department="deep"') expect(initial).toContain('data-total-people="3"') expect(parentOpened).toContain('data-total-people="3"') const expanded = renderToStaticMarkup() expect(expanded).toContain('data-parent-department="deep"') }) it('keeps parent values unchanged when child departments are collapsed', () => { const data = parseDepartmentStatistics(departmentStatisticsFixture, { period: 'all' }).snapshot! const html = renderToStaticMarkup() expect(html).toContain('data-department-id="root"') expect(html).not.toContain('data-department-id="child"') expect(html).toContain('66.7%') expect(html).toContain('总人数') })