- Shrimp.ts (875 行) 按类拆分到 Shrimp/(7 文件: Shrimp, ShrimpModule, ShrimpTableModule, ShrimpPower, ShrimpCrud, UserRoleModule, OperationLogModule) - forMongo/: MongoShrimpCrud + MongoShrimp(原 ShrimpServer 的 mongo 装配部分,无 express) - forExpress/: buildShrimpRouter + jwtAuth + types - main/: index(0 依赖核心) + expmon(express+mongo 整合,导出 ShrimpServer) + types(纯协议) - 入口子路径 server → expmon(express+mongo 整合方案命名更准确) - 删除冗余的 index.ts / core.ts(合并为 main/index.ts)
60 lines
2.1 KiB
TypeScript
60 lines
2.1 KiB
TypeScript
import { describe, it, expect } from "vitest"
|
|
import { ShrimpError, ShrimpCode } from "../src/main"
|
|
import type { ILabel } from "../src/main"
|
|
|
|
describe("ILabel", () => {
|
|
it("ShrimpError 接受字面量 string", () => {
|
|
const e = new ShrimpError("余额不足")
|
|
expect(e.tip).toBe("余额不足")
|
|
expect(e.message).toBe("余额不足")
|
|
expect(e.code).toBe(ShrimpCode.FAIL)
|
|
})
|
|
|
|
it("ShrimpError 接受 {t} 翻译键", () => {
|
|
const tip: ILabel = { t: "myapp.notEnough", params: { need: 100 } }
|
|
const e = new ShrimpError(tip)
|
|
expect(e.tip).toEqual(tip)
|
|
// message 用 i18n key 作为日志友好的字符串
|
|
expect(e.message).toBe("myapp.notEnough")
|
|
})
|
|
|
|
it("ShrimpError 接受 {tc} 复数键", () => {
|
|
const tip: ILabel = { tc: "myapp.items", count: 3 }
|
|
const e = new ShrimpError(tip)
|
|
expect(e.tip).toEqual(tip)
|
|
expect(e.message).toBe("myapp.items")
|
|
})
|
|
|
|
it("ShrimpError 接受 {d} 日期形态", () => {
|
|
const now = Date.now()
|
|
const tip: ILabel = { d: now, format: "short" }
|
|
const e = new ShrimpError(tip)
|
|
expect(e.message).toBe(String(now))
|
|
})
|
|
|
|
it("ShrimpError 接受 {n} 数字/货币形态", () => {
|
|
const tip: ILabel = { n: 99.5, format: "currency" }
|
|
const e = new ShrimpError(tip)
|
|
expect(e.message).toBe("99.5")
|
|
})
|
|
|
|
it("ShrimpError 接受内联多语言对象", () => {
|
|
const tip: ILabel = { zh: "提交", en: "Submit" }
|
|
const e = new ShrimpError(tip)
|
|
// 对内联多语言 extractILabelText 没有特殊处理,返回 fallback
|
|
expect(e.message).toBe("[ILabel]")
|
|
expect(e.tip).toEqual(tip)
|
|
})
|
|
|
|
it("ShrimpError 支持自定义 code", () => {
|
|
const e = new ShrimpError("权限不足", ShrimpCode.FORBIDDEN)
|
|
expect(e.code).toBe(ShrimpCode.FORBIDDEN)
|
|
})
|
|
|
|
it("ShrimpError 是 Error 的子类", () => {
|
|
const e = new ShrimpError("err")
|
|
expect(e).toBeInstanceOf(Error)
|
|
expect(e.name).toBe("ShrimpError")
|
|
})
|
|
})
|