Files
Stirling-PDF/frontend/editor/src/proprietary/services/policyStorage.test.ts
T
Reece BrowneandGitHub 8dde4262ec feat(policies): backend-driven policy enforcement (frontend) (#6598)
## Summary
Adds the **Policies** feature (proprietary, behind the
`POLICIES_ENABLED` flag): backend-driven enforcement that runs a fixed
tool pipeline on documents, docked in the right tool sidebar alongside
Tools.

## Highlights
- **Policy catalog** — 5 categories; **Security** is wired (redact PII +
sanitize), the others are marked "Coming soon".
- **Backend as source of truth** — policies persist via the Policies
engine (`/api/v1/policies`), one policy per category, with a local cache
+ offline fallback.
- **Auto-run** — enabled policies run on every uploaded file: dispatch →
poll → import outputs into the workspace.
- **Security redact config** — PII preset dropdown + custom word/regex
entry + advanced options; tool params map to the backend endpoint
fields.
- **Activity feed** with retry on failures; **file badges** showing
which policies ran on a file (sidebar + files page), tinted to the
policy colour.
- Reuses the **Watched Folders** engine for each policy's backing
folder; policy-owned folders are filtered out of the Watched Folders UI.

## Notes
- Gated by `POLICIES_ENABLED` (true in proprietary, false in core) —
unreachable in the open-source build.
- Frontend-only diff; depends on the backend Policies engine and the
merged Watched Folders feature.
2026-06-10 15:57:08 +01:00

73 lines
2.4 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from "vitest";
import {
loadPolicies,
updatePolicy,
resetPolicy,
onPoliciesChange,
} from "@app/services/policyStorage";
describe("policyStorage", () => {
beforeEach(() => {
localStorage.clear();
});
it("defaults every category to unconfigured (backend is the source of truth)", () => {
const p = loadPolicies();
expect(p.ingestion.configured).toBe(false);
expect(p.ingestion.status).toBe("default");
expect(p.security.configured).toBe(false);
expect(p.security.status).toBe("default");
expect(p.retention.configured).toBe(false);
});
it("persists an update and reflects it on reload", () => {
updatePolicy("security", { configured: true, status: "active" });
const reloaded = loadPolicies();
expect(reloaded.security.configured).toBe(true);
expect(reloaded.security.status).toBe("active");
// Other categories untouched.
expect(reloaded.retention.configured).toBe(false);
});
it("merges partial field-value updates without clobbering siblings", () => {
updatePolicy("security", { fieldValues: { detectPII: false } });
updatePolicy("security", { reviewerEmail: "[email protected]" });
const p = loadPolicies();
expect(p.security.fieldValues).toEqual({ detectPII: false });
expect(p.security.reviewerEmail).toBe("[email protected]");
});
it("resetPolicy reverts a category to unconfigured default", () => {
updatePolicy("compliance", {
configured: true,
status: "active",
reviewerEmail: "[email protected]",
});
resetPolicy("compliance");
const p = loadPolicies();
expect(p.compliance.configured).toBe(false);
expect(p.compliance.status).toBe("default");
});
it("heals missing categories from corrupt/partial storage", () => {
localStorage.setItem(
"stirling-policies-state",
JSON.stringify({ ingestion: { configured: true, status: "active" } }),
);
const p = loadPolicies();
// Missing category gets a default rather than being undefined.
expect(p.routing).toBeDefined();
expect(p.routing.configured).toBe(false);
});
it("fires a change event on update", () => {
const cb = vi.fn();
const off = onPoliciesChange(cb);
updatePolicy("routing", { status: "paused" });
expect(cb).toHaveBeenCalledTimes(1);
off();
updatePolicy("routing", { status: "active" });
expect(cb).toHaveBeenCalledTimes(1); // not called after unsubscribe
});
});