#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { renderHook, act } from "@testing-library/react";
// The auto-run hook reaches into several contexts + the network; stub those so we can drive just
// the queue-rejection retry path against the REAL run store.
vi.mock("@editor/contexts/FileContext", () => ({
useAllFiles: () => ({ fileStubs: [] }),
useFileManagement: () => ({ addFiles: vi.fn() }),
useFileContext: () => ({ consumeFiles: vi.fn() }),
}));
vi.mock("@editor/hooks/usePolicies", () => ({
usePolicies: () => ({
policies: {
security: {
configured: true,
runsOnEditor: true,
enabled: true,
backendId: "backend-1",
runOn: "upload",
},
},
}),
}));
vi.mock("@editor/services/policyApi", () => ({
runStoredPolicy: vi.fn(),
getPolicyRun: vi.fn(),
downloadPolicyOutput: vi.fn(),
resolvePolicyRunTarget: () => "saas",
}));
vi.mock("@editor/services/fileStorage", () => ({
fileStorage: {
getStirlingFile: vi.fn(),
getStirlingFileStub: vi.fn().mockResolvedValue(null),
},
}));
vi.mock("@editor/contexts/IndexedDBContext", () => ({
useIndexedDB: () => ({ bumpRevision: vi.fn() }),
}));
import { usePolicyAutoRun } from "@editor/components/policies/usePolicyAutoRun";
import {
recordRunStart,
getRun,
resetPolicyRuns,
usePolicyRuns,
} from "@editor/components/policies/policyRunStore";
import { runStoredPolicy, getPolicyRun } from "@editor/services/policyApi";
import { fileStorage } from "@editor/services/fileStorage";
const getRunApi = vi.mocked(getPolicyRun);
const runStored = vi.mocked(runStoredPolicy);
const getFile = vi.mocked(fileStorage.getStirlingFile);
const queueFullView = {
runId: "run-1",
status: "FAILED",
currentStep: 0,
stepCount: 2,
error: "Policy run could not be queued: Job queue full",
errorCode: "POLICY_QUEUE_FULL",
outputs: [],
} as never;
beforeEach(() => {
vi.useFakeTimers();
localStorage.clear();
resetPolicyRuns();
getRunApi.mockReset();
runStored.mockReset();
getFile.mockReset();
});
afterEach(() => vi.useRealTimers());
describe("auto-run queue-rejection retry", () => {
it("relabels a queue-rejected run as retrying, then re-dispatches it in place", async () => {
// The polled run comes back queue-rejected once; the retry resolves the file
// and fires a fresh run, whose own polls then see it genuinely running.
getRunApi.mockResolvedValueOnce(queueFullView).mockResolvedValue({
runId: "run-2",
status: "RUNNING",
currentStep: 1,
stepCount: 2,
error: null,
errorCode: null,
outputs: [],
} as never);
getFile.mockResolvedValue({ size: 1234 } as never);
runStored.mockResolvedValue("run-2");
recordRunStart({
runId: "run-1",
policyKey: "security",
fileId: "file-1",
fileName: "doc.pdf",
fileSize: 1234,
target: "saas",
status: "RUNNING",
outputs: [],
error: null,
startedAt: 0,
});
renderHook(() => {
usePolicyAutoRun();
return usePolicyRuns();
});
// First poll sees the rejection → relabel as a soft "retrying" row.
await act(async () => {
await vi.advanceTimersByTimeAsync(2000);
});
expect(getRun("run-1")?.retrying).toBe(true);
expect(runStored).not.toHaveBeenCalled();
// After the first backoff window (BASE 4s) the rejected record is dropped and
// a fresh run fires; its own first poll shows it genuinely running.
await act(async () => {
await vi.advanceTimersByTimeAsync(6000);
});
// The workspace id travels with the retry too, so a failure of it can still name the
// document this browser is holding.
expect(runStored).toHaveBeenCalledWith(
"backend-1",
[{ size: 1234 }],
"file-1",
"background",
);
expect(getRun("run-1")).toBeUndefined();
expect(getRun("run-2")?.status).toBe("RUNNING");
});
});