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.

8.9 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 type { ManagedDownload } from "../src/features/hub/download-manager/download-manager-types.ts";
import {
  installLocalStorageFake,
  registerBundlerResolver,
} from "./helpers/kit.ts";

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

const PERSIST_KEY = "unsloth.studio.downloads";
let flushPersistedState: (() => void) | undefined;
Object.assign(globalThis.window, {
  addEventListener: (type: string, listener: () => void) => {
    if (type === "pagehide") flushPersistedState = listener;
  },
});

function persistedJob(
  repoId: string,
  transport: unknown,
  cancelTransport?: unknown,
) {
  return {
    key: `model:${repoId}`,
    kind: "model",
    repoId,
    variant: null,
    state: "running",
    downloadedBytes: 25,
    completedBytes: 0,
    expectedBytes: 100,
    fraction: 0.25,
    error: null,
    startedAt: 1,
    transport,
    ...(cancelTransport === undefined ? {} : { cancelTransport }),
  };
}

store.set(
  PERSIST_KEY,
  JSON.stringify({
    state: {
      jobs: {
        http: persistedJob("org/http-model", "http"),
        invalid: persistedJob("org/auto-model", "auto"),
        fallback: persistedJob("org/fallback-model", "http", "xet"),
        badMarker: persistedJob("org/bad-marker-model", "http", "auto"),
        pinned: {
          ...persistedJob("org/pinned-model", "http"),
          downloadDir: "/mnt/e/AI/Models",
        },
        presented: {
          ...persistedJob("org/presented-model", "http"),
          presentation: {
            label: "MTP companion",
            filename: "mtp-shared-Q8_0.gguf",
            expectedBytes: 20,
          },
        },
      },
      conflicts: {},
    },
    version: 1,
  }),
);

const {
  getState,
  hasActiveDownloadJob,
  jobKeyOf,
  putJob,
  setExpectedBytesForJob,
} = await import(
  "../src/features/hub/download-manager/download-manager-state.ts"
);
const { presentedProgress } = await import(
  "../src/features/hub/download-manager/download-presentation.ts"
);

test("reload hydration keeps only a resolved active transport", () => {
  const jobs = getState().jobs;
  assert.equal(
    jobs[jobKeyOf("model", "org/http-model", null)]?.transport,
    "http",
  );
  assert.equal(
    jobs[jobKeyOf("model", "org/auto-model", null)]?.transport,
    undefined,
  );
});

test("the active transport is written with the persisted job", () => {
  const key = jobKeyOf("dataset", "org/dataset", null);
  const job: ManagedDownload = {
    key,
    kind: "dataset",
    repoId: "org/dataset",
    variant: null,
    state: "running",
    downloadedBytes: 50,
    completedBytes: 0,
    completeOnDisk: false,
    expectedBytes: 100,
    fraction: 0.5,
    bytesPerSec: 10,
    etaSeconds: 0,
    error: null,
    startedAt: 2,
    transport: "xet",
  };

  putJob(job);
  assert.ok(flushPersistedState);
  flushPersistedState();

  const persisted = JSON.parse(store.get(PERSIST_KEY) ?? "null");
  assert.equal(persisted.state.jobs[key].transport, "xet");
});

test("a fallback run's cancel marker survives the reload too", () => {
  const jobs = getState().jobs;
  const job = jobs[jobKeyOf("model", "org/fallback-model", null)];
  assert.equal(job?.transport, "http");
  assert.equal(job?.cancelTransport, "xet");
});

test("an unresolved persisted marker is dropped, not trusted", () => {
  const jobs = getState().jobs;
  assert.equal(
    jobs[jobKeyOf("model", "org/bad-marker-model", null)]?.cancelTransport,
    undefined,
  );
});

test("reload hydration keeps a valid companion-only presentation", () => {
  assert.deepEqual(
    getState().jobs[jobKeyOf("model", "org/presented-model", null)]
      ?.presentation,
    {
      label: "MTP companion",
      filename: "mtp-shared-Q8_0.gguf",
      expectedBytes: 20,
    },
  );
});

test("the cancel marker is written with the persisted job", () => {
  const key = jobKeyOf("model", "org/retry-model", null);
  putJob({
    key,
    kind: "model",
    repoId: "org/retry-model",
    variant: null,
    state: "running",
    downloadedBytes: 10,
    completedBytes: 0,
    completeOnDisk: false,
    expectedBytes: 100,
    fraction: 0.1,
    bytesPerSec: 0,
    etaSeconds: 0,
    error: null,
    startedAt: 3,
    transport: "http",
    cancelTransport: "xet",
  });
  assert.ok(flushPersistedState);
  flushPersistedState();

  const persisted = JSON.parse(store.get(PERSIST_KEY) ?? "null");
  assert.equal(persisted.state.jobs[key].transport, "http");
  assert.equal(persisted.state.jobs[key].cancelTransport, "xet");
});

