Skip to content

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

21.7 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";

// Pins the sweep's contract: browser-only classification, results written locked so the
// classification demo's files never reach the AI, and the batch accounting follow-ups depend on.

const classifyFileHeuristically = vi.fn();
const meterAutomationRun = vi.fn();
const listDirectory = vi.fn();
const readDiskFile = vi.fn();

vi.mock("@editor/services/serverAutomationSession", () => ({
  getServerAutomationSession: vi
    .fn()
    .mockResolvedValue({ key: "session", baseUrl: "https://server.test" }),
  requireAutomationSession: vi.fn().mockResolvedValue(undefined),
}));

vi.mock("@editor/services/localFolderContents", () => ({
  canListDirectory: true,
  listDirectory: (...args: unknown[]) => listDirectory(...args),
  readDiskFile: (...args: unknown[]) => readDiskFile(...args),
}));
vi.mock("@editor/services/heuristic/heuristicClassification", () => ({
  classifyFileHeuristically: (...args: unknown[]) =>
    classifyFileHeuristically(...args),
}));
vi.mock("@editor/services/automationMeter", () => ({
  meterAutomationRun: (...args: unknown[]) => meterAutomationRun(...args),
}));
vi.mock("@editor/services/pdfWorkerManager", () => ({
  pdfWorkerManager: {
    getWorkerStats: () => ({ active: 10, max: 10, total: 10 }),
  },
}));
import { requireAutomationSession } from "@editor/services/serverAutomationSession";
import { LARGE_PDF_PARSE_LIMIT } from "@editor/utils/thumbnailUtils";
import {
  DOCUMENT_DEADLINE_MS,
  HEURISTIC_BUDGET_MS,
  MAX_CONSECUTIVE_STALLS,
  SESSION_CHECK_DEADLINE_MS,
  mergeOutcomes,
  pickRecentPdfs,
  runClassificationDemoSweep,
  type ClassificationDemoDeps,
  type ClassificationDemoOutcome,
} from "@editor/components/onboarding/classificationDemo/classificationDemoSweep";

function entry(name: string, lastModified: number) {
  return { path: `/downloads/${name}`, name, sizeBytes: 1, lastModified };
}

let addedFileId = 0;

function deps(
  overrides: Partial<ClassificationDemoDeps> = {},
): ClassificationDemoDeps {
  return {
    mountFolder: vi.fn().mockResolvedValue(undefined),
    addFiles: vi
      .fn()
      .mockImplementation(() =>
        Promise.resolve([{ fileId: `file-${++addedFileId}` }]),
      ),
    onProgress: vi.fn(),
    ...overrides,
  };
}

beforeEach(() => {
  vi.clearAllMocks();
  // Ids restart per test, so assertions can name the file they expect.
  addedFileId = 0;
  listDirectory.mockResolvedValue({
    files: [entry("a.pdf", 3), entry("notes.txt", 9), entry("b.pdf", 1)],
    directories: [],
  });
  readDiskFile.mockImplementation((e: { name: string }) =>
    Promise.resolve(new File(["x"], e.name, { type: "application/pdf" })),
  );
  classifyFileHeuristically.mockResolvedValue({
    labels: ["invoice"],
    confidence: "low",
    score: 10,
    isEnglish: true,
  });
});

describe("pickRecentPdfs", () => {
  test("keeps PDFs only, newest first, capped at the limit", () => {
    const picked = pickRecentPdfs(
      [entry("old.pdf", 1), entry("skip.txt", 99), entry("new.pdf", 5)],
      1,
    );
    expect(picked.map((f) => f.name)).toEqual(["new.pdf"]);
  });
});

