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.

2.1 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-tab poll gaps time the poller, not the transfer, so hidden samples must be dropped.

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

import {
  type TransferSample,
  appendSample,
  computeTransferStats,
} from "../src/lib/transfer-stats.ts";

import { readSrc } from "./helpers/kit.ts";

const MB = 1e6;

test("the chat estimator drops samples taken while the tab is hidden", () => {
  const source = readSrc("features/chat/hooks/use-chat-model-runtime.ts");
  const guard = source.indexOf("document.hidden");
  assert.ok(guard > 0, "the chat poller should skip a hidden tab");
  const clear = source.indexOf("samples.length = 0", guard);
  const sample = source.indexOf("appendSample(samples,", guard);
  assert.ok(
    clear > 0 && clear < sample,
    "the hidden branch must clear, not sample",
  );
});

test("throttled hidden samples do not hold a stale rate once progress stops", () => {
  const heldSeconds = (dropWhileHidden: boolean) => {
    const samples: TransferSample[] = [];
    let rate = 0;
    const poll = (t: number, b: number, hidden: boolean) => {
      if (dropWhileHidden && hidden) {
        samples.length = 0;
        rate = 0;
        return;
      }
      appendSample(samples, t, b);
      const stats = computeTransferStats(samples, 100_000 * MB);
      rate = stats.stable ? stats.rateBytesPerSecond : 0;
    };

    let bytes = 0;
    let t = 0;
    for (; t <= 60; t += 2) {
      bytes = t * 50 * MB;
      poll(t, bytes, false);
    }
    for (; t <= 660; t += 60) {
      bytes = t * 50 * MB;
      poll(t, bytes, true);
    }
    let held = 0;
    for (const end = t + 300; t <= end; t += 2) {
      poll(t, bytes, false);
      if (rate > 0) held += 2;
    }
    return held;
  };

  assert.ok(
    heldSeconds(false) > 30,
    "without the guard the throttled cadence should still hold a stale rate",
  );
  assert.equal(
    heldSeconds(true),
    0,
    "with the guard nothing survives the hidden stretch",
  );
});