Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

9.5 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import assert from "node:assert/strict";
import { register } from "node:module";
import test from "node:test";

// api-base derives isTauri at module evaluation; the resolver busts the import chain per case
// and stubs the Tauri clipboard plugin.
register("./helpers/clipboard-resolver.mjs", import.meta.url);

// Must be a file:// URL: Windows native paths fail with ERR_UNSUPPORTED_ESM_URL_SCHEME.
const MODULE = new URL("../src/lib/copy-to-clipboard.ts", import.meta.url).href;

type StubMode = "ok" | "write-fails" | "module-missing";

type Recorder = {
  webWrites: string[];
  execCommands: string[];
  appended: number;
  removed: number;
  nativeWrites: string[];
  clipboardData: string[];
  copyHandling: string[];
  copyListeners: number;
};

type EnvOptions = {
  tauri: boolean;
  stub?: StubMode;
  clipboard?: "ok" | "reject" | "absent";
  execCommandResult?: boolean;
  copyEvent?: boolean;
};

let generation = 0;

function define(name: string, value: unknown) {
  Object.defineProperty(globalThis, name, {
    value,
    configurable: true,
    writable: true,
  });
}

async function load(options: EnvOptions) {
  const {
    tauri,
    stub = "ok",
    clipboard = "ok",
    execCommandResult = true,
    copyEvent = true,
  } = options;

  const recorder: Recorder = {
    webWrites: [],
    execCommands: [],
    appended: 0,
    removed: 0,
    nativeWrites: [],
    clipboardData: [],
    copyHandling: [],
    copyListeners: 0,
  };
  const listeners = new Map<(event: unknown) => void, boolean>();

  const windowStub: Record<string, unknown> = {
    location: { protocol: tauri ? "tauri:" : "https:" },
  };
  if (tauri) {
    windowStub.__TAURI_INTERNALS__ = {};
  }
  define("window", windowStub);

  define("document", {
    body: {
      appendChild() {
        recorder.appended += 1;
      },
      removeChild() {
        recorder.removed += 1;
      },
    },
    createElement() {
      return {
        value: "",
        readOnly: false,
        style: {} as Record<string, string>,
        setAttribute() {},
        focus() {},
        select() {},
      };
    },
    addEventListener(
      type: string,
      listener: (event: unknown) => void,
      capture?: boolean,
    ) {
      if (type !== "copy") return;
      listeners.set(listener, capture === true);
      if (capture === true) recorder.copyHandling.push("capture");
      recorder.copyListeners = listeners.size;
    },
    removeEventListener(
      type: string,
      listener: (event: unknown) => void,
      capture?: boolean,
    ) {
      if (type === "copy" && listeners.get(listener) === (capture === true)) {
        listeners.delete(listener);
      }
      recorder.copyListeners = listeners.size;
    },
    execCommand(command: string) {
      recorder.execCommands.push(command);
      if (copyEvent) {
        const event = {
          clipboardData: {
            setData: (_type: string, data: string) =>
              recorder.clipboardData.push(data),
          },
          preventDefault: () => recorder.copyHandling.push("preventDefault"),
          stopImmediatePropagation: () =>
            recorder.copyHandling.push("stopImmediatePropagation"),
        };
        for (const listener of listeners.keys()) listener(event);
      }
      return execCommandResult;
    },
  });

  define("navigator", {
    clipboard:
      clipboard === "absent"
        ? undefined
        : {
            writeText(text: string) {
              recorder.webWrites.push(text);
              if (clipboard === "reject") {
                return Promise.reject(new Error("NotAllowedError"));
              }
              return Promise.resolve();
            },
          },
  });

  generation += 1;
  const control = ((globalThis as Record<string, unknown>).__TAURI_CLIPBOARD_STUB__ ??=
    {}) as { calls: string[]; mode: StubMode };
  control.calls = recorder.nativeWrites;
  control.mode = stub;

  const mod = (await import(`${MODULE}?bust=${generation}`)) as {
    copyToClipboard: (text: string) => Promise<boolean>;
  };
  const api = (await import(
    `${new URL("../src/lib/api-base.ts", import.meta.url).href}?bust=${generation}`
  )) as { isTauri: boolean };

  assert.equal(api.isTauri, tauri, "isTauri did not match the staged environment");
  return { copyToClipboard: mod.copyToClipboard, recorder };
}

const realWarn = console.warn;
test.before(() => {
  console.warn = () => {};
});
test.after(() => {
  console.warn = realWarn;
});

