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.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { PRODUCTS, findProduct } from "../src/catalog";
|
||||
|
||||
describe("catalog", () => {
|
||||
it("findProduct 返回存在的商品", () => {
|
||||
expect(findProduct("apple").name).toBe("Apple");
|
||||
expect(PRODUCTS.length).toBeGreaterThanOrEqual(5);
|
||||
});
|
||||
|
||||
it("findProduct 对未知商品抛错", () => {
|
||||
expect(() => findProduct("rock")).toThrow(/Unknown product/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
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");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { round2, lineTotal, shippingCost } from "../src/pricing";
|
||||
import { findProduct } from "../src/catalog";
|
||||
|
||||
describe("pricing", () => {
|
||||
it("round2 保留两位小数", () => {
|
||||
expect(round2(1.156)).toBe(1.16);
|
||||
expect(round2(2.5)).toBe(2.5);
|
||||
});
|
||||
|
||||
it("lineTotal 按数量计价(未税)", () => {
|
||||
expect(lineTotal(findProduct("apple"), 3)).toBeCloseTo(4.5);
|
||||
});
|
||||
|
||||
it("lineTotal 满 10 件打 95 折", () => {
|
||||
expect(lineTotal(findProduct("apple"), 10)).toBeCloseTo(14.25);
|
||||
});
|
||||
|
||||
it("shippingCost 满 50 免运费,否则收运费", () => {
|
||||
expect(shippingCost(60)).toBe(0);
|
||||
expect(shippingCost(49.99)).toBeCloseTo(4.99);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { Receipt } from "../src/receipt";
|
||||
import { createOrder } from "../src/orders";
|
||||
|
||||
describe("Receipt", () => {
|
||||
it("lines() 返回订单每一行的描述", () => {
|
||||
const order = createOrder([{ productId: "apple", quantity: 3 }]);
|
||||
const receipt = new Receipt(order);
|
||||
expect(receipt.lines()).toEqual(["Apple x3 $4.50"]);
|
||||
});
|
||||
|
||||
it("total() 等于订单 total", () => {
|
||||
const order = createOrder([{ productId: "bread", quantity: 1 }]);
|
||||
expect(new Receipt(order).total()).toBe(order.total);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user