describe("runClassificationDemoSweep", () => {
  test("writes each document to storage with its verdict already on the stub", async () => {
    // A "low" verdict is exactly what the local pass would escalate to the AI engine.
    // Stamping the labels at birth is what stops it claiming the file at all.
    const d = deps();
    const outcome = await runClassificationDemoSweep("/downloads", d);

    expect(classifyFileHeuristically).toHaveBeenCalledTimes(2);
    expect(d.addFiles).toHaveBeenCalledTimes(2);
    expect(d.addFiles).toHaveBeenCalledWith(
      [expect.any(File)],
      expect.objectContaining({
        // Locked, not merely labelled: the lock is what the policy engine checks.
        presetClassification: { labels: ["invoice"], confidence: "low" },
        // Straight to storage: the library groups the document without it becoming an
        // open file, so nothing is selected and the user's workspace is left alone.
        skipWorkspaceDispatch: true,
        selectFiles: false,
        // Already parsed once by the heuristic; no second parse, no File kept in memory.
        skipMetadataHydration: true,
      }),
    );
    expect(outcome.processed).toBe(2);
    expect(outcome.groups).toEqual([
      {
        id: "finance",
        name: "Financial",
        count: 2,
        labels: [{ id: "invoice", name: "Invoice", count: 2 }],
      },
    ]);
  });

  test("meters the batch once, for the documents it actually classified", async () => {
    await runClassificationDemoSweep("/downloads", deps());

    expect(meterAutomationRun).toHaveBeenCalledTimes(1);
    expect(meterAutomationRun).toHaveBeenCalledWith(
      expect.objectContaining({
        inputs: [
          { pages: 0, bytes: 1 },
          { pages: 0, bytes: 1 },
        ],
      }),
      { key: "session", baseUrl: "https://server.test" },
    );
  });

  test("checks the session once per batch, just before metering", async () => {
    await runClassificationDemoSweep("/downloads", deps());

    expect(requireAutomationSession).toHaveBeenCalledTimes(1);
    expect(requireAutomationSession).toHaveBeenCalledWith("session");
  });

  test("fails the sweep unbilled when the session changed during it", async () => {
    vi.mocked(requireAutomationSession).mockRejectedValueOnce(
      new Error("The server connection changed."),
    );

    await expect(
      runClassificationDemoSweep("/downloads", deps()),
    ).rejects.toThrow("connection changed");
    expect(classifyFileHeuristically).toHaveBeenCalledTimes(2);
    expect(meterAutomationRun).not.toHaveBeenCalled();
  });

  test("meters nothing when no document could be classified", async () => {
    classifyFileHeuristically.mockRejectedValue(new Error("encrypted"));

    const outcome = await runClassificationDemoSweep("/downloads", deps());

    expect(outcome.processed).toBe(0);
    expect(meterAutomationRun).not.toHaveBeenCalled();
  });

  test("skips an unreadable document but still retires it", async () => {
    classifyFileHeuristically.mockRejectedValueOnce(new Error("encrypted"));

    const outcome = await runClassificationDemoSweep("/downloads", deps());

    expect(outcome.processed).toBe(1);
    expect(outcome.sweptPaths).toHaveLength(2);
  });

  test("reports what the sweep left behind so a follow-up batch can be offered", async () => {
    const outcome = await runClassificationDemoSweep("/downloads", deps(), {
      limit: 1,
    });

    expect(outcome.processed).toBe(1);
    expect(outcome.pdfsInFolder).toBe(2);
    expect(outcome.remaining).toBe(1);
  });

  test("resumes after the documents a previous sweep already covered", async () => {
    const d = deps();
    await runClassificationDemoSweep("/downloads", d, {
      limit: 1,
      exclude: new Set(["/downloads/a.pdf"]),
    });

    expect(readDiskFile).toHaveBeenCalledTimes(1);
    expect(readDiskFile).toHaveBeenCalledWith(
      expect.objectContaining({ name: "b.pdf" }),
    );
  });

  test("retires a document it could not classify instead of re-offering it forever", async () => {
    // Regression: resuming from the count of SUCCESSES pulled each follow-up back over
    // covered ground and stranded as many at the end ("19 more", then "4 more").
    classifyFileHeuristically.mockRejectedValueOnce(new Error("encrypted"));

    const first = await runClassificationDemoSweep("/downloads", deps(), {
      limit: 1,
    });
    expect(first.processed).toBe(0);
    expect(first.sweptPaths).toEqual(["/downloads/a.pdf"]);
    expect(first.remaining).toBe(1);

    const second = await runClassificationDemoSweep("/downloads", deps(), {
      exclude: new Set(first.sweptPaths),
    });
    expect(second.sweptPaths).toEqual(["/downloads/b.pdf"]);
    expect(second.remaining).toBe(0);
  });

  test("counts a document the sweep took on even when it is cancelled part-way", async () => {
    let seen = 0;
    const d = deps({ isCancelled: () => seen++ > 0 });

    const outcome = await runClassificationDemoSweep("/downloads", d);

    // The first document was swept; the second was never started, so it stays eligible.
    expect(outcome.sweptPaths).toEqual(["/downloads/a.pdf"]);
    expect(outcome.remaining).toBe(1);
  });

  test("reports how far the folder listing has got, so the view has something true to say", async () => {
    listDirectory.mockImplementation(
      async (
        _dir: string,
        options?: { onProgress?: (c: number, t: number) => void },
      ) => {
        options?.onProgress?.(32, 70);
        options?.onProgress?.(64, 70);
        return { files: [entry("a.pdf", 1)], directories: [] };
      },
    );
    const d = deps();
    await runClassificationDemoSweep("/downloads", d);

    expect(d.onProgress).toHaveBeenCalledWith(
      expect.objectContaining({
        phase: "gathering",
        total: 0,
        listing: { checked: 32, total: 70 },
      }),
    );
    expect(d.onProgress).toHaveBeenCalledWith(
      expect.objectContaining({ listing: { checked: 64, total: 70 } }),
    );
  });

  test("gives each document a time budget, so one cannot hold the sweep", async () => {
    const d = deps();
    await runClassificationDemoSweep("/downloads", d);
    expect(classifyFileHeuristically).toHaveBeenCalledWith(expect.any(File), {
      budgetMs: HEURISTIC_BUDGET_MS,
    });
  });

  describe("a document that runs out of time", () => {
    const never = <T>() => new Promise<T>(() => {});
    const after = <T>(ms: number, value: T) =>
      new Promise<T>((resolve) => setTimeout(() => resolve(value), ms));

    beforeEach(() => {
      vi.useFakeTimers();
      vi.spyOn(console, "warn").mockImplementation(() => {});
    });
    afterEach(() => {
      vi.useRealTimers();
      vi.mocked(console.warn).mockRestore();
    });

    async function settle<T>(run: Promise<T>): Promise<T> {
      const outcome = run.then(
        (value) => ({ value }),
        (error: unknown) => ({ error }),
      );
      await vi.advanceTimersByTimeAsync(SESSION_CHECK_DEADLINE_MS * 2);
      const result = await outcome;
      if ("error" in result) throw result.error;
      return result.value;
    }

    test("retires a document whose read stalls and carries on", async () => {
      readDiskFile.mockImplementationOnce(() => never());
      const outcome = await settle(
        runClassificationDemoSweep("/downloads", deps()),
      );

      expect(outcome.processed).toBe(1);
      expect(outcome.sweptPaths).toEqual([
        "/downloads/a.pdf",
        "/downloads/b.pdf",
      ]);
    });

    test("retires a document whose classification stalls and carries on", async () => {
      classifyFileHeuristically.mockImplementationOnce(() => never());
      const outcome = await settle(
        runClassificationDemoSweep("/downloads", deps()),
      );

      expect(outcome.processed).toBe(1);
    });

    test("still counts a document whose save stalls: the verdict stands", async () => {
      const addFiles = vi
        .fn()
        .mockImplementationOnce(() => never())
        .mockResolvedValue([]);
      const outcome = await settle(
        runClassificationDemoSweep("/downloads", deps({ addFiles })),
      );

      expect(outcome.processed).toBe(2);
    });

    test("shares one deadline across a document's steps", async () => {
      // Each step is inside the deadline on its own; together they are over it.
      readDiskFile.mockImplementationOnce((e: { name: string }) =>
        after(
          DOCUMENT_DEADLINE_MS * 0.6,
          new File(["x"], e.name, { type: "application/pdf" }),
        ),
      );
      classifyFileHeuristically.mockImplementationOnce(() =>
        after(DOCUMENT_DEADLINE_MS * 0.6, {
          labels: ["invoice"],
          confidence: "low",
          score: 10,
          isEnglish: true,
        }),
      );
      const outcome = await settle(
        runClassificationDemoSweep("/downloads", deps()),
      );

      expect(outcome.processed).toBe(1);
    });

    test("stops the sweep after a run of stalls, keeping what it classified", async () => {
      const names = [
        "ok.pdf",
        ...Array.from({ length: 5 }, (_, i) => `s${i}.pdf`),
      ];
      listDirectory.mockResolvedValue({
        files: names.map((name, i) => entry(name, 100 - i)),
        directories: [],
      });
      classifyFileHeuristically
        .mockResolvedValueOnce({
          labels: ["invoice"],
          confidence: "low",
          score: 10,
          isEnglish: true,
        })
        .mockImplementation(() => never());
      const outcome = await settle(
        runClassificationDemoSweep("/downloads", deps()),
      );

      expect(classifyFileHeuristically).toHaveBeenCalledTimes(
        1 + MAX_CONSECUTIVE_STALLS,
      );
      expect(outcome.processed).toBe(1);
      expect(outcome.remaining).toBe(names.length - 1 - MAX_CONSECUTIVE_STALLS);
      expect(meterAutomationRun).toHaveBeenCalledWith(
        expect.objectContaining({ inputs: [{ pages: 0, bytes: 1 }] }),
        expect.anything(),
      );
    });

    test("names the step that stalled, so a hang says where it happened", async () => {
      classifyFileHeuristically.mockImplementationOnce(() => never());
      await settle(runClassificationDemoSweep("/downloads", deps()));

      expect(console.warn).toHaveBeenCalledWith(
        expect.stringContaining("Classifying for a.pdf"),
        { pdfWorkers: { active: 10, max: 10, total: 10 } },
      );
    });

    test("waits out a slow session check longer than a document gets", async () => {
      vi.mocked(requireAutomationSession).mockImplementationOnce(() =>
        after(DOCUMENT_DEADLINE_MS * 2, undefined),
      );
      await settle(runClassificationDemoSweep("/downloads", deps()));

      expect(meterAutomationRun).toHaveBeenCalledTimes(1);
    });

    test("fails the sweep unbilled when the session cannot be confirmed", async () => {
      vi.mocked(requireAutomationSession).mockImplementationOnce(() => never());

      await expect(
        settle(runClassificationDemoSweep("/downloads", deps())),
      ).rejects.toThrow("could not be confirmed");
      expect(meterAutomationRun).not.toHaveBeenCalled();
    });
  });

  test("retires a document over the parse limit without reading it", async () => {
    listDirectory.mockResolvedValue({
      files: [
        { ...entry("huge.pdf", 9), sizeBytes: LARGE_PDF_PARSE_LIMIT },
        entry("small.pdf", 1),
      ],
      directories: [],
    });
    const outcome = await runClassificationDemoSweep("/downloads", deps());

    // Never copied across the IPC bridge, never classified, but not offered again.
    expect(readDiskFile).toHaveBeenCalledTimes(1);
    expect(readDiskFile).toHaveBeenCalledWith(
      expect.objectContaining({ name: "small.pdf" }),
    );
    expect(outcome.processed).toBe(1);
    expect(outcome.sweptPaths).toEqual([
      "/downloads/huge.pdf",
      "/downloads/small.pdf",
    ]);
  });

  test("reads the next document while the current one is being classified", async () => {
    let finishFirst!: (verdict: unknown) => void;
    classifyFileHeuristically.mockReturnValueOnce(
      new Promise((resolve) => {
        finishFirst = resolve;
      }),
    );
    const run = runClassificationDemoSweep("/downloads", deps());

    // The first classification is still pending, and the second read has started.
    await vi.waitFor(() => expect(readDiskFile).toHaveBeenCalledTimes(2));
    expect(classifyFileHeuristically).toHaveBeenCalledTimes(1);

    finishFirst({ labels: ["invoice"], confidence: "low", score: 10 });
    const outcome = await run;
    expect(outcome.processed).toBe(2);
    // Read-ahead never over-reads: two PDFs, two reads.
    expect(readDiskFile).toHaveBeenCalledTimes(2);
  });

  test("stops between files once cancelled", async () => {
    const outcome = await runClassificationDemoSweep(
      "/downloads",
      deps({ isCancelled: () => true }),
    );

    expect(classifyFileHeuristically).not.toHaveBeenCalled();
    expect(outcome.processed).toBe(0);
  });

  test("rolls unlabelled documents up under Other", async () => {
    classifyFileHeuristically.mockResolvedValue({
      labels: [],
      confidence: "none",
      score: 0,
      isEnglish: false,
    });

    const outcome = await runClassificationDemoSweep("/downloads", deps(), {
      unclassifiedName: "Other",
    });

    // Nothing matched, so there is no document type to break the slice down into.
    expect(outcome.groups).toEqual([
      { id: "other", name: "Other", count: 2, labels: [] },
    ]);
  });

  test("keeps the document types behind each roll-up, biggest first", async () => {
    // The heuristic names a real label, not just a family, so the results chart can
    // drill into "Financial" and show the Invoice/Receipt split behind it.
    const verdict = (label: string) => ({
      labels: [label],
      confidence: "high",
      score: 9,
      isEnglish: true,
    });
    listDirectory.mockResolvedValue({
      files: [entry("a.pdf", 3), entry("b.pdf", 2), entry("c.pdf", 1)],
      directories: [],
    });
    classifyFileHeuristically
      .mockResolvedValueOnce(verdict("receipt"))
      .mockResolvedValueOnce(verdict("invoice"))
      .mockResolvedValueOnce(verdict("invoice"));

    const outcome = await runClassificationDemoSweep("/downloads", deps());

    const finance = outcome.groups.find((g) => g.id === "finance");
    expect(finance?.count).toBe(3);
    expect(finance?.labels).toEqual([
      { id: "invoice", name: "Invoice", count: 2 },
      { id: "receipt", name: "Receipt", count: 1 },
    ]);
  });
});

