chore: 项目基线提交 - 拼豆图纸生成器 V1.0 核心代码
- 前端: React+Vite 首页/AI专区, 算法库(MARD色板/量化/背景去除), API对接 - 后端: Express+SQLite, 兑换码/订单/支付/chibi生成, 风控+云端同步 - CI/CD: GitHub Actions + Docker + nginx 配置 - 文档: PRD/DESIGN/LAUNCH/ROADMAP/E2E/竞品对照等 15 份 - 工程: 任务看板(taskboard) + 健康巡检(agent-health) + Git流程(gitflow)
This commit is contained in:
commit
02762400fb
174 files changed
+28059
No files matched your search
@@ -0,0 +1,140 @@
|
||||
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("请输入兑换码");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user