118 lines
3.1 KiB
TypeScript
118 lines
3.1 KiB
TypeScript
import { beforeEach, expect, test, vi } from "vitest";
|
|
|
|
import { DELETE, PATCH } from "../../../app/api/libraries/[libraryType]/[resourceId]/route";
|
|
|
|
const { backendRequestMock } = vi.hoisted(() => ({
|
|
backendRequestMock: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("@/lib/http/backend-client", () => ({
|
|
backendRequest: backendRequestMock,
|
|
}));
|
|
|
|
beforeEach(() => {
|
|
backendRequestMock.mockReset();
|
|
});
|
|
|
|
test("proxies resource updates and adapts the updated item into the library view-model", async () => {
|
|
backendRequestMock.mockResolvedValue({
|
|
status: 200,
|
|
data: {
|
|
id: 12,
|
|
resource_type: "model",
|
|
name: "Ava Studio Updated",
|
|
description: "新的描述",
|
|
tags: ["女装", "更新"],
|
|
status: "active",
|
|
gender: "female",
|
|
age_group: "adult",
|
|
pose_id: null,
|
|
environment: null,
|
|
category: null,
|
|
files: [],
|
|
cover_url: "https://images.marcusd.me/library/models/ava/thumb.png",
|
|
original_url: "https://images.marcusd.me/library/models/ava/original.png",
|
|
created_at: "2026-03-28T10:00:00Z",
|
|
updated_at: "2026-03-28T11:00:00Z",
|
|
},
|
|
});
|
|
|
|
const response = await PATCH(
|
|
new Request("http://localhost/api/libraries/models/12", {
|
|
method: "PATCH",
|
|
headers: {
|
|
"content-type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
name: "Ava Studio Updated",
|
|
description: "新的描述",
|
|
tags: ["女装", "更新"],
|
|
}),
|
|
}),
|
|
{
|
|
params: Promise.resolve({ libraryType: "models", resourceId: "12" }),
|
|
},
|
|
);
|
|
const payload = await response.json();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(payload).toEqual({
|
|
mode: "proxy",
|
|
data: {
|
|
item: {
|
|
id: "12",
|
|
libraryType: "models",
|
|
name: "Ava Studio Updated",
|
|
description: "新的描述",
|
|
previewUri: "https://images.marcusd.me/library/models/ava/thumb.png",
|
|
originalUri: "https://images.marcusd.me/library/models/ava/original.png",
|
|
tags: ["女装", "更新"],
|
|
files: [],
|
|
isMock: false,
|
|
backendId: 12,
|
|
poseId: null,
|
|
},
|
|
},
|
|
message: "资源更新成功。",
|
|
});
|
|
expect(backendRequestMock).toHaveBeenCalledWith("/library/resources/12", {
|
|
method: "PATCH",
|
|
body: JSON.stringify({
|
|
name: "Ava Studio Updated",
|
|
description: "新的描述",
|
|
tags: ["女装", "更新"],
|
|
}),
|
|
});
|
|
});
|
|
|
|
test("soft deletes a library resource through the backend proxy", async () => {
|
|
backendRequestMock.mockResolvedValue({
|
|
status: 200,
|
|
data: {
|
|
id: 12,
|
|
},
|
|
});
|
|
|
|
const response = await DELETE(
|
|
new Request("http://localhost/api/libraries/models/12", {
|
|
method: "DELETE",
|
|
}),
|
|
{
|
|
params: Promise.resolve({ libraryType: "models", resourceId: "12" }),
|
|
},
|
|
);
|
|
const payload = await response.json();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(payload).toEqual({
|
|
mode: "proxy",
|
|
data: {
|
|
id: "12",
|
|
},
|
|
message: "资源已移入归档。",
|
|
});
|
|
expect(backendRequestMock).toHaveBeenCalledWith("/library/resources/12", {
|
|
method: "DELETE",
|
|
});
|
|
});
|