describe("mergeOutcomes", () => {
  const outcome = (
    partial: Partial<ClassificationDemoOutcome>,
  ): ClassificationDemoOutcome => ({
    processed: 0,
    groups: [],
    pdfsInFolder: 0,
    remaining: 0,
    sweptPaths: [],
    ...partial,
  });

  test("adds a follow-up batch to the results already on screen", () => {
    // "Process the rest" continues the same pile: showing only the last batch made a
    // final run of one document look like the whole sweep.
    const merged = mergeOutcomes(
      outcome({
        processed: 47,
        groups: [
          {
            id: "finance",
            name: "Financial",
            count: 47,
            labels: [{ id: "invoice", name: "Invoice", count: 47 }],
          },
        ],
        sweptPaths: ["/a.pdf"],
      }),
      outcome({
        processed: 1,
        groups: [
          {
            id: "legal",
            name: "Legal",
            count: 1,
            labels: [{ id: "contract", name: "Contract", count: 1 }],
          },
        ],
        sweptPaths: ["/b.pdf"],
      }),
    );

    expect(merged.processed).toBe(48);
    expect(merged.groups.map((g) => [g.id, g.count])).toEqual([
      ["finance", 47],
      ["legal", 1],
    ]);
    expect(merged.sweptPaths).toEqual(["/a.pdf", "/b.pdf"]);
  });

  test("combines counts for a type both batches found", () => {
    const one = {
      id: "finance",
      name: "Financial",
      count: 2,
      labels: [{ id: "invoice", name: "Invoice", count: 2 }],
    };
    const merged = mergeOutcomes(
      outcome({ processed: 2, groups: [one] }),
      outcome({
        processed: 3,
        groups: [
          {
            ...one,
            count: 3,
            labels: [
              { id: "invoice", name: "Invoice", count: 1 },
              { id: "receipt", name: "Receipt", count: 2 },
            ],
          },
        ],
      }),
    );

    expect(merged.groups).toHaveLength(1);
    expect(merged.groups[0].count).toBe(5);
    // Biggest type first, so the breakdown stays ordered after the fold.
    expect(merged.groups[0].labels).toEqual([
      { id: "invoice", name: "Invoice", count: 3 },
      { id: "receipt", name: "Receipt", count: 2 },
    ]);
  });

  test("takes what is left from the newer sweep, which re-listed the folder", () => {
    const merged = mergeOutcomes(
      outcome({ pdfsInFolder: 60, remaining: 10 }),
      outcome({ pdfsInFolder: 62, remaining: 0 }),
    );

    expect(merged.pdfsInFolder).toBe(62);
    expect(merged.remaining).toBe(0);
  });

  test("does not mutate either input", () => {
    const previous = outcome({
      processed: 1,
      groups: [
        {
          id: "finance",
          name: "Financial",
          count: 1,
          labels: [{ id: "invoice", name: "Invoice", count: 1 }],
        },
      ],
    });
    mergeOutcomes(previous, previous);

    expect(previous.processed).toBe(1);
    expect(previous.groups[0].count).toBe(1);
    expect(previous.groups[0].labels[0].count).toBe(1);
  });
});

