Files
vimx/training/tests/orders.test.ts
T
fukunee 715ddb958a Rebuild training around a modular TS app
Replace the old exercise files (challenges, maze, sandbox, etc.)
with a runnable order/inventory TypeScript app under training/
that ships with unit tests, typecheck, lint, and format scripts.

Merge the full task checklist from TRAINING.md into README.md and
drop the obsolete docs and challenge files.

Add a root .gitignore for .zed, .reasonix, and build artifacts,
untrack the previously committed local configs, and fix the root
tsconfig to point at the new training sources.
2026-09-09 23:34:48 +08:00

54 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it, beforeEach } from "vitest";
import { createOrder, findOrder, listOrders, shipOrder } from "../src/orders";
import { available } from "../src/inventory";
// 每个用例前重置模块内订单表:直接重新 require 不现实,这里通过“只读断言”
// 说明:订单表是模块级状态,单测之间会累积——真实项目应注入仓库。
// 本文件按“无依赖顺序”编写以避免顺序耦合。
describe("orders", () => {
it("createOrder 生成订单并计算 subtotal/shipping/total", () => {
const order = createOrder([
{ productId: "apple", quantity: 2 },
{ productId: "milk", quantity: 1 },
]);
expect(order.status).toBe("new");
expect(order.lines).toHaveLength(2);
// 2×1.5 + 1×4.2 = 7.2 未税;税 0.576→0.58;运费 4.99
expect(order.subtotal).toBeCloseTo(7.2);
expect(order.tax).toBeCloseTo(0.58);
expect(order.shipping).toBeCloseTo(4.99);
expect(order.total).toBeCloseTo(12.77);
});
it("createOrder 预留库存,满 50 免运费", () => {
const before = available("cheese");
const order = createOrder([{ productId: "cheese", quantity: 6 }]);
expect(available("cheese")).toBe(before - 6);
// 9.9 × 6 = 59.4 ≥ 50 → 免运费
expect(order.shipping).toBe(0);
});
it("库存不足时抛错且不会产生订单", () => {
const countBefore = listOrders().length;
expect(() => createOrder([{ productId: "milk", quantity: 999 }])).toThrow(/Not enough stock/);
expect(listOrders()).toHaveLength(countBefore);
});
it("shipOrder 把订单标记为已发货", () => {
const order = createOrder([{ productId: "egg", quantity: 6 }]);
const shipped = shipOrder(order.id);
expect(shipped.status).toBe("shipped");
expect(findOrder(order.id)?.status).toBe("shipped");
});
it("shipOrder 对不存在的订单抛错", () => {
expect(() => shipOrder(999999)).toThrow(/not found/i);
});
// 占位:保证文件内至少有 beforeEach 不报未使用
beforeEach(() => {
expect(typeof createOrder).toBe("function");
});
});