Files
persionalBrand/apps/api/test/booking.service.test.ts
T
2026-09-19 18:46:26 +08:00

113 lines
3.5 KiB
TypeScript

import assert from "node:assert/strict";
import { test } from "node:test";
import { BadRequestException, NotFoundException } from "@nestjs/common";
import { BookingService } from "../src/booking.service.js";
const brandId = "brand-1";
const customerId = "customer-1";
const service = {
id: "service-1",
name: "套餐",
bookingEnabled: true,
priceAmount: 2800,
};
function createPrisma(overrides: Record<string, unknown> = {}): any {
return {
service: {
findMany: async () => [service],
},
booking: {
create: async ({ data }: any) => ({
bookingNo: "AC-test",
status: "received",
estimatedAmount: data.estimatedAmount,
createdAt: new Date(),
}),
findFirst: async () => null,
update: async ({ data }: any) => ({ status: data.status }),
findMany: async () => [],
count: async () => 0,
},
tag: { findMany: async () => [] },
bookingTag: { deleteMany: {}, createMany: {} },
$transaction: async () => undefined,
...overrides,
};
}
test("创建普通套餐订单时保存价格快照和预计金额", async () => {
const prisma = createPrisma();
const result = await new BookingService(prisma).create(brandId, customerId, {
requestType: "package",
customerName: "陈同学",
wechatId: "wx-test",
contactPreference: "wechat",
items: [{ serviceId: service.id, quantity: 2 }],
});
assert.equal(result.estimatedAmount, 5600);
assert.equal(result.status, "received");
});
test("普通套餐不能在没有明细时创建", async () => {
await assert.rejects(
() =>
new BookingService(createPrisma()).create(brandId, customerId, {
requestType: "package",
customerName: "陈同学",
phone: "13800000000",
contactPreference: "phone",
}),
(error: unknown) => error instanceof BadRequestException,
);
});
test("客户只能取消自己的 received 订单", async () => {
const prisma = createPrisma({
booking: {
findFirst: async () => ({ id: "booking-1", status: "confirmed" }),
update: async () => ({ status: "cancelled" }),
},
});
await assert.rejects(
() => new BookingService(prisma).cancelForCustomer(brandId, customerId, "booking-1"),
(error: unknown) => error instanceof BadRequestException,
);
});
test("取消不存在或不属于当前客户的订单返回不存在", async () => {
await assert.rejects(
() => new BookingService(createPrisma()).cancelForCustomer(brandId, customerId, "missing"),
(error: unknown) => error instanceof NotFoundException,
);
});
test("订单状态只能按定义的状态机流转", async () => {
const prisma = createPrisma({
booking: {
findFirst: async () => ({ id: "booking-1", status: "received" }),
update: async ({ data }: any) => data,
},
});
const bookings = new BookingService(prisma);
assert.equal(
(await bookings.updateStatus(brandId, "booking-1", "contacting")).status,
"contacting",
);
await assert.rejects(
() => bookings.updateStatus(brandId, "booking-1", "completed"),
(error: unknown) => error instanceof BadRequestException,
);
});
test("更新订单标签时拒绝其他品牌的标签", async () => {
const prisma = createPrisma({
booking: { findFirst: async () => ({ id: "booking-1", brandId }) },
tag: { findMany: async () => [] },
});
await assert.rejects(
() => new BookingService(prisma).updateTags(brandId, "booking-1", ["foreign-tag"]),
(error: unknown) => error instanceof BadRequestException,
);
});