Skip to content

armgit / clipy

Public Imported from GitHub
0

The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.

1.5 KB
Raw Blame
import { afterEach, expect, it, vi } from "vitest";
import { useRows } from "../src/routes/Library";

const harness = vi.hoisted(() => ({
  effects: [] as Array<() => unknown>,
  setRows: vi.fn(),
  list: vi.fn(),
}));

vi.mock("react", async (load) => ({
  ...await load<typeof import("react")>(),
  useState: (initial: unknown) => [initial, harness.setRows],
  useEffect: (effect: () => unknown) => harness.effects.push(effect),
  useRef: (current: unknown) => ({ current }),
  useCallback: (fn: unknown) => fn,
}));

vi.mock("../src/lib/bridge", () => ({ listLocalLibrary: harness.list }));

afterEach(() => {
  vi.useRealTimers();
  vi.unstubAllGlobals();
});

it("lets a slow library scan finish before the next polling scan starts", async () => {
  vi.useFakeTimers();
  vi.stubGlobal("window", {
    setInterval,
    clearInterval,
    addEventListener: vi.fn(),
    removeEventListener: vi.fn(),
  });
  let finishScan!: (rows: unknown[]) => void;
  harness.list.mockReturnValue(new Promise<unknown[]>((resolve) => { finishScan = resolve; }));
  useRows();
  const dispose = harness.effects[0]() as () => void;

  try {
    await vi.advanceTimersByTimeAsync(5_000);
    expect(harness.list).toHaveBeenCalledTimes(1);

    const rows = [{ id: "first-recording" }];
    finishScan(rows);
    await vi.advanceTimersByTimeAsync(0);
    expect(harness.setRows).toHaveBeenCalledWith(rows);

    await vi.advanceTimersByTimeAsync(2_500);
    expect(harness.list).toHaveBeenCalledTimes(2);
  } finally {
    dispose();
  }
});