来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
89 lines
3.4 KiB
TypeScript
89 lines
3.4 KiB
TypeScript
import { readFileSync } from 'fs';
|
||
import path from 'path';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
/**
|
||
* 看板「需求将上涨 N%」告警:
|
||
* - 旧实现用 predicted_demand/1.25 反推百分比,代数上恒等于 25%(P 被约掉);
|
||
* - 服务端算了真实 trendRatio 却没有下发;
|
||
* - 现在改为真实环比(trend_ratio)+ 真实需求预测(demandForecastService)。
|
||
* 背景见 docs/design/20260914-dashboard-demand-forecast-alert.md
|
||
*/
|
||
|
||
const readSource = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
|
||
|
||
describe('服务端:看板预测字段', () => {
|
||
const src = readSource('src/services/dashboardService.ts');
|
||
|
||
it('接入真实需求预测服务(与「需求预测」Tab 同源)', () => {
|
||
expect(src).toContain("from './demandForecastService'");
|
||
expect(src).toContain('getDemandForecast({');
|
||
expect(src).toContain('forecastBySku');
|
||
});
|
||
|
||
it('下发真实环比 trend_ratio(前端不再自行反推)', () => {
|
||
expect(src).toContain('trend_ratio:');
|
||
});
|
||
|
||
it('下发真实预测字段:未来需求量/日均/可用量/缺货风险/置信度', () => {
|
||
for (const field of [
|
||
'suggested_qty:',
|
||
'avg_daily_qty:',
|
||
'available_qty:',
|
||
'stockout_risk:',
|
||
'forecast_confidence:',
|
||
'forecast_horizon_days:',
|
||
]) {
|
||
expect(src, field).toContain(field);
|
||
}
|
||
});
|
||
|
||
it('预测失败要降级而不拖垮看板', () => {
|
||
expect(src).toContain('demand forecast unavailable');
|
||
});
|
||
});
|
||
|
||
describe('前端:不再用常数反推百分比', () => {
|
||
const page = readSource('../src/pages/Dashboard.tsx');
|
||
|
||
it('删掉了 predicted_demand/1.25 的恒等公式与 Math.max(…,15) 兜底', () => {
|
||
expect(page).not.toContain('/ 1.25');
|
||
expect(page).not.toContain('Math.max(trendPercent, 15)');
|
||
expect(page).not.toContain('trendPercent');
|
||
});
|
||
|
||
it('直接显示服务端下发的真实环比', () => {
|
||
expect(page).toContain('trend_ratio');
|
||
expect(page).toContain("t('dashboard.alert.demandUp'");
|
||
});
|
||
|
||
it('缺货风险单独一条提示(真实预测用量与可用量)', () => {
|
||
expect(page).toContain("t('dashboard.alert.demandStockout'");
|
||
expect(page).toContain('stockout_risk');
|
||
expect(page).toContain('forecast_confidence');
|
||
});
|
||
});
|
||
|
||
describe('i18n:告警文案三语齐备', () => {
|
||
for (const loc of ['zh-CN', 'en', 'id']) {
|
||
it(`${loc} 有 demandUp / demandStockout 且带真实预测占位符`, () => {
|
||
const text = readFileSync(
|
||
path.join(__dirname, '..', '..', 'src', 'i18n', 'locales', loc, 'dashboard.ts'),
|
||
'utf8'
|
||
);
|
||
expect(text).toContain("'alert.demandStockout'");
|
||
const upLine = text.split('\n').find((l) => l.includes("'alert.demandUp'")) || '';
|
||
const stockLine = text.split('\n').find((l) => l.includes("'alert.demandStockout'")) || '';
|
||
const upBlock = text.slice(text.indexOf("'alert.demandUp'"), text.indexOf("'alert.demandUp'") + 260);
|
||
const stockBlock = text.slice(text.indexOf("'alert.demandStockout'"), text.indexOf("'alert.demandStockout'") + 260);
|
||
for (const holder of ['{name}', '{pct}', '{qty}']) {
|
||
expect(upBlock, `demandUp ${loc} ${holder}`).toContain(holder);
|
||
}
|
||
for (const holder of ['{name}', '{qty}', '{available}']) {
|
||
expect(stockBlock, `demandStockout ${loc} ${holder}`).toContain(holder);
|
||
}
|
||
expect(upLine || stockLine).toBeTruthy();
|
||
});
|
||
}
|
||
});
|