fix: update the logic code for combos pages

This commit is contained in:
2026-07-14 17:09:56 +07:00
parent d5bf9d2e2e
commit 76a82bcfc9
11 changed files with 615 additions and 51 deletions
+78
View File
@@ -0,0 +1,78 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const getCustomModels = vi.fn();
const addCustomModel = vi.fn();
const deleteCustomModel = vi.fn();
const requireAdminUser = vi.fn();
vi.mock("@/models", () => ({
getCustomModels,
addCustomModel,
deleteCustomModel,
}));
vi.mock("@/lib/auth/currentUser", () => ({ requireAdminUser }));
const { GET, POST, DELETE } = await import("../../src/app/api/models/custom/route.js");
describe("/api/models/custom", () => {
beforeEach(() => {
getCustomModels.mockReset();
addCustomModel.mockReset();
deleteCustomModel.mockReset();
requireAdminUser.mockReset();
});
it("keeps the shared catalog readable to authenticated model selectors", async () => {
getCustomModels.mockResolvedValue([{ providerAlias: "openai", id: "gpt-test", type: "llm" }]);
const response = await GET();
expect(response.status).toBe(200);
await expect(response.json()).resolves.toEqual({
models: [{ providerAlias: "openai", id: "gpt-test", type: "llm" }],
});
expect(requireAdminUser).not.toHaveBeenCalled();
});
it("rejects a non-admin adding a shared custom model", async () => {
requireAdminUser.mockRejectedValue(new Error("Forbidden"));
const response = await POST(new Request("http://localhost/api/models/custom", {
method: "POST",
body: JSON.stringify({ providerAlias: "openai", id: "gpt-test", type: "llm" }),
}));
expect(response.status).toBe(403);
expect(addCustomModel).not.toHaveBeenCalled();
});
it("allows an admin to add a shared custom model", async () => {
requireAdminUser.mockResolvedValue({ id: "admin", role: "admin" });
addCustomModel.mockResolvedValue(true);
const response = await POST(new Request("http://localhost/api/models/custom", {
method: "POST",
body: JSON.stringify({ providerAlias: "openai", id: "gpt-test", type: "llm" }),
}));
expect(response.status).toBe(200);
expect(addCustomModel).toHaveBeenCalledWith({
providerAlias: "openai",
id: "gpt-test",
type: "llm",
name: undefined,
});
});
it("rejects a non-admin deleting a shared custom model", async () => {
requireAdminUser.mockRejectedValue(new Error("Forbidden"));
const response = await DELETE(new Request(
"http://localhost/api/models/custom?providerAlias=openai&id=gpt-test&type=llm",
{ method: "DELETE" },
));
expect(response.status).toBe(403);
expect(deleteCustomModel).not.toHaveBeenCalled();
});
});