import assert from "node:assert/strict"; import { test } from "node:test"; import { BadRequestException, NotFoundException } from "@nestjs/common"; import { BookingService } from "../src/booking.service.js"; import type { PrismaService } from "../src/prisma.service.js"; import type { BrandReleaseService } from "../src/brand-release.service.js"; const brandId = "brand-1"; const customerId = "customer-1"; const offering = { id: "service-1", name: "套餐", priceAmount: 2800, }; function createPrisma(overrides: Record = {}): PrismaService { return { offering: { count: async () => 1, }, booking: { create: async ({ data }: { data: { estimatedAmount: number | null } }) => ({ bookingNo: "AC-test", status: "received", estimatedAmount: data.estimatedAmount, createdAt: new Date(), }), findFirst: async () => null, update: async ({ data }: { data: { status: string } }) => ({ status: data.status }), findMany: async () => [], count: async () => 0, }, tag: { findMany: async () => [] }, bookingTag: { deleteMany: {}, createMany: {} }, $transaction: async (callback: (tx: unknown) => unknown) => callback(undefined), ...overrides, } as unknown as PrismaService; } function createService(prisma = createPrisma()): BookingService { const releases = { getPublishedSnapshot: async () => ({ offerings: [{ ...offering, isEnabled: true }] }), publishedOfferingIds: () => new Set([offering.id]), } as unknown as BrandReleaseService; return new BookingService(prisma, releases); } test("创建普通套餐订单时保存价格快照和预计金额", async () => { const prisma = createPrisma(); const result = await createService(prisma).create(brandId, customerId, { requestType: "package", customerName: "陈同学", wechatId: "wx-test", contactPreference: "wechat", items: [{ offeringId: offering.id, quantity: 2 }], }); assert.equal(result.estimatedAmount, 5600); assert.equal(result.status, "received"); }); test("普通套餐不能在没有明细时创建", async () => { await assert.rejects( () => createService().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( () => createService(prisma).cancelForCustomer(brandId, customerId, "booking-1"), (error: unknown) => error instanceof BadRequestException, ); }); test("取消不存在或不属于当前客户的订单返回不存在", async () => { await assert.rejects( () => createService().cancelForCustomer(brandId, customerId, "missing"), (error: unknown) => error instanceof NotFoundException, ); }); test("订单状态只能按定义的状态机流转", async () => { const prisma = createPrisma({ booking: { findFirst: async () => ({ id: "booking-1", status: "received", currentState: "received", workflowKey: "booking.basic", workflowVersion: "1.0.0", }), update: async ({ data }: { data: { status: string } }) => data, }, $transaction: async (callback: (tx: unknown) => unknown) => callback({ booking: { update: async ({ data }: { data: { status: string } }) => data }, bookingStateTransition: { create: async () => undefined }, }), }); const bookings = createService(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( () => createService(prisma).updateTags(brandId, "booking-1", ["foreign-tag"]), (error: unknown) => error instanceof BadRequestException, ); });