Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

11.3 KB
Raw Blame
import { describe, it, expect, beforeEach, vi } from "vitest";
import {
  appliedPoliciesFor,
  dispatchKey,
  getRun,
  getPolicyRunOutcomes,
  isDispatched,
  markDispatched,
  recordRunStart,
  removeRun,
  updateRun,
  resetPolicyRuns,
  type PolicyRunRecord,
} from "@editor/components/policies/policyRunStore";

function rec(over: Partial<PolicyRunRecord>): PolicyRunRecord {
  return {
    runId: "r1",
    policyKey: "security",
    fileId: "f1",
    fileName: "f.pdf",
    fileSize: 10,
    target: "saas",
    status: "PENDING",
    outputs: [],
    error: null,
    startedAt: 1,
    ...over,
  };
}

// The store reads localStorage at import; reset state + storage per test.
function read(key: string) {
  return JSON.parse(localStorage.getItem(key) ?? "{}");
}

describe("policyRunStore", () => {
  beforeEach(() => {
    localStorage.clear();
    resetPolicyRuns();
  });

  it("reads a run recorded before the key was renamed", async () => {
    // Persisted records predate policyKey; without the migration their policy is undefined, so
    // the badge disappears and the chain re-runs from the start. The store reads storage at
    // import, so this asserts against a fresh one rather than the module already loaded.
    localStorage.setItem(
      "stirling-policy-runs",
      JSON.stringify({
        runs: [{ ...rec({}), policyKey: undefined, categoryId: "security" }],
        dispatched: [],
        waveStartedAt: 0,
      }),
    );
    vi.resetModules();

    const fresh = await import("@editor/components/policies/policyRunStore");

    expect(fresh.getRun("r1")?.policyKey).toBe("security");
  });

  it("records a run start and marks the (policy, file) pair dispatched", () => {
    expect(isDispatched("security", "f1")).toBe(false);
    recordRunStart(rec({}));
    expect(isDispatched("security", "f1")).toBe(true);
    const stored = read("stirling-policy-runs");
    expect(stored.runs).toHaveLength(1);
    expect(stored.dispatched).toContain(dispatchKey("security", "f1"));
  });

  it("markDispatched is idempotent and independent of a run record", () => {
    markDispatched("routing", "f9");
    markDispatched("routing", "f9");
    expect(isDispatched("routing", "f9")).toBe(true);
    expect(read("stirling-policy-runs").dispatched).toHaveLength(1);
  });

  it("updateRun patches an in-flight run's status + outputs", () => {
    recordRunStart(rec({ runId: "abc" }));
    updateRun("abc", {
      status: "COMPLETED",
      outputs: [{ fileId: "out-1", fileName: "redacted.pdf" }],
    });
    const run = read("stirling-policy-runs").runs[0];
    expect(run.status).toBe("COMPLETED");
    expect(run.outputs).toEqual([
      { fileId: "out-1", fileName: "redacted.pdf" },
    ]);
  });

  it("updateRun ignores an unknown run id", () => {
    recordRunStart(rec({ runId: "abc", status: "PENDING" }));
    updateRun("nope", { status: "FAILED" });
    expect(read("stirling-policy-runs").runs[0].status).toBe("PENDING");
  });

  it("getRun returns the record by id, removeRun drops it but keeps the dispatched key", () => {
    recordRunStart(rec({ runId: "abc" }));
    expect(getRun("abc")?.fileId).toBe("f1");
    removeRun("abc");
    expect(getRun("abc")).toBeUndefined();
    expect(read("stirling-policy-runs").runs).toHaveLength(0);
    // The (policy, file) pair stays dispatched so the auto-run doesn't re-fire on its own.
    expect(isDispatched("security", "f1")).toBe(true);
  });

  it("a real backend run still claims the dispatch key", () => {
    recordRunStart(rec({ runId: "srv-1", policyKey: "classification" }));
    expect(isDispatched("classification", "f1")).toBe(true);
  });

  describe("appliedPoliciesFor", () => {
    it("walks a rewriting chain back to the uploaded document", () => {
      recordRunStart(
        rec({ runId: "r-w", policyKey: "watermark", fileId: "f1" }),
      );
      updateRun("r-w", { status: "COMPLETED", outputFileIds: ["f2"] });
      recordRunStart(
        rec({ runId: "r-s", policyKey: "security", fileId: "f2" }),
      );
      updateRun("r-s", { status: "COMPLETED", outputFileIds: ["f3"] });

      expect([...appliedPoliciesFor("f3")].sort()).toEqual([
        "security",
        "watermark",
      ]);
    });

    it("counts an annotating run, which names its input as its own output", () => {
      recordRunStart(
        rec({ runId: "r-c", policyKey: "classification", fileId: "f1" }),
      );
      updateRun("r-c", { status: "COMPLETED", outputFileIds: ["f1"] });

      expect([...appliedPoliciesFor("f1")]).toEqual(["classification"]);
    });

    it("does not count a browser-local pass as the policy having run", () => {
      // The local heuristic settles COMPLETED with the input as its own output. Counting it
      // would make a retry skip classification, killing the escalation #7667 restored.
      recordRunStart(
        rec({
          runId: "local-c",
          policyKey: "classification",
          fileId: "f1",
          target: "local",
          browserLocal: true,
        }),
      );
      updateRun("local-c", { status: "COMPLETED", outputFileIds: ["f1"] });

      expect(appliedPoliciesFor("f1").size).toBe(0);
    });

    it("keeps climbing past an annotating run rather than stalling on it", () => {
      // The annotating run's output IS its input, so the walk must not treat that as a
      // lineage step - otherwise the cursor never moves and earlier policies are missed.
      recordRunStart(
        rec({ runId: "r-w", policyKey: "watermark", fileId: "f1" }),
      );
      updateRun("r-w", { status: "COMPLETED", outputFileIds: ["f2"] });
      recordRunStart(
        rec({ runId: "r-c", policyKey: "classification", fileId: "f2" }),
      );
      updateRun("r-c", { status: "COMPLETED", outputFileIds: ["f2"] });

      expect([...appliedPoliciesFor("f2")].sort()).toEqual([
        "classification",
        "watermark",
      ]);
    });

    it("ignores a run that failed, so it stays eligible to run again", () => {
      recordRunStart(
        rec({ runId: "r-f", policyKey: "security", fileId: "f1" }),
      );
      updateRun("r-f", { status: "FAILED", outputFileIds: ["f2"] });

      expect(appliedPoliciesFor("f2").size).toBe(0);
    });
  });

  it("never evicts in-flight runs, even past the soft cap", () => {
    // A large upload batch can exceed the cap while still processing. Dropping a
    // live run would orphan its polling/import and undercount progress, so every
    // in-flight run is kept regardless of the cap.
    for (let i = 0; i < 210; i++) {
      recordRunStart(
        rec({
          runId: `r${i}`,
          fileId: `f${i}`,
          status: "PENDING",
          startedAt: i,
        }),
      );
    }
    const runs = read("stirling-policy-runs").runs;
    expect(runs).toHaveLength(210);
    expect(runs[0].runId).toBe("r209"); // newest first
  });

  it("evicts the oldest TERMINAL runs first once over the cap", () => {
    // Imported, since a COMPLETED-but-not-yet-imported run still counts as
    // in-flight (see isRunInFlight) and must never be evicted.
    for (let i = 0; i < 210; i++) {
      recordRunStart(
        rec({
          runId: `r${i}`,
          fileId: `f${i}`,
          status: "COMPLETED",
          imported: true,
          startedAt: i,
        }),
      );
    }
    const runs = read("stirling-policy-runs").runs;
    expect(runs).toHaveLength(200); // trimmed to MAX_RUNS
    expect(runs[0].runId).toBe("r209"); // newest kept
    expect(runs.some((r: PolicyRunRecord) => r.runId === "r0")).toBe(false); // oldest dropped
  });

  it("retains a failed outcome after its run is evicted and storage is reloaded", () => {
    recordRunStart(rec({ runId: "failed", status: "FAILED" }));
    for (let i = 0; i < 200; i++) {
      recordRunStart(
        rec({
          runId: `other-${i}`,
          fileId: `other-${i}`,
          status: "COMPLETED",
          imported: true,
          startedAt: i + 2,
        }),
      );
    }
    expect(getRun("failed")).toBeUndefined();
    const saved = localStorage.getItem("stirling-policy-runs")!;
    resetPolicyRuns();
    localStorage.setItem("stirling-policy-runs", saved);
    window.dispatchEvent(
      new StorageEvent("storage", { key: "stirling-policy-runs" }),
    );

    expect(getPolicyRunOutcomes()[dispatchKey("security", "f1")]?.status).toBe(
      "FAILED",
    );
  });

  it("keeps a failure through retry-record removal and cancellation until a successful retry", () => {
    recordRunStart(rec({ runId: "failed", status: "FAILED" }));
    removeRun("failed");
    recordRunStart(rec({ runId: "retry", startedAt: 2 }));
    updateRun("retry", { status: "CANCELLED" });
    expect(getPolicyRunOutcomes()[dispatchKey("security", "f1")]?.status).toBe(
      "FAILED",
    );

    recordRunStart(rec({ runId: "success", startedAt: 3 }));
    updateRun("success", { status: "COMPLETED" });
    expect(getPolicyRunOutcomes()[dispatchKey("security", "f1")]?.status).toBe(
      "FAILED",
    );
    updateRun("success", { imported: true });
    expect(getPolicyRunOutcomes()[dispatchKey("security", "f1")]?.status).toBe(
      "COMPLETED",
    );

    updateRun("retry", { status: "FAILED" });
    expect(getPolicyRunOutcomes()[dispatchKey("security", "f1")]?.status).toBe(
      "COMPLETED",
    );
  });

  it("migrates legacy history without allowing a cancelled retry to clear a failure", () => {
    localStorage.setItem(
      "stirling-policy-runs",
      JSON.stringify({
        runs: [
          rec({ runId: "retry", startedAt: 2, status: "CANCELLED" }),
          rec({ runId: "failed", status: "FAILED" }),
        ],
        dispatched: [],
      }),
    );
    window.dispatchEvent(
      new StorageEvent("storage", { key: "stirling-policy-runs" }),
    );
    expect(getPolicyRunOutcomes()[dispatchKey("security", "f1")]?.status).toBe(
      "FAILED",
    );
  });

  it("does not evict a COMPLETED run that hasn't been imported yet, even past the cap", () => {
    for (let i = 0; i < 210; i++) {
      recordRunStart(
        rec({
          runId: `r${i}`,
          fileId: `f${i}`,
          status: "COMPLETED",
          startedAt: i,
        }),
      );
    }
    const runs = read("stirling-policy-runs").runs;
    expect(runs).toHaveLength(210);
    expect(runs.some((r: PolicyRunRecord) => r.runId === "r0")).toBe(true);
  });

  describe("processing wave (scopes the panel's progress counts to this upload)", () => {
    it("begins a new wave when recording with nothing in flight", () => {
      recordRunStart(rec({ runId: "a", fileId: "fa", startedAt: 500 }));
      expect(read("stirling-policy-runs").waveStartedAt).toBe(500);
    });

    it("keeps the wave while earlier runs are still in flight", () => {
      recordRunStart(rec({ runId: "a", fileId: "fa", startedAt: 100 }));
      recordRunStart(rec({ runId: "b", fileId: "fb", startedAt: 200 }));
      // b joined a's wave (a still PENDING) — the boundary stays at a.
      expect(read("stirling-policy-runs").waveStartedAt).toBe(100);
    });

    it("starts a fresh wave once the prior batch has all finished", () => {
      recordRunStart(rec({ runId: "a", fileId: "fa", startedAt: 100 }));
      // Prior batch completes AND imports → no longer in flight.
      updateRun("a", { status: "COMPLETED", imported: true });
      // A new upload after the lull resets the wave to itself.
      recordRunStart(rec({ runId: "b", fileId: "fb", startedAt: 5000 }));
      expect(read("stirling-policy-runs").waveStartedAt).toBe(5000);
    });
  });
});