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.

18.2 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 test from "node:test";

import { attachedMediaUnavailableReason } from "../src/features/chat/lib/attached-media-gate.ts";
import {
  getAudioAddError,
  MAX_AUDIO_FILES,
  MAX_AUDIO_SIZE,
  maxAudioFilesFor,
} from "../src/lib/audio-utils.ts";
import {
  ATTACHMENT_KIND_ICON_CLASS,
  attachmentFileKind,
  attachmentKindLabel,
} from "../src/features/chat/lib/attachment-file-kind.ts";
import {
  COMPOSER_ATTACHMENT_MAX_ROWS,
  composerAttachmentsOverflow,
  SENT_ATTACHMENT_LIST_MAX,
  sentAttachmentLayout,
} from "../src/features/chat/lib/attachment-layout.ts";
import {
  DEFAULT_CUSTOMIZATION,
  sanitizeCustomization,
} from "../src/features/settings/stores/appearance-custom-store.ts";
import { en } from "../src/i18n/locales/en.ts";
import { readSrcAsync } from "./helpers/kit.ts";

const ATTACHMENT = await readSrcAsync("components/assistant-ui/attachment.tsx");

test("each file kind is read off the name, with a decisive MIME type first", () => {
  const cases: Array<[string, string, string]> = [
    ["invoice_INV-6.pdf", "", "pdf"],
    ["scan", "application/pdf", "pdf"],
    ["Rain 2.6-1.mp3", "audio/mpeg", "audio"],
    ["Book.m4a", "", "audio"],
    ["clip.mov", "", "video"],
    ["Untitled document.docx", "", "word"],
    ["notes.odt", "", "word"],
    ["Draft", "application/vnd.google-apps.document", "word"],
    ["Template", "application/vnd.oasis.opendocument.text-template", "word"],
    ["book.epub", "", "document"],
    ["results.csv", "text/csv", "spreadsheet"],
    ["deck.pptx", "", "presentation"],
    ["flappy-bird(1)(1).html", "", "web"],
    ["glm-5-next-sgl.patch", "", "text"],
    ["README.md", "text/markdown", "text"],
    ["train.py", "text/x-python", "code"],
    ["Dockerfile", "", "code"],
    ["weights.zip", "application/zip", "archive"],
    ["photo.HEIC", "", "image"],
    ["mystery.bin", "application/octet-stream", "file"],
    ["notes", "text/plain", "text"],
  ];
  for (const [name, mime, kind] of cases) {
    assert.equal(
      attachmentFileKind(name, mime),
      kind,
      `${name} (${mime || "no type"})`,
    );
  }
  assert.equal(attachmentFileKind("recording.txt", "audio/wav"), "audio");
  assert.equal(attachmentFileKind("x.constructor", ""), "file");
});

