import { existsSync, readFileSync } from 'fs'; import path from 'path'; import { describe, expect, it } from 'vitest'; import { LOW_STOCK_ALERT_QTY_MAX, LOW_STOCK_ALERT_QTY_MIN, lowStockAlertSkuSql, lowStockTotalHavingSql, } from '../src/services/lowStockAlertSql'; /** * 库存预警口径(2026-09-14 改版):生鲜不做库存,安全线**按 SKU 各自设置** * (skus.low_stock_alert_qty,0~300,默认 0;0 = 不预警),按 SKU 汇总可用量取 (0, 该 SKU 阈值)。 * 背景与验收标准见 docs/design/20260914-sku-low-stock-alert-qty.md * * 替代上一版的 tests/lowStockAlertPerSku.test.ts(布尔勾选 + 分类默认 + 全局常量 10,已废弃)。 */ /** 读取文件:先在 server/ 下找,找不到再按仓库根找(前端文件在 ../src) */ const readSource = (rel: string) => { const inServer = path.join(__dirname, '..', rel); if (existsSync(inServer)) return readFileSync(inServer, 'utf8'); return readFileSync(path.join(__dirname, '..', '..', rel), 'utf8'); }; describe('lowStockAlertSkuSql', () => { it('只认「阈值 > 0」;0 / NULL 一律不参与', () => { const sql = lowStockAlertSkuSql('sk'); expect(sql).toBe('COALESCE(sk.low_stock_alert_qty, 0) > 0'); expect(sql).not.toContain('stock_mode'); }); it('别名可替换', () => { expect(lowStockAlertSkuSql('s')).toContain('s.low_stock_alert_qty'); }); }); describe('低库存阈值', () => { it('阈值来自该 SKU 自己的列,区间是开区间 (0, skus.low_stock_alert_qty)', () => { expect(lowStockTotalHavingSql('SUM(COALESCE(i.available_qty, 0))', 'sk')).toBe( 'SUM(COALESCE(i.available_qty, 0)) > 0 AND SUM(COALESCE(i.available_qty, 0)) < COALESCE(sk.low_stock_alert_qty, 0)' ); }); it('取值上下限为 0 ~ 300(与迁移 CHECK 约束一致)', () => { expect(LOW_STOCK_ALERT_QTY_MIN).toBe(0); expect(LOW_STOCK_ALERT_QTY_MAX).toBe(300); }); it('不再存在全局硬编码安全线常量', () => { const src = readSource('src/services/lowStockAlertSql.ts'); expect(src).not.toContain('LOW_STOCK_SAFE_QTY'); }); }); describe('两处调用防回退', () => { it('看板 KPI 走阈值口径 + 按 SKU 汇总(含阈值进 GROUP BY)', () => { const src = readSource('src/services/dashboardService.ts'); expect(src).toContain('lowStockAlertSkuSql('); expect(src).toContain('lowStockTotalHavingSql('); expect(src).toContain('GROUP BY i.sku_code, sk.low_stock_alert_qty'); expect(src).not.toContain('FROM inventory i WHERE available_qty <= 10'); }); it('库存页 low_stock 走同一阈值口径,不再引用全局常量', () => { const src = readSource('src/services/inventoryQueryService.ts'); expect(src).toContain("lowStockAlertSkuSql('sk')"); expect(src).toContain('COALESCE(sk.low_stock_alert_qty, 0)'); expect(src).not.toContain('LOW_STOCK_SAFE_QTY'); expect(src).not.toMatch(/available_qty, 0\) < 10/); }); it('不再按生鲜分类推断(旧口径已废)', () => { const src = readSource('src/services/dashboardService.ts') + readSource('src/services/inventoryQueryService.ts'); expect(src).not.toContain('is_fresh'); }); }); describe('迁移与商品管理入口', () => { const migration = readFileSync( path.join(__dirname, '..', '..', 'migrations', '20260914_sku_low_stock_alert_qty.sql'), 'utf8' ); it('迁移给 skus / product_catalog 加数值列并带 CHECK 0~300', () => { expect(migration).toContain( 'ADD COLUMN IF NOT EXISTS low_stock_alert_qty NUMERIC(10,2) NOT NULL DEFAULT 0' ); expect(migration).toContain('skus_low_stock_alert_qty_range'); expect(migration).toContain('low_stock_alert_qty >= 0 AND low_stock_alert_qty <= 300'); }); it('迁移把旧布尔 true 回填为 10(行为不变),并删除旧列', () => { expect(migration).toContain('SET low_stock_alert_qty = 10 WHERE COALESCE(low_stock_alert, false) = true'); expect(migration).toContain('DROP COLUMN low_stock_alert'); expect(migration).toContain('DROP COLUMN low_stock_alert_default'); }); it('同步触发器改用新列,且回填时临时禁用触发器(顺序不可颠倒)', () => { expect(migration).toMatch(/sync_product_catalog_to_legacy/); expect(migration).toContain('low_stock_alert_qty = EXCLUDED.low_stock_alert_qty'); expect(migration).toContain('DISABLE TRIGGER trg_sync_product_catalog_to_legacy'); expect(migration).toContain('ENABLE TRIGGER trg_sync_product_catalog_to_legacy'); // 加列 → 回填 → 重建函数 → 删列 const iAlter = migration.indexOf('ADD COLUMN IF NOT EXISTS low_stock_alert_qty'); const iDisable = migration.indexOf('DISABLE TRIGGER trg_sync_product_catalog_to_legacy'); const iFunc = migration.indexOf('CREATE OR REPLACE FUNCTION public.sync_product_catalog_to_legacy'); const iDrop = migration.indexOf('DROP COLUMN low_stock_alert'); expect(iAlter).toBeGreaterThan(-1); expect(iAlter).toBeLessThan(iDisable); expect(iDisable).toBeLessThan(iFunc); expect(iFunc).toBeLessThan(iDrop); }); it('商品管理页有可填数量的入口、批量按钮与保存链路', () => { const page = readSource('src/pages/products/SkuManagement.tsx'); expect(page).toContain('low_stock_alert_qty'); expect(page).toContain("t('products.form.lowStockAlertQty')"); expect(page).toContain('handleBatchSetLowStockAlertQty'); expect(page).toContain('handleInlineLowStockAlertQtySave'); expect(page).toContain('CATALOG_PRODUCT_LIST_COLUMNS'); }); it('批量设置按钮位于「生成税收编码」之前', () => { const page = readSource('src/pages/products/SkuManagement.tsx'); const iBatchAlert = page.indexOf("t('products.act.batchAlertQty')"); const iTaxCodes = page.indexOf("t('products.act.genTaxCodes')"); expect(iBatchAlert).toBeGreaterThan(-1); expect(iTaxCodes).toBeGreaterThan(-1); expect(iBatchAlert).toBeLessThan(iTaxCodes); }); it('分类默认值链路已彻底退役(前端不再读取该列)', () => { const categories = readSource('src/utils/categories.ts'); const page = readSource('src/pages/products/SkuManagement.tsx'); expect(categories).not.toContain('low_stock_alert_default'); expect(page).not.toContain('categoryLowStockAlertDefault'); }); });