596 lines
23 KiB
JavaScript
596 lines
23 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { after, before, test } from "node:test";
|
|
import { PrismaClient } from "@prisma/client";
|
|
import { createApp } from "../dist/main.js";
|
|
import { signIdentity } from "../dist/auth.js";
|
|
|
|
let app;
|
|
const prisma = new PrismaClient();
|
|
const brandId = process.env.DEFAULT_BRAND_ID;
|
|
const openid = `integration-${Date.now()}`;
|
|
let customerToken;
|
|
const createdBookingIds = [];
|
|
|
|
before(async () => {
|
|
await prisma.customerUser.create({ data: { brandId, openid } });
|
|
customerToken = signIdentity({ subject: openid, brandId, role: "customer" });
|
|
app = await createApp();
|
|
await app.init();
|
|
});
|
|
|
|
after(async () => {
|
|
await app.close();
|
|
await prisma.booking.deleteMany({ where: { id: { in: createdBookingIds } } });
|
|
await prisma.customerUser.deleteMany({ where: { openid } });
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
async function request(url, options = {}) {
|
|
return app
|
|
.getHttpAdapter()
|
|
.getInstance()
|
|
.inject({ method: "GET", url, ...options });
|
|
}
|
|
|
|
test("health and public content are available without authentication", async () => {
|
|
const health = await request("/health");
|
|
assert.equal(health.statusCode, 200);
|
|
assert.deepEqual(JSON.parse(health.body), { status: "ok", service: "personal-brand-api" });
|
|
|
|
const brand = await request("/public/brand");
|
|
assert.equal(brand.statusCode, 200);
|
|
assert.equal(JSON.parse(brand.body).name, "阿成家常菜");
|
|
|
|
const services = await request("/public/services");
|
|
assert.equal(services.statusCode, 200);
|
|
assert.ok(JSON.parse(services.body).length >= 3);
|
|
});
|
|
|
|
test("admin can install a template and public workspace exposes enabled configuration", async () => {
|
|
await prisma.inquirySubmission.deleteMany({ where: { brandId } });
|
|
await prisma.inquiryForm.deleteMany({ where: { brandId } });
|
|
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
|
|
await prisma.brandModule.deleteMany({ where: { brandId } });
|
|
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
const token = JSON.parse(login.body).token;
|
|
const before = await request("/admin/workspace");
|
|
assert.equal(before.statusCode, 401);
|
|
const installed = await request("/admin/workspace/install", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { templateKey: "personal" },
|
|
});
|
|
assert.equal(installed.statusCode, 201);
|
|
assert.equal(JSON.parse(installed.body).template.key, "personal");
|
|
try {
|
|
const previewUnauthorized = await request("/admin/workspace/template/preview");
|
|
assert.equal(previewUnauthorized.statusCode, 401);
|
|
const preview = await request("/admin/workspace/template/preview", {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(preview.statusCode, 200);
|
|
assert.equal(JSON.parse(preview.body).canUpgrade, false);
|
|
const rollbackUnauthorized = await request("/admin/workspace/template/rollback", {
|
|
method: "POST",
|
|
});
|
|
assert.equal(rollbackUnauthorized.statusCode, 401);
|
|
const installedRecord = JSON.parse(installed.body).installation;
|
|
await prisma.brandTemplateInstallation.update({
|
|
where: { id: installedRecord.id },
|
|
data: {
|
|
templateVersion: "2.0.0",
|
|
previousSnapshots: [installedRecord.configSnapshot],
|
|
},
|
|
});
|
|
const rollback = await request("/admin/workspace/template/rollback", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(rollback.statusCode, 201);
|
|
assert.equal(JSON.parse(rollback.body).installation.templateVersion, "1.0.0");
|
|
assert.equal(JSON.parse(rollback.body).installation.previousSnapshots.length, 0);
|
|
assert.match(JSON.parse(rollback.body).note, /配置基线/);
|
|
const workspace = await request("/admin/workspace", {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(workspace.statusCode, 200);
|
|
assert.equal(JSON.parse(workspace.body).installations.length, 1);
|
|
assert.equal(JSON.parse(workspace.body).fields.length, 5);
|
|
const publicWorkspace = await request("/public/workspace");
|
|
assert.equal(publicWorkspace.statusCode, 200);
|
|
const workspaceBody = JSON.parse(publicWorkspace.body);
|
|
assert.equal(workspaceBody.contractVersion, "1.0");
|
|
assert.equal(workspaceBody.template.key, "personal");
|
|
assert.deepEqual(workspaceBody.capabilities, ["offerings", "inquiry"]);
|
|
assert.equal(workspaceBody.modules.length, 5);
|
|
assert.equal(workspaceBody.runtimeModules.length, 5);
|
|
assert.equal(workspaceBody.runtimeModules[0].schemaVersion, "1.0");
|
|
assert.equal(workspaceBody.inquiryForm, null);
|
|
const conflict = await request("/admin/workspace/install", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { templateKey: "food" },
|
|
});
|
|
assert.equal(conflict.statusCode, 400);
|
|
} finally {
|
|
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
|
|
await prisma.brandModule.deleteMany({ where: { brandId } });
|
|
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
|
|
}
|
|
});
|
|
|
|
test("admin can customize workspace modules and fields with brand isolation", async () => {
|
|
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
|
|
await prisma.brandModule.deleteMany({ where: { brandId } });
|
|
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
const token = JSON.parse(login.body).token;
|
|
const install = await request("/admin/workspace/install", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { templateKey: "food" },
|
|
});
|
|
assert.equal(install.statusCode, 201);
|
|
const workspace = await request("/admin/workspace", {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
const moduleId = JSON.parse(workspace.body).modules[0].id;
|
|
const invalidField = await request("/admin/workspace/fields", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { scope: "unknown", key: "Bad-Key", label: "非法字段", type: "text" },
|
|
});
|
|
assert.equal(invalidField.statusCode, 400);
|
|
const updatedModule = await request(`/admin/workspace/modules/${moduleId}`, {
|
|
method: "PATCH",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { title: "我的品牌介绍", isEnabled: false },
|
|
});
|
|
assert.equal(updatedModule.statusCode, 200);
|
|
const createdField = await request("/admin/workspace/fields", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: {
|
|
scope: "profile",
|
|
key: `region_${Date.now()}`,
|
|
label: "服务区域",
|
|
type: "text",
|
|
required: false,
|
|
},
|
|
});
|
|
assert.equal(createdField.statusCode, 201);
|
|
const fieldId = JSON.parse(createdField.body).id;
|
|
try {
|
|
const updatedField = await request(`/admin/workspace/fields/${fieldId}`, {
|
|
method: "PATCH",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { label: "主要服务区域", visibleWhen: { mode: "all", rules: [] } },
|
|
});
|
|
assert.equal(updatedField.statusCode, 200);
|
|
const deletedField = await request(`/admin/workspace/fields/${fieldId}`, {
|
|
method: "DELETE",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(deletedField.statusCode, 200);
|
|
} finally {
|
|
await prisma.brandFieldDefinition.deleteMany({ where: { brandId } });
|
|
await prisma.brandModule.deleteMany({ where: { brandId } });
|
|
await prisma.brandTemplateInstallation.deleteMany({ where: { brandId } });
|
|
}
|
|
});
|
|
|
|
test("admin can create and version an inquiry form", async () => {
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
const token = JSON.parse(login.body).token;
|
|
const created = await request("/admin/workspace/forms", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: {
|
|
name: "品牌合作咨询",
|
|
fieldsSnapshot: [{ key: "project", type: "textarea", label: "项目背景", required: true }],
|
|
},
|
|
});
|
|
assert.equal(created.statusCode, 201);
|
|
const id = JSON.parse(created.body).id;
|
|
try {
|
|
const updated = await request(`/admin/workspace/forms/${id}`, {
|
|
method: "PATCH",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { fieldsSnapshot: [{ key: "budget", type: "number", label: "预算" }] },
|
|
});
|
|
assert.equal(updated.statusCode, 200);
|
|
assert.equal(JSON.parse(updated.body).version, 2);
|
|
const publicForm = await request(`/public/inquiry-forms/${id}`);
|
|
assert.equal(publicForm.statusCode, 200);
|
|
const publicWorkspace = await request("/public/workspace");
|
|
assert.equal(publicWorkspace.statusCode, 200);
|
|
assert.equal(JSON.parse(publicWorkspace.body).inquiryForm.id, id);
|
|
const submission = await request("/customer/inquiry-submissions", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
payload: { formId: id, values: { project: "品牌升级" } },
|
|
});
|
|
assert.equal(submission.statusCode, 201);
|
|
assert.equal(JSON.parse(submission.body).formVersion, 2);
|
|
const adminSubmissionsWithoutToken = await request("/admin/workspace/submissions");
|
|
assert.equal(adminSubmissionsWithoutToken.statusCode, 401);
|
|
const adminSubmissions = await request("/admin/workspace/submissions", {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(adminSubmissions.statusCode, 200);
|
|
assert.ok(
|
|
JSON.parse(adminSubmissions.body).some((item) => item.id === JSON.parse(submission.body).id),
|
|
);
|
|
} finally {
|
|
await prisma.inquirySubmission.deleteMany({ where: { inquiryFormId: id } });
|
|
await prisma.inquiryForm.delete({ where: { id } });
|
|
}
|
|
});
|
|
|
|
test("admin can manage offerings and public endpoint hides disabled offerings", async () => {
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
const token = JSON.parse(login.body).token;
|
|
const created = await request("/admin/workspace/offerings", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: {
|
|
kind: "product",
|
|
name: `测试提供项-${Date.now()}`,
|
|
summary: "测试摘要",
|
|
isEnabled: true,
|
|
},
|
|
});
|
|
assert.equal(created.statusCode, 201);
|
|
const id = JSON.parse(created.body).id;
|
|
try {
|
|
const listed = await request("/admin/workspace/offerings", {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(listed.statusCode, 200);
|
|
assert.ok(JSON.parse(listed.body).some((item) => item.id === id));
|
|
const publicItem = await request(`/public/offerings/${id}`);
|
|
assert.equal(publicItem.statusCode, 200);
|
|
const publicList = await request("/public/offerings");
|
|
assert.equal(publicList.statusCode, 200);
|
|
assert.ok(JSON.parse(publicList.body).some((item) => item.id === id));
|
|
const updated = await request(`/admin/workspace/offerings/${id}`, {
|
|
method: "PATCH",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { isEnabled: false },
|
|
});
|
|
assert.equal(updated.statusCode, 200);
|
|
const hidden = await request(`/public/offerings/${id}`);
|
|
assert.equal(hidden.statusCode, 400);
|
|
} finally {
|
|
await prisma.offering.delete({ where: { id } });
|
|
}
|
|
});
|
|
|
|
test("admin booking endpoints reject missing or customer credentials", async () => {
|
|
const missing = await request("/admin/bookings");
|
|
assert.equal(missing.statusCode, 401);
|
|
|
|
const customer = await request("/admin/bookings", {
|
|
headers: { authorization: "Bearer invalid-token" },
|
|
});
|
|
assert.equal(customer.statusCode, 401);
|
|
});
|
|
|
|
test("seeded admin can log in and read protected booking resources", async () => {
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
assert.equal(login.statusCode, 201);
|
|
const token = JSON.parse(login.body).token;
|
|
assert.ok(token);
|
|
|
|
const count = await request("/admin/bookings/unread-count", {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(count.statusCode, 200);
|
|
assert.equal(typeof JSON.parse(count.body).count, "number");
|
|
|
|
const tags = await request("/admin/bookings/tags", {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(tags.statusCode, 200);
|
|
assert.equal(JSON.parse(tags.body).length, 6);
|
|
});
|
|
|
|
test("admin login validates credentials", async () => {
|
|
const response = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: "wrong-password" },
|
|
});
|
|
assert.equal(response.statusCode, 401);
|
|
});
|
|
|
|
test("admin can read and update brand and service configuration", async () => {
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
const adminToken = JSON.parse(login.body).token;
|
|
assert.ok(adminToken);
|
|
|
|
const unauthorized = await request("/admin/config/brand");
|
|
assert.equal(unauthorized.statusCode, 401);
|
|
|
|
const brandBefore = await prisma.brandProfile.findUnique({ where: { id: brandId } });
|
|
assert.ok(brandBefore);
|
|
const serviceBefore = await prisma.service.findFirst({ where: { brandId, isEnabled: true } });
|
|
assert.ok(serviceBefore);
|
|
let createdServiceId;
|
|
|
|
try {
|
|
const brand = await request("/admin/config/brand", {
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
});
|
|
assert.equal(brand.statusCode, 200);
|
|
assert.equal(JSON.parse(brand.body).id, brandId);
|
|
|
|
const updatedBrand = await request("/admin/config/brand", {
|
|
method: "PATCH",
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
payload: {
|
|
description: "集成测试品牌简介",
|
|
contactPhone: "13800000000",
|
|
templateKey: "personal",
|
|
},
|
|
});
|
|
assert.equal(updatedBrand.statusCode, 200);
|
|
assert.equal(JSON.parse(updatedBrand.body).description, "集成测试品牌简介");
|
|
assert.equal(JSON.parse(updatedBrand.body).templateKey, "personal");
|
|
|
|
const updatedService = await request(`/admin/config/services/${serviceBefore.id}`, {
|
|
method: "PATCH",
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
payload: { priceText: "集成测试价格", sortOrder: 99 },
|
|
});
|
|
assert.equal(updatedService.statusCode, 200);
|
|
assert.equal(JSON.parse(updatedService.body).priceText, "集成测试价格");
|
|
assert.equal(JSON.parse(updatedService.body).sortOrder, 99);
|
|
|
|
const listedServices = await request("/admin/config/services", {
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
});
|
|
assert.equal(listedServices.statusCode, 200);
|
|
assert.ok(JSON.parse(listedServices.body).some((item) => item.id === serviceBefore.id));
|
|
|
|
const createdService = await request("/admin/config/services", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
payload: {
|
|
name: "集成测试通用服务",
|
|
description: "通用品牌服务",
|
|
categoryKey: "consulting",
|
|
config: { duration: 60 },
|
|
},
|
|
});
|
|
assert.equal(createdService.statusCode, 201);
|
|
createdServiceId = JSON.parse(createdService.body).id;
|
|
assert.equal(JSON.parse(createdService.body).categoryKey, "consulting");
|
|
} finally {
|
|
await prisma.brandProfile.update({
|
|
where: { id: brandId },
|
|
data: {
|
|
description: brandBefore.description,
|
|
contactPhone: brandBefore.contactPhone,
|
|
templateKey: brandBefore.templateKey,
|
|
},
|
|
});
|
|
await prisma.service.update({
|
|
where: { id: serviceBefore.id },
|
|
data: { priceText: serviceBefore.priceText, sortOrder: serviceBefore.sortOrder },
|
|
});
|
|
if (createdServiceId) await prisma.service.delete({ where: { id: createdServiceId } });
|
|
}
|
|
});
|
|
|
|
test("admin can manage trust content", async () => {
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
const token = JSON.parse(login.body).token;
|
|
assert.equal((await request("/admin/config/trust-content")).statusCode, 401);
|
|
const created = await request("/admin/config/trust-content", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { type: "profile", title: "关于我", content: "集成测试内容", summary: "测试摘要" },
|
|
});
|
|
assert.equal(created.statusCode, 201);
|
|
const id = JSON.parse(created.body).id;
|
|
const draft = await request("/admin/config/trust-content", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { type: "experience", title: "草稿", content: "不应公开", isPublished: false },
|
|
});
|
|
assert.equal(draft.statusCode, 201);
|
|
const publicContent = await request("/public/trust-content");
|
|
assert.equal(publicContent.statusCode, 200);
|
|
const publicItems = JSON.parse(publicContent.body);
|
|
assert.ok(publicItems.some((item) => item.id === id));
|
|
assert.ok(!publicItems.some((item) => item.id === JSON.parse(draft.body).id));
|
|
try {
|
|
const list = await request("/admin/config/trust-content", {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(list.statusCode, 200);
|
|
const updated = await request(`/admin/config/trust-content/${id}`, {
|
|
method: "PATCH",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { content: "更新后的内容" },
|
|
});
|
|
assert.equal(updated.statusCode, 200);
|
|
assert.equal(JSON.parse(updated.body).content, "更新后的内容");
|
|
} finally {
|
|
const deleted = await request(`/admin/config/trust-content/${id}`, {
|
|
method: "DELETE",
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(deleted.statusCode, 200);
|
|
await prisma.trustContent.delete({ where: { id: JSON.parse(draft.body).id } });
|
|
}
|
|
});
|
|
|
|
test("customer booking endpoints require a valid customer session", async () => {
|
|
const missing = await request("/customer/bookings");
|
|
assert.equal(missing.statusCode, 400);
|
|
|
|
const list = await request("/customer/bookings", {
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
});
|
|
assert.equal(list.statusCode, 200);
|
|
assert.deepEqual(JSON.parse(list.body), []);
|
|
});
|
|
|
|
test("wechat login validates the request DTO before exchanging the code", async () => {
|
|
const response = await request("/customer/session/wechat", {
|
|
method: "POST",
|
|
payload: {},
|
|
});
|
|
assert.equal(response.statusCode, 400);
|
|
});
|
|
|
|
test("customer booking DTO rejects incomplete package requests", async () => {
|
|
const response = await request("/customer/bookings", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
payload: {
|
|
requestType: "package",
|
|
customerName: "集成测试客户",
|
|
contactPreference: "wechat",
|
|
},
|
|
});
|
|
assert.equal(response.statusCode, 400);
|
|
assert.equal(JSON.parse(response.body).error.code, "HTTP_400");
|
|
});
|
|
|
|
test("customer can create a generic inquiry without food-specific fields", async () => {
|
|
const response = await request("/customer/bookings", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
payload: {
|
|
requestType: "inquiry",
|
|
customerName: "个人品牌咨询客户",
|
|
wechatId: "inquiry-wechat",
|
|
contactPreference: "wechat",
|
|
requestedTime: "下周三 14:00",
|
|
note: "希望讨论品牌合作方案",
|
|
},
|
|
});
|
|
assert.equal(response.statusCode, 201);
|
|
const body = JSON.parse(response.body);
|
|
const booking = await prisma.booking.findUnique({ where: { bookingNo: body.bookingNo } });
|
|
assert.ok(booking);
|
|
assert.equal(booking.requestType, "inquiry");
|
|
createdBookingIds.push(booking.id);
|
|
});
|
|
|
|
test("customer can create and cancel a package booking", async () => {
|
|
const service = await prisma.service.findFirst({
|
|
where: { brandId, isEnabled: true, priceAmount: { not: null } },
|
|
});
|
|
assert.ok(service);
|
|
const created = await request("/customer/bookings", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
payload: {
|
|
requestType: "package",
|
|
customerName: "集成测试客户",
|
|
wechatId: "integration-wechat",
|
|
contactPreference: "wechat",
|
|
items: [{ serviceId: service.id, quantity: 2 }],
|
|
},
|
|
});
|
|
assert.equal(created.statusCode, 201);
|
|
const createdBody = JSON.parse(created.body);
|
|
assert.equal(createdBody.estimatedAmount, service.priceAmount * 2);
|
|
const booking = await prisma.booking.findUnique({ where: { bookingNo: createdBody.bookingNo } });
|
|
assert.ok(booking);
|
|
assert.equal(booking.workflowKey, "booking.basic");
|
|
assert.equal(booking.currentState, "received");
|
|
assert.equal(booking.offeringId, service.offeringId);
|
|
const initialTransitions = await prisma.bookingStateTransition.findMany({
|
|
where: { bookingId: booking.id },
|
|
});
|
|
assert.equal(initialTransitions.length, 1);
|
|
assert.equal(initialTransitions[0].toState, "received");
|
|
createdBookingIds.push(booking.id);
|
|
|
|
const cancelled = await request(`/customer/bookings/${booking.id}/cancel`, {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
});
|
|
assert.equal(cancelled.statusCode, 201);
|
|
assert.equal(JSON.parse(cancelled.body).status, "cancelled");
|
|
const transitions = await prisma.bookingStateTransition.findMany({
|
|
where: { bookingId: booking.id },
|
|
orderBy: { createdAt: "asc" },
|
|
});
|
|
assert.deepEqual(
|
|
transitions.map((item) => item.toState),
|
|
["received", "cancelled"],
|
|
);
|
|
});
|
|
|
|
test("admin can advance booking status but cannot skip the state machine", async () => {
|
|
const service = await prisma.service.findFirst({
|
|
where: { brandId, isEnabled: true, priceAmount: { not: null } },
|
|
});
|
|
assert.ok(service);
|
|
const created = await request("/customer/bookings", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
payload: {
|
|
requestType: "package",
|
|
customerName: "状态测试客户",
|
|
phone: "13800000000",
|
|
contactPreference: "phone",
|
|
items: [{ serviceId: service.id, quantity: 1 }],
|
|
},
|
|
});
|
|
const booking = await prisma.booking.findUnique({
|
|
where: { bookingNo: JSON.parse(created.body).bookingNo },
|
|
});
|
|
assert.ok(booking);
|
|
createdBookingIds.push(booking.id);
|
|
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
const adminToken = JSON.parse(login.body).token;
|
|
const contacting = await request(`/admin/bookings/${booking.id}/status`, {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
payload: { status: "contacting", adminNote: "已联系客户" },
|
|
});
|
|
assert.equal(contacting.statusCode, 201);
|
|
assert.equal(JSON.parse(contacting.body).status, "contacting");
|
|
const contactingState = await prisma.booking.findUnique({ where: { id: booking.id } });
|
|
assert.equal(contactingState?.currentState, "contacting");
|
|
|
|
const skipped = await request(`/admin/bookings/${booking.id}/status`, {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
payload: { status: "completed" },
|
|
});
|
|
assert.equal(skipped.statusCode, 400);
|
|
});
|