test: add widget proxy tests (batch 4)

This commit is contained in:
shamoon
2026-02-03 13:54:54 -08:00
parent 02c1435b5e
commit 5a06c22e8e
11 changed files with 718 additions and 1 deletions

View File

@@ -0,0 +1,68 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import createMockRes from "test-utils/create-mock-res";
const { httpProxy, getServiceWidget, logger } = vi.hoisted(() => ({
httpProxy: vi.fn(),
getServiceWidget: vi.fn(),
logger: { debug: vi.fn(), error: vi.fn() },
}));
vi.mock("utils/logger", () => ({
default: () => logger,
}));
vi.mock("utils/config/service-helpers", () => ({
default: getServiceWidget,
}));
vi.mock("utils/proxy/http", () => ({
httpProxy,
}));
vi.mock("widgets/widgets", () => ({
default: {
suwayomi: {
api: "{url}/graphql",
},
},
}));
import suwayomiProxyHandler from "./proxy";
describe("widgets/suwayomi/proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("returns extracted counts from GraphQL response (no category)", async () => {
getServiceWidget.mockResolvedValue({ type: "suwayomi", url: "http://su", fields: ["download", "unread"] });
httpProxy.mockResolvedValueOnce([
200,
"application/json",
Buffer.from(JSON.stringify({ data: { download: { totalCount: 2 }, unread: { totalCount: 5 } } })),
]);
const req = { query: { group: "g", service: "svc", endpoint: "graphql", index: "0" } };
const res = createMockRes();
await suwayomiProxyHandler(req, res);
expect(res.statusCode).toBe(200);
expect(res.body).toEqual([
{ count: 2, label: "suwayomi.download" },
{ count: 5, label: "suwayomi.unread" },
]);
});
it("returns 401 when credentials are invalid", async () => {
getServiceWidget.mockResolvedValue({ type: "suwayomi", url: "http://su", username: "u", password: "p" });
httpProxy.mockResolvedValueOnce([401, "application/json", Buffer.from("{}")]);
const req = { query: { group: "g", service: "svc", endpoint: "graphql", index: "0" } };
const res = createMockRes();
await suwayomiProxyHandler(req, res);
expect(res.statusCode).toBe(401);
expect(res.body.error.message).toContain("unauthorized");
});
});