Files
STTracker/plugins/mangabaka/test/mangabaka.test.ts
T

98 lines
4.9 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { collect, createFixtureHttp, createTestContext } from "@sttracker/plugin-sdk/testing";
import type { Status } from "@sttracker/plugin-sdk/types";
import plugin from "../index.ts";
import { mapDeletion, mapState } from "../src/map.ts";
import page1 from "./fixtures/library-page1.json";
import page2 from "./fixtures/library-page2.json";
import changes from "./fixtures/changes.json";
import search from "./fixtures/search.json";
const B = "https://api.mangabaka.org";
const ctxFor = (routes: Record<string, unknown>) => {
const h = createFixtureHttp(routes);
return { h, ctx: createTestContext({ fetch: h.fetch, config: { token: "tok" } }) };
};
describe("mangabaka", () => {
test("manifest", () => {
expect(plugin.manifest.id).toBe("mangabaka");
expect(plugin.manifest.capabilities).toEqual(["pull", "pullChanges", "lookup"]);
});
test.each<[string, Status]>([
["considering", "planned"], ["plan_to_read", "planned"], ["reading", "in_progress"],
["rereading", "in_progress"], ["paused", "paused"], ["dropped", "dropped"], ["completed", "completed"],
])("state %s -> %s", (a, b) => expect(mapState(a)).toBe(b));
test("pull pages, maps entries and engagements, sends api key", async () => {
const { h, ctx } = ctxFor({
[`GET ${B}/v1/my/library?page=1`]: page1,
[`GET ${B}/v1/my/library?page=2`]: page2,
});
const recs = await collect(plugin.pull!(ctx));
expect(recs.map((r) => r.key)).toEqual(["series:1", "progress:1", "series:2", "series:3", "progress:3"]);
expect(h.calls.every((c) => c.headers.get("x-api-key") === "tok")).toBe(true);
const a = recs[0]!;
if (a.kind !== "entry") throw new Error();
expect(a.entry).toEqual({ status: "in_progress", rating: 80, notes: "good" });
expect(a.work.externalIds).toEqual({ mangabaka: "1", anilist_manga: "11", mal_manga: "22", mangaupdates: "33", kitsu_manga: "44" });
const p = recs[1]!;
if (p.kind !== "engagement") throw new Error();
expect(p.engagement.progress).toEqual({ chapter: 12, volume: 2 });
expect(p.engagement.startedAt).toEqual({ value: "2024-03-05", precision: "day" });
expect(p.engagement.finishedAt).toBeNull();
});
test("merged series sets mergedIntoKey", async () => {
const { ctx } = ctxFor({ [`GET ${B}/v1/my/library?page=1`]: page1, [`GET ${B}/v1/my/library?page=2`]: page2 });
const recs = await collect(plugin.pull!(ctx));
expect(recs.find((r) => r.key === "series:2")!.mergedIntoKey).toBe("series:9");
expect(recs.find((r) => r.key === "series:1")!.mergedIntoKey).toBeUndefined();
});
test("pullChanges yields upsert and deletion, saves cursor", async () => {
const { h, ctx } = ctxFor({
[`GET ${B}/v2/my/library/changes`]: (req: Request) =>
new URL(req.url).searchParams.get("cursor") === "cur-2" ? { data: [], next_cursor: "cur-2" } : changes,
});
await ctx.cursor.set("cur-1");
ctx.saved.cursor.length = 0;
const recs = await collect(plugin.pullChanges!(ctx));
expect(recs.filter((r) => r.deleted).map((r) => r.key)).toEqual(["series:7", "progress:7"]);
expect(recs[0]!.key).toBe("series:1");
expect(ctx.saved.cursor).toEqual(["cur-2", "cur-2"]);
expect(await ctx.cursor.get()).toBe("cur-2");
expect(new URL(h.calls[0]!.url).searchParams.get("cursor")).toBe("cur-1");
});
test("upsert without entry is skipped and logged, never deleted", async () => {
const logs: string[] = [];
const h = createFixtureHttp({
[`GET ${B}/v2/my/library/changes`]: (req: Request) =>
new URL(req.url).searchParams.has("cursor") ? { data: [], next_cursor: "c" } : { data: [{ type: "upsert", series_id: 4 }], next_cursor: "c" },
});
const ctx = createTestContext({ fetch: h.fetch, config: { token: "tok" }, log: (m) => logs.push(m) });
const recs = await collect(plugin.pullChanges!(ctx));
expect(recs).toEqual([]);
expect(logs).toEqual(["mangabaka: upsert without entry for series 4, skipped"]);
});
test("inherited keys are not states", () => expect(mapState("constructor")).toBe("none"));
test("delete keys are a subset of keys a full pull produced", async () => {
const { ctx } = ctxFor({ [`GET ${B}/v1/my/library?page=1`]: page1, [`GET ${B}/v1/my/library?page=2`]: page2 });
const pulled = (await collect(plugin.pull!(ctx))).map((r) => r.key);
const del = mapDeletion(1).map((r) => r.key);
expect(del).toEqual(["series:1", "progress:1"]);
for (const k of del) expect(pulled).toContain(k);
});
test("lookup searches series", async () => {
const { h, ctx } = ctxFor({ [`GET ${B}/v1/series/search`]: search });
const r = await plugin.lookup!(ctx, { title: "Delta", mediaType: "manga" });
expect(r).toEqual([{ title: "Delta", mediaType: "manga", year: 2018, externalIds: { mangabaka: "5", anilist_manga: "55" } }]);
expect(new URL(h.calls[0]!.url).searchParams.get("q")).toBe("Delta");
});
});