test("a companion-only presentation is written for reload and Resume", () => {
  const key = jobKeyOf("model", "org/flash-next", "UD-Q4_K_XL");
  const presentation = {
    label: "MTP companion",
    filename: "mtp-Flash-Next-shared-Q8_0.gguf",
    expectedBytes: 2_786_568_256,
  };
  putJob({
    key,
    kind: "model",
    repoId: "org/flash-next",
    variant: "UD-Q4_K_XL",
    state: "running",
    downloadedBytes: 100,
    completedBytes: 0,
    completeOnDisk: false,
    expectedBytes: 120,
    presentation,
    fraction: 0.5,
    bytesPerSec: 0,
    etaSeconds: 0,
    error: null,
    startedAt: 4,
    transport: "http",
  });
  assert.ok(flushPersistedState);
  flushPersistedState();

  const persisted = JSON.parse(store.get(PERSIST_KEY) ?? "null");
  assert.deepEqual(persisted.state.jobs[key].presentation, presentation);
  assert.deepEqual(getState().jobs[key]?.presentation, presentation);
});

test("growing a plan freezes the old cached prefix before changing totals", () => {
  const key = jobKeyOf("model", "org/growing-plan", "Q4_K_M");
  putJob({
    key,
    kind: "model",
    repoId: "org/growing-plan",
    variant: "Q4_K_M",
    state: "running",
    downloadedBytes: 105,
    completedBytes: 100,
    completeOnDisk: false,
    expectedBytes: 120,
    presentation: {
      label: "MTP companion",
      filename: "mtp-shared-Q8_0.gguf",
      expectedBytes: 20,
    },
    fraction: 0.875,
    bytesPerSec: 0,
    etaSeconds: 0,
    error: null,
    startedAt: 5,
  });

  setExpectedBytesForJob("model", "org/growing-plan", "Q4_K_M", 200);
  const job = getState().jobs[key];
  assert.equal(job?.presentation?.cachedPlanPrefixBytes, 100);
  assert.equal(job && presentedProgress(job).downloadedBytes, 5);
});

test("the first known plan total stabilizes an adopted companion", () => {
  const key = jobKeyOf("model", "org/adopted-plan", "Q4_K_M");
  putJob({
    key,
    kind: "model",
    repoId: "org/adopted-plan",
    variant: "Q4_K_M",
    state: "running",
    downloadedBytes: 105,
    completedBytes: 100,
    completeOnDisk: false,
    expectedBytes: 0,
    presentation: {
      label: "MTP companion",
      filename: "mtp-shared-Q8_0.gguf",
      expectedBytes: 20,
    },
    fraction: 0,
    bytesPerSec: 0,
    etaSeconds: 0,
    error: null,
    startedAt: 6,
  });

  setExpectedBytesForJob("model", "org/adopted-plan", "Q4_K_M", 120);
  const job = getState().jobs[key];
  assert.equal(job?.presentation?.cachedPlanPrefixBytes, 100);
  assert.equal(job && presentedProgress(job).downloadedBytes, 5);
});

test("a running job is the activity the desktop quit path asks about", () => {
  assert.equal(hasActiveDownloadJob(getState().jobs), true);
});

test("an external job counts too, since a quit kills its transfer as well", () => {
  assert.equal(hasActiveDownloadJob({}), false);
  assert.equal(
    hasActiveDownloadJob({
      external: {
        key: "model:org/external",
        kind: "model",
        repoId: "org/external",
        variant: null,
        state: "running",
        downloadedBytes: 0,
        completedBytes: 0,
        completeOnDisk: false,
        expectedBytes: 0,
        fraction: 0,
        bytesPerSec: 0,
        etaSeconds: 0,
        error: null,
        startedAt: 4,
        external: true,
      },
    }),
    true,
  );
  assert.equal(
    hasActiveDownloadJob({
      done: {
        key: "model:org/done",
        kind: "model",
        repoId: "org/done",
        variant: null,
        state: "complete",
        downloadedBytes: 0,
        completedBytes: 0,
        completeOnDisk: true,
        expectedBytes: 0,
        fraction: 1,
        bytesPerSec: 0,
        etaSeconds: 0,
        error: null,
        startedAt: 4,
      },
    }),
    false,
  );
});

test("a Download to folder survives the reload and is written back", () => {
  // Lost, a cancel after the reload would resume into whatever folder is picked now.
  const key = jobKeyOf("model", "org/pinned-model", null);
  assert.equal(getState().jobs[key]?.downloadDir, "/mnt/e/AI/Models");
  putJob({ ...getState().jobs[key], downloadedBytes: 30 });
  assert.ok(flushPersistedState);
  flushPersistedState();
  const persisted = JSON.parse(store.get(PERSIST_KEY) ?? "null");
  assert.equal(persisted.state.jobs[key].downloadDir, "/mnt/e/AI/Models");
});