import { readFileSync } from 'fs'; import path from 'path'; import { describe, expect, it } from 'vitest'; /** * 风险雷达必须每条都有服务端真实数据源: * - 删除查 notifications(timeout_alert) 的前端实现(该 notify_type 全仓无写入方,恒 0); * - 逾期未送达改用 orders 真实字段(配送日已过且未完成); * - 临期/过期只有在确实跟踪效期时才显示,避免把"没数据"显示成"没风险"。 * 背景见 docs/design/20260914-risk-radar-real-sources.md */ const readSource = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8'); describe('服务端:风险雷达真实字段', () => { const src = readSource('src/services/dashboardService.ts'); it('逾期未送达按 orders 真实字段统计(含未发车/配送中拆分)', () => { expect(src).toContain('AS overdue,'); expect(src).toContain('AS not_shipped,'); expect(src).toContain('AS in_transit'); expect(src).toContain('overdueOrders:'); expect(src).toContain('overdueNotShipped:'); expect(src).toContain('overdueInTransit:'); expect(src).toContain("o.delivery_date < (NOW() AT TIME ZONE 'Asia/Shanghai')::date"); expect(src).toContain('o.order_status < 90'); }); it('售后进行中按 after_sales.status IN (10,20) 统计', () => { expect(src).toContain('FROM after_sales a'); expect(src).toContain('COALESCE(a.status, 0) IN (10, 20)'); }); it('临期/过期只统计有可用量的行,并给出效期跟踪行数', () => { expect(src).toContain('AS expiring_soon,'); expect(src).toContain('AS expired'); expect(src).toContain('AS tracked,'); expect(src).toContain('expiryTrackedCount:'); expect(src).toContain('COALESCE(i.available_qty, 0) > 0'); }); it('不再依赖通知表的超时预警(注释里的历史说明不算)', () => { expect(src).not.toContain('FROM notifications'); expect(src).not.toContain("from('notifications')"); }); }); describe('前端:雷达条目都有真实来源', () => { const page = readSource('../src/pages/Dashboard.tsx'); it('删除了查通知表超时预警的前端实现(注释里保留历史说明不算)', () => { // 断言的是"查询行为"已移除:不再 from('notifications') / notify_type 过滤 / 该 state expect(page).not.toContain("from('notifications')"); expect(page).not.toContain('notify_type'); expect(page).not.toContain('loadTimeoutAlertCount'); expect(page).not.toContain('setTimeoutAlertCount'); expect(page).not.toMatch(/const \[timeoutAlertCount/); }); it('逾期/售后/临期/过期都读服务端字段', () => { for (const field of [ 'stats.overdueOrders', 'stats.overdueNotShipped', 'stats.overdueInTransit', 'stats.openAfterSalesCount', 'stats.expiredCount', 'stats.expiryTrackedCount', ]) { expect(page, field).toContain(field); } }); it('临期/过期条目受"是否跟踪效期"约束', () => { expect(page).toContain('expiryTracked > 0 && expiringSoon > 0'); expect(page).toContain('expiryTracked > 0 && expired > 0'); }); }); describe('i18n:雷达文案三语齐备', () => { for (const loc of ['zh-CN', 'en', 'id']) { it(`${loc} 有 overdue/afterSales/expired 且已无 timeout 文案`, () => { const text = readFileSync( path.join(__dirname, '..', '..', 'src', 'i18n', 'locales', loc, 'dashboard.ts'), 'utf8' ); expect(text).toContain("'alert.overdue'"); expect(text).toContain("'alert.afterSales'"); expect(text).toContain("'alert.expired'"); expect(text).not.toContain("'alert.timeout'"); const overdueBlock = text.slice(text.indexOf("'alert.overdue'"), text.indexOf("'alert.overdue'") + 300); for (const holder of ['{count}', '{notShipped}', '{inTransit}']) { expect(overdueBlock, `${loc} ${holder}`).toContain(holder); } }); } });