test("the common kinds carry the colors people recognize", () => {
  assert.match(ATTACHMENT_KIND_ICON_CLASS.pdf, /red/);
  assert.match(ATTACHMENT_KIND_ICON_CLASS.audio, /violet/);
  assert.match(ATTACHMENT_KIND_ICON_CLASS.word, /#4285F4/);
  assert.match(ATTACHMENT_KIND_ICON_CLASS.spreadsheet, /emerald/);
  assert.match(ATTACHMENT_KIND_ICON_CLASS.presentation, /orange/);
});

test("a sent file is labeled by its kind, or by its extension when unknown", () => {
  assert.equal(attachmentKindLabel("pdf", "a.pdf"), "PDF");
  assert.equal(attachmentKindLabel("audio", "a.mp3"), "Audio");
  assert.equal(attachmentKindLabel("document", "a.docx"), "Document");
  assert.equal(attachmentKindLabel("file", "model.safetensors"), "File");
  assert.equal(attachmentKindLabel("file", "blob.bin"), "BIN");
  assert.equal(attachmentKindLabel("file", "noextension"), "File");
});

test("Auto lists up to six sent files, then collapses them to chips", () => {
  assert.equal(SENT_ATTACHMENT_LIST_MAX, 6);
  assert.equal(sentAttachmentLayout("auto", 1), "list");
  assert.equal(sentAttachmentLayout("auto", 6), "list");
  assert.equal(sentAttachmentLayout("auto", 7), "chips");
  assert.equal(sentAttachmentLayout("list", 40), "list");
  assert.equal(sentAttachmentLayout("chips", 1), "chips");
});

test("composer cards wrap for two rows, then become one scrolling strip", () => {
  assert.equal(COMPOSER_ATTACHMENT_MAX_ROWS, 2);
  const quarter = (724 - 3 * 8) / 4;
  const cards = (n: number, w = quarter) => Array.from({ length: n }, () => w);
  assert.equal(composerAttachmentsOverflow(cards(8), 724, 8), false);
  assert.equal(composerAttachmentsOverflow(cards(9), 724, 8), true);
  assert.equal(composerAttachmentsOverflow(cards(8, quarter + 0.004), 724, 8), false);
  assert.equal(composerAttachmentsOverflow(cards(7, 105), 300, 8), true);
  assert.equal(composerAttachmentsOverflow([], 724, 8), false);
  assert.equal(composerAttachmentsOverflow(cards(20, 0), 724, 8), false);
  // In a chat the strip allows one row: a fifth card at a quarter of the width already scrolls.
  assert.equal(composerAttachmentsOverflow(cards(4), 724, 8, 1), false);
  assert.equal(composerAttachmentsOverflow(cards(5), 724, 8, 1), true);
  // Image cards take their own widths.
  assert.equal(composerAttachmentsOverflow(cards(8, 75), 724, 8, 1), false);
  assert.equal(composerAttachmentsOverflow([168, 168, 168, 168, 75], 724, 8, 1), true);
  assert.match(
    ATTACHMENT,
    /const CARD_SLOT =\n\s*"shrink-0 w-\[calc\(\(100%_-_var\(--spacing\)\*6\)\/4\.15\)\] min-w-\[calc\(7\.5rem\*var\(--ui-space-scale,1\)\)\]";/,
  );
  assert.match(
    ATTACHMENT,
    /const CARD_SIZE = "h-\[calc\(7rem\*var\(--ui-space-scale,1\)\)\] w-full";/,
  );
  assert.match(
    ATTACHMENT,
    /"aui-attachment-root aui-attachment-card group\/attachment-card relative",\n\s*src \? "shrink-0" : CARD_SLOT,/,
  );
  assert.match(
    ATTACHMENT,
    /variant === "card" && cn\("group\/attachment-card", CARD_SLOT\)/,
  );
  assert.match(ATTACHMENT, /aui-composer-attachment-cards [^"]*\bgap-2\b/);
});

test("cards keep their hairline edge in dark mode", () => {
  const edge = /const CARD_EDGE =\n\s*"([^"]*)";/.exec(ATTACHMENT)?.[1] ?? "";
  const surface =
    /const CARD_SURFACE =\n\s*"([^"]*)";/.exec(ATTACHMENT)?.[1] ?? "";
  assert.match(edge, /^border border-\[/);
  assert.doesNotMatch(edge + surface, /dark:/);
});

test("the strip is laid out through the DOM, never through React state", () => {
  const strip = ATTACHMENT.slice(
    ATTACHMENT.indexOf("export const ComposerAttachments: FC"),
    ATTACHMENT.indexOf("export const ComposerAddAttachment: FC"),
  );
  assert.match(strip, /el\.dataset\.layout = next/);
  assert.doesNotMatch(strip, /useState/);
  // maxRows flips once, when the first message lands; resizes still go through the observer.
  assert.match(strip, /\}, \[count, maxRows\]\);/);
  assert.match(strip, /new ResizeObserver\(layout\)/);
});

test("sent attachments split images from files by type, with stable component maps", () => {
  assert.match(
    ATTACHMENT,
    /const SENT_IMAGE_COMPONENTS = \{\n\s*Image: SentImageTile,\n\s*Document: NoAttachment,\n\s*File: NoAttachment,\n\};/,
  );
  assert.match(
    ATTACHMENT,
    /const SENT_FILE_COMPONENTS = \{\n\s*Image: NoAttachment,\n\s*Document: SentFileItem,\n\s*File: SentFileItem,\n\};/,
  );
  const sent = ATTACHMENT.slice(ATTACHMENT.indexOf("const SentFileItem: FC"));
  assert.match(sent, /<AttachmentPreviewDialog redactFromReload=\{false\}>/);
});

