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

// Status polls twice as often as progress, so a generation change is held until a progress poll.

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

import {
  POLL_INTERVAL_MS,
  PROGRESS_POLL_INTERVAL_MS,
} from "../src/features/hub/download-manager/download-manager-config.ts";

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

const source = readSrc("features/hub/download-manager/poll-loop.ts");

test("status ticks outnumber progress polls, so the race is the normal case", () => {
  assert.ok(
    PROGRESS_POLL_INTERVAL_MS > POLL_INTERVAL_MS,
    "the race only exists because progress is polled less often than status",
  );
});

test("a generation change is held until a progress poll consumes it", () => {
  const set = source.indexOf("rt.pendingGenerationChange = true");
  const read = source.indexOf("rt.pendingGenerationChange === true");
  const clear = source.indexOf("rt.pendingGenerationChange = false");
  assert.ok(set > 0, "a generation change should be recorded on the runtime");
  assert.ok(read > 0, "the progress path should read the held change");
  assert.ok(clear > read, "it should be cleared only after it is read");
  assert.doesNotMatch(
    source,
    /const generationChanged = syncServerGeneration\(/,
    "reading the return value directly reintroduces the swallow",
  );
});

test("a change seen between progress polls still reaches reconcile", () => {
  const consumed: boolean[] = [];
  const rt: { pendingGenerationChange?: boolean } = {};
  let storedGeneration = 1;
  let lastProgressAt = 0;

  const tick = (now: number, serverGeneration: number, sticky: boolean) => {
    const changed = serverGeneration !== storedGeneration;
    storedGeneration = serverGeneration;
    if (sticky && changed) rt.pendingGenerationChange = true;
    if (now - lastProgressAt < PROGRESS_POLL_INTERVAL_MS) return;
    lastProgressAt = now;
    if (sticky) {
      consumed.push(rt.pendingGenerationChange === true);
      rt.pendingGenerationChange = false;
    } else {
      consumed.push(changed);
    }
  };

  const run = (sticky: boolean) => {
    consumed.length = 0;
    rt.pendingGenerationChange = false;
    storedGeneration = 1;
    lastProgressAt = 0;
    for (let now = 0; now <= 3_000; now += POLL_INTERVAL_MS) {
      tick(now, now >= 500 ? 2 : 1, sticky);
    }
    return consumed.some(Boolean);
  };

  assert.equal(run(false), false, "without the flag the change is swallowed");
  assert.equal(run(true), true, "with it the progress path still sees it");
});