Reorganize the README checklist: fold the standalone Ex-command task into earlier tasks, give the outline panel its own bindings (g o / Ctrl+Shift+O), and add a dedicated debugging task with breakpoint, stepping, and fault-finding drills anchored to real lines in the training app. Annotate the training app sources and tests with Chinese practice hints (multi-cursor, macro, text-object, and breakpoint spots). Drop the redundant root tsconfig, since training/tsconfig.json already covers src and tests, and remove the stale LEADERBOARD.md left over from the deleted challenge mode.
55 lines
2.2 KiB
TypeScript
55 lines
2.2 KiB
TypeScript
// vitest 单测:任务11 的 f4 调试入口(选个用例跑,就能撞上 src 里的断点)
|
||
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");
|
||
});
|
||
});
|