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 }), total_stock: fc.integer({ min: 0, max: 10000 }), }) // Feature: jewelry-mall, Property 7: 库存预警阈值判断 // **Validates: Requirements 8.4** describe('Property 7: 库存预警阈值判断', () => { it('预警列表应恰好包含所有低库存商品且按库存从少到多排序', () => { fc.assert( fc.property( fc.array(productArb, { minLength: 0, maxLength: 20 }), (products) => { const alerts = filterStockAlerts(products) // Every alert should satisfy the threshold condition for (const a of alerts) { expect(a.total_stock).toBeGreaterThan(0) expect(a.stock / a.total_stock).toBeLessThan(0.1) } // Every product meeting the condition should be in alerts const expectedIds = products .filter((p) => p.total_stock > 0 && p.stock / p.total_stock < 0.1) .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 } ) }) })