import { vi, describe, it, expect } from "vitest"; import "@testing-library/jest-dom"; import { fireEvent, render } from "@testing-library/react"; import { BrowserRouter as Router } from "react-router"; import { buildFileDetailVM } from "@hivegrid/view-models"; import { FileDetailPage, splitFileName } from "./FileDetailPage"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (str: string) => str, i18n: { language: "de" } }), })); const ENTITY_TYPES = ["contact", "company", "lead"] as const; function detailData(overrides: Record = {}) { const vm = buildFileDetailVM({ id: "f1", displayName: "vertrag.pdf", mimeType: "application/pdf", sizeBytes: 4096, createdAtIso: "2026-01-02T10:00:00.000Z", tags: ["wichtig"], notes: "Bitte prüfen", source: "upload", previewStatus: "ready", scanStatus: "clean", previewUrl: "/acme/media/f1/preview", downloadUrl: "/acme/media/f1/original", assignedToUserId: null, linkedEntities: [{ entityType: "contact", entityId: "c1", linkRole: "owner", href: null }], canWrite: true, canDelete: true, canAdmin: false, ...overrides, }); if (vm.status !== "ready" || !vm.data) throw new Error("fixture VM not ready"); return vm.data; } describe("splitFileName", () => { it("splits a name at its last dot", () => { expect(splitFileName("angebot.2026.pdf")).toEqual({ stem: "angebot.2026", extension: ".pdf" }); }); it("leaves a name without a usable dot whole", () => { // No dot, a trailing dot, and a dotfile all have no suffix to protect. expect(splitFileName("scan")).toEqual({ stem: "scan", extension: "" }); expect(splitFileName("scan.")).toEqual({ stem: "scan.", extension: "" }); expect(splitFileName(".gitignore")).toEqual({ stem: ".gitignore", extension: "" }); }); }); describe("FileDetailPage (interim-UI)", () => { it("renders the detail surface, download link and linked entities", () => { const { container, getAllByText } = render( , ); expect(container.querySelector('[data-ui-id="ui.files.detail"]')).toBeInTheDocument(); expect(container.querySelector('[data-ui-id="ui.files.detail.download"]')).toBeInTheDocument(); expect(container.querySelector('[data-ui-id="ui.files.detail.links"]')).toBeInTheDocument(); expect(container.querySelector('[data-link-id="contact-c1"]')).toBeInTheDocument(); // displayName appears in both the breadcrumb and the header. expect(getAllByText("vertrag.pdf").length).toBeGreaterThan(0); }); it("shows the scan notice (fail-closed) for infected files", () => { const { container } = render( , ); expect(container.querySelector('[data-ui-id="ui.files.detail.scanNotice"]')).toBeInTheDocument(); }); it("edits the tags in a dialog, pre-filled and posting updateMeta", () => { const { container } = render( , ); fireEvent.click(container.querySelector('[data-ui-id="ui.files.detail.tags.open"]')!); // The dialog renders in a portal, so it is not inside `container`. const form = document.querySelector('[data-ui-id="ui.files.detail.tagsDialog"] form'); expect(form).toBeTruthy(); expect(form?.querySelector('input[type="hidden"][name="intent"][value="updateMeta"]')).toBeTruthy(); const input = form?.querySelector('input[name="tags"]') as HTMLInputElement | null; expect(input?.value).toBe("wichtig"); }); it("hides the tag editor for read-only users", () => { const { container } = render( , ); expect(container.querySelector('[data-ui-id="ui.files.detail.tags.open"]')).not.toBeInTheDocument(); }); it("offers both paging arrows and keeps the end of the library inert", () => { const { container } = render( , ); const previous = container.querySelector('[data-ui-id="ui.files.detail.nav.previous"]'); const next = container.querySelector('[data-ui-id="ui.files.detail.nav.next"]'); expect(previous?.getAttribute("href")).toBe("/acme/files/f0"); // No neighbour: a disabled button, not a link that goes nowhere. expect(next?.getAttribute("href")).toBeNull(); expect((next as HTMLButtonElement | null)?.disabled).toBe(true); }); it("edits the file name without its extension, saved by the header action", () => { const { container } = render( , ); const form = container.querySelector('[data-ui-id="ui.files.detail.rename"]'); expect(form).toBeInTheDocument(); expect(form?.querySelector('input[type="hidden"][name="intent"][value="rename"]')).toBeInTheDocument(); // The field carries the stem only; the suffix rides along in the hidden // value, so a rename cannot break what the download is saved as. const input = container.querySelector('[data-ui-id="ui.files.detail.rename.input"]') as HTMLInputElement | null; expect(input?.value).toBe("vertrag"); expect(input?.getAttribute("name")).toBeNull(); const submitted = () => (form?.querySelector('input[type="hidden"][name="displayName"]') as HTMLInputElement).value; expect(submitted()).toBe("vertrag.pdf"); // The header's save button belongs to that form and stays inert until the // name actually changed. const save = container.querySelector('[data-ui-id="ui.files.detail.save"]') as HTMLButtonElement | null; expect(save?.getAttribute("form")).toBe(form?.getAttribute("id")); expect(save?.disabled).toBe(true); fireEvent.change(input!, { target: { value: "vertrag-2026" } }); expect(submitted()).toBe("vertrag-2026.pdf"); expect((container.querySelector('[data-ui-id="ui.files.detail.save"]') as HTMLButtonElement).disabled).toBe(false); }); it("puts save at the end of the header actions", () => { const { container } = render( , ); const order = ["download", "archive", "delete", "save"].map((key) => [...container.querySelectorAll("[data-ui-id]")].findIndex((node) => node.getAttribute("data-ui-id") === `ui.files.detail.${key}`), ); expect(order.every((index) => index >= 0)).toBe(true); expect([...order]).toEqual([...order].sort((left, right) => left - right)); }); it("hides name editing and the save action for read-only users", () => { const { container } = render( , ); expect(container.querySelector('[data-ui-id="ui.files.detail.rename"]')).not.toBeInTheDocument(); expect(container.querySelector('[data-ui-id="ui.files.detail.save"]')).not.toBeInTheDocument(); }); });