Files
siftlode/frontend/src/lib/downloads.test.ts
T
peter fdca12968b feat(downloads): one-click Share + downloadable-by-default links (E7 S3)
D2-A: the library row's Share button becomes a single action — quickShareLink()
ensures a public link (minting on demand), copies it to the clipboard, and toasts
with an "Open in new tab" action. A copy failure still toasts (warning) with that
action so the link is never a dead end. Advanced controls (password/expiry/revoke/
user-ACL) stay in the existing ShareDialog, reached via a new "Manage sharing" row
button.

D1 (forward-only): CreateLinkIn.allow_download defaults to true, so a newly minted
link is downloadable by default. The default lives in the API, not a migration —
the download_links column default stays false, so no existing link flips. The
ShareDialog create-default is flipped to match.

Tests: TestCreateLinkDefault (default true; explicit false honored); 4 vitest cases
for quickShareLink (mint-vs-reuse, copy ok/fail, create failure). HU/EN parity.
2026-08-29 06:36:07 +02:00

98 lines
3.7 KiB
TypeScript

// @vitest-environment jsdom
//
// One-click share (E7 S3 · D2-A): quickShareLink turns the row's Share button into a single action —
// ensure a public link (mint on demand, downloadable per D1), copy it, and toast with an "open in new
// tab" action. These pin the branch matrix: reuse-vs-mint, copy-ok-vs-fail, and the create-failure path.
import { describe, it, expect, beforeEach, vi } from "vitest";
import { QueryClient } from "@tanstack/react-query";
// vi.mock is hoisted above these consts, so the mock fns must live in a hoisted block too.
const { downloadLinks, createDownloadLink, copyText, notify } = vi.hoisted(() => ({
downloadLinks: vi.fn(),
createDownloadLink: vi.fn(),
copyText: vi.fn(),
notify: vi.fn(),
}));
vi.mock("./api", () => ({ api: { downloadLinks, createDownloadLink } }));
vi.mock("./clipboard", () => ({ copyText }));
vi.mock("./notifications", () => ({ notify }));
import { quickShareLink } from "./downloads";
const t = (k: string) => k;
const origin = window.location.origin;
type ToastArg = {
level: string;
message: string;
action?: { label: string; onClick: () => void };
};
const firstToast = () => notify.mock.calls[0]![0] as ToastArg;
function qc() {
return new QueryClient({ defaultOptions: { queries: { retry: false } } });
}
beforeEach(() => {
downloadLinks.mockReset();
createDownloadLink.mockReset();
copyText.mockReset().mockResolvedValue(true);
notify.mockReset();
});
describe("quickShareLink", () => {
it("mints a downloadable link on demand, copies it, and toasts with an open-in-new-tab action", async () => {
downloadLinks.mockResolvedValue([]); // none yet
createDownloadLink.mockResolvedValue({ id: 7, url: "/watch/tok7" });
const openSpy = vi.spyOn(window, "open").mockReturnValue(null);
const url = await quickShareLink(qc(), { id: 42 }, t);
expect(url).toBe(`${origin}/watch/tok7`);
// Minted with an EMPTY body so the backend D1 default (allow_download=true) applies.
expect(createDownloadLink).toHaveBeenCalledWith(42, {});
expect(copyText).toHaveBeenCalledWith(`${origin}/watch/tok7`);
const arg = firstToast();
expect(arg.level).toBe("success");
expect(arg.message).toBe("downloads.share.linkCopied");
expect(arg.action!.label).toBe("downloads.share.openNewTab");
arg.action!.onClick();
expect(openSpy).toHaveBeenCalledWith(`${origin}/watch/tok7`, "_blank", "noopener,noreferrer");
});
it("reuses an existing link instead of minting a duplicate", async () => {
downloadLinks.mockResolvedValue([{ id: 3, url: "/watch/old" }]);
const url = await quickShareLink(qc(), { id: 1 }, t);
expect(url).toBe(`${origin}/watch/old`);
expect(createDownloadLink).not.toHaveBeenCalled();
expect(firstToast().level).toBe("success");
});
it("still toasts (warning) with the open action when the clipboard copy fails", async () => {
downloadLinks.mockResolvedValue([{ id: 5, url: "/watch/x" }]);
copyText.mockResolvedValue(false);
const url = await quickShareLink(qc(), { id: 2 }, t);
expect(url).toBe(`${origin}/watch/x`); // the link is never a dead end
const arg = firstToast();
expect(arg.level).toBe("warning");
expect(arg.message).toBe("downloads.share.copyFailed");
expect(arg.action!.label).toBe("downloads.share.openNewTab");
});
it("reports an error and returns null when link creation fails", async () => {
downloadLinks.mockResolvedValue([]);
createDownloadLink.mockRejectedValue(new Error("boom"));
const url = await quickShareLink(qc(), { id: 9 }, t);
expect(url).toBeNull();
expect(copyText).not.toHaveBeenCalled();
expect(notify).toHaveBeenCalledWith({ level: "error", message: "downloads.share.linkFailed" });
});
});