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

/** The Downloads overlay must return null when empty; matched via the AST, not text. */

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";

import ts from "@typescript/typescript6";

import { openingTag } from "./helpers/tsx-ast.ts";

const parse = (relative: string, name: string): ts.SourceFile =>
  ts.createSourceFile(
    name,
    readFileSync(new URL(`../src/${relative}`, import.meta.url), "utf8"),
    ts.ScriptTarget.ESNext,
    true,
    ts.ScriptKind.TSX,
  );

const PANEL_PATH = "features/hub/download-manager/download-manager-panel.tsx";
const panel = parse(PANEL_PATH, "download-manager-panel.tsx");
const provider = parse("app/provider.tsx", "provider.tsx");

function panelComponent(): ts.FunctionDeclaration {
  let found: ts.FunctionDeclaration | null = null;
  const visit = (node: ts.Node): void => {
    if (
      ts.isFunctionDeclaration(node) &&
      node.name?.getText() === "DownloadManagerPanel"
    ) {
      found = node;
    }
    ts.forEachChild(node, visit);
  };
  ts.forEachChild(panel, visit);
  assert.ok(found, `${PANEL_PATH} no longer exports a DownloadManagerPanel`);
  return found;
}

function jobKeysBinding(component: ts.FunctionDeclaration): string {
  let name: string | null = null;
  const visit = (node: ts.Node): void => {
    if (
      ts.isVariableDeclaration(node) &&
      ts.isIdentifier(node.name) &&
      node.initializer &&
      ts.isCallExpression(node.initializer) &&
      node.initializer.expression.getText() === "useDownloadManagerStore" &&
      node.initializer.arguments.some((arg) =>
        /orderedJobKeys/i.test(arg.getText()),
      )
    ) {
      name = node.name.getText();
    }
    ts.forEachChild(node, visit);
  };
  ts.forEachChild(component, visit);
  assert.ok(
    name,
    `${PANEL_PATH}: no local is bound from useDownloadManagerStore with the
ordered-job-keys selector, so the guard below cannot be located. If the
selector was renamed, update this test; if the panel stopped reading the
job list at all, it can no longer know when to unmount.`,
  );
  return name;
}

function isEmptyTest(node: ts.Node, jobKeys: string): boolean {
  const length = `${jobKeys}.length`;
  const K = ts.SyntaxKind;
  const shapes: [string, ts.SyntaxKind, string][] = [
    [length, K.EqualsEqualsEqualsToken, "0"],
    ["0", K.EqualsEqualsEqualsToken, length],
    [length, K.LessThanToken, "1"],
    [length, K.LessThanEqualsToken, "0"],
    ["0", K.GreaterThanEqualsToken, length],
    ["1", K.GreaterThanToken, length],
  ];
  if (ts.isBinaryExpression(node)) {
    const left = node.left.getText();
    const right = node.right.getText();
    const op = node.operatorToken.kind;
    return shapes.some(([l, o, r]) => left === l && op === o && right === r);
  }
  return (
    ts.isPrefixUnaryExpression(node) &&
    node.operator === K.ExclamationToken &&
    node.operand.getText() === length
  );
}

type Truth = true | false | null;

function underAnEmptyList(node: ts.Node, jobKeys: string): Truth {
  // Queued plans and preflight starts show before any job exists.
  const declarations = panelComponent().body?.statements.flatMap(statement =>
    ts.isVariableStatement(statement) ? [...statement.declarationList.declarations] : []
  ) ?? [];
  // Prefer the combined route queue over its Hub-only input.
  const combined = declarations.find(declaration =>
    declaration.initializer && ts.isArrayLiteralExpression(declaration.initializer) &&
    declaration.initializer.elements.some(element =>
      ts.isSpreadElement(element) && ts.isCallExpression(element.expression) &&
      element.expression.expression.getText() === "queuedStagedEntries"
    )
  );
  const queued = (combined ?? declarations.find(declaration =>
    declaration.initializer?.getText() === "useQueuedHubEntries()"
  ))?.name.getText();
  const pending = declarations.find(declaration =>
    declaration.initializer && ts.isCallExpression(declaration.initializer) &&
    declaration.initializer.expression.getText() === "visiblePendingStarts"
  )?.name.getText();
  if (
    isEmptyTest(node, jobKeys) ||
    (queued && isEmptyTest(node, queued)) ||
    (pending && isEmptyTest(node, pending))
  ) return true;
  if (ts.isParenthesizedExpression(node)) {
    return underAnEmptyList(node.expression, jobKeys);
  }
  if (
    ts.isPrefixUnaryExpression(node) &&
    node.operator === ts.SyntaxKind.ExclamationToken
  ) {
    const inner = underAnEmptyList(node.operand, jobKeys);
    return inner === null ? null : !inner;
  }
  if (ts.isBinaryExpression(node)) {
    const op = node.operatorToken.kind;
    const left = underAnEmptyList(node.left, jobKeys);
    const right = underAnEmptyList(node.right, jobKeys);
    if (op === ts.SyntaxKind.BarBarToken) {
      if (left === true || right === true) return true;
      if (left === false && right === false) return false;
      return null;
    }
    if (op === ts.SyntaxKind.AmpersandAmpersandToken) {
      if (left === false || right === false) return false;
      if (left === true && right === true) return true;
      return null;
    }
  }
  return null;
}