test("the sent attachments setting defaults to auto and rejects anything else", () => {
  assert.equal(DEFAULT_CUSTOMIZATION.sentAttachments, "auto");
  assert.equal(
    sanitizeCustomization({ sentAttachments: "chips" }).sentAttachments,
    "chips",
  );
  assert.equal(
    sanitizeCustomization({ sentAttachments: 7 }).sentAttachments,
    "auto",
  );
});

test("the sent attachments options read as Standard and Compact", () => {
  const options = en.settings.appearance.custom.sentAttachments;
  assert.equal(options.list, "Standard");
  assert.equal(options.chips, "Compact");
  assert.doesNotMatch(options.description, /\b(list|chips?)\b/i);
});

test("the composer always shows attachments as cards", () => {
  const saved = sanitizeCustomization({ composerAttachments: "compact" });
  assert.equal("composerAttachments" in saved, false);
  assert.match(
    ATTACHMENT,
    /<ComposerAttachmentByIdProvider key=\{id\} id=\{id\}>\s*<ComposerAttachmentCard \/>/,
  );
  assert.doesNotMatch(
    ATTACHMENT,
    /COMPACT_COMPONENTS|const AttachmentUI\b|aui-pasted-text-chip/,
  );
  assert.doesNotMatch(ATTACHMENT, /customization\.composerAttachments/);
});

// assistant-ui keys composer attachments by index, so removing the first remounted every card after
// it (each card is keyed by its attachment id), and each remount blanked its preview and decoded the
// image again. The list is keyed and scoped by id instead; an index scope would also throw for a
// moved card, whose new index applies a step after the list re-renders.
test("composer attachment cards are keyed and scoped by attachment id", async () => {
  const list = ATTACHMENT.slice(
    ATTACHMENT.indexOf("const ComposerAttachmentCardList: FC"),
  );
  assert.match(
    list,
    /composer\.attachments\.map\(\(attachment\) => attachment\.id\)/,
  );
  assert.doesNotMatch(ATTACHMENT, /<ComposerPrimitive\.Attachments\b/);
  const scopes = await readSrcAsync(
    "components/assistant-ui/stale-safe-scopes.tsx",
  );
  assert.match(scopes, /query: \{ type: "id", id \}/);
  assert.match(scopes, /aui\.composer\(\)\.attachment\(\{ id \}\)/);
});

test("a custom OpenAI-compatible connection is sent video like an image", async () => {
  const base = {
    checkpoint: "external::conn::vision-model",
    modelLabel: "vision-model",
    activeModel: undefined,
    audio: false,
    video: true,
  };
  assert.equal(
    attachedMediaUnavailableReason({ ...base, externalTakesVideo: true }),
    null,
  );
  assert.match(
    attachedMediaUnavailableReason(base) ?? "",
    /^vision-model cannot accept video\./,
  );
  const adapter = await readSrcAsync("features/chat/api/chat-adapter.ts");
  const start = adapter.indexOf("model: externalSelection.modelId,");
  const external = adapter.slice(
    start,
    adapter.indexOf("...externalReasoningFields,", start),
  );
  assert.match(external, /video_base64: findLatestUserVideoBase64\(currentTurnMessages\)/);
});

test("audio or video attached before a model loaded is checked when sent", () => {
  const listener = { hasAudioInput: true, hasVideoInput: false };
  const textOnly = { hasAudioInput: false, hasVideoInput: false };
  const base = { checkpoint: "unsloth/model", modelLabel: "Model" };
  assert.equal(
    attachedMediaUnavailableReason({
      ...base,
      activeModel: listener,
      audio: true,
      video: false,
    }),
    null,
  );
  assert.match(
    attachedMediaUnavailableReason({
      ...base,
      activeModel: textOnly,
      audio: true,
      video: false,
    }) ?? "",
    /^Model cannot accept audio\./,
  );
  assert.match(
    attachedMediaUnavailableReason({
      ...base,
      activeModel: listener,
      audio: false,
      video: true,
    }) ?? "",
    /^Model cannot accept video\./,
  );
  assert.ok(
    attachedMediaUnavailableReason({
      ...base,
      activeModel: undefined,
      audio: true,
      video: false,
    }),
  );
  assert.equal(
    attachedMediaUnavailableReason({
      ...base,
      activeModel: textOnly,
      audio: false,
      video: false,
    }),
    null,
  );
  assert.equal(
    attachedMediaUnavailableReason({
      ...base,
      checkpoint: null,
      activeModel: textOnly,
      audio: true,
      video: false,
    }),
    null,
  );
});

