Files
knowledge-governance/web/src/qaRegionCommit.test.ts
T
姚章浩 154b0b62ad
build / build (push) Successful in 1m20s
feat(qaops): 完成区域统计与部门绑定六项联动
新增北京时间自然周校验与缓存隔离,默认展开到业务二级部门。
选中部门在统计页就地绑定;区域名称列表原位增删改,双版本锁、自动预览、独立读回及删除确认。
筛选和两张区域看板使用同一已保存目录,删除失效筛选回全部。

验证:完整 make verify、全 mirror 隔离 PostgreSQL race、自然周边界与缓存键负例通过;真实恢复库327部门逐行对拍及六项浏览器验收通过。
schema57无迁移,不改真实公司区域绑定或原始问答,不连接DobotAI生产源库;PNG导出本轮不冒充已验证。
2026-10-19 23:41:01 +08:00

116 lines
3.8 KiB
TypeScript

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()
}
})