import { vi, describe, it, expect } from "vitest"; import "@testing-library/jest-dom"; import { act, render, fireEvent, waitFor } from "@testing-library/react"; import { BrowserRouter as Router } from "react-router"; import { buildVaultDetailVM } from "@hivegrid/view-models"; import { HrVaultDetailPage } from "./HrVaultDetailPage"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (str: string) => str, i18n: { language: "de" } }), })); function vault(overrides: Record = {}) { return buildVaultDetailVM({ vaultId: "v1", vaultName: "Team Vault", accessLevel: "admin", exportHref: "/acme/passwords/v1/export", canExport: true, rows: [ { id: "e1", title: "GitHub", tags: ["dev"], url: "https://github.com", hasUsername: true, hasPassword: true, hasNotes: false, revealHref: "/acme/passwords/v1/reveal", }, ], ...overrides, }); } const noopActions = { onReveal: vi.fn(async () => null), onCopy: vi.fn(async () => null), onCreateEntry: vi.fn(), onUpdateEntry: vi.fn(), onArchiveEntry: vi.fn(), onRestoreEntry: vi.fn(), onGrantAccess: vi.fn(), onRevokeAccess: vi.fn(), onExport: vi.fn(async () => true), }; describe("HrVaultDetailPage (interim-UI, security-critical)", () => { it("never renders secret values in the entry list — only reveal controls", () => { const { container, queryByText } = render( ); // The entry surface renders... expect(container.querySelector('[data-row-id="e1"]')).toBeInTheDocument(); // ...with reveal controls, but no plaintext secret anywhere before an explicit reveal. expect(container.querySelectorAll('[data-ui-id="ui.hr.vault.reveal"]').length).toBeGreaterThan(0); expect(queryByText("s3cret")).not.toBeInTheDocument(); expect(container.querySelector('[data-ui-id="ui.hr.vault.revealed"]')).not.toBeInTheDocument(); }); it("reveals a single field on demand via the injected onReveal action and then offers copy", async () => { const onReveal = vi.fn(async () => "s3cret"); const { container, findByText } = render( ); const revealBtn = container.querySelector('[data-ui-id="ui.hr.vault.reveal"]') as HTMLElement; fireEvent.click(revealBtn); await waitFor(() => expect(onReveal).toHaveBeenCalledWith("e1", "username")); // The plaintext now appears next to a separately audited copy control. expect(await findByText("s3cret")).toBeInTheDocument(); expect(container.querySelector('[data-ui-id="ui.hr.vault.revealed"]')).toBeInTheDocument(); expect(container.querySelector('[data-ui-id="ui.hr.vault.fieldActions"]')).toBeInTheDocument(); }); it("removes revealed plaintext after 30 seconds and immediately in the background", async () => { vi.useFakeTimers(); const visibilityDescriptor = Object.getOwnPropertyDescriptor(document, "visibilityState"); try { const onReveal = vi.fn(async () => "short-lived-secret"); const { container, queryByText } = render( ); await act(async () => { fireEvent.click(container.querySelector('[data-ui-id="ui.hr.vault.reveal"]')!); }); expect(queryByText("short-lived-secret")).toBeInTheDocument(); act(() => vi.advanceTimersByTime(30_000)); expect(queryByText("short-lived-secret")).not.toBeInTheDocument(); await act(async () => { fireEvent.click(container.querySelector('[data-ui-id="ui.hr.vault.reveal"]')!); }); expect(queryByText("short-lived-secret")).toBeInTheDocument(); Object.defineProperty(document, "visibilityState", { configurable: true, value: "hidden" }); fireEvent(document, new Event("visibilitychange")); expect(queryByText("short-lived-secret")).not.toBeInTheDocument(); } finally { if (visibilityDescriptor) Object.defineProperty(document, "visibilityState", visibilityDescriptor); else Reflect.deleteProperty(document, "visibilityState"); vi.useRealTimers(); } }); it("copies only through the audited command and clears the clipboard best-effort after 30 seconds", async () => { vi.useFakeTimers(); const clipboardDescriptor = Object.getOwnPropertyDescriptor(navigator, "clipboard"); const writeText = vi.fn(async () => undefined); Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText } }); try { const onCopy = vi.fn(async () => "copy-once-secret"); const { getByRole, getByText } = render( ); await act(async () => { fireEvent.click(getByRole("button", { name: "hr.passwordSafe.field.username: hr.passwordSafe.detail.copy" })); }); expect(onCopy).toHaveBeenCalledWith("e1", "username"); expect(writeText).toHaveBeenCalledWith("copy-once-secret"); expect(getByText("hr.passwordSafe.detail.copyCleared")).toBeInTheDocument(); act(() => vi.advanceTimersByTime(30_000)); expect(writeText).toHaveBeenLastCalledWith(""); } finally { if (clipboardDescriptor) Object.defineProperty(navigator, "clipboard", clipboardDescriptor); else Reflect.deleteProperty(navigator, "clipboard"); vi.useRealTimers(); } }); it("requires explicit confirmation before invoking export", async () => { const onExport = vi.fn(async () => true); const { getByRole } = render( ); fireEvent.click(getByRole("button", { name: "hr.passwordSafe.detail.export" })); expect(onExport).not.toHaveBeenCalled(); fireEvent.click(document.querySelector('[data-ui-id="ui.hr.vault.exportConfirm.submit"]')!); await waitFor(() => expect(onExport).toHaveBeenCalledOnce()); }); it("keeps the export confirmation open and shows a clear failure state when export is denied", async () => { const onExport = vi.fn(async () => false); const { getByRole } = render( ); fireEvent.click(getByRole("button", { name: "hr.passwordSafe.detail.export" })); fireEvent.click(document.querySelector('[data-ui-id="ui.hr.vault.exportConfirm.submit"]')!); await waitFor(() => expect(document.querySelector('[data-ui-id="ui.hr.vault.export.error"]')).toBeInTheDocument()); expect(document.querySelector('[data-ui-id="ui.hr.vault.exportConfirm"]')).toBeInTheDocument(); }); it("shows a clear secret-action error instead of an empty result", async () => { const onReveal = vi.fn(async () => null); const { container, findByText } = render( ); fireEvent.click(container.querySelector('[data-ui-id="ui.hr.vault.reveal"]')!); expect(await findByText("hr.passwordSafe.detail.secretActionFailed")).toBeInTheDocument(); }); it("archives an entry only after a clear confirmation", () => { const onArchiveEntry = vi.fn(); const confirm = vi.spyOn(window, "confirm").mockReturnValueOnce(false).mockReturnValueOnce(true); const { getByRole } = render( ); const archive = getByRole("button", { name: "hr.passwordSafe.detail.archiveEntry" }); fireEvent.click(archive); expect(onArchiveEntry).not.toHaveBeenCalled(); fireEvent.click(archive); expect(onArchiveEntry).toHaveBeenCalledWith("e1"); confirm.mockRestore(); }); it("keeps archived entries reachable and restores them through the injected action", () => { const onRestoreEntry = vi.fn(); const archived = { id: "e2", title: "Old router", tags: [], url: null, hasUsername: true, hasPassword: true, hasNotes: false, revealHref: "/acme/passwords/v1/reveal", }; const { container } = render( ); expect(container.querySelector('[data-archived-row-id="e2"]')).toBeInTheDocument(); fireEvent.click(container.querySelector('[data-ui-id="ui.hr.vault.restoreEntry"]')!); expect(onRestoreEntry).toHaveBeenCalledWith("e2"); expect(container.querySelector('[data-archived-row-id="e2"] [data-ui-id="ui.hr.vault.reveal"]')).not.toBeInTheDocument(); }); });