#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 type { StirlingFileStub } from "@editor/types/fileContext";
import type { FileId } from "@editor/types/file";
import type { DiskFileState } from "@editor/services/desktopFileLink";
// The seam is a module-level const, so each behaviour is exercised by re-importing
// the module under a fresh mock rather than by mutating a flag.
const diskState = vi.hoisted<{
supported: boolean;
state: DiskFileState;
bytes: ArrayBuffer | null;
}>(() => ({
supported: true,
state: { availability: "present", size: 100, modifiedMs: 5000 },
bytes: new Uint8Array([1, 2, 3]).buffer,
}));
vi.mock("@editor/services/desktopFileLink", () => ({
get desktopFileLinkingSupported() {
return diskState.supported;
},
getDiskFileState: vi.fn(async () => diskState.state),
readFileFromDisk: vi.fn(async () => diskState.bytes),
}));
const updateFileMetadata = vi.hoisted(() => vi.fn(async () => true));
const deleteStirlingFile = vi.hoisted(() => vi.fn(async () => undefined));
const getStirlingFile = vi.hoisted(() =>
vi.fn(async () => new File([new Uint8Array([9])], "report.pdf")),
);
vi.mock("@editor/services/fileStorage", () => ({
fileStorage: { updateFileMetadata, deleteStirlingFile, getStirlingFile },
}));
const downloadFileWithPolicy = vi.hoisted(() =>
vi.fn(
async (
_request: Record<string, unknown>,
): Promise<{ savedPath?: string; cancelled?: boolean }> => ({
savedPath: "C:/elsewhere/report.pdf",
}),
),
);
vi.mock("@editor/services/exportWithPolicy", () => ({
downloadFileWithPolicy,
}));
const alertMock = vi.hoisted(() => vi.fn());
vi.mock("@editor/components/toast", () => ({ alert: alertMock }));
import { getDiskFileState } from "@editor/services/desktopFileLink";
import {
syncLinkedFileFromDisk,
hasDiskChanged,
detachedFields,
loadDiskVersion,
persistDiskUpdate,
refreshDiskBaselineAfterSave,
saveOrphanAsCopy,
notifyFileVanished,
notifyDiskReloaded,
notifyOpenFileDeleted,
beginSelfWrite,
endSelfWrite,
__resetSelfWrites,
} from "@editor/services/diskFileSync";
function stub(overrides: Partial<StirlingFileStub> = {}): StirlingFileStub {
return {
id: "file-1" as FileId,
name: "report.pdf",
type: "application/pdf",
size: 100,
lastModified: 5000,
isLeaf: true,
originalFileId: "file-1",
versionNumber: 1,
localFilePath: "C:/docs/report.pdf",
diskSyncedSize: 100,
diskSyncedModifiedMs: 5000,
...overrides,
};
}
beforeEach(() => {
diskState.supported = true;
diskState.state = { availability: "present", size: 100, modifiedMs: 5000 };
diskState.bytes = new Uint8Array([1, 2, 3]).buffer;
__resetSelfWrites();
vi.clearAllMocks();
downloadFileWithPolicy.mockResolvedValue({
savedPath: "C:/elsewhere/report.pdf",
});
getStirlingFile.mockResolvedValue(
new File([new Uint8Array([9])], "report.pdf"),
);
});
afterEach(() => vi.clearAllMocks());
describe("hasDiskChanged", () => {
it("treats a record with no baseline as changed so it re-reads once", () => {
expect(
hasDiskChanged(
{ diskSyncedSize: undefined, diskSyncedModifiedMs: undefined },
{ availability: "present", size: 100, modifiedMs: 5000 },
),
).toBe(true);
});
it("is unchanged when size and mtime both match", () => {
expect(
hasDiskChanged(
{ diskSyncedSize: 100, diskSyncedModifiedMs: 5000 },
{ availability: "present", size: 100, modifiedMs: 5000 },
),
).toBe(false);
});
it("spots an edit that changed the size", () => {
expect(
hasDiskChanged(
{ diskSyncedSize: 100, diskSyncedModifiedMs: 5000 },
{ availability: "present", size: 240, modifiedMs: 5000 },
),
).toBe(true);
});
it("spots an edit that kept the size but moved the mtime", () => {
expect(
hasDiskChanged(
{ diskSyncedSize: 100, diskSyncedModifiedMs: 5000 },
{ availability: "present", size: 100, modifiedMs: 9999 },
),
).toBe(true);
});
it("falls back to size alone when the platform reports no mtime", () => {
expect(
hasDiskChanged(
{ diskSyncedSize: 100, diskSyncedModifiedMs: 5000 },
{ availability: "present", size: 100, modifiedMs: 0 },
),
).toBe(false);
});
});
describe("syncLinkedFileFromDisk", () => {
it("ignores files with no disk link", async () => {
const result = await syncLinkedFileFromDisk(
stub({ localFilePath: undefined }),
);
expect(result.status).toBe("not-linked");
});
it("ignores every file when the platform has no disk linking", async () => {
diskState.supported = false;
const result = await syncLinkedFileFromDisk(stub());
expect(result.status).toBe("not-linked");
});
it("reports a deleted file as missing", async () => {
diskState.state = { availability: "gone" };
const result = await syncLinkedFileFromDisk(stub());
expect(result.status).toBe("missing");
});
it("reports a disk it could not look at as unavailable, not missing", async () => {
diskState.state = { availability: "unavailable", reason: "permission" };
const result = await syncLinkedFileFromDisk(stub());
expect(result.status).toBe("unavailable");
if (result.status !== "unavailable")
throw new Error("expected unavailable");
expect(result.reason).toBe("permission");
});
it("does not mistake an unreadable stat for a truncation", async () => {
diskState.state = { availability: "unavailable", reason: "offline" };
const result = await syncLinkedFileFromDisk(
stub({ diskSyncedSize: 100, diskSyncedModifiedMs: 5000 }),
);
expect(result.status).not.toBe("updated");
});
it("leaves an untouched file alone", async () => {
const result = await syncLinkedFileFromDisk(stub());
expect(result.status).toBe("unchanged");
});
it("reads the live bytes when the file was edited outside the app", async () => {
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
const result = await syncLinkedFileFromDisk(stub());
expect(result.status).toBe("updated");
if (result.status !== "updated") throw new Error("expected updated");
expect(result.file.name).toBe("report.pdf");
expect(await result.file.arrayBuffer()).toEqual(
new Uint8Array([1, 2, 3]).buffer,
);
expect(result.file.lastModified).toBe(9000);
});
it("keeps unsaved in-app edits instead of overwriting them from disk", async () => {
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
const result = await syncLinkedFileFromDisk(stub({ isDirty: true }));
expect(result.status).toBe("conflict");
});
it("keeps a recorded conflict after the live editor state is lost on reload", async () => {
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
const result = await syncLinkedFileFromDisk(
stub({ isDirty: false, diskConflictAt: 8000 }),
);
expect(result.status).toBe("conflict");
});
it("keeps a recorded conflict when disk metadata matches the saved baseline", async () => {
const result = await syncLinkedFileFromDisk(
stub({ isDirty: false, diskConflictAt: 8000 }),
);
expect(result.status).toBe("conflict");
});
it("keeps an open editor's edits, which no stub flag records", async () => {
// A page editor, annotation or redaction session is dirty long before any
// version exists to set isDirty, so disk must not simply win.
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
const result = await syncLinkedFileFromDisk(stub({ isDirty: false }), true);
expect(result.status).toBe("conflict");
});
it("never replaces a superseded version from disk", async () => {
// Its child was saved over the path, so disk reads as changed while these
// are the only bytes that version has.
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
const result = await syncLinkedFileFromDisk(
stub({ isLeaf: false, versionNumber: 1, diskConflictAt: 8000 }),
);
expect(result.status).toBe("superseded");
});
it("still reloads the leaf that does answer for the path", async () => {
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
const result = await syncLinkedFileFromDisk(
stub({ isLeaf: true, versionNumber: 2 }),
);
expect(result.status).toBe("updated");
});
it("treats a record predating the leaf flag as the live one", async () => {
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
const result = await syncLinkedFileFromDisk(
stub({ isLeaf: undefined as unknown as boolean }),
);
expect(result.status).toBe("updated");
});
it("keeps the stored copy when the disk file cannot be read", async () => {
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
diskState.bytes = null;
const result = await syncLinkedFileFromDisk(stub());
expect(result.status).toBe("unchanged");
});
it("re-reads a legacy record that has no baseline", async () => {
diskState.state = { availability: "present", size: 3, modifiedMs: 5000 };
const result = await syncLinkedFileFromDisk(
stub({ diskSyncedSize: undefined, diskSyncedModifiedMs: undefined }),
);
expect(result.status).toBe("updated");
});
it("refuses bytes that moved on disk while we were reading them", async () => {
// Another process was mid-write: committing this read would install a
// truncated PDF and then persist it over the cached copy.
const stat = vi.mocked(getDiskFileState);
stat.mockResolvedValueOnce({
availability: "present",
size: 200,
modifiedMs: 9000,
});
stat.mockResolvedValueOnce({
availability: "present",
size: 400,
modifiedMs: 9500,
});
const result = await syncLinkedFileFromDisk(stub());
expect(result.status).toBe("unchanged");
});
it("refuses a read shorter than the file it was stat'd against", async () => {
const stat = vi.mocked(getDiskFileState);
stat.mockResolvedValueOnce({
availability: "present",
size: 400,
modifiedMs: 9000,
});
stat.mockResolvedValueOnce({
availability: "present",
size: 400,
modifiedMs: 9000,
});
const result = await syncLinkedFileFromDisk(stub());
expect(result.status).toBe("unchanged");
});
});
describe("self-write muting", () => {
const path = "C:/docs/report.pdf";
const moved = () => stub({ diskSyncedSize: 1, diskSyncedModifiedMs: 1 });
it("does not report our own save as an external change", async () => {
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
beginSelfWrite(path);
const result = await syncLinkedFileFromDisk(moved());
expect(result.status).toBe("unchanged");
// Muted before the stat, so a half-written file is never even read.
expect(vi.mocked(getDiskFileState)).not.toHaveBeenCalled();
});
it("picks the file up again once the write is accounted for", async () => {
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
beginSelfWrite(path);
endSelfWrite(path);
const result = await syncLinkedFileFromDisk(moved());
expect(result.status).toBe("updated");
});
it("releases itself if the save never re-baselines", async () => {
vi.useFakeTimers();
try {
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
beginSelfWrite(path);
vi.advanceTimersByTime(11_000);
const result = await syncLinkedFileFromDisk(moved());
expect(result.status).toBe("updated");
} finally {
vi.useRealTimers();
}
});
});
describe("detachedFields", () => {
it("cuts the link but remembers where it pointed", () => {
const fields = detachedFields("C:/docs/gone.pdf");
expect(fields.localFilePath).toBeUndefined();
expect(fields.diskSyncedSize).toBeUndefined();
expect(fields.diskSyncedModifiedMs).toBeUndefined();
expect(fields.diskConflictAt).toBeUndefined();
// Kept so the badge can go on saying "not on disk" after the toast has gone.
expect(fields.orphanedFilePath).toBe("C:/docs/gone.pdf");
});
});
describe("loadDiskVersion", () => {
it("reads the disk copy so a conflict can be resolved towards it", async () => {
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
const loaded = await loadDiskVersion(stub({ isDirty: true }));
expect(loaded?.file.name).toBe("report.pdf");
expect(loaded?.state.modifiedMs).toBe(9000);
});
it("gives nothing when the disk file has since gone too", async () => {
diskState.state = { availability: "gone" };
expect(await loadDiskVersion(stub())).toBeNull();
});
});
describe("persistDiskUpdate", () => {
it("stores the new bytes, re-baselines, and drops the stale thumbnail", async () => {
const file = new File([new Uint8Array([1, 2, 3])], "report.pdf", {
type: "application/pdf",
lastModified: 9000,
});
await persistDiskUpdate(
"file-1" as FileId,
file,
{ availability: "present", size: 3, modifiedMs: 9000 },
12345,
);
expect(updateFileMetadata).toHaveBeenCalledWith(
"file-1",
expect.objectContaining({
data: file,
size: 3,
lastModified: 9000,
quickKey: "report.pdf|3|9000",
thumbnail: undefined,
thumbnailStoredAt: undefined,
diskSyncedSize: 3,
diskSyncedModifiedMs: 9000,
// What is on screen is now the disk version, so nothing is unsaved and
// any earlier divergence is settled.
isDirty: false,
diskConflictAt: undefined,
diskReloadedAt: 12345,
}),
);
});
});
describe("saveOrphanAsCopy", () => {
it("prompts for a location and re-links the file there", async () => {
diskState.state = { availability: "present", size: 42, modifiedMs: 8000 };
const saved = await saveOrphanAsCopy(
stub({ localFilePath: undefined, orphanedFilePath: "C:/docs/gone.pdf" }),
);
// No localPath passed, which is what forces the save dialog.
expect(downloadFileWithPolicy).toHaveBeenCalledWith(
expect.objectContaining({ filename: "report.pdf" }),
);
expect(downloadFileWithPolicy.mock.calls[0][0]).not.toHaveProperty(
"localPath",
);
expect(saved?.path).toBe("C:/elsewhere/report.pdf");
expect(saved?.updates).toMatchObject({
localFilePath: "C:/elsewhere/report.pdf",
orphanedFilePath: undefined,
isDirty: false,
diskSyncedSize: 42,
diskSyncedModifiedMs: 8000,
});
});
it("changes nothing when the user cancels the picker", async () => {
downloadFileWithPolicy.mockResolvedValue({ cancelled: true });
expect(await saveOrphanAsCopy(stub())).toBeNull();
expect(updateFileMetadata).not.toHaveBeenCalled();
});
});
describe("refreshDiskBaselineAfterSave", () => {
it("re-baselines against the file just written and clears the conflict", async () => {
diskState.state = { availability: "present", size: 512, modifiedMs: 7777 };
const baseline = await refreshDiskBaselineAfterSave(
"file-1" as FileId,
"C:/docs/report.pdf",
);
expect(baseline).toEqual({
diskSyncedSize: 512,
diskSyncedModifiedMs: 7777,
// Writing our version out settles the divergence one way.
diskConflictAt: undefined,
});
expect(updateFileMetadata).toHaveBeenCalledWith("file-1", baseline);
});
it("stamps nothing when the save did not land on disk", async () => {
diskState.state = { availability: "gone" };
const baseline = await refreshDiskBaselineAfterSave(
"file-1" as FileId,
"C:/docs/report.pdf",
);
expect(baseline).toBeNull();
expect(updateFileMetadata).not.toHaveBeenCalled();
});
it("does nothing off the desktop app", async () => {
diskState.supported = false;
expect(
await refreshDiskBaselineAfterSave("file-1" as FileId, "/x.pdf"),
).toBeNull();
});
it("releases the self-write mute, even when it stamps nothing", async () => {
diskState.state = { availability: "gone" };
beginSelfWrite("C:/docs/report.pdf");
await refreshDiskBaselineAfterSave(
"file-1" as FileId,
"C:/docs/report.pdf",
);
diskState.state = { availability: "present", size: 3, modifiedMs: 9000 };
const result = await syncLinkedFileFromDisk(
stub({ diskSyncedSize: 1, diskSyncedModifiedMs: 1 }),
);
expect(result.status).toBe("updated");
});
});
describe("user-facing notifications", () => {
// The toast module is imported lazily, so the alert lands a microtask later.
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
it("names the file that vanished", async () => {
notifyFileVanished("report.pdf");
await flush();
expect(alertMock).toHaveBeenCalledWith(
expect.objectContaining({
alertType: "warning",
body: expect.stringContaining("report.pdf"),
}),
);
});
it("tells the user an open file lost its original but is still here", async () => {
notifyOpenFileDeleted(["report.pdf"]);
await flush();
expect(alertMock).toHaveBeenCalledWith(
expect.objectContaining({
alertType: "warning",
body: expect.stringContaining("still open here"),
}),
);
expect(alertMock.mock.calls[0][0].body).toContain("report.pdf");
});
it("warns that saving will ask for a new location, and offers to do it", async () => {
const onSaveAs = vi.fn();
notifyOpenFileDeleted(["report.pdf"], onSaveAs);
await flush();
const toast = alertMock.mock.calls[0][0];
// Saying "save it" without saying where makes the file picker read as an error.
expect(toast.body).toContain("new location");
expect(toast.isPersistentPopup).toBe(true);
toast.buttonCallback();
expect(onSaveAs).toHaveBeenCalled();
});
it("counts them instead of listing when several are lost at once", async () => {
notifyOpenFileDeleted(["a.pdf", "b.pdf", "c.pdf"]);
await flush();
expect(alertMock.mock.calls[0][0].body).toContain("3 files");
});
it("offers no single-file action when several were lost", async () => {
notifyOpenFileDeleted(["a.pdf", "b.pdf"], vi.fn());
await flush();
expect(alertMock.mock.calls[0][0].buttonText).toBeUndefined();
});
it("leaves a trace when an external edit is picked up", async () => {
notifyDiskReloaded("report.pdf");
await flush();
// Neutral, not a warning: nothing went wrong, the user just needs to be able
// to tell whose version is on screen.
expect(alertMock).toHaveBeenCalledWith(
expect.objectContaining({
alertType: "neutral",
body: expect.stringContaining("report.pdf"),
}),
);
});
});