(null);
+ const [hasCache, setHasCache] = useState(false);
+
+ return (
+
+ {hasCache ? "true" : "false"}
+ {loaded?.headers ?? ""}
+ {loaded?.result.metadata.totalTests ?? ""}
+
+ {loaded ? loaded.config.testIds.join(",") : ""}
+
+ {isNearLimit ? "true" : "false"}
+
+
+
+
+
+ );
+};
+
+beforeEach(() => {
+ localStorage.clear();
+});
+
+afterEach(() => {
+ while (cleanups.length > 0) {
+ const cleanup = cleanups.pop();
+ if (cleanup) {
+ cleanup();
+ }
+ }
+ localStorage.clear();
+});
+
+describe("useAnalysisCache", () => {
+ it("saves, loads, and clears cached analysis data", () => {
+ const { container } = render();
+
+ act(() => {
+ getByTestId(container, "check").dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ });
+
+ expect(getByTestId(container, "has-cache").textContent).toBe("false");
+
+ act(() => {
+ getByTestId(container, "save").dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ });
+
+ expect(localStorage.getItem("wha:headers")).toBe(basePayload.headers);
+ expect(localStorage.getItem("wha:config")).toBe(JSON.stringify(basePayload.config));
+ expect(localStorage.getItem("wha:result")).toBe(JSON.stringify(basePayload.result));
+ const timestampValue = localStorage.getItem("wha:timestamp");
+ expect(timestampValue).not.toBeNull();
+ expect(Number.isNaN(Number(timestampValue))).toBe(false);
+
+ act(() => {
+ getByTestId(container, "check").dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ });
+
+ expect(getByTestId(container, "has-cache").textContent).toBe("true");
+
+ act(() => {
+ getByTestId(container, "load").dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ });
+
+ expect(getByTestId(container, "loaded-headers").textContent).toBe(basePayload.headers);
+ expect(getByTestId(container, "loaded-test-count").textContent).toBe("3");
+ expect(getByTestId(container, "loaded-test-ids").textContent).toBe("101,202");
+
+ act(() => {
+ getByTestId(container, "clear").dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ });
+
+ expect(localStorage.getItem("wha:headers")).toBeNull();
+ expect(localStorage.getItem("wha:config")).toBeNull();
+ expect(localStorage.getItem("wha:result")).toBeNull();
+ expect(localStorage.getItem("wha:timestamp")).toBeNull();
+
+ act(() => {
+ getByTestId(container, "check").dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ });
+
+ expect(getByTestId(container, "has-cache").textContent).toBe("false");
+ });
+
+ it("flags when cache usage is near the localStorage limit", () => {
+ const storageLimitBytes = 5 * 1024 * 1024;
+ const warningThresholdBytes = Math.floor(storageLimitBytes * 0.9) + 1024;
+ const largeHeaders = "a".repeat(warningThresholdBytes);
+ const payload: CachedPayload = {
+ ...basePayload,
+ headers: largeHeaders,
+ };
+
+ const { container } = render();
+
+ act(() => {
+ getByTestId(container, "save").dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ });
+
+ expect(getByTestId(container, "near-limit").textContent).toBe("true");
+ });
+});