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.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 {
  installLocalStorageFake,
  readSrc,
  readSrcAsync,
  registerBundlerResolver,
} from "./helpers/kit.ts";

registerBundlerResolver();
const { store } = installLocalStorageFake();

const KEY = "unsloth_settings_panel_prefs";

store.set(
  KEY,
  JSON.stringify({
    state: {
      agentsAgent: "codex",
      agentsModel: "unsloth/Foo-GGUF",
      agentsVariant: "UD-Q4_K_XL",
      agentsVariantModel: "unsloth/Foo-GGUF",
      apiExampleLang: "pythonTools",
      apiExampleOs: "windows",
      apiExampleAgent: "codex",
      mcpAgent: "hermes",
      mcpOs: "windows",
      resourcesLiveUpdates: false,
      fineTuneAction: "recipes",
    },
    version: 0,
  }),
);

const AGENTS_TAB = readSrc("features/settings/tabs/agents-tab.tsx");

const { useSettingsPanelPrefsStore, SETTINGS_PANEL_PREFS_STORAGE_KEY } =
  await import("../src/features/settings/stores/settings-panel-prefs-store.ts");

test("a version 0 record hydrates every field", () => {
  const s = useSettingsPanelPrefsStore.getState();
  assert.equal(s.agentsAgent, "codex");
  assert.equal(s.agentsModel, "unsloth/Foo-GGUF");
  assert.equal(s.agentsOs, null);
  assert.equal(s.agentsVariant, "UD-Q4_K_XL");
  assert.equal(s.apiExampleOs, "windows");
  assert.equal(s.mcpAgent, "hermes");
  assert.equal(s.mcpOs, "windows");
  assert.equal(s.resourcesLiveUpdates, false);
  assert.equal(s.fineTuneAction, "recipes");
});

test("picking a model carries its quant, and clearing it clears the quant", () => {
  const s = useSettingsPanelPrefsStore.getState();
  s.setAgentsModel("unsloth/Bar-GGUF", "Q4_K_M");
  let next = useSettingsPanelPrefsStore.getState();
  assert.equal(next.agentsModel, "unsloth/Bar-GGUF");
  assert.equal(next.agentsVariantModel, "unsloth/Bar-GGUF");
  next.setAgentsModel(null, null);
  next = useSettingsPanelPrefsStore.getState();
  assert.equal(next.agentsModel, null);
  assert.equal(next.agentsVariant, null);
  assert.equal(next.agentsVariantModel, null);
});

test("a quant remembered for one model does not follow onto another", () => {
  const s = useSettingsPanelPrefsStore.getState();
  s.setAgentsVariant("unsloth/Baz-GGUF", "Q4_K_M");
  const next = useSettingsPanelPrefsStore.getState();
  assert.equal(next.agentsVariant, "Q4_K_M");
  assert.equal(next.agentsVariantModel, "unsloth/Baz-GGUF");
  assert.equal(next.agentsModel, null, "a quant pick must not pin the model");
});

test("a setter write round-trips through localStorage", () => {
  useSettingsPanelPrefsStore.getState().setFineTuneAction("export");
  const raw = store.get(KEY);
  assert.ok(raw, "nothing was written");
  assert.equal(JSON.parse(raw as string).state.fineTuneAction, "export");
});

test("the Agents command shell override persists and rejects unknown values", () => {
  useSettingsPanelPrefsStore.getState().setAgentsOs("unix");
  assert.equal(useSettingsPanelPrefsStore.getState().agentsOs, "unix");
  const raw = store.get(KEY);
  assert.ok(raw);
  assert.equal(JSON.parse(raw as string).state.agentsOs, "unix");

  const merged = useSettingsPanelPrefsStore.persist.getOptions().merge;
  assert.ok(merged);
  const out = merged(
    { agentsOs: "fish" },
    useSettingsPanelPrefsStore.getState(),
  ) as { agentsOs: unknown };
  assert.equal(out.agentsOs, null);
});

// agentsModel reaches .toLowerCase(), so a non-string takes the whole app down.
test("a non-string model is refused rather than handed to the tab", () => {
  const merged = useSettingsPanelPrefsStore.persist.getOptions().merge;
  assert.ok(merged, "merge must be supplied, or untrusted JSON reaches the UI");
  const out = merged(
    { agentsModel: 42, agentsVariant: [], fineTuneAction: "obliterate" },
    useSettingsPanelPrefsStore.getState(),
  ) as { agentsModel: unknown; fineTuneAction: string };
  assert.equal(out.agentsModel, null);
  assert.equal(out.fineTuneAction, "train");
});

test("the Audio API tab persists and a non-string is dropped", () => {
  useSettingsPanelPrefsStore.getState().setApiAudioExample("clone");
  assert.equal(
    JSON.parse(store.get(KEY) as string).state.apiAudioExample,
    "clone",
  );
  const merged = useSettingsPanelPrefsStore.persist.getOptions().merge;
  assert.ok(merged);
  const out = merged(
    { apiAudioExample: 7 },
    useSettingsPanelPrefsStore.getState(),
  ) as { apiAudioExample: unknown };
  assert.equal(out.apiAudioExample, null);
});

test("a persisted blob cannot replace the store actions", () => {
  const merged = useSettingsPanelPrefsStore.persist.getOptions().merge;
  assert.ok(merged);
  const out = merged(
    { setFineTuneAction: 5 },
    useSettingsPanelPrefsStore.getState(),
  ) as { setFineTuneAction: unknown };
  assert.equal(typeof out.setFineTuneAction, "function");
});

