// 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";

// The indicator .tsx pulls in the router, motion and hugeicons, so it cannot be
// imported here. The status to row mapping lives in a plain module, driven directly.
import { modelIdsMatch } from "../src/features/hub/lib/model-identity.ts";
import {
  type LoadedModelEntry,
  describeDiffusionStatus,
  describeInferenceStatus,
  describeSttStatus,
  describeVideoStatus,
  loadedModelTarget,
  mergeLoadedModels,
  withPendingLoads,
  shortModelLabel,
  verifyResident,
} from "../src/features/loaded-models/loaded-models-sources.ts";

// Only the fields the mapping reads; the real responses carry dozens more.
function inferenceStatus(
  overrides: Record<string, unknown> = {},
): Parameters<typeof describeInferenceStatus>[0] {
  return {
    active_model: null,
    is_vision: false,
    loading: [],
    loaded: [],
    ...overrides,
  } as Parameters<typeof describeInferenceStatus>[0];
}

test("no runtime loaded produces no rows", () => {
  assert.deepEqual(describeInferenceStatus(inferenceStatus()), []);
  assert.deepEqual(describeDiffusionStatus({ loaded: false } as never), []);
  assert.deepEqual(describeVideoStatus({ loaded: false } as never), []);
  assert.deepEqual(describeSttStatus({}), []);
});

test("an unreachable runtime yields no rows rather than throwing", () => {
  assert.deepEqual(describeInferenceStatus(null), []);
  assert.deepEqual(describeDiffusionStatus(null), []);
  assert.deepEqual(describeVideoStatus(null), []);
  assert.deepEqual(describeSttStatus(null), []);
});

test("a GGUF chat model reports its variant", () => {
  const [row] = describeInferenceStatus(
    inferenceStatus({
      active_model: "unsloth/gemma-3-4b-it-GGUF",
      is_gguf: true,
      gguf_variant: "Q4_K_M",
      loaded: ["unsloth/gemma-3-4b-it-GGUF"],
    }),
  );
  assert.equal(row.kind, "text");
  assert.equal(row.source, "chat");
  assert.equal(row.detail, "GGUF · Q4_K_M");
});

// Same picker, same memory, but only one of them answers prompts.
test("an audio model is a speech row, and a whisper one is dictation", () => {
  const [tts] = describeInferenceStatus(
    inferenceStatus({
      active_model: "unsloth/orpheus-3b-0.1-ft",
      is_audio: true,
      audio_type: "tts",
    }),
  );
  assert.equal(tts.kind, "tts");
  const [stt] = describeInferenceStatus(
    inferenceStatus({
      active_model: "openai/whisper-large-v3",
      is_audio: true,
      audio_type: "whisper",
    }),
  );
  assert.equal(stt.kind, "stt");
  // Still the chat runtime's, so it ejects through /api/inference/unload.
  assert.equal(stt.source, "chat");
});

test("a model the runtime still holds besides the active one gets its own row", () => {
  const rows = describeInferenceStatus(
    inferenceStatus({
      active_model: "unsloth/Llama-3.2-3B",
      loaded: ["unsloth/Llama-3.2-3B", "unsloth/Qwen3-4B"],
    }),
  );
  assert.equal(rows.length, 2);
  assert.equal(rows[0].inactive, undefined);
  assert.equal(rows[1].name, "unsloth/Qwen3-4B");
  assert.equal(rows[1].inactive, true);
});

// A server predating the engine split reports only the top-level fields.
test("a legacy STT status still shows its resident Transformers model", () => {
  const rows = describeSttStatus({
    loaded_model: "openai/whisper-large-v3",
    device: "cuda",
  });
  assert.deepEqual(
    rows.map((row) => [row.sttEngine, row.name, row.detail]),
    [["transformers", "openai/whisper-large-v3", "Transformers · cuda"]],
  );
});

test("an engine block wins over the legacy fields, and never doubles a row", () => {
  const rows = describeSttStatus({
    loaded_model: "openai/whisper-large-v3",
    device: "cuda",
    transformers: { loaded_model: null },
  });
  assert.deepEqual(rows, []);
});

test("each STT engine that has a model resident gets a row naming its engine", () => {
  const rows = describeSttStatus({
    transformers: { loaded_model: null },
    mtmd: { loaded_model: "unsloth/voxtral-mini", device: "cuda" },
    gguf: { loaded_model: "ggml-base.en", device: "metal" },
  });
  assert.deepEqual(
    rows.map((row) => [row.sttEngine, row.name]),
    [
      ["mtmd", "unsloth/voxtral-mini"],
      ["gguf", "ggml-base.en"],
    ],
  );
  assert.equal(rows[0].detail, "llama.cpp · cuda");
});

