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.5 KB
Raw Blame
import { describe, expect, it } from "vitest";
import type { CaptureSourcesDto } from "../src/lib/bridge";
import {
  displayLayoutKey,
  fallbackSourceForTab,
  savedDisplayLayoutMatches,
  sourceMatchesTab,
} from "../src/lib/captureSelection";

const main = { id: 10, name: "Main", x: 0, y: 0, width: 1440, height: 900 };
const left = { id: 2, name: "Left", x: -1024.2, y: 0, width: 1024.2, height: 768 };
const editor = { id: 3, app_name: "Editor", title: "Notes", x: 30, y: 40, width: 800, height: 600 };
const sources: CaptureSourcesDto = {
  displays: [main, left],
  windows: [editor, { ...editor, id: 4, title: "Draft" }],
};

describe("capture source selection", () => {
  it("accepts a selection only for its own tab", () => {
    expect(sourceMatchesTab({ type: "window", id: 3 }, "window")).toBe(true);
    expect(sourceMatchesTab({ type: "display", id: 3 }, "window")).toBe(false);
    expect(sourceMatchesTab({ type: "camera", device_id: "front" }, "display")).toBe(false);
    expect(sourceMatchesTab(null, "area")).toBe(false);
  });

  it("falls back within the chosen tab and requires an explicit area", () => {
    expect(fallbackSourceForTab("display", sources)).toEqual({ type: "display", id: 10 });
    expect(fallbackSourceForTab("window", sources)).toEqual({ type: "window", id: 3 });
    expect(fallbackSourceForTab("area", sources)).toBeNull();
    expect(fallbackSourceForTab("window", { ...sources, windows: [] })).toBeNull();
    expect(fallbackSourceForTab("display", { ...sources, displays: [] })).toBeNull();
  });

  it("recognizes a saved layout across enumeration order and subpixel jitter", () => {
    const displays = [main, left];
    const saved = displayLayoutKey(displays);
    expect(saved).toBe("2@-1024,0,1024x768|10@0,0,1440x900");
    expect(displays).toEqual([main, left]);
    expect(savedDisplayLayoutMatches(saved, [{ ...left, x: -1024.4 }, main])).toBe(true);
  });

  it("invalidates a layout when a monitor moves, resizes or disappears", () => {
    const saved = displayLayoutKey([main, left]);
    expect(savedDisplayLayoutMatches(saved, [main, { ...left, x: 1440 }])).toBe(false);
    expect(savedDisplayLayoutMatches(saved, [main, { ...left, width: 1280 }])).toBe(false);
    expect(savedDisplayLayoutMatches(saved, [main])).toBe(false);
  });

  it("accepts settings saved before display layouts were recorded", () => {
    expect(savedDisplayLayoutMatches(undefined, [main])).toBe(true);
    expect(savedDisplayLayoutMatches(null, [main])).toBe(true);
  });
});