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.

17.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 test from "node:test";
import {
  useChatNavigationStore,
  visibleChatItems,
} from "../src/features/chat/stores/chat-navigation-store.ts";
import { applyManualOrder } from "../src/features/chat/stores/sidebar-organization-store.ts";
import type { SidebarItem } from "../src/features/chat/hooks/use-chat-sidebar-items.ts";
import { readSrcAsync } from "./helpers/kit.ts";

const APP_SIDEBAR = await readSrcAsync("components/app-sidebar.tsx");

// The open-state gutter rule is more specific than hover, so both must reserve the same. The row's
// parts look at the actions after them (`:has(~ ...)`), not the row at its descendants (group-has-*).
test("an open row menu reserves the same gutter hover does", () => {
  const pattern =
    /group-hover\/([a-z-]+):pr-(\d+)[^"]*\[&:has\(~\.sidebar-row-action\[data-state=open\]\)\]:pr-(\d+)/g;
  const seen: string[] = [];
  for (const [, hoverGroup, hoverPad, openPad] of APP_SIDEBAR.matchAll(pattern)) {
    assert.equal(
      openPad,
      hoverPad,
      `${hoverGroup} reserves pr-${hoverPad} on hover but pr-${openPad} with its menu open, so the title runs under the actions`,
    );
    seen.push(`${hoverGroup}:${openPad}`);
  }
  // Counts kinds of row, not branches.
  assert.ok(seen.length >= 3, `only ${seen.length} rows carry both gutters`);
});

test("the quick-action beside an open menu is revealed with it", () => {
  for (const name of ["PROJECT_CHAT", "RECENT"]) {
    const reveal = new RegExp(
      `REVEAL_WITH_OPEN_MENU_${name} =\\s*"\\[&:has\\(~\\.sidebar-row-action\\[data-state=open\\]\\)\\]:opacity-100 \\[&:has\\(~\\.sidebar-row-action\\[data-state=open\\]\\)\\]:pointer-events-auto"`,
    );
    assert.match(APP_SIDEBAR, reveal, `${name} has no open-menu reveal`);
  }
  const secondaries = APP_SIDEBAR.match(/is-unpin-action/g) ?? [];
  assert.equal(secondaries.length, 3, "a secondary row action came or went");
  assert.equal(
    (APP_SIDEBAR.match(/REVEAL_WITH_OPEN_MENU_(PROJECT_CHAT|RECENT),/g) ?? [])
      .length,
    3,
    "a secondary row action is not revealed with its row's menu",
  );
});

// The row toggles its chats, so the folder says which way it is.
test("a folder row's icon follows its disclosure", () => {
  assert.match(APP_SIDEBAR, /icon=\{expanded \? Folder02Icon : ProjectIcon\}/);
});

test("an empty open folder says it is empty", () => {
  assert.match(
    APP_SIDEBAR,
    /\{expanded && projectChats\.length === 0 && \(\n\s*<SidebarMenuItem\n\s*\{\.\.\.dnd\.dropZoneProps\(\{ section: order\.section, folderId: project\.id, blockEnd \}\)\}\n\s*>\n\s*<p className="[^"]*text-nav-fg-muted">\n\s*\{t\("shell\.navigation\.noChats"\)\}/,
  );
});

test("a chat's Move to submenu groups projects and sections, New first and Remove last", () => {
  const sub = APP_SIDEBAR.slice(
    APP_SIDEBAR.indexOf("{/* Projects and sections in one place"),
    APP_SIDEBAR.indexOf("<span>Export</span>"),
  );
  assert.ok(sub.length > 0, "the Move to submenu moved");
  const at = {
    trigger: sub.indexOf('<span>{t("shell.sections.moveTo")}</span>'),
    projects: sub.indexOf('<P.Label>{t("shell.navigation.projects")}</P.Label>'),
    newProject: sub.indexOf("<span>New project</span>"),
    destinations: sub.indexOf("{moveProjects.map((project) => ("),
    removeProject: sub.indexOf('t("shell.sections.removeFromProject")'),
    rule: sub.indexOf("<P.Separator />"),
    sections: sub.indexOf("{renderSectionItems(P, {"),
  };
  for (const [name, index] of Object.entries(at)) {
    assert.notEqual(index, -1, `${name} is gone from the Move to submenu`);
  }
  const order = Object.values(at);
  assert.deepEqual([...order].sort((a, b) => a - b), order, "the Move to submenu is out of order");
  assert.match(sub, /\{item\.projectId && \(\n\s*<P\.Item onSelect=\{\(\) => void moveChatToProject\(item, null\)\}>/);
  assert.match(sub, /icon=\{PlusSignIcon\}[^\n]*\n\s*<span>New project<\/span>/);
  const items = APP_SIDEBAR.slice(
    APP_SIDEBAR.indexOf("function renderSectionItems("),
    APP_SIDEBAR.indexOf('/** "Move to" for rows that only sections take'),
  );
  assert.match(items, /icon=\{PlusSignIcon\}[^\n]*\n\s*<span>\{t\("shell\.sections\.newSection"\)\}<\/span>/);
  const newSection = items.indexOf('t("shell.sections.newSection")');
  const list = items.indexOf("{destinations.map((section) => (");
  assert.match(items, /const destinations = recentSections\n\s*\.filter\(\(section\) => section\.id !== config\.current\)\n\s*\.slice\(0, MOVE_TO_MAX\);/);
  assert.doesNotMatch(APP_SIDEBAR, /disabled=\{config\.current === section\.id\}|disabled=\{item\.projectId === project\.id\}/);
  const remove = items.indexOf("{config.anyFiled && (");
  assert.ok(newSection < list && list < remove, "the Sections group is out of order");
  assert.ok(!APP_SIDEBAR.includes("<span>Project sources</span>"));
  assert.ok(!APP_SIDEBAR.includes("<span>Project</span>"));
  const sectionOnly = APP_SIDEBAR.slice(APP_SIDEBAR.indexOf("function renderSectionSubmenu("));
  assert.match(
    sectionOnly,
    /<P\.SubTrigger>\n\s*<HugeiconsIcon icon=\{LayerIcon\} strokeWidth=\{1\.75\} className="size-icon" \/>\n\s*<span>\{t\("shell\.sections\.section"\)\}<\/span>/,
  );
  assert.match(
    APP_SIDEBAR,
    /<P\.Item onSelect=\{\(\) => setEditingProject\(project\)\}>\n\s*<HugeiconsIcon icon=\{Settings01Icon\}/,
  );
  assert.ok(!APP_SIDEBAR.includes("newSectionEllipsis"));
});

test("the folder menu leads with where to go, then what to change", async () => {
  const menu = APP_SIDEBAR.slice(
    APP_SIDEBAR.indexOf("<span>Project home</span>"),
    APP_SIDEBAR.indexOf("<span>Delete project</span>"),
  );
  assert.ok(menu.length > 0, "the folder menu moved");
  const pin = menu.indexOf('{isProjectPinned ? "Unpin" : "Pin"}');
  const edit = menu.indexOf("<span>Edit</span>");
  assert.notEqual(pin, -1, "the pin toggle left the folder menu");
  assert.notEqual(edit, -1, "Edit is not in the folder menu");
  assert.ok(pin < edit, "Pin project is not directly under Project home");
  assert.ok(
    !menu.includes("<span>New chat</span>"),
    "New chat is still in the folder menu",
  );
  assert.ok(!APP_SIDEBAR.includes("<span>Rename project</span>"));
  assert.match(APP_SIDEBAR, /onSelect=\{\(\) => setEditingProject\(project\)\}/);
  assert.match(
    APP_SIDEBAR,
    /<EditProjectDialog\n\s*project=\{editingProject\}/,
  );
  assert.match(
    APP_SIDEBAR,
    /onDelete=\{\(project\) => openDeleteDialog\(\{ kind: "project", project \}\)\}/,
  );
  assert.ok(!APP_SIDEBAR.includes('renamingTarget?.kind === "project"'));

  const dialog = await readSrcAsync(
    "features/chat/components/edit-project-dialog.tsx",
  );
  assert.match(dialog, /renameChatProject\(target\.id, trimmedName\)/);
  assert.match(
    dialog,
    /updateChatProjectInstructions\(target\.id, trimmedInstructions\)/,
  );
  assert.match(dialog, /scope=\{\{ type: "project", id: project\.id \}\}/);
});

test("a pinned folder is a row of Pinned, not of Projects", () => {
  assert.match(
    APP_SIDEBAR,
    /row\.kind === "project"\n\s*\? renderProjectFolderRow\(row\.project, \{\n\s*scope: PINNED_ORDER_SCOPE,\n\s*orderedIds: pinnedRowIds,/,
  );
  assert.match(
    APP_SIDEBAR,
    /items=\{visibleProjectRecords\}[\s\S]{0,200}renderItem=\{\(project\) =>\n\s*renderProjectFolderRow\(project, \{\n\s*scope: PROJECT_ORDER_SCOPE,\n\s*orderedIds: projectRowIds,/,
  );
  const records = APP_SIDEBAR.slice(
    APP_SIDEBAR.indexOf("const sidebarProjectRecords = useMemo("),
    APP_SIDEBAR.indexOf("const visibleProjectRecords"),
  );
  assert.match(records, /\.filter\(\(p\) => !pinnedProjectIdSet\.has\(p\.id\) && !sectionByProjectId\[p\.id\]\)/);
  assert.ok(
    !records.includes("pinnedProjectIds"),
    "the Projects list still folds the pinned folders in",
  );
  assert.match(
    APP_SIDEBAR,
    /const projectsSectionConfigured =\n\s*organizeBy === "project" &&\n\s*!projectsSectionHidden &&\n\s*\(projects\.length > 0 \|\| projectsLoaded\);/,
  );
  assert.match(
    APP_SIDEBAR,
    /const projectsSectionRendered =\n[\s\S]{0,200}?projectsSectionConfigured;/,
  );
  assert.match(APP_SIDEBAR, /if \(!projectsSectionRendered\) return null;/);
});

// The Pinned scope is empty at first while Projects still holds the dragged order.
test("a pinned folder keeps the order it was dragged into", () => {
  const projects = [{ id: "a" }, { id: "b" }, { id: "c" }];
  const byId = new Map(projects.map((p) => [p.id, p]));
  const pinned = ["b", "a"].map((id) => byId.get(id)!);
  const draggedInProjects = ["c", "a", "b"];
  const order = (pinnedScope: string[] | undefined) =>
    applyManualOrder(
      pinned,
      pinnedScope?.length ? pinnedScope : draggedInProjects,
      (project) => project.id,
    ).map((project) => project.id);
  assert.deepEqual(order(undefined), ["a", "b"]);
  assert.deepEqual(order(["b", "a"]), ["b", "a"]);
  assert.match(
    APP_SIDEBAR,
    /manualOrder\[PINNED_PROJECT_ORDER_SCOPE\]\?\.length\n\s*\? manualOrder\[PINNED_PROJECT_ORDER_SCOPE\]\n\s*: manualOrder\[PROJECT_ORDER_SCOPE\],/,
  );
});

test("the walk reads the rows in the order Pinned draws them", () => {
  const item = (id: string): SidebarItem => ({
    type: "single",
    id,
    title: id,
    createdAt: 0,
    updatedAt: 0,
  });
  const folderChats = [item("folder-1"), item("folder-2")];
  // folder-2 is pinned as well as being in a pinned folder, so it is drawn twice.
  const pinnedChats = [item("pin-1"), item("folder-2")];
  useChatNavigationStore.getState().publishLists({
    pinnedItems: [...folderChats, ...pinnedChats],
    projectItems: [item("proj-1")],
    recentItems: [item("recent-1")],
    attentionItemIds: [],
    activeItemId: null,
  });
  assert.deepEqual(
    visibleChatItems(useChatNavigationStore.getState()).map((i) => i.id),
    ["folder-1", "folder-2", "pin-1", "proj-1", "recent-1"],
  );
  // The sidebar publishes Pinned in its drawn order, folders and chats interleaved with each open
  // folder's chats under its row (pinned pages have none), and leaves the section's own chats to
  // projectItems.
  assert.match(
    APP_SIDEBAR,
    /const pinnedSectionChatItems = useMemo\(\n\s*\(\) =>\n\s*chatListsOnScreen && pinnedOpen\n\s*\? pinnedRows\.flatMap\(\(row\) =>\n\s*row\.kind === "project"\n\s*\? folderChatItems\(true, \[row\.project\]\)\n\s*: row\.kind === "chat"\n\s*\? \[row\.item\]\n\s*: \[\],/,
  );
  assert.match(
    APP_SIDEBAR,
    /pinnedItems: upToPinnedChatItems,\n(?:\s*\/\/[^\n]*\n)*\s*projectItems: belowPinnedChatItems,/,
  );
  assert.match(
    APP_SIDEBAR,
    /for \(const key of orderedSectionKeys\) \{\n\s*const items =\n\s*key === PINNED_SECTION_KEY\n\s*\? pinnedSectionChatItems\n\s*: key === PROJECTS_SECTION_KEY\n\s*\? sectionProjectChatItems/,
  );
});

test("a pinned folder stays in Pinned when the sidebar is one list", () => {
  const rows = APP_SIDEBAR.slice(
    APP_SIDEBAR.indexOf("const pinnedRows = useMemo("),
    APP_SIDEBAR.indexOf("const pinnedRowIds = useMemo("),
  );
  assert.ok(rows.length > 0, "pinnedRows moved");
  assert.ok(
    !rows.includes("organizeBy"),
    "Pinned still drops its folders when the sidebar organizes in one list",
  );
  assert.match(
    rows,
    /for \(const project of pinnedProjectBase\) \{\n\s*rows\.push\(\{ kind: "project", id: project\.id, project \}\);/,
  );
  assert.match(
    APP_SIDEBAR,
    /if \(pinnedOpen\) \{\n\s*for \(const project of pinnedProjectRecords\) ids\.add\(project\.id\);/,
  );
  assert.match(
    APP_SIDEBAR,
    /if \(projectsOpen && projectsSectionConfigured\) \{\n\s*for \(const project of visibleProjectRecords\) ids\.add\(project\.id\);/,
  );
});

test("the sort options are names, with nothing written under them", () => {
  const options = APP_SIDEBAR.slice(
    APP_SIDEBAR.indexOf("const CHAT_SORT_OPTIONS"),
    APP_SIDEBAR.indexOf("const ORGANIZE_OPTIONS"),
  );
  assert.ok(!options.includes("hint"), "the sort options still carry captions");
  assert.match(
    APP_SIDEBAR,
    /\{config\.options\.map\(\(option\) => \(\n\s*<DropdownMenuRadioItem key=\{option\.value\} value=\{option\.value\}>\n\s*\{t\(option\.key\)\}\n\s*<\/DropdownMenuRadioItem>/,
  );
  assert.match(APP_SIDEBAR, /options: CHAT_SORT_OPTIONS,/);
  for (const key of ["priorityHint", "lastUpdatedHint", "manualOrderHint"]) {
    assert.ok(!APP_SIDEBAR.includes(key), `${key} is still read`);
  }
});

test("the project page edits through the same dialog the sidebar opens", async () => {
  const page = await readSrcAsync("features/chat/chat-page.tsx");
  assert.ok(!page.includes("Rename project"), "the rename-only dialog is still there");
  assert.ok(!page.includes("commitProjectRename"), "the rename call is still there");
  assert.match(page, /onEdit=\{\(\) => setEditingProject\(true\)\}/);
  assert.match(
    page,
    /projects\.find\(\(project\) => project\.id === projectId\)/,
  );
  assert.match(
    page,
    /project=\{active && editingProject \? \(currentProject \?\? null\) : null\}/,
  );
  assert.match(
    page,
    /onDelete=\{\(\) => \{\n\s*setEditingProject\(false\);\n\s*openProjectDelete\(\);/,
  );
  assert.match(
    page,
    /function openProjectDelete\(\): void \{\n\s*setDeleteFilesOnDelete\(false\);\n\s*setDeletingProject\(true\);/,
  );
  assert.match(
    page,
    /const deleteFiles = deleteFilesOnDelete;\n\s*setDeletingProject\(false\);\n\s*setDeleteFilesOnDelete\(false\);/,
  );
  assert.match(page, /await deleteChatProject\(projectId, \{ deleteFiles \}\);/);
  assert.match(
    page,
    /id="chat-landing-delete-project-files"[\s\S]{0,220}?currentProject\?\.rootPath \?\?\n\s*"The project workspace folder will be removed from disk\."/,
  );
  assert.match(page, /\{deleteFilesOnDelete \? "Delete all" : "Delete"\}/);
  assert.equal(
    (page.match(/setDeletingProject\((?!false\))/g) ?? []).length,
    1,
    "setDeletingProject is called outside its opener",
  );
});

// In one-list mode its chats are already Recents rows, so expanding would draw them twice.
test("a pinned folder in one list is a way in, not a second copy of its chats", () => {
  assert.match(
    APP_SIDEBAR,
    /if \(!chatListsOnScreen \|\| organizeBy !== "project" \|\| !open\) return \[\];/,
  );
  assert.match(APP_SIDEBAR, /const listsChats = organizeBy === "project";/);
  assert.match(
    APP_SIDEBAR,
    /const expanded = listsChats && !collapsedProjectIds\.has\(project\.id\);/,
  );
  assert.match(
    APP_SIDEBAR,
    /if \(listsChats\) toggleProjectCollapsed\(project\.id\);\n\s*else openProject\(project\.id\);/,
  );
  for (const gated of [
    /\{expanded && \(\n\s*<ProgressiveRows\n\s*items=\{visibleChats\}/,
    /\{expanded && projectChats\.length === 0 && \(/,
    /\{expanded &&\n\s*projectChats\.length > PROJECT_CHAT_LIMIT && \(/,
  ]) {
    assert.match(APP_SIDEBAR, gated);
  }
});

test("Move to names what a row leaves, and its lists scroll inside the window", async () => {
  assert.match(
    APP_SIDEBAR,
    /leaving\n\s*\? t\("shell\.sections\.removeFrom", \{ name: leaving\.name \}\)\n\s*: t\("shell\.sections\.removeFromProject"\);/,
  );
  assert.match(
    APP_SIDEBAR,
    /\{leaving\n\s*\? t\("shell\.sections\.removeFrom", \{ name: leaving\.name \}\)\n\s*: t\("shell\.sections\.removeFromSection"\)\}/,
  );
  for (const locale of ["en", "de", "ja"]) {
    const source = await readSrcAsync(`i18n/locales/${locale}.ts`);
    assert.match(source, /removeFrom: "[^"]*\{name\}[^"]*",/, locale);
  }
  assert.match(APP_SIDEBAR, /const MOVE_TO_LIST =\n\s*"no-scrollbar -my-0\.5 max-h-\[calc\(260px\*var\(--ui-space-scale,1\)\)\] overflow-y-auto overscroll-contain";/);
  assert.match(APP_SIDEBAR, /const MENU_ROW_MARGIN_PX = 2;/);
  assert.equal((APP_SIDEBAR.match(/icon=\{Cancel01Icon\}[^\n]*\n\s*<span className="truncate">/g) ?? []).length, 2);
  assert.doesNotMatch(APP_SIDEBAR, /MinusSignCircleIcon/);
  assert.match(APP_SIDEBAR, /const MOVE_TO_MENU =\n\s*"max-h-\[var\(--radix-dropdown-menu-content-available-height,var\(--radix-context-menu-content-available-height\)\)\] overflow-y-auto";/);
  assert.equal((APP_SIDEBAR.match(/<div className=\{MOVE_TO_LIST\}>/g) ?? []).length, 2);
  assert.equal((APP_SIDEBAR.match(/sidebar-menu w-52", MOVE_TO_MENU\)/g) ?? []).length, 2);
});

test("a sidebar menu heading has more room above it than below", async () => {
  const css = await readSrcAsync("index.css");
  assert.match(
    css,
    /\.unsloth-plus-menu\.sidebar-row-menu :is\(\n\s*\[data-slot="dropdown-menu-label"\],\n\s*\[data-slot="context-menu-label"\]\n\s*\) \{\n\s*@apply pl-2\.5 pr-2\.5 pt-2 pb-1 text-ui-11;/,
  );
});

// updatedAt and section order are not recency, so both rank by their chats' activity.
test("Move to lists at most the 12 most recently active projects and sections", () => {
  assert.match(APP_SIDEBAR, /const MOVE_TO_MAX = 12;/);
  const activity = APP_SIDEBAR.slice(
    APP_SIDEBAR.indexOf("const projectActivityAt = useMemo("),
    APP_SIDEBAR.indexOf("const recentProjects = useMemo("),
  );
  assert.match(activity, /chatsByProjectId\.get\(project\.id\)\?\.\[0\]\?\.updatedAt/);
  assert.match(
    APP_SIDEBAR,
    /\[\.\.\.projects\]\.sort\(\n\s*\(a, b\) => \(projectActivityAt\.get\(b\.id\) \?\? 0\) - \(projectActivityAt\.get\(a\.id\) \?\? 0\),/,
  );
  assert.match(APP_SIDEBAR, /const lastActivityAt = \(project: ProjectRecord\) => projectActivityAt\.get\(project\.id\) \?\? 0;/);
  const sections = APP_SIDEBAR.slice(
    APP_SIDEBAR.indexOf("const recentSections = useMemo("),
    APP_SIDEBAR.indexOf("const sidebarProjectRecords = useMemo("),
  );
  assert.match(sections, /Math\.max\(section\.createdAt \?\? 0, section\.modifiedAt \?\? 0\)/);
  assert.match(sections, /touch\(sectionByChatId\[item\.id\], item\.updatedAt\)/);
  assert.match(sections, /touch\(sectionId, projectActivityAt\.get\(projectId\) \?\? 0\)/);
  assert.match(
    APP_SIDEBAR,
    /const moveProjects = recentProjects\n\s*\.filter\(\(project\) => project\.id !== item\.projectId\)\n\s*\.slice\(0, MOVE_TO_MAX\);/,
  );
});