// Those two sidecars report their engine name as the device, so the label and
// the device are the same string and must not print twice.
test("an engine that reports itself as its device is named once", () => {
  const rows = describeSttStatus({
    mtmd: { loaded_model: "qwen3-asr-0.6b", device: "llama.cpp" },
    gguf: { loaded_model: "ggml-base.en", device: "whisper.cpp" },
  });
  assert.deepEqual(
    rows.map((row) => row.detail),
    ["llama.cpp", "whisper.cpp"],
  );
});

test("a real device is still reported next to its engine", () => {
  const [row] = describeSttStatus({
    transformers: { loaded_model: "openai/whisper-large-v3", device: "cuda" },
  });
  assert.equal(row.detail, "Transformers · cuda");
});

test("image and video rows omit the parts the backend did not report", () => {
  const [image] = describeDiffusionStatus({
    loaded: true,
    repo_id: "unsloth/FLUX.1-dev",
    family: "flux",
    device: null,
  } as never);
  assert.equal(image.detail, "flux");
  const [video] = describeVideoStatus({
    loaded: true,
    repo_id: "unsloth/Wan2.2-T2V-A14B",
    family: "wan",
    model_kind: "gguf",
    device: "cuda",
  } as never);
  assert.equal(video.detail, "wan · GGUF · cuda");
});

test("a row names the precision the pipeline actually loaded at", () => {
  const [image] = describeDiffusionStatus({
    loaded: true,
    repo_id: "unsloth/FLUX.1-dev",
    family: "flux",
    dtype: "bfloat16",
    device: "cuda",
  } as never);
  assert.equal(image.detail, "flux · BF16 · cuda");

  // The dense transformer's quantisation is what tells the builds apart, so it
  // wins over the pipeline dtype.
  const [video] = describeVideoStatus({
    loaded: true,
    repo_id: "unsloth/Wan2.2-T2V-A14B",
    family: "wan",
    dtype: "bfloat16",
    transformer_quant: "fp8",
    device: "cuda",
  } as never);
  assert.equal(video.detail, "wan · FP8 · cuda");
});

test("a bf16 video load falls back to the pipeline dtype", () => {
  const [video] = describeVideoStatus({
    loaded: true,
    repo_id: "unsloth/Wan2.2-T2V-A14B",
    family: "wan",
    dtype: "bfloat16",
    // "none" is the backend's word for plain bf16, not a precision to print.
    transformer_quant: "none",
    device: "cuda",
  } as never);
  assert.equal(video.detail, "wan · BF16 · cuda");
});

test("a GGUF video row names its selected quant instead of its compute dtype", () => {
  const [video] = describeVideoStatus({
    loaded: true,
    repo_id: "unsloth/Wan2.2-T2V-A14B-GGUF",
    family: "wan",
    model_kind: "gguf",
    gguf_variant: "Q4_K_M",
    dtype: "bfloat16",
    device: "cuda",
  } as never);
  assert.equal(video.detail, "wan · GGUF · Q4_K_M · cuda");
});

test("a lowercase quant filename still reads as an upper-case quant", () => {
  // Hub repos ship q8_0 filenames, and every other quant label in the UI is upper-cased.
  const [image] = describeDiffusionStatus({
    loaded: true,
    repo_id: "unsloth/Z-Image-Turbo-GGUF",
    family: "z-image",
    model_kind: "gguf",
    gguf_variant: "q8_0",
    dtype: "bfloat16",
    device: "cuda",
  } as never);
  assert.equal(image.detail, "z-image · GGUF · Q8_0 · cuda");
});

test("a GGUF image load does not print GGUF twice", () => {
  const [image] = describeDiffusionStatus({
    loaded: true,
    repo_id: "unsloth/FLUX.1-dev-GGUF",
    family: "flux",
    model_kind: "gguf",
    dtype: "gguf",
    device: "cuda",
  } as never);
  assert.equal(image.detail, "flux · GGUF · cuda");
});

test("a GGUF image row names the quant that was picked, not the compute dtype", () => {
  // The reported bug: the picker chip said "GGUF \u00b7 Q8_0" and the row beside it said "BF16",
  // because `dtype` is the pipeline COMPUTE dtype and reads bf16 for every CUDA load. The
  // quant is what distinguishes the file that was downloaded and opened.
  const [image] = describeDiffusionStatus({
    loaded: true,
    repo_id: "unsloth/Z-Image-Turbo-GGUF",
    family: "z-image",
    model_kind: "gguf",
    gguf_variant: "Q8_0",
    transformer_quant: null,
    dtype: "bfloat16",
    device: "cuda",
  } as never);
  assert.equal(image.detail, "z-image \u00b7 GGUF \u00b7 Q8_0 \u00b7 cuda");
});

