206 lines
7.1 KiB
TypeScript
206 lines
7.1 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
||
import request from "supertest";
|
||
import type Database from "better-sqlite3";
|
||
import { buildPrompt, findTemplate } from "../chibi/prompts.js";
|
||
import { generateChibiImage, ChibiError, type GenerateRequest } from "../chibi/generate.js";
|
||
import { createApp } from "../server.js";
|
||
import { createTestDatabase } from "../db/index.js";
|
||
import { generateCodes, insertCodes, consumeCode, getCodeStatus } from "../codes/service.js";
|
||
|
||
/** 建一个含有效兑换码的测试库。 */
|
||
function setupDb(uses = 3): { db: Database.Database; code: string } {
|
||
const db = createTestDatabase();
|
||
const codes = generateCodes(1);
|
||
insertCodes(db, codes, "体验包", uses);
|
||
return { db, code: codes[0]! };
|
||
}
|
||
|
||
/** 构造合法请求体,overrides 可覆盖任意字段。 */
|
||
function baseRequest(code: string, overrides: Record<string, unknown> = {}): Record<string, unknown> {
|
||
return {
|
||
code,
|
||
image: "data:image/png;base64,iVBORw0KGgo=",
|
||
category: "pet",
|
||
subtype: "cat",
|
||
gridSize: 48,
|
||
colorCount: 16,
|
||
composition: "主体特写",
|
||
detailTags: ["眼睛更有神"],
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
function toGenerateRequest(body: Record<string, unknown>): GenerateRequest {
|
||
return body as unknown as GenerateRequest;
|
||
}
|
||
|
||
const PNG_MAGIC = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
||
|
||
describe("chibi 生成服务", () => {
|
||
it("buildPrompt 集成: 全部参数参与组装", () => {
|
||
const p = buildPrompt({
|
||
category: "food",
|
||
subtype: "dessert",
|
||
gridSize: 80,
|
||
colorCount: 32,
|
||
composition: "半幅构图",
|
||
detailTags: ["奶油清晰", "装饰丰富"],
|
||
customPrompt: "不要奶油",
|
||
hasReferenceImage: true,
|
||
});
|
||
expect(p).toContain("甜品蛋糕");
|
||
expect(p).toContain("80×80 网格");
|
||
expect(p).toContain("最多 32 色");
|
||
expect(p).toContain("主体半幅构图");
|
||
expect(p).toContain("奶油清晰");
|
||
expect(p).toContain("不要奶油");
|
||
expect(p).toContain("参考示例风格");
|
||
expect(findTemplate("food", "dessert").subtypeName).toBe("甜品蛋糕");
|
||
});
|
||
|
||
it("mock 模式返回 gridSize 尺寸的占位 PNG dataURL + 下载凭证", async () => {
|
||
const { db, code } = setupDb();
|
||
const result = await generateChibiImage(db, toGenerateRequest(baseRequest(code)));
|
||
|
||
// dataURL 且是合法 PNG
|
||
expect(result.image.startsWith("data:image/png;base64,")).toBe(true);
|
||
const buf = Buffer.from(result.image.split(",")[1]!, "base64");
|
||
expect(buf.subarray(0, 8)).toEqual(PNG_MAGIC);
|
||
// IHDR width/height 位于偏移 16/20,等于 gridSize
|
||
expect(buf.readUInt32BE(16)).toBe(48);
|
||
expect(buf.readUInt32BE(20)).toBe(48);
|
||
|
||
// 下载凭证非空
|
||
expect(typeof result.downloadGrant).toBe("string");
|
||
expect(result.downloadGrant.length).toBeGreaterThan(0);
|
||
|
||
// 成功消耗 1 次
|
||
expect(result.access.remainingUses).toBe(2);
|
||
expect(getCodeStatus(db, code).remainingUses).toBe(2);
|
||
});
|
||
|
||
it("次数不足时抛出 生成次数不足 (402)", async () => {
|
||
const { db, code } = setupDb(1);
|
||
consumeCode(db, code); // 用尽剩余次数
|
||
|
||
const err = await generateChibiImage(db, toGenerateRequest(baseRequest(code))).then(
|
||
() => null,
|
||
(e: unknown) => e,
|
||
);
|
||
expect(err).toBeInstanceOf(ChibiError);
|
||
expect((err as ChibiError).message).toContain("生成次数不足");
|
||
expect((err as ChibiError).status).toBe(402);
|
||
// 失败时不应再扣次数
|
||
expect(getCodeStatus(db, code).remainingUses).toBe(0);
|
||
});
|
||
});
|
||
|
||
describe("POST /api/chibi/generate 路由", () => {
|
||
it("200 返回 image / access / downloadGrant", async () => {
|
||
const { db, code } = setupDb();
|
||
const app = createApp(db);
|
||
const res = await request(app).post("/api/chibi/generate").send(baseRequest(code));
|
||
|
||
expect(res.status).toBe(200);
|
||
expect(res.body.image).toContain("data:image/png;base64,");
|
||
expect(res.body.access.remainingUses).toBe(2);
|
||
expect(typeof res.body.downloadGrant).toBe("string");
|
||
expect(res.body.downloadGrant.length).toBeGreaterThan(0);
|
||
});
|
||
|
||
it("次数不足返回 402 生成次数不足", async () => {
|
||
const { db, code } = setupDb(1);
|
||
consumeCode(db, code);
|
||
const app = createApp(db);
|
||
const res = await request(app).post("/api/chibi/generate").send(baseRequest(code));
|
||
|
||
expect(res.status).toBe(402);
|
||
expect(res.body.error).toBe("生成次数不足");
|
||
});
|
||
|
||
it("未知模板返回 400", async () => {
|
||
const { db, code } = setupDb();
|
||
const app = createApp(db);
|
||
const res = await request(app)
|
||
.post("/api/chibi/generate")
|
||
.send(baseRequest(code, { subtype: "不存在" }));
|
||
|
||
expect(res.status).toBe(400);
|
||
expect(res.body.error).toBe("未知的模板类型");
|
||
});
|
||
|
||
it("缺少兑换码返回 400", async () => {
|
||
const { db } = setupDb();
|
||
const app = createApp(db);
|
||
const res = await request(app).post("/api/chibi/generate").send(baseRequest(""));
|
||
|
||
expect(res.status).toBe(400);
|
||
expect(res.body.error).toBe("请输入兑换码");
|
||
});
|
||
});
|
||
|
||
describe("GET /api/chibi/history 路由", () => {
|
||
it("缺少 code 返回 400", async () => {
|
||
const { db } = setupDb();
|
||
const app = createApp(db);
|
||
const res = await request(app).get("/api/chibi/history");
|
||
|
||
expect(res.status).toBe(400);
|
||
expect(res.body.error).toBe("请输入兑换码");
|
||
});
|
||
|
||
it("格式不正确返回 400", async () => {
|
||
const { db } = setupDb();
|
||
const app = createApp(db);
|
||
const res = await request(app).get("/api/chibi/history?code=SHORT");
|
||
|
||
expect(res.status).toBe(400);
|
||
expect(res.body.error).toBe("兑换码格式不正确");
|
||
});
|
||
|
||
it("无记录返回空 items", async () => {
|
||
const { db, code } = setupDb();
|
||
const app = createApp(db);
|
||
const res = await request(app).get(`/api/chibi/history?code=${code}`);
|
||
|
||
expect(res.status).toBe(200);
|
||
expect(res.body.items).toEqual([]);
|
||
});
|
||
|
||
it("有生成记录时按倒序返回 items(含 image)", async () => {
|
||
const { db, code } = setupDb(5);
|
||
const app = createApp(db);
|
||
|
||
// 触发两次生成,写入两条记录
|
||
await request(app).post("/api/chibi/generate").send(baseRequest(code));
|
||
await request(app).post("/api/chibi/generate").send(baseRequest(code));
|
||
|
||
const res = await request(app).get(`/api/chibi/history?code=${code}`);
|
||
|
||
expect(res.status).toBe(200);
|
||
expect(Array.isArray(res.body.items)).toBe(true);
|
||
expect(res.body.items.length).toBe(2);
|
||
|
||
// 最新一条在前
|
||
const [latest, earlier] = res.body.items;
|
||
expect(latest.createdAt >= earlier.createdAt).toBe(true);
|
||
|
||
// 字段映射:grid_size → gridSize,image_data → image
|
||
expect(latest).toMatchObject({
|
||
category: "pet",
|
||
subtype: "cat",
|
||
gridSize: 48,
|
||
colorCount: 16,
|
||
status: "completed",
|
||
});
|
||
expect(typeof latest.image).toBe("string");
|
||
expect(latest.image).toContain("data:image/png;base64,");
|
||
|
||
// code 不存在的兑换码无记录
|
||
const other = setupDb().code;
|
||
const empty = await request(app).get(`/api/chibi/history?code=${other}`);
|
||
expect(empty.status).toBe(200);
|
||
expect(empty.body.items).toEqual([]);
|
||
});
|
||
});
|