152 lines
5.3 KiB
JavaScript
152 lines
5.3 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { ProjectApi, ProjectApiError } from "../src/projects/api.ts";
|
|
import { clearAuthSession, getAuthSession, setAuthSession } from "../src/auth/auth.ts";
|
|
|
|
const user = {
|
|
userCode: "0123456789ABCDEF0123456789ABCDEF",
|
|
nickname: "测试用户",
|
|
email: "user@example.com",
|
|
};
|
|
|
|
function signedIn() {
|
|
setAuthSession({
|
|
token: "project-token",
|
|
tokenType: "Bearer",
|
|
expiresAt: Date.now() + 60_000,
|
|
user,
|
|
});
|
|
}
|
|
|
|
function ok(data = {}) {
|
|
return Response.json({ code: "OK", message: "success", data });
|
|
}
|
|
|
|
test("project write requests require a token before calling fetch", async () => {
|
|
clearAuthSession();
|
|
const api = new ProjectApi(async () => {
|
|
throw new Error("fetch must not be called");
|
|
});
|
|
await assert.rejects(api.listMine(), (error) => {
|
|
assert.ok(error instanceof ProjectApiError);
|
|
assert.equal(error.code, "UNAUTHORIZED");
|
|
assert.equal(error.status, 401);
|
|
return true;
|
|
});
|
|
});
|
|
|
|
test("project API sends bearer auth, no-store, JSON bodies and correct endpoints", async (t) => {
|
|
signedIn();
|
|
t.after(clearAuthSession);
|
|
const calls = [];
|
|
const api = new ProjectApi(async (url, options = {}) => {
|
|
calls.push({ url: String(url), options });
|
|
return ok({ items: [], page: 2, size: 12, total: 0 });
|
|
});
|
|
|
|
await api.listMine(2, 12);
|
|
await api.getMine("RP/2");
|
|
await api.create({ analysisCode: "DN-1", projectName: "机器人", description: "简介" });
|
|
await api.update("RP-2", { projectName: "新名称", description: "" });
|
|
await api.remove("RP-2");
|
|
await api.publish("RP-2");
|
|
await api.unpublish("RP-2");
|
|
|
|
assert.deepEqual(
|
|
calls.map(({ url, options }) => [url, options.method || "GET"]),
|
|
[
|
|
["/api/projects?page=2&size=12", "GET"],
|
|
["/api/projects/RP%2F2", "GET"],
|
|
["/api/projects", "POST"],
|
|
["/api/projects/RP-2", "PATCH"],
|
|
["/api/projects/RP-2", "DELETE"],
|
|
["/api/projects/RP-2/publish", "POST"],
|
|
["/api/projects/RP-2/unpublish", "POST"],
|
|
],
|
|
);
|
|
for (const { options } of calls) {
|
|
assert.equal(options.cache, "no-store");
|
|
assert.equal(options.credentials, "omit");
|
|
assert.equal(options.headers.get("Authorization"), "Bearer project-token");
|
|
assert.equal(options.headers.get("Accept"), "application/json");
|
|
}
|
|
assert.equal(calls[2].options.headers.get("Content-Type"), "application/json");
|
|
assert.deepEqual(JSON.parse(calls[2].options.body), {
|
|
analysisCode: "DN-1",
|
|
projectName: "机器人",
|
|
description: "简介",
|
|
});
|
|
assert.deepEqual(JSON.parse(calls[3].options.body), {
|
|
projectName: "新名称",
|
|
description: "",
|
|
});
|
|
});
|
|
|
|
test("cover upload uses multipart without a manual Content-Type header", async (t) => {
|
|
signedIn();
|
|
t.after(clearAuthSession);
|
|
let call;
|
|
const api = new ProjectApi(async (url, options = {}) => {
|
|
call = { url: String(url), options };
|
|
return ok({ projectCode: "RP-1" });
|
|
});
|
|
await api.uploadCover("RP-1", new Blob(["png"], { type: "image/png" }));
|
|
assert.equal(call.url, "/api/projects/RP-1/cover");
|
|
assert.equal(call.options.method, "POST");
|
|
assert.equal(call.options.headers.get("Authorization"), "Bearer project-token");
|
|
assert.equal(call.options.headers.get("Content-Type"), null);
|
|
assert.ok(call.options.body instanceof FormData);
|
|
const file = call.options.body.get("file");
|
|
assert.equal(file.type, "image/png");
|
|
assert.equal(file.name, "cover.png");
|
|
});
|
|
|
|
test("plaza GET requests stay anonymous and fork remains authenticated", async (t) => {
|
|
signedIn();
|
|
t.after(clearAuthSession);
|
|
const calls = [];
|
|
const api = new ProjectApi(async (url, options = {}) => {
|
|
calls.push({ url: String(url), options });
|
|
return ok({ items: [], page: 1, size: 20, total: 0 });
|
|
});
|
|
await api.listPublic(3, 5);
|
|
await api.getPublic("RP-public");
|
|
await api.fork("RP-public");
|
|
assert.equal(calls[0].url, "/api/plaza/projects?page=3&size=5");
|
|
assert.equal(calls[1].url, "/api/plaza/projects/RP-public");
|
|
assert.equal(calls[0].options.headers.get("Authorization"), null);
|
|
assert.equal(calls[1].options.headers.get("Authorization"), null);
|
|
assert.equal(calls[2].options.headers.get("Authorization"), "Bearer project-token");
|
|
assert.equal(calls[0].options.cache, "no-store");
|
|
assert.equal(calls[1].options.cache, "no-store");
|
|
});
|
|
|
|
test("project API preserves business error codes and request IDs", async (t) => {
|
|
signedIn();
|
|
t.after(clearAuthSession);
|
|
const api = new ProjectApi(async () =>
|
|
new Response(
|
|
JSON.stringify({ code: "PROJECT_COVER_REQUIRED", message: "cover required", data: null, requestId: "req-42" }),
|
|
{ status: 409, headers: { "Content-Type": "application/json" } },
|
|
),
|
|
);
|
|
await assert.rejects(api.publish("RP-1"), (error) => {
|
|
assert.ok(error instanceof ProjectApiError);
|
|
assert.equal(error.code, "PROJECT_COVER_REQUIRED");
|
|
assert.equal(error.status, 409);
|
|
assert.equal(error.requestId, "req-42");
|
|
assert.equal(error.message, "cover required");
|
|
return true;
|
|
});
|
|
});
|
|
|
|
test("401 project responses clear the stored session", async (t) => {
|
|
signedIn();
|
|
t.after(clearAuthSession);
|
|
const api = new ProjectApi(async () =>
|
|
Response.json({ code: "UNAUTHORIZED", data: null }, { status: 401 }),
|
|
);
|
|
await assert.rejects(api.getMine("RP-1"), ProjectApiError);
|
|
assert.equal(getAuthSession(), null);
|
|
});
|