JewelryMall/server/tests/stockAlert.test.ts
2026-03-19 19:33:13 +08:00

45 lines
1.5 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import * as fc from 'fast-check'
import { filterStockAlerts } from '../src/controllers/stockAlert'
// Generate a product with arbitrary stock values
const productArb = fc.record({
id: fc.integer({ min: 1, max: 10000 }),
name: fc.string({ minLength: 1, maxLength: 20 }),
style_no: fc.string({ minLength: 0, maxLength: 10 }),
stock: fc.integer({ min: 0, max: 10000 }),
})
// Feature: jewelry-mall, Property 7: 库存预警阈值判断
// **Validates: Requirements 8.4**
describe('Property 7: 库存预警阈值判断', () => {
it('预警列表应恰好包含所有库存<20的商品且按库存从少到多排序', () => {
fc.assert(
fc.property(
fc.array(productArb, { minLength: 0, maxLength: 20 }),
(products) => {
const alerts = filterStockAlerts(products)
// Every alert should satisfy the threshold condition (stock < 20)
for (const a of alerts) {
expect(a.stock).toBeLessThan(20)
}
// Every product meeting the condition should be in alerts
const expectedIds = products
.filter((p) => p.stock < 20)
.map((p) => p.id)
const alertIds = alerts.map((a) => a.id)
expect(alertIds.sort()).toEqual(expectedIds.sort())
// Alerts should be sorted by stock ascending
for (let i = 1; i < alerts.length; i++) {
expect(alerts[i].stock).toBeGreaterThanOrEqual(alerts[i - 1].stock)
}
}
),
{ numRuns: 100 }
)
})
})