test("several clips are sent to GGUF and transformers, but refused on MLX", () => {
  const base = { checkpoint: "unsloth/gemma-4-E4B-it", modelLabel: "Gemma" };
  const gguf = { hasAudioInput: true, isMlx: false };
  const mlx = { hasAudioInput: true, isMlx: true };
  assert.equal(
    attachedMediaUnavailableReason({
      ...base,
      activeModel: gguf,
      audio: true,
      audioCount: 3,
      video: false,
    }),
    null,
  );
  assert.equal(
    attachedMediaUnavailableReason({
      ...base,
      activeModel: mlx,
      audio: true,
      audioCount: 1,
      video: false,
    }),
    null,
  );
  assert.match(
    attachedMediaUnavailableReason({
      ...base,
      activeModel: mlx,
      audio: true,
      audioCount: 2,
      video: false,
    }) ?? "",
    /^Gemma takes one audio file per message\./,
  );
});

test("audio caps cover a message's clips together", () => {
  assert.equal(getAudioAddError(0, 0, 1024), null);
  assert.equal(getAudioAddError(3, 10 * 1024 * 1024, 10 * 1024 * 1024), null);
  assert.match(
    getAudioAddError(1, 20 * 1024 * 1024, 10 * 1024 * 1024) ?? "",
    /together exceed/,
  );
  assert.match(getAudioAddError(0, 0, MAX_AUDIO_SIZE + 1) ?? "", /exceeds/);
  assert.match(
    getAudioAddError(MAX_AUDIO_FILES, 0, 1) ?? "",
    new RegExp(`Up to ${MAX_AUDIO_FILES} audio files`),
  );
  assert.equal(maxAudioFilesFor({ isMlx: true }), 1);
  assert.equal(maxAudioFilesFor(undefined), MAX_AUDIO_FILES);
  assert.match(
    getAudioAddError(1, 0, 1, maxAudioFilesFor({ isMlx: true })) ?? "",
    /one audio file per message/,
  );
});

test("attaching no longer needs a loaded model, only a capable one when one is loaded", async () => {
  const audio = await readSrcAsync("features/chat/audio-attachment-adapter.ts");
  const video = await readSrcAsync("features/chat/video-attachment-adapter.ts");
  const provider = await readSrcAsync("features/chat/runtime-provider.tsx");
  assert.ok(!audio.includes("Load a model before adding audio files."));
  assert.ok(!video.includes("Load a model before adding video."));
  assert.match(provider, /const unavailableReason = !modelLoaded\n\s*\? null/);
});

test("every attachment opens in the Library's viewer, from the composer and from a message", async () => {
  const preview = await readSrcAsync(
    "components/assistant-ui/attachment-preview.tsx",
  );
  const viewer = await readSrcAsync(
    "components/assistant-ui/attachment-document-dialog.tsx",
  );
  for (const dialog of [
    "ImageGalleryDialog",
    "AttachmentTextDialog",
    "AttachmentAudioDialog",
    "AttachmentVideoDialog",
  ]) {
    const body = preview.slice(preview.indexOf(`const ${dialog}: FC`));
    assert.match(body, /^[\s\S]*?<AttachmentViewer\n/, dialog);
  }
  assert.match(viewer, /const DocumentDialog[\s\S]*?<AttachmentViewer\n/);
  assert.match(viewer, /export const AttachmentViewer[\s\S]*?<MediaViewer\n/);
  assert.doesNotMatch(preview, /<DialogContent/);
  assert.match(viewer, /load && !redactFromReload/);
  assert.match(viewer, /startLibraryChat\(navigate, \{ files: \[file\] \}\)/);
  assert.match(preview, /<ArtifactHtmlFrame code=\{preview\.text\}/);
});

