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.

5.5 KB
Raw Blame
import assert from "node:assert/strict";
import { after, before, test } from "node:test";
import { fileURLToPath } from "node:url";
import { createElement as h } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { IntlProvider } from "use-intl";
import { createServer } from "vite";

let server;
let ui;
let englishT;
before(async () => {
  server = await createServer({
    root: fileURLToPath(new URL("..", import.meta.url)),
    configFile: false,
    optimizeDeps: { noDiscovery: true },
    server: { middlewareMode: true, hmr: false, watch: null },
  });
  ui = await server.ssrLoadModule("/src/components/recorder/ui.tsx");
  ({ englishT } = await server.ssrLoadModule("/src/lib/messages.ts"));
});
after(() => server.close());

test("the picker exposes its name, value, disabled options and row description", () => {
  const title = englishT("local.preferences.model.title");
  const subtitle = englishT("local.preferences.model.subtitle");
  const rules = englishT("local.preferences.providers.none");
  const custom = englishT("local.preferences.providers.custom");
  const markup = renderToStaticMarkup(h(ui.SettingRow, {
    title, subtitle,
    trailing: h(ui.InlineSelect, {
      ariaLabel: title, value: "none", onChange() {},
      items: [{ value: "none", label: rules }, { value: "custom", label: custom, disabled: true }],
    }),
  }));
  const select = markup.match(/<select\b[^>]*>/)?.[0];
  assert.ok(select, "A native picker supplies keyboard navigation and the selected value");
  assert.ok(select.includes(`aria-label="${title}"`));
  const help = select.match(/aria-describedby="([^"]+)"/)?.[1];
  assert.ok(help && markup.includes(`id="${help}"`));
  const selected = markup.match(/<option(?=[^>]*selected)(?=[^>]*value="none")[^>]*>([^<]*)<\/option>/);
  const disabled = markup.match(/<option(?=[^>]*disabled)(?=[^>]*value="custom")[^>]*>([^<]*)<\/option>/);
  assert.equal(selected?.[1], rules);
  assert.equal(disabled?.[1], custom);
});

test("a missing saved value still lets the user pick the first available item", () => {
  const markup = renderToStaticMarkup(h(ui.InlineSelect, {
    ariaLabel: englishT("settings.camera.title"), value: "disconnected", onChange() {},
    items: [{ value: "", label: englishT("settings.systemDefault") }],
  }));
  assert.match(markup, /<option(?=[^>]*hidden)(?=[^>]*selected)(?=[^>]*value="disconnected")[^>]*>/);
  assert.ok(markup.includes(`<option value="">${englishT("settings.systemDefault")}</option>`));
});

test("switches keep their checked state and notices expose changes", () => {
  const markup = renderToStaticMarkup(h(ui.SettingRow, {
    title: englishT("settings.recordingControls.title"),
    subtitle: englishT("settings.recordingControls.subtitle"),
    trailing: h(ui.Switch, { label: englishT("settings.recordingControls.title"), checked: true, onChange() {} }),
  }));
  assert.match(markup, /role="group"/);
  assert.match(markup, /<button(?=[^>]*role="switch")(?=[^>]*aria-checked="true")(?=[^>]*aria-describedby=)[^>]*>/);
  for (const [tone, role] of [["info", "status"], ["error", "alert"]]) {
    assert.ok(renderToStaticMarkup(h(ui.Notice, { tone }, "Changed")).includes(`role="${role}"`));
  }
});

test("the compact Settings tablist names and links its single tab stop", () => {
  const title = englishT("settings.title");
  const tabs = ["enhance", "video", "preferences", "shortcuts"].map(
    (value) => ({ value, label: englishT(`settings.tabs.${value}`) }),
  );
  const markup = renderToStaticMarkup(h(ui.Tabs, {
    tabs, label: title, panelId: "panel", value: "preferences", onChange() {},
  }));
  assert.ok(markup.includes(`role="tablist" aria-label="${title}"`));
  // The compact layout (four or more tabs); its gap is a styling choice.
  assert.match(markup, /justify-between gap-\d/);
  const buttons = markup.match(/<button\b[^>]*>/g);
  assert.equal(buttons.length, 4);
  assert.ok(buttons.every((button) => button.includes('aria-controls="panel"')));
  assert.equal(buttons.filter((button) => button.includes('tabindex="-1"')).length, 3);
  assert.match(buttons[2], /id="panel-preferences".*tabindex="0".*aria-selected="true"/);
});

test("tab keys follow the list's orientation and wrap", () => {
  const keys = ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", "Home", "End", "a"];
  const targets = (i, orientation) => keys.map((key) => ui.tabKeyTarget(key, i, 5, orientation));
  assert.deepEqual(targets(0, "horizontal"), [4, 1, null, null, 0, 4, null]);
  assert.deepEqual(targets(4, "vertical"), [null, null, 3, 0, 0, 4, null]);
});

test("the Settings window renders the linked tabs without a dialog", async () => {
  const { SettingsSheet } = await server.ssrLoadModule("/src/components/recorder/SettingsSheet.tsx");
  const english = (await server.ssrLoadModule("/src/vendor/i18n/messages/en/desktop.json")).default;
  const render = (layout) => renderToStaticMarkup(h(IntlProvider, { locale: "en", timeZone: "UTC", messages: { desktop: english } },
    h(SettingsSheet, { layout, tab: "shortcuts", onBack() {}, user: null, version: null })));
  const windowMarkup = render("window");
  assert.ok(!windowMarkup.includes("<dialog"));
  assert.match(windowMarkup, /role="tablist" aria-label="[^"]+" aria-orientation="vertical"/);
  const panel = windowMarkup.match(/<div id="([^"]+)" role="tabpanel" aria-labelledby="([^"]+)"/);
  assert.ok(panel && windowMarkup.includes(`id="${panel[2]}" aria-controls="${panel[1]}" tabindex="0"`));
  // There is no sheet layout any more: Settings always renders the window.
  assert.ok(!render("sheet").includes("<dialog"));
});