test("a native GGUF image row names its selected quant without model_kind", () => {
  // The sd.cpp engine reports dtype "gguf" and no model_kind, so the GGUF chip and the quant
  // both have to survive on that field alone.
  const [image] = describeDiffusionStatus({
    loaded: true,
    repo_id: "unsloth/Z-Image-Turbo-GGUF",
    family: "z-image",
    gguf_variant: "Q8_0",
    dtype: "gguf",
    device: "cpu",
  } as never);
  assert.equal(image.detail, "z-image \u00b7 GGUF \u00b7 Q8_0 \u00b7 cpu");
});

test("a GGUF pick the dense fast path replaced names that build instead", () => {
  // The fast path denoises with a torchao build of the base transformer and never opens the
  // .gguf, so the row must neither call it GGUF nor print a quant no tensor carries.
  const [image] = describeDiffusionStatus({
    loaded: true,
    repo_id: "unsloth/Z-Image-Turbo-GGUF",
    family: "z-image",
    model_kind: "gguf",
    gguf_variant: "Q8_0",
    transformer_quant: "fp8",
    dtype: "bfloat16",
    device: "cuda",
  } as never);
  assert.equal(image.detail, "z-image \u00b7 FP8 \u00b7 cuda");
});

// Gemma 3n and friends take audio in but answer as chat. Every backend sets
// is_audio from `audio_type is not None and audio_type != "audio_vlm"`
// (model_config.py, mlx_inference.py; llama_cpp.py keeps _is_audio False for
// csm/whisper/audio_vlm), so the TTS test never sees one.
test("an audio-input VLM stays a chat row, not Speech", () => {
  const [row] = describeInferenceStatus(
    inferenceStatus({
      active_model: "unsloth/gemma-3n-E4B-it",
      is_audio: false,
      audio_type: "audio_vlm",
      has_audio_input: true,
    }),
  );
  assert.equal(row.kind, "text");
});

test("a row opens the page its runtime is used on", () => {
  assert.deepEqual(loadedModelTarget("chat"), {
    open: "route",
    to: "/chat",
    label: "Chat",
  });
  assert.deepEqual(loadedModelTarget("image"), {
    open: "route",
    to: "/images",
    label: "Images",
  });
  assert.deepEqual(loadedModelTarget("video"), {
    open: "route",
    to: "/video",
    label: "Video",
  });
});

// Dictation has no page of its own, so it opens the tab that drives it.
test("a dictation row opens Voice settings", () => {
  assert.deepEqual(loadedModelTarget("stt"), {
    open: "settings",
    tab: "voice",
    label: "Voice settings",
  });
});

// A Whisper checkpoint in the chat slot is Chat's, not dictation's: the target
// follows the runtime holding the weights, not what the model does.
test("the target follows the runtime, not the kind", () => {
  const [chatWhisper] = describeInferenceStatus(
    inferenceStatus({
      active_model: "unsloth/whisper-large-v3",
      is_audio: true,
      audio_type: "whisper",
    }),
  );
  assert.equal(chatWhisper.kind, "stt");
  assert.equal(loadedModelTarget(chatWhisper.source).label, "Chat");
});

test("every runtime's rows appear together, in a fixed order", () => {
  const merged = mergeLoadedModels([
    describeInferenceStatus(
      inferenceStatus({
        active_model: "unsloth/orpheus-3b-0.1-ft",
        is_audio: true,
      }),
    ),
    describeDiffusionStatus({
      loaded: true,
      repo_id: "unsloth/FLUX.1-dev",
    } as never),
    describeVideoStatus(null),
    describeSttStatus({ gguf: { loaded_model: "ggml-base.en" } }),
  ]);
  assert.deepEqual(
    merged.map((row) => row.kind),
    ["tts", "image", "stt"],
  );
});

test("one runtime naming the same model twice is still one row", () => {
  const duplicated: LoadedModelEntry[] = [
    {
      id: "chat:unsloth/Qwen3-4B",
      kind: "text",
      source: "chat",
      name: "unsloth/Qwen3-4B",
      detail: "GGUF",
    },
  ];
  assert.equal(mergeLoadedModels([duplicated, duplicated]).length, 1);
});

// /images/unload, /video/unload and the STT unload carry no model id, so a row
// up to one poll old must be checked against the runtime before either fires.
test("a runtime holding the row's model is safe to unload", () => {
  assert.equal(
    verifyResident("unsloth/FLUX.1-dev", "unsloth/FLUX.1-dev", modelIdsMatch),
    "match",
  );
});

test("a runtime holding something else must not be unloaded", () => {
  assert.equal(
    verifyResident("unsloth/FLUX.1-dev", "unsloth/Qwen-Image", modelIdsMatch),
    "replaced",
  );
});

