Files
knowledge-governance/web/src/qaMonthly.test.ts
T
2026-10-18 21:50:29 +08:00

50 lines
2.4 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest'
import { parseQAMonthly, fetchQAMonthly, beijingMonth } from './qaMonthly'
import { monthlyFixture } from './qaMonthly.fixture'
describe('personal natural-month contracts', () => {
it('accepts renamed custom regions only from the shared organization catalog', () => {
const custom = {
...monthlyFixture,
organization: { ...monthlyFixture.organization, region_names: ['国内教育'] },
rows: monthlyFixture.rows.map((r, i) => (i === 0 ? { ...r, region: '国内教育' } : r)),
}
expect(parseQAMonthly(custom).rows[0].region).toBe('国内教育')
expect(() =>
parseQAMonthly({ ...custom, organization: { ...custom.organization, region_names: ['别的区域'] } })
).toThrow()
})
it('sends personnel filters only in a same-origin no-store read body', async () => {
const fake = vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: monthlyFixture }), { status: 200 }))
vi.stubGlobal('fetch', fake)
try {
await fetchQAMonthly({ month: '2026-10', search: '示例敏感筛选' })
expect(fake.mock.calls[0][0]).toBe('/api/v1/qa-ops/usage/people')
expect(fake.mock.calls[0][1]).toMatchObject({ method: 'POST', credentials: 'same-origin', cache: 'no-store' })
expect(JSON.parse(fake.mock.calls[0][1].body).search).toBe('示例敏感筛选')
} finally {
vi.unstubAllGlobals()
}
})
it('selects the month in Beijing rather than the browser timezone', () => {
expect(beijingMonth(new Date('2026-09-30T16:00:00Z'))).toBe('2026-10')
})
it('keeps named and missing-name zero-use members with a separate unresolved bucket', () => {
expect(parseQAMonthly(monthlyFixture)).toMatchObject({ zero_people: 1, missing_names: 1, unresolved_questions: 1 })
})
it('rejects nonconserving totals, fabricated pagination and inconsistent month boundaries', () => {
for (const invalid of [
{ ...monthlyFixture, zero_people: 0 },
{ ...monthlyFixture, linked_questions: 3 },
{ ...monthlyFixture, rows: [monthlyFixture.rows[0]] },
{ ...monthlyFixture, partial: false },
{ ...monthlyFixture, source: '1.0' },
{ ...monthlyFixture, month: '2026-1' },
{ ...monthlyFixture, month_end: '2026-11-02T00:00:00+08:00' },
{ ...monthlyFixture, rows: [monthlyFixture.rows[0], monthlyFixture.rows[0]] },
])
expect(() => parseQAMonthly(invalid)).toThrow()
})
})