Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/__tests__/features/opds/useOpdsScreenOrchestration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import {
} from "@/features/opds/useOpdsScreenOrchestration";
import { savedCatalogsService } from "@/services/savedCatalogs";
import type { Publication } from "@/types/opds";
import { notifyOpdsError } from "@/utils/notifyOpdsError";

vi.mock("@/services/savedCatalogs", () => ({
savedCatalogsService: {
save: vi.fn(),
},
}));

vi.mock("@/utils/notifyOpdsError", () => ({
notifyOpdsError: vi.fn(),
}));

const mockNotify = vi.mocked(notifyOpdsError);

const makePublication = (id: string): Publication => ({
id,
title: "Dune",
Expand Down Expand Up @@ -112,12 +119,35 @@ describe("useOpdsScreenOrchestration", () => {
);
});

it("is best-effort: swallows a save failure without bumping the refresh key", async () => {
it("is best-effort: swallows a save failure without bumping the refresh key and surfaces it via notifyOpdsError", async () => {
vi.mocked(savedCatalogsService.save).mockRejectedValue(new Error("backend down"));
const { result } = render();

await expect(result.current.handleSaveCatalog()).resolves.toBeUndefined();
expect(result.current.savedCatalogsKey).toBe(0);
expect(mockNotify).toHaveBeenCalledTimes(1);
expect(mockNotify).toHaveBeenCalledWith(
expect.objectContaining({ message: "backend down" }),
{ context: "Save catalog", fallback: "Failed to save the catalog" },
);
});

it("does not toast when the save succeeds", async () => {
vi.mocked(savedCatalogsService.save).mockResolvedValue({
id: "cat-ok",
name: "https://example.com/opds",
url: "https://example.com/opds",
username: "alice",
added_at: "2026-08-28T00:00:00Z",
});
const { result } = render();

await act(async () => {
await result.current.handleSaveCatalog();
});

expect(result.current.savedCatalogsKey).toBe(1);
expect(mockNotify).not.toHaveBeenCalled();
});
});

Expand Down
9 changes: 7 additions & 2 deletions src/features/opds/useOpdsScreenOrchestration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback, useState } from "react";
import { savedCatalogsService } from "@/services/savedCatalogs";
import type { Publication } from "@/types/opds";
import { notifyOpdsError } from "@/utils/notifyOpdsError";
import type { UseCatalogConnectionResult } from "./useCatalogConnection";
import type { UseDownloadRegistryResult } from "./useDownloadRegistry";

Expand Down Expand Up @@ -36,8 +37,12 @@ export function useOpdsScreenOrchestration({
connection.username,
);
setSavedCatalogsKey((k) => k + 1);
} catch {
// Non-fatal: saving the catalog is best-effort.
} catch (error) {
// Non-fatal: saving the catalog is best-effort, but surface it.
notifyOpdsError(error, {
context: "Save catalog",
fallback: "Failed to save the catalog",
});
}
}, [connection]);

Expand Down
Loading