test("an idle runtime is already free, so there is nothing to unload", () => {
  assert.equal(
    verifyResident("unsloth/FLUX.1-dev", null, modelIdsMatch),
    "gone",
  );
  assert.equal(
    verifyResident("unsloth/FLUX.1-dev", undefined, modelIdsMatch),
    "gone",
  );
});

// These runtimes report repo_id / loaded_model, the same fields the rows were
// built from, so matching is exact bar the tolerance modelIdsMatch already has.
// A spurious "replaced" would refuse a legitimate eject, so pin that too.
test("a trailing separator or casing difference is not a replacement", () => {
  assert.equal(
    verifyResident("/models/flux", "/models/flux/", modelIdsMatch),
    "match",
  );
  assert.equal(
    verifyResident("unsloth/FLUX.1-dev", "unsloth/flux.1-dev", modelIdsMatch),
    "match",
  );
});

test("a local load shows its model folder rather than leading directories", () => {
  assert.equal(
    shortModelLabel("unsloth/gemma-3-4b-it"),
    "unsloth/gemma-3-4b-it",
  );
  assert.equal(
    shortModelLabel("/Users/me/models/hub/gemma-3-4b-it"),
    "hub/gemma-3-4b-it",
  );
  // Windows path, trailing separator: still the last two segments.
  assert.equal(shortModelLabel("C:\\models\\hub\\gemma\\"), "hub/gemma");
});

// The load toast appears at once, the poll is 5s behind it. /status reports a
// load for its whole duration, so the row can match the toast.
test("a chat model still loading gets its own row", () => {
  const rows = describeInferenceStatus(
    inferenceStatus({ loading: ["unsloth/Qwen3.5-9B-GGUF"] }),
  );
  assert.equal(rows.length, 1);
  assert.equal(rows[0].loading, true);
  assert.equal(rows[0].detail, "Loading");
});

test("a model that finished loading is not listed twice", () => {
  const rows = describeInferenceStatus(
    inferenceStatus({
      active_model: "unsloth/Qwen3.5-9B-GGUF",
      is_gguf: true,
      loading: ["unsloth/Qwen3.5-9B-GGUF"],
    }),
  );
  assert.equal(rows.length, 1);
  assert.notEqual(rows[0].loading, true);
});

test("a dictation sidecar that is starting shows as loading", () => {
  const [row] = describeSttStatus({ mtmd: { loading: true } });
  assert.equal(row.loading, true);
  assert.equal(row.sttEngine, "mtmd");
});

test("an announced load shows before any status confirms it", () => {
  const rows = withPendingLoads(
    [],
    new Map([["image", "unsloth/Z-Image-Turbo-GGUF"]]),
  );
  assert.equal(rows.length, 1);
  assert.equal(rows[0].kind, "image");
  assert.equal(rows[0].loading, true);
  assert.equal(rows[0].name, "unsloth/Z-Image-Turbo-GGUF");
});

// The backend's answer wins: otherwise a finished load shows twice for the
// moment between the status arriving and the settle event.
test("a status row for that runtime replaces the announced one", () => {
  const loaded = describeDiffusionStatus({
    loaded: true,
    repo_id: "unsloth/Z-Image-Turbo-GGUF",
    family: "z-image",
  } as never);
  const rows = withPendingLoads(
    loaded,
    new Map([["image", "unsloth/Z-Image-Turbo-GGUF"]]),
  );
  assert.equal(rows.length, 1);
  assert.notEqual(rows[0].loading, true);
});

test("nothing announced leaves the polled rows untouched", () => {
  const rows: LoadedModelEntry[] = [];
  assert.equal(withPendingLoads(rows, new Map()), rows);
});

// Swapping one image model for another: the outgoing one stays resident until
// the backend drops it, so yielding on source alone showed nothing loading for
// the whole swap, which is exactly when the toast says it is working.
test("a swap shows the incoming model alongside the outgoing one", () => {
  const resident = describeDiffusionStatus({
    loaded: true,
    repo_id: "unsloth/FLUX.1-dev",
    family: "flux",
  } as never);
  const rows = withPendingLoads(
    resident,
    new Map([["image", "unsloth/Z-Image-Turbo-GGUF"]]),
  );
  assert.equal(rows.length, 2);
  const incoming = rows.find((row) => row.loading);
  assert.equal(incoming?.name, "unsloth/Z-Image-Turbo-GGUF");
  assert.ok(rows.some((row) => row.name === "unsloth/FLUX.1-dev"));
});

test("the announced row yields once that same model is resident", () => {
  const resident = describeDiffusionStatus({
    loaded: true,
    repo_id: "unsloth/Z-Image-Turbo-GGUF",
    family: "z-image",
  } as never);
  const rows = withPendingLoads(
    resident,
    new Map([["image", "unsloth/Z-Image-Turbo-GGUF"]]),
  );
  assert.equal(rows.length, 1);
  assert.notEqual(rows[0].loading, true);
});