test("web build writes through navigator.clipboard before yielding", async () => {
  const { copyToClipboard, recorder } = await load({ tauri: false });

  // Not awaited yet: writeText must run before the first await to keep transient activation.
  const pending = copyToClipboard("hello");
  const writtenInGesture = [...recorder.webWrites];
  const nativeInGesture = recorder.nativeWrites.length;
  const result = await pending;

  assert.deepEqual(
    writtenInGesture,
    ["hello"],
    "navigator.clipboard.writeText must be called in the same tick as the click",
  );
  assert.equal(nativeInGesture, 0, "web build must not reach Tauri IPC");
  assert.equal(result, true);
  assert.deepEqual(recorder.execCommands, []);
});

test("web build reaches execCommand in the same tick when clipboard is absent", async () => {
  const { copyToClipboard, recorder } = await load({
    tauri: false,
    clipboard: "absent",
  });

  const pending = copyToClipboard("hello");
  const ranInGesture = [...recorder.execCommands];
  const result = await pending;

  assert.deepEqual(
    ranInGesture,
    ["copy"],
    "the synchronous fallback must also run inside the gesture",
  );
  assert.equal(result, true);
  assert.deepEqual(
    recorder.clipboardData,
    ["hello"],
    "written by the copy event",
  );
  assert.deepEqual(recorder.copyHandling, [
    "capture",
    "preventDefault",
    "stopImmediatePropagation",
  ]);
  assert.equal(recorder.removed, recorder.appended, "textarea must be cleaned up");
  assert.equal(recorder.copyListeners, 0, "copy listener must be removed");
});

test("execCommand fallback reports failure when no copy event carried the text", async () => {
  const { copyToClipboard, recorder } = await load({
    tauri: false,
    clipboard: "absent",
    copyEvent: false,
  });

  assert.equal(await copyToClipboard("hello"), false);
  assert.deepEqual(recorder.execCommands, ["copy"]);
  assert.equal(recorder.copyListeners, 0);
});

test("Tauri build copies natively and never touches navigator.clipboard", async () => {
  const { copyToClipboard, recorder } = await load({ tauri: true });

  assert.equal(await copyToClipboard("model/path.gguf"), true);
  assert.deepEqual(recorder.nativeWrites, ["model/path.gguf"]);
  assert.deepEqual(recorder.webWrites, []);
  assert.deepEqual(recorder.execCommands, []);
});

test("Tauri build does not resolve the native writer before the first await", async () => {
  const { copyToClipboard, recorder } = await load({ tauri: true });

  const pending = copyToClipboard("model/path.gguf");
  const nativeInGesture = [...recorder.nativeWrites];
  const result = await pending;

  assert.deepEqual(
    nativeInGesture,
    [],
    "the dynamic import necessarily yields; native IPC is exempt from the gesture rule",
  );
  assert.equal(result, true);
  assert.deepEqual(recorder.nativeWrites, ["model/path.gguf"]);
});

test("execCommand fallback runs when navigator.clipboard.writeText rejects", async () => {
  const { copyToClipboard, recorder } = await load({
    tauri: false,
    clipboard: "reject",
  });

  assert.equal(await copyToClipboard("hello"), true);
  assert.deepEqual(recorder.webWrites, ["hello"]);
  assert.deepEqual(recorder.execCommands, ["copy"]);
});

test("copyToClipboard reports failure when every writer fails", async () => {
  const { copyToClipboard, recorder } = await load({
    tauri: false,
    clipboard: "reject",
    execCommandResult: false,
  });

  assert.equal(await copyToClipboard("hello"), false);
  assert.deepEqual(recorder.execCommands, ["copy"]);
  assert.equal(recorder.removed, recorder.appended);
});

test("a failed native write still falls through to the web writers", async () => {
  const { copyToClipboard, recorder } = await load({
    tauri: true,
    stub: "write-fails",
  });

  assert.equal(await copyToClipboard("hello"), true);
  assert.deepEqual(recorder.nativeWrites, ["hello"]);
  assert.deepEqual(recorder.webWrites, ["hello"], "must degrade, not give up");
});

test("an install without the clipboard plugin falls through to the web writers", async () => {
  const { copyToClipboard, recorder } = await load({
    tauri: true,
    stub: "module-missing",
  });

  assert.equal(await copyToClipboard("hello"), true);
  assert.deepEqual(recorder.nativeWrites, [], "the import threw before writeText");
  assert.deepEqual(recorder.webWrites, ["hello"]);
});

test("empty and non-string input returns false and touches no writer", async () => {
  for (const tauri of [false, true]) {
    const { copyToClipboard, recorder } = await load({ tauri });

    assert.equal(await copyToClipboard(""), false);
    assert.equal(await copyToClipboard(undefined as unknown as string), false);
    assert.equal(await copyToClipboard(null as unknown as string), false);
    assert.equal(await copyToClipboard(42 as unknown as string), false);

    assert.deepEqual(recorder.nativeWrites, []);
    assert.deepEqual(recorder.webWrites, []);
    assert.deepEqual(recorder.execCommands, []);
    assert.equal(recorder.appended, 0);
  }
});