function returnsNull(statement: ts.Statement): boolean {
  const body = ts.isBlock(statement) ? statement.statements : [statement];
  return body.some(
    (node) =>
      ts.isReturnStatement(node) &&
      node.expression?.kind === ts.SyntaxKind.NullKeyword,
  );
}

test("the Downloads overlay unmounts when there are no jobs", () => {
  const component = panelComponent();
  const jobKeys = jobKeysBinding(component);
  const statements = component.body?.statements ?? ts.factory.createNodeArray();

  const guarded = statements.some(
    (statement) =>
      ts.isIfStatement(statement) &&
      returnsNull(statement.thenStatement) &&
      underAnEmptyList(statement.expression, jobKeys) === true,
  );

  assert.ok(
    guarded,
    `${PANEL_PATH}: DownloadManagerPanel must return null while ${jobKeys} is
empty, at the top level of the component.

Without it the Downloads overlay is permanent: a bottom-right FAB on
every hub route of a fresh install, and a flex slot in the rail that
pushes the loaded models card off the corner even when it paints
nothing. That is PR #9849, which had to be reverted by PR #10298.

Expected something of the shape:  if (... || ${jobKeys}.length === 0) return null;`,
  );
});

test("the rail-facing wrapper can still be squeezed by the rail's cap", () => {
  const source = readFileSync(
    new URL(`../src/${PANEL_PATH}`, import.meta.url),
    "utf8",
  );
  const ternary = source.match(/positioned\s*\?\s*"([^"]*)"\s*:\s*"([^"]*)"/);
  assert.ok(
    ternary,
    `${PANEL_PATH}: the positioned/stacked className ternary is gone, so the
rail-facing branch can no longer be checked`,
  );
  assert.match(
    ternary[1],
    /\bfixed\b.*\bbottom-4\b.*\bright-4\b/,
    `${PANEL_PATH}: the standalone panel has left the bottom-right corner`,
  );
  assert.match(
    ternary[2],
    /\bmin-h-0\b/,
    `${PANEL_PATH}: the stacked branch dropped min-h-0, so a capped rail will
squeeze the update card above this list instead of the list`,
  );
});

test("the Downloads panel sits above the loaded models card in both rails", () => {
  const tags: { name: string; at: number }[] = [];
  const visit = (node: ts.Node): void => {
    const opening = openingTag(node);
    const name = opening?.tagName.getText();
    if (name === "DownloadManagerPanel" || name === "LoadedModelsIndicator") {
      tags.push({ name, at: node.getStart() });
    }
    ts.forEachChild(node, visit);
  };
  ts.forEachChild(provider, visit);
  tags.sort((a, b) => a.at - b.at);

  const panels = tags.filter((t) => t.name === "DownloadManagerPanel");
  const indicators = tags.filter((t) => t.name === "LoadedModelsIndicator");
  assert.ok(
    panels.length > 0 && panels.length === indicators.length,
    `provider.tsx: expected the download panel and the loaded models card to be
paired in every rail, found ${panels.length} and ${indicators.length}`,
  );
  for (let i = 0; i < panels.length; i += 1) {
    assert.ok(
      panels[i].at < indicators[i].at,
      "provider.tsx: DownloadManagerPanel is rendered below LoadedModelsIndicator.\n" +
        "The rail is a bottom-anchored column, so the last child is the one on\n" +
        "the corner, and that is meant to be the loaded models card.",
    );
  }
});