import { describe, expect, it } from "vitest"; import type { AdminMetadataSourcesConfig } from "./adminAutomation"; import { metadataSourcesPayload, moveSource, normalizeMetadataSources, providerLabels, providerUiMessage, providerUiStateLabel, scheduleSummary } from "./adminAutomation"; const config: AdminMetadataSourcesConfig = { isbnPriorityEnabled: true, sources: [ { provider: "googlebooks", enabled: false, priority: 2, hasApiKey: true }, { provider: "local", enabled: false, priority: 99, hasApiKey: false }, { provider: "openlibrary", enabled: true, priority: 1, hasApiKey: false }, { provider: "bnf", enabled: false, priority: 3, hasApiKey: false }, { provider: "comicvine", enabled: true, priority: 4, hasApiKey: false, requiresCredentials: true }, { provider: "mangadex", enabled: false, priority: 5, hasApiKey: false } ] }; describe("admin automation helpers", () => { it("keeps the local metadata source active and first", () => { expect(normalizeMetadataSources(config).sources[0]).toMatchObject({ provider: "local", enabled: true, priority: 0 }); }); it("excludes the local source from the update payload", () => { expect(metadataSourcesPayload(normalizeMetadataSources(config))).toEqual({ isbnPriorityEnabled: true, sources: [ { provider: "openlibrary", enabled: true, priority: 1 }, { provider: "googlebooks", enabled: false, priority: 2 }, { provider: "bnf", enabled: false, priority: 3 }, { provider: "comicvine", enabled: true, priority: 4 }, { provider: "mangadex", enabled: false, priority: 5 } ] }); }); it("moves only external providers", () => { const moved = moveSource(normalizeMetadataSources(config).sources, "bnf", -1); expect(moved.map((source) => source.provider)).toEqual(["local", "openlibrary", "bnf", "googlebooks", "comicvine", "mangadex"]); }); it("summarizes weekly schedules", () => { expect(scheduleSummary({ frequency: "weekly", time: "04:30", dayOfWeek: 1 }, "Scan")).toBe("Scan chaque lundi a 04:30."); }); it("adds Comic Vine and MangaDex when the backend omits them", () => { const normalized = normalizeMetadataSources({ isbnPriorityEnabled: true, sources: [{ provider: "local", enabled: true, priority: 0, hasApiKey: false }] }); expect(normalized.sources.map((source) => source.provider)).toContain("comicvine"); expect(normalized.sources.map((source) => source.provider)).toContain("mangadex"); expect(providerLabels.comicvine).toBe("Comic Vine"); expect(providerLabels.mangadex).toBe("MangaDex"); }); it("labels provider configuration, rate limit and error states", () => { expect(providerUiStateLabel({ provider: "comicvine", enabled: true, priority: 1, hasApiKey: false, requiresCredentials: true })).toBe( "A configurer" ); expect(providerUiMessage({ provider: "comicvine", enabled: true, priority: 1, hasApiKey: false, requiresCredentials: true })).toBe( "Source activee, configuration incomplete." ); expect(providerUiStateLabel({ provider: "mangadex", enabled: true, priority: 2, hasApiKey: false, rateLimited: true })).toBe("Limite"); expect(providerUiMessage({ provider: "mangadex", enabled: true, priority: 2, hasApiKey: false, status: "quota_exceeded" })).toBe( "Quota ou limite temporaire atteint. ReadaBook reessaiera plus tard." ); expect(providerUiStateLabel({ provider: "mangadex", enabled: true, priority: 2, hasApiKey: false, lastError: "500 stack" })).toBe("Erreur"); }); });