Files
NianAIGC/tests/assets-http-contract.test.ts
2026-09-30 13:22:53 +08:00

29 lines
1.7 KiB
TypeScript

import { readFile } from "node:fs/promises";
import { describe, expect, it } from "vitest";
describe("assets HTTP v1 compatibility contract", () => {
it("freezes route, limit, safe-error, and binary-cache semantics", async () => {
const contract = JSON.parse(await readFile(new URL("../contracts/assets/http-v1.json", import.meta.url), "utf8"));
expect(contract.version).toBe(1);
expect(contract.limits).toEqual({ jsonBytes: 1_048_576, uploadBytes: 20_971_520, remoteBytes: 20_971_520 });
expect(contract.routes.map((route: { path: string }) => route.path)).toEqual([
"/api/assets", "/api/assets/upload", "/api/assets/import-image", "/api/assets/{id}", "/api/assets/{id}/download",
"/api/v1/assets", "/api/v1/assets/{id}", "/api/v1/assets/{id}/download",
"/uploads/{path...}", "/generated-results/{path...}"
]);
expect(contract.errors.internal.body).toEqual({ error: "Internal server error." });
expect(contract.errors.importImageUnsupported).toEqual({ status: 415, body: { error: "不支持的图片格式" } });
expect(contract.importImage).toEqual({
method: "POST", path: "/api/assets/import-image", auth: "platform-session", contentType: "application/json",
request: { url: "string" }, success: { status: 201, body: { asset: "Asset" } }, source: "upload",
formats: ["png", "jpeg", "webp", "gif", "bmp"], maxURLBytes: 8192, maxImageBytes: 20_971_520,
maxImagePixels: 25_000_000
});
expect(contract.methods).toEqual({ headExecutesGet: true, optionsStatus: 204, unsupportedStatus: 405, unsupportedBody: "empty" });
expect(contract.binary).toEqual({
downloadCacheControl: "private, no-store",
servedCacheControl: "public, max-age=31536000, immutable"
});
});
});