describe("settling swept documents locally", () => {
  test("writes every verdict locked, whatever the confidence", async () => {
    // A low-confidence verdict is exactly what the policy path would escalate, so the
    // lock has to be unconditional rather than a property of confident results.
    classifyFileHeuristically.mockResolvedValue({
      labels: ["invoice"],
      confidence: "none",
      score: 0,
      isEnglish: true,
    });
    const d = deps();

    await runClassificationDemoSweep("/downloads", d);

    for (const call of vi.mocked(d.addFiles).mock.calls) {
      expect(call[1]?.presetClassification).toBeDefined();
    }
  });

  test("adds nothing for a document that could not be classified", async () => {
    classifyFileHeuristically.mockRejectedValue(new Error("encrypted"));

    const d = deps();
    await runClassificationDemoSweep("/downloads", d);

    expect(d.addFiles).not.toHaveBeenCalled();
  });

  test("meters under its own name, not the Classification policy's", async () => {
    // Same billing today, separate identity, so onboarding can be repriced alone.
    await runClassificationDemoSweep("/downloads", deps());

    expect(meterAutomationRun).toHaveBeenCalledWith(
      expect.objectContaining({ automationName: "Onboarding classification" }),
      { key: "session", baseUrl: "https://server.test" },
    );
  });
});