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.

2.1 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { PasteFallbackDto } from "../src/lib/bridge";
import PasteFallback from "../src/routes/PasteFallback";

const harness = vi.hoisted(() => ({
  effects: [] as Array<() => void | (() => void)>,
  setState: vi.fn(),
  get: vi.fn(),
  subscribe: vi.fn(),
}));

vi.mock("react", async (load) => ({
  ...await load<typeof import("react")>(),
  useState: (initial: unknown) => [initial, harness.setState],
  useEffect: (effect: () => void | (() => void)) => harness.effects.push(effect),
}));

vi.mock("../src/lib/bridge", () => ({
  getPasteFallback: harness.get,
  onPasteFallbackShow: harness.subscribe,
  closePasteFallback: vi.fn(),
  copyPasteFallback: vi.fn(),
}));
vi.mock("../src/lib/messages", () => ({ useDesktopT: () => (key: string) => key }));

beforeEach(() => {
  vi.clearAllMocks();
  harness.effects.length = 0;
});

const earlier: PasteFallbackDto = { kind: "dictate", reason: "appChanged", text: "First dictation" };
const later: PasteFallbackDto = { ...earlier, text: "Second dictation" };

function openWindow() {
  let finishRead!: (value: PasteFallbackDto) => void;
  harness.get.mockReturnValueOnce(new Promise<PasteFallbackDto>((resolve) => {
    finishRead = resolve;
  }));
  harness.subscribe.mockResolvedValueOnce(() => {});
  PasteFallback();
  const dispose = harness.effects[0]();
  return {
    show: harness.subscribe.mock.calls[0][0] as (event: PasteFallbackDto) => void,
    load: async () => {
      finishRead(earlier);
      await Promise.resolve();
    },
    dispose,
  };
}

describe("paste fallback startup", () => {
  it("keeps a newer live dictation when the initial read resolves late", async () => {
    const win = openWindow();
    win.show(later);
    await win.load();
    expect(harness.setState.mock.calls).toEqual([[later], [false]]);
    win.dispose?.();
  });

  it("loads the initial dictation if there was no new event", async () => {
    const win = openWindow();
    await win.load();
    expect(harness.setState.mock.calls).toEqual([[earlier]]);
    win.dispose?.();
  });
});