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.4 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

// Hidden tabs clamp timers to about once a minute, which would skew the rate estimator.

import assert from "node:assert/strict";
import test from "node:test";

import {
  installLocalStorageFake,
  registerBundlerResolver,
} from "./helpers/kit.ts";

registerBundlerResolver();
installLocalStorageFake();
Object.assign(globalThis.window, { addEventListener: () => {} });

const visibility = { hidden: false };
Object.defineProperty(globalThis, "document", {
  configurable: true,
  value: {
    get hidden() {
      return visibility.hidden;
    },
  },
});

const { startExternalJob, updateExternalJob } = await import(
  "../src/features/hub/download-manager/external-jobs.ts"
);
const { getState } = await import(
  "../src/features/hub/download-manager/download-manager-state.ts"
);

const KEY = "model:org/stt-mirror";
const MB = 1e6;

test("a hidden tab's throttled samples never become a published rate", () => {
  const realNow = Date.now;
  let now = realNow();
  Date.now = () => now;
  try {
    startExternalJob({
      key: KEY,
      repoId: "org/stt-mirror",
      variant: null,
      expectedBytes: 8_000 * MB,
      cancel: () => {},
    });

    visibility.hidden = false;
    let bytes = 0;
    for (let i = 0; i <= 60; i += 1) {
      now += 750;
      bytes += 37.5 * MB;
      updateExternalJob(KEY, {
        downloadedBytes: bytes,
        expectedBytes: 8_000 * MB,
      });
    }
    assert.ok(
      getState().jobs[KEY].bytesPerSec > 0,
      "the visible phase should publish a rate, or this test proves nothing",
    );

    visibility.hidden = true;
    for (let i = 0; i < 10; i += 1) {
      now += 60_000;
      bytes += 3_000 * MB;
      updateExternalJob(KEY, {
        downloadedBytes: bytes,
        expectedBytes: 8_000 * MB,
      });
    }

    const job = getState().jobs[KEY];
    assert.ok(job, "the external job should still exist");
    assert.equal(job.bytesPerSec, 0, "a hidden tab must publish no rate");
    assert.equal(job.etaSeconds, 0, "a hidden tab must publish no ETA");
    assert.equal(job.downloadedBytes, bytes);
  } finally {
    Date.now = realNow;
  }
});

test("package installation activity stays indeterminate and keeps cancellation", async () => {
  const { updateExternalActivity, cancelExternalJob, finishExternalJob } =
    await import("../src/features/hub/download-manager/external-jobs.ts");
  Object.assign(globalThis.window, {
    setTimeout: () => 1,
    clearTimeout: () => {},
  });
  let cancelled = false;
  const key = "engine:vllm";
  startExternalJob({
    key,
    repoId: "vLLM",
    variant: "0.20.0",
    expectedBytes: 0,
    cancel: () => {
      cancelled = true;
    },
  });
  updateExternalActivity(key, "Downloading torch (900 MiB)", [
    "Downloading torch (900 MiB)",
  ]);
  const job = getState().jobs[key];
  assert.equal(job.activity, "Downloading torch (900 MiB)");
  assert.deepEqual(job.details, ["Downloading torch (900 MiB)"]);
  assert.equal(job.expectedBytes, 0);
  assert.equal(job.fraction, 0);
  assert.equal(job.etaSeconds, 0);
  await cancelExternalJob(key);
  assert.equal(cancelled, true);
  assert.equal(getState().jobs[key].state, "cancelling");
  finishExternalJob(key, "cancelled");
  updateExternalActivity(key, "stale update", []);
  assert.equal(getState().jobs[key].state, "cancelled");
  assert.notEqual(getState().jobs[key].activity, "stale update");
});