feat(chibi): GET /api/chibi/history 近7天生成记录 + image_data 幂等迁移 + 4测试
This commit is contained in:
1 parent
125c20906c
commit
b33d5c466d
5 files changed
+171
-4
No files matched your search
@@ -138,3 +138,68 @@ describe("POST /api/chibi/generate 路由", () => {
|
||||
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([]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user