ionian/packages/server/tests/diff-router.integration.test.ts

104 lines
2.8 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { Hono } from "hono";
import { createDiffRouter } from "../src/diff/router.ts";
import type { DiffManager } from "../src/diff/diff.ts";
import type { AppEnv, AuthStore } from "../src/login.ts";
const adminUser: AuthStore["user"] = { username: "admin", permission: [] };
const createTestApp = (diffManager: DiffManager) => {
const app = new Hono<AppEnv>();
app.use("*", async (c, next) => {
const auth: AuthStore = {
user: adminUser,
refreshed: false,
authenticated: true,
};
c.set("auth", auth);
await next();
});
app.route("/diff", createDiffRouter(diffManager));
return app;
};
describe("Diff router integration", () => {
let app: ReturnType<typeof createTestApp>
let diffManager: DiffManager;
let getAddedMock: ReturnType<typeof vi.fn>;
let commitMock: ReturnType<typeof vi.fn>;
let commitAllMock: ReturnType<typeof vi.fn>;
beforeEach(() => {
getAddedMock = vi.fn(() => [
{
type: "comic",
value: [
{ path: "alpha.zip", type: "archive" },
],
},
]);
commitMock = vi.fn(async () => 101);
commitAllMock = vi.fn(async () => [201, 202]);
diffManager = {
getAdded: getAddedMock,
commit: commitMock,
commitAll: commitAllMock,
} as unknown as DiffManager;
app = createTestApp(diffManager);
});
it("GET /diff/list returns grouped pending items", async () => {
const response = await app.fetch(new Request("http://localhost/diff/list"));
expect(response.status).toBe(200);
const payload = await response.json();
expect(payload).toEqual([
{
type: "comic",
value: [
{ path: "alpha.zip", type: "archive" },
],
},
]);
expect(getAddedMock).toHaveBeenCalledTimes(1);
});
it("POST /diff/commit commits each queued item", async () => {
const request = new Request("http://localhost/diff/commit", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify([
{ type: "comic", path: "alpha.zip" },
]),
});
commitMock.mockResolvedValueOnce(555);
const response = await app.fetch(request);
expect(response.status).toBe(200);
const payload = await response.json();
expect(payload).toEqual({ ok: true, docs: [555] });
expect(commitMock).toHaveBeenCalledWith("comic", "alpha.zip");
});
it("POST /diff/commitall flushes all entries for the type", async () => {
const request = new Request("http://localhost/diff/commitall", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ type: "comic" }),
});
const response = await app.fetch(request);
expect(response.status).toBe(200);
const payload = await response.json();
expect(payload).toEqual({ ok: true });
expect(commitAllMock).toHaveBeenCalledWith("comic");
});
});