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.

3.7 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 { readFileSync } from "node:fs";
import test from "node:test";

const SKILLS_API_SOURCE = readFileSync(
  new URL("../src/features/chat/api/skills-api.ts", import.meta.url),
  "utf8",
);
const CHAT_ADAPTER_SOURCE = readFileSync(
  new URL("../src/features/chat/api/chat-adapter.ts", import.meta.url),
  "utf8",
);

test("token counting waits for the initial skills discovery", () => {
  assert.match(SKILLS_API_SOURCE, /initialized: boolean;/);
  assert.match(SKILLS_API_SOURCE, /initialized: false,/);
  assert.match(SKILLS_API_SOURCE, /initialized: true,/);
  assert.match(
    SKILLS_API_SOURCE,
    /if \(pending\) await Promise\.race\(\[pending\.catch\(\(\) => undefined\), deadline\]\);/,
  );
  assert.match(SKILLS_API_SOURCE, /let stale = !snapshot\.initialized;/);
  assert.match(CHAT_ADAPTER_SOURCE, /await settleSkillsForText\(userTexts\(messages\)\.join\("\\n"\)\);/);
  assert.match(
    CHAT_ADAPTER_SOURCE,
    /const skillTools = skillToolNames\(\s*skillsForTools\(\),\s*codeToolsEnabled,\s*userTexts\(messages\),\s*\);/,
  );
});

test("a pre-send catalog re-read cannot hold the request past the settle deadline", () => {
  assert.match(SKILLS_API_SOURCE, /const SETTLE_TIMEOUT_MS = 3000;/);
  assert.match(
    SKILLS_API_SOURCE,
    /await Promise\.race\(\[listSkills\(true\)\.catch\(\(\) => undefined\), deadline\]\);/,
  );
});

test("token counting lists the skill tools only when the completion would", () => {
  const countExtras = CHAT_ADAPTER_SOURCE.slice(
    CHAT_ADAPTER_SOURCE.indexOf("export async function buildLocalTokenCountExtras"),
    CHAT_ADAPTER_SOURCE.indexOf("mcp_enabled: mcpEnabledForChat"),
  );
  assert.match(countExtras, /skillTools\.length === 0\n\s*\) \{/);
  assert.match(countExtras, /^\s*\.\.\.skillTools,$/m);
  assert.doesNotMatch(countExtras, /^\s*"read_skill",$/m);
  assert.doesNotMatch(countExtras, /^\s*"create_skill",$/m);
});

test("request building waits for skills and preserves the launcher tool catalog", () => {
  const payloadBuilder = CHAT_ADAPTER_SOURCE.slice(
    CHAT_ADAPTER_SOURCE.indexOf("const buildRequestPayload = async"),
    CHAT_ADAPTER_SOURCE.indexOf(
      "while (true)",
      CHAT_ADAPTER_SOURCE.indexOf("const buildRequestPayload = async"),
    ),
  );
  assert.match(
    payloadBuilder,
    /if \(supportsStudioToolsForThisTurn\) \{\s*\/\/[^\n]*\n\s*await settleSkillsForText\(userTexts\(outboundMessages\)\.join\("\\n"\)\);/,
  );
  assert.match(
    payloadBuilder,
    /const skillTools = skillToolNames\(\s*skillsForTools\(\),\s*codeToolsEnabled,\s*userTexts\(outboundMessages\),\s*\);/,
  );
  assert.match(
    payloadBuilder,
    /supportsTools &&\s*\([\s\S]*skillTools\.length > 0[\s\S]*\)\s*\? \{\s*enable_tools: true/,
  );
  const localToolCatalog = payloadBuilder.slice(
    payloadBuilder.indexOf("// Sent for every local chat"),
  );
  assert.doesNotMatch(localToolCatalog, /^\s*"read_skill",$/m);
  assert.doesNotMatch(localToolCatalog, /^\s*"create_skill",$/m);
});

test("a sign-out drops the module-level skills snapshot", () => {
  assert.ok(SKILLS_API_SOURCE.includes("window.addEventListener(AUTH_SESSION_CLEARED_EVENT"));
  assert.ok(SKILLS_API_SOURCE.includes("publish(EMPTY_SNAPSHOT)"));
});

test("the count passes the counted history so a Tools-off @mention is priced like the send", () => {
  const refresh = readFileSync(
    new URL("../src/features/chat/utils/refresh-context-usage.ts", import.meta.url),
    "utf8",
  );
  assert.match(
    refresh,
    /buildLocalTokenCountExtras\(\s*payloadThreadId,\s*countHistory\.messages,\s*\)/,
  );
});