import assert from 'node:assert/strict' import test from 'node:test' import { resolveDeptStats } from './dept-stats.mjs' const snapshot = { now: new Date(2026, 2, 1, 1, 0), departments: [{ dept_id: 1, dept_name: '演示一组' }, { dept_id: 2, dept_name: '演示二组' }, { dept_id: 3, dept_name: '无预约组' }], assistants: [{ id: 101, dept_id: 1 }, { id: 102, dept_id: 2 }], // Deliberately different from the assistant: StatisticsLists groups by assistant, not doctor. doctors: [{ id: 401, dept_id: 2 }], patients: [], slots: [], appointments: [ ['2026-03-01', 'completed', 101], ['2026-03-01', 'registered', 101], ['2026-03-01', 'expired', 101], ['2026-03-01', 'canceled', 102], ['2026-02-23', 'completed', 101], ['2026-02-22', 'canceled', 102], ['2026-01-31', 'completed', 102], ['2026-01-30', 'registered', 101], ['2026-03-02', 'registered', 102], ['2026-03-01', 'completed', 999] ].map(([date, status, assistant_id], i) => ({ id: i + 1, doctor_id: 401, date, status, assistant_id })) } const path = '/adminapi/doctor.statistics/deptLists' const rows = (query = {}) => resolveDeptStats(path, query, snapshot).data.lists test('department options, query format, strict route allowlist', () => { assert.deepEqual(resolveDeptStats('/adminapi/dept.dept/all', {}, snapshot).data.map(d => [d.id, d.name]), [[1, '演示一组'], [2, '演示二组'], [3, '无预约组']]) assert.equal(resolveDeptStats('/adminapi/dept.dept/edit', {}, snapshot), null) const reply = resolveDeptStats(path, new URLSearchParams({ dept_id: '1' }), snapshot) assert.deepEqual([reply.code, reply.show, reply.msg, reply.data.count], [1, 0, '', 1]) assert.equal(reply.data.lists[0].dept_id, 1) }) test('today: joins via assistant, status counts reconcile, rates use total, no padded empty department', () => { assert.deepEqual(rows(), [ { dept_id: 1, dept_name: '演示一组', registered_count: 1, completed_count: 1, canceled_count: 0, expired_count: 1, total_count: 3, completion_rate: 33.33, time_range: '今天 (2026-03-01)', start_date: '2026-03-01', end_date: '2026-03-01' }, { dept_id: 2, dept_name: '演示二组', registered_count: 0, completed_count: 0, canceled_count: 1, expired_count: 0, total_count: 1, completion_rate: 0, time_range: '今天 (2026-03-01)', start_date: '2026-03-01', end_date: '2026-03-01' } ]) }) test('week/month include today and 6/29 previous calendar dates across a month boundary', () => { for (const [time_type, start, expected] of [['week', '2026-02-23', [4, 1]], ['month', '2026-01-31', [4, 3]]]) { const result = rows({ time_type }) assert.deepEqual(result.map(r => r.total_count), expected) assert.ok(result.every(r => r.start_date === start && r.end_date === '2026-03-01')) } }) test('custom dates and department narrow together; links reconcile to the matching appointments', () => { const [row] = rows({ time_type: 'custom', start_date: '2026-02-22', end_date: '2026-03-01', dept_id: '2' }) const members = snapshot.assistants.filter(a => a.dept_id === row.dept_id).map(a => a.id) const detail = snapshot.appointments.filter(a => members.includes(a.assistant_id) && a.date >= row.start_date && a.date <= row.end_date) assert.equal(row.time_range, '自定义 (2026-02-22 至 2026-03-01)') assert.equal(row.total_count, 2) assert.equal(detail.length, row.total_count) for (const status of ['registered', 'completed', 'canceled', 'expired']) { assert.equal(row[status + '_count'], detail.filter(r => r.status === status).length) } }) test('no matching dates, no matching appointments or unknown department returns an empty list', () => { assert.deepEqual(rows({ time_type: 'custom', start_date: '2000-01-01', end_date: '2000-12-31' }), []) assert.deepEqual(rows({ dept_id: '3' }), []) assert.deepEqual(rows({ dept_id: '999' }), []) assert.deepEqual(rows({ time_type: 'custom' }), rows()) })