test("a quant picked while following the resident model does not pin a model", () => {
  const s = useSettingsPanelPrefsStore.getState();
  s.setAgentsModel(null, null);
  s.setAgentsVariant("unsloth/Qux-GGUF", "Q6_K");
  const next = useSettingsPanelPrefsStore.getState();
  assert.equal(next.agentsModel, null);
  assert.equal(next.agentsVariant, "Q6_K");
  assert.equal(next.agentsVariantModel, "unsloth/Qux-GGUF");
});

test("a quant with no model to scope it to is dropped", () => {
  const merged = useSettingsPanelPrefsStore.persist.getOptions().merge;
  assert.ok(merged);
  const out = merged(
    { agentsVariant: "Q6_K" },
    useSettingsPanelPrefsStore.getState(),
  ) as { agentsVariant: unknown; agentsVariantModel: unknown };
  assert.equal(out.agentsVariant, null);
  assert.equal(out.agentsVariantModel, null);
});

// A downgrade must not read a newer record: field names may be reused.
test("a record from a newer build falls back to defaults", () => {
  const { migrate, version } = useSettingsPanelPrefsStore.persist.getOptions();
  assert.ok(migrate);
  assert.equal(version, 1);
  assert.deepEqual(migrate({ agentsModel: "unsloth/Foo-GGUF" }, 2), {});
  assert.deepEqual(migrate({ agentsModel: "unsloth/Foo-GGUF" }, 0), {
    agentsModel: "unsloth/Foo-GGUF",
  });
});

// Settling in .finally let a superseded poll erase the saved model and quant.
test("the status poll settles only on the read that applied", async () => {
  const sync = AGENTS_TAB.slice(
    AGENTS_TAB.indexOf("const sync = ()"),
    AGENTS_TAB.indexOf("const timer = window.setInterval"),
  );
  assert.ok(sync, "the status poll moved; this contract needs updating");
  assert.ok(
    !sync.includes(".finally("),
    "a stale or failed poll must not settle",
  );
  const applied = sync.slice(
    sync.indexOf("seq === statusSeq.current"),
    sync.indexOf(".catch("),
  );
  assert.match(applied, /setStatusSettled\(true\)/);
});

test("Reset all local preferences clears this key", async () => {
  const source = await readSrcAsync("features/settings/tabs/general-tab.tsx");
  const keys = source.slice(
    source.indexOf("const PREFS_KEYS"),
    source.indexOf("];", source.indexOf("const PREFS_KEYS")),
  );
  assert.ok(
    keys.includes("SETTINGS_PANEL_PREFS_STORAGE_KEY") ||
      keys.includes(`"${SETTINGS_PANEL_PREFS_STORAGE_KEY}"`),
    `${SETTINGS_PANEL_PREFS_STORAGE_KEY} missing from PREFS_KEYS`,
  );
});

// Endpoints disagree on repo-id casing, so the scope check normalizes.
test("the remembered quant is scoped through modelKey, not an exact compare", async () => {
  assert.match(
    AGENTS_TAB,
    /modelKey\(chosen\.model\) === modelKey\(model\)/,
    "rememberedVariant must compare through modelKey",
  );
  const exact = AGENTS_TAB.split("\n").filter((line) =>
    line.includes("chosenVariant.current?.model ==="),
  );
  assert.deepEqual(
    exact,
    [],
    "an exact repo-id compare on chosenVariant drops the quant on a casing difference",
  );
});

test("an unreadable record leaves the defaults", () => {
  const merged = useSettingsPanelPrefsStore.persist.getOptions().merge;
  assert.ok(merged);
  const out = merged(null, useSettingsPanelPrefsStore.getState());
  assert.equal(out.agentsModel, null);
  assert.equal(out.agentsVariant, null);
  assert.equal(out.resourcesLiveUpdates, true);
  assert.equal(out.fineTuneAction, "train");
});

test("a dismissed driver notice round-trips, and a non-string record is dropped", () => {
  useSettingsPanelPrefsStore.getState().dismissDriverWarning("32.0.31041.1004");
  assert.equal(
    JSON.parse(store.get(KEY) as string).state.dismissedDriverWarning,
    "32.0.31041.1004",
  );
  const merged = useSettingsPanelPrefsStore.persist.getOptions().merge;
  assert.ok(merged);
  const state = useSettingsPanelPrefsStore.getState();
  assert.equal(
    merged({ dismissedDriverWarning: 42 }, state).dismissedDriverWarning,
    null,
  );
});

// Last: it rehydrates the store. Corrupt JSON must not take settings down.
test("corrupt JSON does not break the store", async () => {
  store.set(KEY, "{not json");
  await assert.doesNotReject(async () => {
    await useSettingsPanelPrefsStore.persist.rehydrate();
  });
  assert.equal(
    typeof useSettingsPanelPrefsStore.getState().setAgentsModel,
    "function",
  );
});

test("the agent access (MCP) picks persist and reject unknown values", () => {
  const s = useSettingsPanelPrefsStore.getState();
  s.setMcpAgent("dsh");
  s.setMcpOs("unix");
  const raw = store.get(KEY);
  assert.ok(raw);
  const state = JSON.parse(raw as string).state;
  assert.equal(state.mcpAgent, "dsh");
  assert.equal(state.mcpOs, "unix");

  const merged = useSettingsPanelPrefsStore.persist.getOptions().merge;
  assert.ok(merged);
  const out = merged(
    { mcpAgent: 7, mcpOs: "fish" },
    useSettingsPanelPrefsStore.getState(),
  ) as { mcpAgent: unknown; mcpOs: unknown };
  assert.equal(out.mcpAgent, null);
  assert.equal(out.mcpOs, null);
});