test("files handed to a new chat wait until it is on screen", async () => {
  const start = await readSrcAsync("features/library/start-chat.ts");
  const fn = start.slice(start.indexOf("export function startLibraryChat("));
  const navigate = fn.indexOf('navigate({ to: "/chat"');
  const offer = fn.indexOf(".offer(");
  assert.ok(
    navigate !== -1 && offer > navigate,
    "the files are offered before the new chat opens",
  );
  assert.match(fn, /\.then\(\(\) => \{\n\s*requestAnimationFrame\(/);
});

test("an image card in the composer shares the file card border but has no fill", () => {
  const card = ATTACHMENT.slice(
    ATTACHMENT.indexOf("const ComposerAttachmentCard: FC"),
    ATTACHMENT.indexOf("const SentAttachmentLayoutContext"),
  );
  assert.match(card, /const src = useAttachmentImageSrc\(\);/);
  assert.match(card, /CARD_EDGE,\n\s*!src && CARD_SURFACE,/);
  assert.match(
    card,
    /Math\.min\(Math\.max\(ratio \?\? 1, MIN_IMAGE_RATIO\), MAX_IMAGE_RATIO\)/,
  );
  assert.match(
    card,
    /<CardImageOrBody name=\{name\} kind=\{kind\} src=\{src\} onRatio=\{setRatio\} \/>/,
  );
});

test("an attachment's viewer mounts on first open, not with every tile", async () => {
  const viewer = await readSrcAsync(
    "components/assistant-ui/attachment-document-dialog.tsx",
  );
  assert.match(
    viewer,
    /const \[mounted, setMounted\] = useState\(open\);\n\s*if \(open && !mounted\) setMounted\(true\);/,
  );
  assert.match(viewer, /\{mounted && \(\n\s*<MediaViewer/);
});

test("the viewer's chat and download report a file they could not read", async () => {
  const viewer = await readSrcAsync(
    "components/assistant-ui/attachment-document-dialog.tsx",
  );
  assert.match(
    viewer,
    /\.catch\(\(\) => toast\.error\(`Could not read \$\{name\}`\)\)/,
  );
  assert.match(
    viewer,
    /\.catch\(\(\) => toast\.error\(t\("library\.toast\.downloadFailed", \{ name \}\)\)\)/,
  );
});

test("a sent text file downloads whole, not the capped preview", async () => {
  const preview = await readSrcAsync(
    "components/assistant-ui/attachment-preview.tsx",
  );
  assert.match(preview, /new Blob\(\[attachmentBodyText\(sentText\)\]/);
  assert.doesNotMatch(preview, /new Blob\(\[ready\.text\]/);
});

test("a composer clip reads as a video, not by its .mp4 name as audio", () => {
  assert.equal(attachmentFileKind("clip.mp4", "video/mp4"), "video");
  assert.equal(attachmentFileKind("clip.webm", "video/webm"), "video");
  assert.equal(attachmentFileKind("song.mp3", "audio/mpeg"), "audio");
});

test("a sent document shown from its stored text downloads and chats as a .txt", async () => {
  const dialog = await readSrcAsync(
    "components/assistant-ui/attachment-document-dialog.tsx",
  );
  assert.match(
    dialog,
    /saveAs=\{\n\s*loaded\?\.plain !== undefined\n\s*\? \{ name: `\$\{source\.name\.replace\(\/\\\.\[\^\.\]\+\$\/, ""\)\}\.txt`, contentType: "text\/plain" \}/,
  );
  assert.match(
    dialog,
    /const name = saveAs\?\.name \?\? \(source\.name \|\| "attachment"\);/,
  );
  assert.match(
    dialog,
    /downloadFile\(blob, name, contentType \|\| undefined\)/,
  );
});

test("a card preview follows the text adapter before a binary document viewer", async () => {
  const preview = await readSrcAsync(
    "components/assistant-ui/attachment-card-preview.tsx",
  );
  assert.match(
    preview,
    /if \(binary && isTextAttachment\(file\.name, file\.type\)\) return \{ kind: "text" \};\n\s*if \(document\)/,
  );
});

test("the main composer keeps attachments to one scrolling row once the chat has messages", async () => {
  const attachment = await readSrcAsync(
    "components/assistant-ui/attachment.tsx",
  );
  assert.match(
    attachment,
    /const maxRows = oneRowInChat && inChat \? 1 : COMPOSER_ATTACHMENT_MAX_ROWS;/,
  );
  assert.match(
    attachment,
    /useAuiState\(\(\{ thread \}\) => !thread\.isEmpty\)/,
  );
  const thread = await readSrcAsync("components/assistant-ui/thread.tsx");
  assert.match(thread, /<ComposerAttachments oneRowInChat=\{true\} \/>/);
  // The gap under the strip matches the one between the input and the controls.
  assert.match(
    attachment,
    /aui-composer-attachment-cards mb-\[calc\(0\.5rem\*var\(--ui-space-scale,1\)\)\]/,
  );
});