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.

3.7 KB
Raw Blame
import assert from "node:assert/strict";
import * as moduleApi from "node:module";
import { test } from "node:test";

// Stub ./messages so clipKind.ts can be imported directly in Node without node_modules.
const messagesStubUrl = "data:text/javascript,export const englishT = (k) => k;";
if (typeof moduleApi.registerHooks === "function") {
  moduleApi.registerHooks({
    resolve(specifier, context, nextResolve) {
      if (specifier === "./messages") {
        return { shortCircuit: true, url: messagesStubUrl };
      }
      return nextResolve(specifier, context);
    },
  });
} else {
  const stubLoader = `
  export function resolve(specifier, context, nextResolve) {
    if (specifier === "./messages") {
      return { shortCircuit: true, url: ${JSON.stringify(messagesStubUrl)} };
    }
    return nextResolve(specifier, context);
  }
  `;
  moduleApi.register(`data:text/javascript,${encodeURIComponent(stubLoader)}`);
}

const { clipKind, clipSwatch } = await import("../src/lib/clipKind.ts");

const entry = (text) => ({ text, image: null });

test("valid hex colors return color", () => {
  for (const text of ["#fff", "#ffff", "#112233", "#11223344", "  #AbC  "]) {
    assert.equal(clipKind(entry(text)), "color", text);
    assert.equal(clipSwatch(entry(text)), text.trim(), text);
  }
});

test("valid legacy rgb/rgba and hsl/hsla colors return color", () => {
  const valid = [
    "rgb(255, 0, 128)",
    "rgb(100%, 0%, 50%)",
    "rgba(255, 0, 128, 0.5)",
    "rgba(100%, 0%, 50%, 50%)",
    "hsl(120, 50%, 50%)",
    "hsl(120deg, 50%, 50%)",
    "hsla(240, 100%, 50%, 0.25)",
    "RGB( 0 , 128 , 255 )",
  ];
  for (const text of valid) {
    assert.equal(clipKind(entry(text)), "color", text);
    assert.equal(clipSwatch(entry(text)), text, text);
  }
});

test("valid modern CSS Color 4 formats return color", () => {
  const valid = [
    "rgb(255 0 128)",
    "rgb(255 0 128 / 0.5)",
    "rgba(100% 0% 50%/50%)",
    "rgb(255 none 0 / none)",
    "hsl(120deg 50% 50%)",
    "hsl(0.5turn 50% 50% / 80%)",
    "hsla(2rad 60% 40% / 0.4)",
    "hsl(120 50 50)",
    "hwb(120 10% 20%)",
    "hwb(120deg 10% 20% / 0.5)",
    "lab(50% 40 59.5)",
    "lab(50 -40 59.5 / 0.8)",
    "lch(52.2% 72.2 50)",
    "lch(52.2 72.2 50deg / 90%)",
    "oklab(40.1% 0.114 0.046)",
    "oklch(0.637 0.237 25.331)",
    "oklch(63.7% 0.237 25.331deg / 0.5)",
    "color(display-p3 1 0.5 0)",
    "color(srgb 0.2 0.4 0.6 / 0.8)",
    "color(srgb-linear 0.1 0.2 0.3)",
    "color(a98-rgb 0.5 0.5 0.5)",
    "color(prophoto-rgb 0.5 0.5 0.5)",
    "color(rec2020 0.5 0.5 0.5)",
    "color(xyz 0.5 0.5 0.5)",
    "color(xyz-d50 0.5 0.5 0.5)",
    "Color(XYZ-D65 0.5 0.5 0.5 / none)",
  ];
  for (const text of valid) {
    assert.equal(clipKind(entry(text)), "color", text);
    assert.equal(clipSwatch(entry(text)), text, text);
  }
});

test("invalid CSS color strings return text and null swatch", () => {
  const invalid = [
    "rgb(1,2)",
    "rgb(1, 2, 3, 4, 5)",
    "rgb(1, 2, )",
    "rgb(255, 50%, 0)",
    "rgb(255, 0, 0 / 0.5)",
    "rgb(1 2)",
    "rgb(1 2 3 4)",
    "rgb(1 2 3 /)",
    "rgb(1 2 3 / 0.5 / 0.5)",
    "rgb (255, 0, 0)",
    "hsl(120, 50, 50)",
    "hsl(50% 50% 50%)",
    "hwb(120, 10%, 20%)",
    "lab(50%, 40, 20)",
    "lch(50%, 40, 20)",
    "oklch(0.637, 0.237, 25.331)",
    "oklch(0.637 0.237 50%)",
    "color(unknown-space 1 0.5 0)",
    "color(display-p3 1 0.5)",
    "color(display-p3 1 0.5 0 1)",
    "notacolor(1, 2, 3)",
    "#12345",
    "red",
  ];
  for (const text of invalid) {
    assert.equal(clipKind(entry(text)), "text", text);
    assert.equal(clipSwatch(entry(text)), null, text);
  }
  assert.equal(clipKind(entry("rgb()")), "code");
  assert.equal(clipSwatch(entry("rgb()")), null);
});