recovery-pending.test.ts

2.2 KB · raw

1import { beforeEach, describe, expect, it, vi } from "vitest";23describe("recovery confirmation state", () => {4  let local: { get: ReturnType<typeof vi.fn>; set: ReturnType<typeof vi.fn>; remove: ReturnType<typeof vi.fn> };5  let session: { get: ReturnType<typeof vi.fn>; set: ReturnType<typeof vi.fn>; remove: ReturnType<typeof vi.fn> };6  let sessionValues: Record<string, unknown>;78  beforeEach(() => {9    vi.resetModules();10    sessionValues = {};11    local = {12      get: vi.fn(async () => ({})),13      set: vi.fn(async () => undefined),14      remove: vi.fn(async () => undefined),15    };16    session = {17      get: vi.fn(async (key: string) => ({ [key]: sessionValues[key] })),18      set: vi.fn(async (values: Record<string, unknown>) => Object.assign(sessionValues, values)),19      remove: vi.fn(async (key: string) => { delete sessionValues[key]; }),20    };21    vi.stubGlobal("chrome", { storage: { local, session } });22  });2324  it("stores the pending exemption in session storage, never local storage", async () => {25    const pending = await import("./recovery-pending");2627    await pending.setRecoveryConfirmPending();2829    expect(session.set).toHaveBeenCalledWith({ peach_recovery_pending: "1" });30    expect(local.set).not.toHaveBeenCalled();31    await expect(pending.isRecoveryConfirmPending()).resolves.toBe(true);32  });3334  it("clears the session-owned exemption", async () => {35    const pending = await import("./recovery-pending");36    await pending.setRecoveryConfirmPending();3738    await pending.clearRecoveryConfirmPending();3940    expect(session.remove).toHaveBeenCalledWith("peach_recovery_pending");41    await expect(pending.isRecoveryConfirmPending()).resolves.toBe(false);42    expect(local.remove).not.toHaveBeenCalled();43  });4445  it("uses background memory when session storage is unavailable", async () => {46    vi.stubGlobal("chrome", { storage: { local } });47    const pending = await import("./recovery-pending");4849    await pending.setRecoveryConfirmPending();5051    await expect(pending.isRecoveryConfirmPending()).resolves.toBe(true);52    await pending.clearRecoveryConfirmPending();53    await expect(pending.isRecoveryConfirmPending()).resolves.toBe(false);54    expect(local.set).not.toHaveBeenCalled();55  });56});