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.

36.5 KB
Raw Blame
"""Checks that one desktop version tag can only ever serve one set of binaries."""

from __future__ import annotations

import base64
import fnmatch
import json
import os
import shlex
import subprocess
from pathlib import Path

import yaml


REPO_ROOT = Path(__file__).resolve().parents[2]
WORKFLOW = REPO_ROOT / ".github" / "workflows" / "release-desktop.yml"

RELEASE_TAG = "v0.1.50-beta"
SOURCE_SHA = "1f02275b86f0e0d3a5b1c9f2a4d6e8b0c2a4e6f8"


def _workflow():
    return yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))


def _steps(workflow, job):
    return workflow["jobs"][job]["steps"]


def _step_index(workflow, job, name):
    """Locate a step and report its available names on failure."""
    names = [step.get("name") for step in _steps(workflow, job)]
    assert name in names, f"{job} has no step named {name!r}; steps are {names}"
    return names.index(name)


def _step(workflow, job, name):
    return _steps(workflow, job)[_step_index(workflow, job, name)]


def test_the_release_build_restores_but_never_saves_the_rust_cache():
    """The release build must never save its Rust cache, since the job holds the signing credentials."""
    cache = _step(_workflow(), "build", "Rust cache")
    assert cache["with"]["workspaces"] == "studio/src-tauri -> target"
    assert cache["with"]["save-if"] is False, (
        f"the release build's rust-cache has save-if={cache['with']['save-if']!r}, so some "
        f"platform writes a cache from the job that holds the signing credentials. Caches "
        f"written on the default branch are restorable by every pull request. Keep this "
        f"`save-if: false`; restoring is free of that risk, and a dispatch-only release "
        f"does not run often enough for the save to be worth it."
    )


def _write_fake_gh(path: Path):
    """Record gh arguments and return configured statuses."""
    path.write_text(
        """#!/bin/sh
set -eu
printf 'gh %s\\n' "$*" >> "$COMMAND_LOG"
if [ "$1" = "api" ]; then
  include=0
  endpoint=""
  for argument in "$@"; do
    case "$argument" in
      --include) include=1 ;;
      repos/*) endpoint="$argument" ;;
    esac
  done
  case "$endpoint" in
    */commits/*) printf '%s\n' "$SOURCE_COMMIT_SHA"; exit 0 ;;
    */releases/tags/*) status="$TARGET_HTTP_STATUS" ;;
    *) exit 0 ;;
  esac
  if [ "$include" = "1" ]; then
    printf 'HTTP/2.0 %s Test Response\n' "$status"
  fi
  if [ "$status" = "200" ]; then
    if [ "$TARGET_HAS_DESKTOP_ASSETS" = "1" ]; then
      printf '{"tag_name":"%s","draft":false,"assets":[{"name":"latest.json"}]}\n' "$DESKTOP_RELEASE_TAG"
    else
      printf '{"tag_name":"%s","draft":false,"assets":[]}\n' "$DESKTOP_RELEASE_TAG"
    fi
    exit 0
  fi
  exit 1
fi

if [ "$1" = "release" ] && [ "$2" = "download" ]; then
  if [ "$TARGET_HAS_DESKTOP_ASSETS" != "1" ]; then
    echo "release not found" >&2
    exit 1
  fi
  directory=""
  want_directory=0
  for argument in "$@"; do
    if [ "$want_directory" = "1" ]; then directory="$argument"; want_directory=0; continue; fi
    [ "$argument" = "--dir" ] && want_directory=1
  done
  [ -n "$directory" ] || directory="."
  mkdir -p "$directory"
  printf '{"version":"%s","platforms":{}}\n' "$TARGET_MANIFEST_VERSION" > "$directory/latest.json"
  exit 0
fi

exit 0
""",
        encoding = "utf-8",
    )
    path.chmod(0o755)


def _run_step(
    workflow,
    job: str,
    name: str,
    tmp_path: Path,
    *,
    target_http_status: int = 200,
    target_has_desktop_assets: bool = False,
    target_manifest_version: str = RELEASE_TAG,
    extra_env: dict[str, str] | None = None,
):
    fake_bin = tmp_path / "bin"
    fake_bin.mkdir(exist_ok = True)
    _write_fake_gh(fake_bin / "gh")
    log = tmp_path / "commands.log"
    log.write_text("", encoding = "utf-8")

    env = os.environ.copy()
    env.update(
        {
            "COMMAND_LOG": str(log),
            "DESKTOP_RELEASE_TAG": RELEASE_TAG,
            "GH_REPO": "unslothai/unsloth",
            "GITHUB_OUTPUT": str(tmp_path / "github-output"),
            "GH_TOKEN": "masked-token",
            "PATH": f"{fake_bin}:{env['PATH']}",
            "RUNNER_TEMP": str(tmp_path),
            "SOURCE_COMMIT_SHA": SOURCE_SHA,
            "TARGET_HAS_DESKTOP_ASSETS": "1" if target_has_desktop_assets else "0",
            "TARGET_HTTP_STATUS": str(target_http_status),
            "TARGET_MANIFEST_VERSION": target_manifest_version,
        }
    )
    env.update(extra_env or {})
    result = subprocess.run(
        ["bash", "-c", _step(workflow, job, name)["run"]],
        cwd = tmp_path,
        env = env,
        text = True,
        capture_output = True,
        check = False,
    )
    return result, log.read_text(encoding = "utf-8").splitlines()


def _stage_assets(tmp_path: Path) -> None:
    """Create the one release asset set."""
    asset_dir = tmp_path / "desktop-release-assets"
    asset_dir.mkdir(exist_ok = True)
    signature = base64.b64encode(
        b"untrusted comment: signature from tauri secret key\n"
        b"test signature bytes\n"
        b"trusted comment: timestamp:1\tfile:test\n"
        b"test global signature bytes\n"
    )
    for name, payload in (
        ("Unsloth-Desktop-MacOS.dmg", b"disk image"),
        ("Unsloth-Desktop-Ubuntu.deb", b"package"),
        ("Unsloth-Desktop-Ubuntu.deb.sig", signature),
        ("Unsloth-Desktop-Ubuntu-ARM64.deb", b"arm64 package"),
        ("Unsloth-Desktop-Ubuntu-ARM64.deb.sig", signature),
        ("Unsloth-Desktop-ARM64.app.tar.gz", b"mac updater"),
        ("Unsloth-Desktop-ARM64.app.tar.gz.sig", signature),
        ("Unsloth-Desktop-Linux.AppImage", b"linux updater"),
        ("Unsloth-Desktop-Linux.AppImage.sig", signature),
        ("Unsloth-Desktop-Windows.exe", b"installer"),
        ("Unsloth-Desktop-Windows.exe.sig", signature),
        ("Unsloth-Desktop-Windows-ARM64.exe", b"arm64 installer"),
        ("Unsloth-Desktop-Windows-ARM64.exe.sig", signature),
    ):
        (asset_dir / name).write_bytes(payload)


def _run_create_release(
    workflow,
    tmp_path: Path,
    *,
    invalid_signature = False,
    missing_debian_signature = False,
    missing_debian_arm64_signature = False,
    **kwargs,
):
    _stage_assets(tmp_path)
    if missing_debian_signature:
        (tmp_path / "desktop-release-assets" / "Unsloth-Desktop-Ubuntu.deb.sig").unlink()
    if missing_debian_arm64_signature:
        (tmp_path / "desktop-release-assets" / "Unsloth-Desktop-Ubuntu-ARM64.deb.sig").unlink()
    if invalid_signature:
        (tmp_path / "desktop-release-assets" / "Unsloth-Desktop-Linux.AppImage.sig").write_text(
            "Tauri signer diagnostic, not a signature\n", encoding = "utf-8"
        )
    env = {
        "DESKTOP_RELEASE_NOTES": workflow["env"]["DESKTOP_RELEASE_NOTES"],
        "APP_VERSION": "0.1.50",
        "GITHUB_SHA": SOURCE_SHA,
        "GITHUB_REPOSITORY": "unslothai/unsloth",
        "PYPI_VERSION": "2026.8.7",
        "RELEASE_DRAFT": "true",
        "STUDIO_VERSION": "v0.1.50-beta",
    }
    env.update(kwargs.pop("extra_env", None) or {})

    names = (
        "Validate versioned release state",
        "Generate versioned updater metadata",
    )
    host = "Generate versioned updater metadata"
    create_step = _step(workflow, "publish-release", host)
    create_step["run"] = "\n".join(
        _step(workflow, "publish-release", name)["run"] for name in names
    )
    return _run_step(
        workflow,
        "publish-release",
        host,
        tmp_path,
        extra_env = env,
        **kwargs,
    )


def _upload_commands(workflow):
    commands = []
    for step in _steps(workflow, "publish-release"):
        for line in step.get("run", "").replace("\\\n", " ").splitlines():
            stripped = line.strip()
            if stripped.startswith("gh release upload"):
                commands.append(stripped)
    return commands


def test_a_used_version_fails_the_guard_before_any_build_work(tmp_path):
    workflow = _workflow()
    assert _step_index(
        workflow, "prepare-version", "Guard against republishing an existing version"
    ) < _step_index(workflow, "prepare-version", "Verify PyPI package and Unsloth stamp")
    assert workflow["jobs"]["build"]["needs"] == "prepare-version"

    for case, expected in (
        ({"target_has_desktop_assets": True}, 1),
        ({"target_http_status": 404}, 1),
        ({}, 0),
    ):
        case_dir = tmp_path / ("-".join(case) or "unused-version")
        case_dir.mkdir()
        result, _ = _run_step(
            workflow,
            "prepare-version",
            "Guard against republishing an existing version",
            case_dir,
            **case,
        )
        assert result.returncode == expected, (case, result.stderr)
        if expected:
            assert RELEASE_TAG in result.stderr


def test_a_missing_target_release_says_how_to_create_it(tmp_path):
    workflow = _workflow()
    result, _ = _run_step(
        workflow,
        "prepare-version",
        "Guard against republishing an existing version",
        tmp_path,
        target_http_status = 404,
    )
    assert result.returncode == 1
    assert f"Release {RELEASE_TAG} does not exist." in result.stderr
    assert "Tag main and publish it first" in result.stderr


def test_existing_desktop_assets_name_the_cleanup_command(tmp_path):
    workflow = _workflow()
    result, _ = _run_step(
        workflow,
        "prepare-version",
        "Guard against republishing an existing version",
        tmp_path,
        target_has_desktop_assets = True,
    )
    assert result.returncode == 1
    assert f"gh release delete-asset {RELEASE_TAG} latest.json --yes" in result.stderr


def test_a_failed_guard_probe_fails_closed_before_any_build_work(tmp_path):
    workflow = _workflow()
    result, _ = _run_step(
        workflow,
        "prepare-version",
        "Guard against republishing an existing version",
        tmp_path,
        target_http_status = 500,
    )
    assert result.returncode == 1
    assert "Could not read release" in result.stderr


def test_publish_refuses_to_reuse_an_existing_release(tmp_path):
    workflow = _workflow()
    result, commands = _run_create_release(workflow, tmp_path, target_has_desktop_assets = True)
    assert result.returncode == 1
    assert "Refusing to republish" in result.stderr
    assert f"gh release delete-asset {RELEASE_TAG} latest.json --yes" in result.stderr
    assert not [line for line in commands if line.startswith("gh release create")]


def test_publish_fails_closed_when_the_target_release_is_missing(tmp_path):
    workflow = _workflow()
    result, commands = _run_create_release(workflow, tmp_path, target_http_status = 404)
    assert result.returncode == 1
    assert f"Release {RELEASE_TAG} does not exist." in result.stderr
    assert not [line for line in commands if line.startswith("gh release create")]


def test_publish_rejects_signer_diagnostics_as_updater_signatures(tmp_path):
    workflow = _workflow()
    result, commands = _run_create_release(workflow, tmp_path, invalid_signature = True)
    assert result.returncode == 1
    assert "Invalid base64 updater signature" in result.stderr
    assert not [line for line in commands if line.startswith("gh release create")]


def test_linux_release_stages_the_debian_signature(tmp_path):
    bundles = tmp_path / "bundles"
    bundles.mkdir()
    files = []
    for suffix in ("deb", "deb.sig", "AppImage", "AppImage.sig"):
        path = bundles / f"Unsloth_0.1.50_amd64.{suffix}"
        path.write_text(suffix, encoding = "utf-8")
        files.append(str(path))
    result, _ = _run_step(
        _workflow(),
        "build",
        "Stage release assets",
        tmp_path,
        extra_env = {"ARTIFACT_PATHS": json.dumps(files), "MATRIX_ARTIFACT": "linux"},
    )
    assert result.returncode == 0, result.stderr
    staged = tmp_path / "desktop-release-assets"
    assert (staged / "Unsloth-Desktop-Ubuntu.deb.sig").read_text() == "deb.sig"
    assert (staged / "Unsloth-Desktop-Linux.AppImage.sig").read_text() == "AppImage.sig"


def test_a_missing_debian_signature_prevents_manifest_publication(tmp_path):
    result, _ = _run_create_release(_workflow(), tmp_path, missing_debian_signature = True)
    assert result.returncode != 0
    # Named in full since the arm64 leg ships a .deb.sig too.
    assert "Expected exactly one -Ubuntu.deb.sig updater asset" in result.stderr


def test_a_missing_debian_arm64_signature_prevents_manifest_publication(tmp_path):
    result, _ = _run_create_release(_workflow(), tmp_path, missing_debian_arm64_signature = True)
    assert result.returncode != 0
    assert "Expected exactly one -Ubuntu-ARM64.deb.sig updater asset" in result.stderr


def test_the_two_linux_legs_never_stage_the_same_asset_name(tmp_path):
    """Linux legs need distinct asset names, since publish-release merges them into one directory."""
    staged = {}
    for artifact, arch in (("linux-x64", "amd64"), ("linux-arm64", "arm64")):
        leg = tmp_path / artifact
        leg.mkdir()
        bundles = leg / "bundles"
        bundles.mkdir()
        files = []
        for suffix in ("deb", "deb.sig"):
            path = bundles / f"Unsloth_0.1.50_{arch}.{suffix}"
            path.write_text(f"{arch}.{suffix}", encoding = "utf-8")
            files.append(str(path))
        result, _ = _run_step(
            _workflow(),
            "build",
            "Stage release assets",
            leg,
            extra_env = {"ARTIFACT_PATHS": json.dumps(files), "MATRIX_ARTIFACT": artifact},
        )
        assert result.returncode == 0, result.stderr
        staged[artifact] = {
            path.name: path.read_text(encoding = "utf-8")
            for path in (leg / "desktop-release-assets").iterdir()
        }

    assert set(staged["linux-x64"]) == {
        "Unsloth-Desktop-Ubuntu.deb",
        "Unsloth-Desktop-Ubuntu.deb.sig",
    }
    assert set(staged["linux-arm64"]) == {
        "Unsloth-Desktop-Ubuntu-ARM64.deb",
        "Unsloth-Desktop-Ubuntu-ARM64.deb.sig",
    }
    assert not set(staged["linux-x64"]) & set(staged["linux-arm64"])
    assert staged["linux-arm64"]["Unsloth-Desktop-Ubuntu-ARM64.deb.sig"] == "arm64.deb.sig"


def test_the_updater_manifest_points_linux_arm64_at_its_own_bundle(tmp_path):
    """An arm64 deb install must never be offered the amd64 package."""
    result, _ = _run_create_release(_workflow(), tmp_path)
    assert result.returncode == 0, result.stderr
    manifest = json.loads((tmp_path / "latest.json").read_text(encoding = "utf-8"))
    platforms = manifest["platforms"]
    for key in ("linux-aarch64", "linux-aarch64-deb"):
        assert platforms[key]["url"].endswith("Unsloth-Desktop-Ubuntu-ARM64.deb"), key
    for key in ("linux-x86_64", "linux-x86_64-appimage"):
        assert platforms[key]["url"].endswith("Unsloth-Desktop-Linux.AppImage"), key
    assert platforms["linux-x86_64-deb"]["url"].endswith("Unsloth-Desktop-Ubuntu.deb")


def test_the_publish_sequence_never_rewrites_the_release_body(tmp_path):
    workflow = _workflow()
    result, commands = _run_create_release(workflow, tmp_path)
    assert result.returncode == 0, result.stderr

    assert not [line for line in commands if line.startswith("gh release create")]
    assert not [line for line in commands if "git/refs" in line]
    assert not [line for line in commands if line.startswith("gh release edit")]
    assert not (tmp_path / "desktop-release-body.md").exists()

    latest = tmp_path / "latest.json"
    assert latest.is_file()
    metadata = yaml.safe_load(latest.read_text(encoding = "utf-8"))
    platforms = metadata["platforms"]
    debian = platforms["linux-x86_64-deb"]
    assert debian["url"].endswith("/Unsloth-Desktop-Ubuntu.deb")
    signature = tmp_path / "desktop-release-assets" / "Unsloth-Desktop-Ubuntu.deb.sig"
    assert debian["signature"] == signature.read_text().strip()
    assert platforms["linux-x86_64-appimage"]["url"].endswith(".AppImage")
    assert platforms["linux-x86_64"] == platforms["linux-x86_64-appimage"]
    for platform in metadata["platforms"].values():
        decoded = base64.b64decode(platform["signature"], validate = True)
        assert decoded.startswith(b"untrusted comment:")
        assert b"\ntrusted comment:" in decoded

    notes = (tmp_path / "desktop-release-notes.md").read_text(encoding = "utf-8")
    assert "Build provenance" not in notes
    assert "Desktop app for Unsloth." in notes


def test_the_build_matrix_covers_windows_on_arm():
    """The leg has to exist, cross-compile from the x64 runner (there is no ARM64
    Windows runner), have its Rust target installed, and be named in the publish
    gate. A leg the gate does not name can fail while the release still publishes."""
    workflow = _workflow()
    include = workflow["jobs"]["build"]["strategy"]["matrix"]["include"]
    arm64 = [entry for entry in include if entry.get("artifact") == "windows-arm64"]
    assert len(arm64) == 1, f"expected one Windows ARM64 leg, got {arm64}"
    leg = arm64[0]
    assert leg["platform"] == "windows-latest"
    assert "--target aarch64-pc-windows-msvc" in leg["args"]
    assert leg["label"] == "Windows (ARM64)"

    rust = _step(workflow, "build", "Install Rust stable")
    assert "aarch64-pc-windows-msvc" in rust["with"]["targets"]

    wait = _step(workflow, "publish-release", "Wait for the build matrix")["run"]
    assert f"'Build {leg['label']}'" in wait


def _stage_windows_leg(workflow, tmp_path: Path, artifact: str) -> list[str]:
    """Run the real "Stage release assets" step for one Windows leg and return what it staged."""
    bundles = tmp_path / f"bundles-{artifact}"
    bundles.mkdir(parents = True, exist_ok = True)
    # Tauri names the NSIS output per target; both end in -setup.exe.
    arch = "arm64" if artifact == "windows-arm64" else "x64"
    installer = bundles / f"Unsloth Studio_0.1.50_{arch}-setup.exe"
    installer.write_bytes(b"installer")
    signature = bundles / f"Unsloth Studio_0.1.50_{arch}-setup.exe.sig"
    signature.write_text("signature", encoding = "utf-8")

    staged = tmp_path / "desktop-release-assets"
    if staged.exists():
        for path in staged.iterdir():
            path.unlink()

    result = _run_step(
        workflow,
        "build",
        "Stage release assets",
        tmp_path,
        extra_env = {
            "ARTIFACT_PATHS": json.dumps([str(installer), str(signature)]),
            "MATRIX_ARTIFACT": artifact,
        },
    )[0]
    assert result.returncode == 0, result.stderr
    return sorted(path.name for path in staged.iterdir())


def test_the_two_windows_legs_never_stage_the_same_asset_name(tmp_path):
    """Windows legs need distinct names: merge-multiple overwrites; the x64 name is the download link."""
    workflow = _workflow()
    x64 = _stage_windows_leg(workflow, tmp_path, "windows-x64")
    arm64 = _stage_windows_leg(workflow, tmp_path, "windows-arm64")

    assert x64 == ["Unsloth-Desktop-Windows.exe", "Unsloth-Desktop-Windows.exe.sig"]
    assert arm64 == [
        "Unsloth-Desktop-Windows-ARM64.exe",
        "Unsloth-Desktop-Windows-ARM64.exe.sig",
    ]
    assert not set(x64) & set(arm64)


def test_no_matrix_leg_shares_a_fixed_artifact_name_with_another(tmp_path):
    """upload-artifact v4 rejects duplicate names in a run, so only matrix.artifact is unique."""
    workflow = _workflow()
    legs = {
        entry["artifact"] for entry in workflow["jobs"]["build"]["strategy"]["matrix"]["include"]
    }

    for step in workflow["jobs"]["build"]["steps"]:
        if "upload-artifact" not in step.get("uses", ""):
            continue
        name = step.get("with", {}).get("name", "")
        if "matrix.artifact" in name:
            continue  # already unique per leg
        condition = step.get("if", "")
        pinned = [leg for leg in legs if f"matrix.artifact == '{leg}'" in condition]
        assert len(pinned) == 1, (
            f"step {step.get('name')!r} uploads the fixed name {name!r} but is gated on "
            f"{condition!r}, which is not pinned to exactly one matrix leg"
        )


def test_the_updater_manifest_points_windows_arm64_at_its_own_bundle(tmp_path):
    """Tauri looks up updates by os-arch, so windows-aarch64 needs its own entry, not the x86_64 one."""
    workflow = _workflow()
    result, _ = _run_create_release(workflow, tmp_path)
    assert result.returncode == 0, result.stderr

    metadata = yaml.safe_load((tmp_path / "latest.json").read_text(encoding = "utf-8"))
    platforms = metadata["platforms"]
    for key in ("windows-x86_64", "windows-x86_64-nsis", "windows-aarch64", "windows-aarch64-nsis"):
        assert key in platforms, f"latest.json is missing {key}"

    arm64_url = platforms["windows-aarch64"]["url"]
    x64_url = platforms["windows-x86_64"]["url"]
    assert arm64_url.endswith("Unsloth-Desktop-Windows-ARM64.exe"), arm64_url
    assert x64_url.endswith("Unsloth-Desktop-Windows.exe"), x64_url
    assert "ARM64" not in x64_url, "the x64 entry picked up the ARM64 bundle"
    assert arm64_url != x64_url
    # The -nsis aliases exist because Tauri appends the installer kind; they must
    # resolve to the same bundle as the bare key or an update can cross architectures.
    assert platforms["windows-aarch64-nsis"] == platforms["windows-aarch64"]
    assert platforms["windows-x86_64-nsis"] == platforms["windows-x86_64"]


def test_release_uploads_never_clobber_or_mutate_the_legacy_channel():
    uploads = _upload_commands(_workflow())
    versioned = [line for line in uploads if "$DESKTOP_RELEASE_TAG" in line]
    channel = [line for line in uploads if "desktop-latest" in line]
    assert len(versioned) == 2, uploads
    assert channel == [], uploads

    for line in versioned:
        assert "--clobber" not in line, line


def test_any_existing_manifest_blocks_republishing_the_release(tmp_path):
    workflow = _workflow()
    result, _ = _run_step(
        workflow,
        "prepare-version",
        "Guard against republishing an existing version",
        tmp_path,
        target_has_desktop_assets = True,
        target_manifest_version = "v0.1.49-beta",
    )
    assert result.returncode == 1
    assert "latest.json" in result.stderr


def test_a_validation_only_run_touches_nothing_public():
    steps = _workflow()["jobs"]["publish-release"]["steps"]
    names = [step.get("name") for step in steps]
    mutating = (
        "Publish release assets",
        "Publish versioned updater metadata",
        "Promote normal release to GitHub latest",
    )
    for name in mutating:
        step = steps[names.index(name)]
        assert step.get("if") == "${{ !inputs.draft }}", name

    # Promotion last, so latest only moves once the assets are actually on the
    # release and a partial upload cannot leave latest pointing at an empty one.
    for upload in mutating[:2]:
        assert names.index(upload) < names.index(mutating[2])


def test_the_guard_rejects_a_prerelease_target_before_anything_is_built():
    workflow = _workflow()
    guard = _step(workflow, "prepare-version", "Guard against republishing an existing version")
    assert "is a prerelease" in guard["run"]
    state = _step(workflow, "publish-release", "Validate versioned release state")
    assert "is a prerelease" in state["run"]


def test_the_build_uses_the_release_tag_not_the_dispatch_ref():
    build = _workflow()["jobs"]["build"]["steps"]
    checkout = next(s for s in build if "actions/checkout" in str(s.get("uses", "")))
    assert checkout["with"]["ref"] == "${{ needs.prepare-version.outputs.desktop_release_tag }}"


def test_the_tag_is_validated_before_it_is_checked_out(tmp_path):
    # actions/checkout resolves the free-text input, so validation must run before it.
    steps = _workflow()["jobs"]["prepare-version"]["steps"]
    names = [step.get("name") or str(step.get("uses")) for step in steps]
    checkout = next(
        i for i, step in enumerate(steps) if "actions/checkout" in str(step.get("uses", ""))
    )
    assert names.index("Validate release versions") < checkout, names
    assert steps[checkout]["with"]["ref"] == "${{ steps.prepare.outputs.studio_version }}"

    for index, (bad, expected) in enumerate(
        (
            ("v.0.1.52-beta", "did you mean v0.1.52-beta?"),
            ("0.1.52-beta", "must start with v"),
            ("2026.8.3", "not a date-style backend version"),
        )
    ):
        case_dir = tmp_path / f"case-{index}"
        case_dir.mkdir()
        result, _ = _run_step(
            _workflow(),
            "prepare-version",
            "Validate release versions",
            case_dir,
            extra_env = {"INPUT_STUDIO_VERSION": bad},
        )
        assert result.returncode == 1, bad
        assert expected in result.stderr, (bad, result.stderr)


def test_the_promotion_guard_orders_numbered_prereleases_by_number():
    guard = _step(_workflow(), "publish-release", "Promote normal release to GitHub latest")["run"]
    body = guard.split('python3 - "$latest_before"', 1)[1].split("\nPY", 1)[0]
    body = "\n".join(line[10:] if line.startswith(" " * 10) else line for line in body.split("\n"))
    body = body.split("\n", 1)[1].lstrip("\n")
    namespace: dict = {}
    exec(body.split("current = json.loads", 1)[0], namespace)
    key = namespace["key"]
    # v1.2.3-beta10 is newer than v1.2.3-beta2, and a release beats its prerelease.
    assert key("v1.2.3-beta10") > key("v1.2.3-beta2")
    assert key("v1.2.3") > key("v1.2.3-beta10")
    assert key("v0.1.527-beta") > key("v0.1.526-beta")
    assert key("not-a-tag") is None


def test_the_promotion_guard_fails_closed_on_a_failed_latest_lookup():
    guard = _step(_workflow(), "publish-release", "Promote normal release to GitHub latest")["run"]
    # A 404 means no latest yet;
    # anything else must stop before the PATCH.
    fallback = guard.split("elif grep -Fq '(HTTP 404)'", 1)[1].split("gh api --method PATCH", 1)[0]
    assert "refusing to promote" in fallback.lower()
    assert "exit 1" in fallback
    assert "2>/dev/null" not in guard.split("releases/latest", 1)[1].split("\n", 1)[0]


def _guarded_bodies(script, header):
    """Return the body of every `header` block, delimited by matching braces."""
    bodies = []
    at = script.find(header)
    while at != -1:
        start = at + len(header)
        depth = 1
        for index in range(start, len(script)):
            if script[index] == "{":
                depth += 1
            elif script[index] == "}":
                depth -= 1
                if depth == 0:
                    bodies.append(script[start:index])
                    break
        else:
            raise AssertionError(f"unbalanced braces after {header!r}")
        at = script.find(header, start)
    assert bodies, f"{header!r} is gone"
    return bodies


def test_dead_defender_cmdlets_do_not_skip_the_bundle_scan():
    """Unavailable Defender cmdlets may only skip config checks, never the MpCmdRun scan itself."""
    scan = _step(_workflow(), "build", "Scan Windows bundles with Defender")["run"]

    unavailable = scan.split("$cmdletsDown = [bool]$unavailable", 1)
    assert len(unavailable) == 2, "the cmdlet-unavailable branch no longer sets $cmdletsDown"
    before_control = unavailable[1].split("EICAR positive control", 1)[0]
    assert (
        "exit 0" not in before_control
    ), "unavailable cmdlets still short-circuit the scan before the positive control"

    # Each cmdlet probe needs its OWN guard: a dead status cmdlet must not discard MAPSReporting.
    guards = {
        "$status": _guarded_bodies(scan, "if ($status) {"),
        "$pref": _guarded_bodies(scan, "if ($pref) {"),
    }
    for probe in (
        "$status.RealTimeProtectionEnabled",
        "$pref.MAPSReporting",
        "$pref.DisableBlockAtFirstSeen",
        "$pref.SubmitSamplesConsent",
        "$pref.CloudBlockLevel",
        "$pref.ExclusionPath",
    ):
        owner = probe.split(".", 1)[0]
        assert any(
            probe in body for body in guards[owner]
        ), f"{probe} left the `if ({owner})` guard that proves it was read"
        for other, bodies in guards.items():
            if other != owner and any(probe in body for body in bodies):
                raise AssertionError(
                    f"{probe} is gated on `if ({other})`, which fails independently "
                    f"of {owner}; one dead cmdlet would discard the other cmdlet's "
                    "readable result"
                )
    outside = scan
    for bodies in guards.values():
        for body in bodies:
            outside = outside.replace(body, "", 1)
    for held in ("$status.", "$pref."):
        assert held not in outside, f"a {held[:-1]} dereference sits outside its availability guard"

    config = scan.split("$fatal = @()", 1)[1].split("# Configuration is not connectivity", 1)[0]
    assert "$cmdletsDown" not in config, (
        "the configuration checks are gated on the blanket flag again; one dead "
        "cmdlet would discard the other cmdlet's readable result"
    )

    skip = scan.split("MpCmdRun could not fire the EICAR positive control", 1)
    assert len(skip) == 2, "the missing-scanner skip no longer keys off the positive control"
    assert "exit 0" in skip[1].split("\n", 3)[1] + skip[1].split("\n", 3)[2]
    assert "not a clean verdict" in skip[0].rsplit("::warning::", 1)[1] + skip[1]

    assert "Refusing to publish a Windows bundle Defender flags" in scan
    assert "Refusing to publish bundles Defender could not scan" in scan


def test_a_sample_quarantined_mid_scan_passes_the_positive_control():
    """A sample quarantined mid-scan means a live engine; only a surviving sample proves no scanner."""
    scan = _step(_workflow(), "build", "Scan Windows bundles with Defender")["run"]

    body = _guarded_bodies(scan, "if (Test-Path $eicarPath) {")[0]
    _, scanned, after = body.partition("-DisableRemediation")
    assert scanned, "the positive control no longer scans the sample with MpCmdRun"
    # The re-check must land after the scan and before this step's own cleanup.
    recheck, cleaned, _ = after.partition("Remove-Item $eicarPath")
    assert cleaned, "the positive control no longer removes the sample afterwards"
    assert "-not (Test-Path $eicarPath)" in recheck, (
        "the positive control never re-checks the sample after the scan, so a "
        "sample quarantined mid-scan reads as a missing scanner and skips the "
        "bundle scan on a runner where Defender is demonstrably live"
    )
    assert (
        "$controlPassed = $true" in recheck
    ), "the vanished sample is noticed but still does not pass the control"
    assert recheck.index("-not (Test-Path $eicarPath)") < recheck.index(
        "$controlPassed = $true"
    ), "the control passes without first confirming the sample is gone"


def test_cloud_block_level_is_verified_against_highplus():
    """A refused HighPlus leaves the previous level, not zero, so -eq 0 passes a
    runner still on High. 4 is HighPlus per the Defender Policy CSP."""
    scan = _step(_workflow(), "build", "Scan Windows bundles with Defender")["run"]

    assert "-CloudBlockLevel HighPlus" in scan
    check = [line for line in scan.splitlines() if "$pref.CloudBlockLevel" in line]
    assert len(check) == 1, f"expected one CloudBlockLevel check, found {check}"
    assert "-ne 4" in check[0], (
        "the CloudBlockLevel check no longer compares against 4 (HighPlus), so a "
        "runner left on High passes as fully configured"
    )


def test_asr_verification_checks_the_action_not_just_the_rule_id():
    """Add-MpPreference is additive, so a rule a policy set to Disabled or Block
    keeps that action and an id-only check calls it applied."""
    scan = _step(_workflow(), "build", "Scan Windows bundles with Defender")["run"]

    verify = scan.split("Add-MpPreference -AttackSurfaceReductionRules_Ids", 1)[1]
    verify = verify.split("$scanStart = Get-Date", 1)[0]
    assert "AttackSurfaceReductionRules_Actions" in verify, (
        "the ASR verification reads only the rule ids, so a rule stuck in Block "
        "or Disabled still reports as applied in audit mode"
    )
    assert "-ne 2" in verify, "the ASR verification no longer requires AuditMode (2)"
    assert (
        "[Math]::Min(" in verify
    ), "the ASR arrays are zipped without guarding a truncated Actions read"


def test_asr_audit_events_are_reported_as_runner_activity_only():
    """All four rules fire on process launch and this step only copies and scans
    the bundle, so a 1121/1122 here is runner activity, not a verdict on it."""
    scan = _step(_workflow(), "build", "Scan Windows bundles with Defender")["run"]

    report = scan.split("$asrEvents = @(Get-WinEvent", 1)[1]
    assert (
        "$_.TimeCreated -ge $scanStart" in report
    ), "the ASR event query is no longer bounded to this step's own window"
    assert "::error::" not in report.split("if ($detected -or $unscanned)", 1)[0], (
        "ASR audit events became fatal; 01443614 fires on low prevalence, which "
        "every freshly built binary has, so this would block every release"
    )
    assert (
        "not attributable" in report or "not a finding against it" in report
    ), "the ASR warning reads as a verdict on the bundle, which is never executed"


def test_linux_arm64_deb_has_a_native_build_and_blocks_publication_on_failure():
    workflow = _workflow()
    legs = workflow["jobs"]["build"]["strategy"]["matrix"]["include"]
    arm64 = [leg for leg in legs if leg.get("artifact") == "linux-arm64"]
    assert len(arm64) == 1
    assert arm64[0]["platform"] == "ubuntu-24.04-arm"
    build = _step(workflow, "build", "Build Linux ARM64 package")
    assert build["if"] == "matrix.platform == 'ubuntu-24.04-arm'"
    assert build["with"]["args"] == "-v --bundles deb"
    assert "TAURI_SIGNING_PRIVATE_KEY" in build["env"]
    wait = _step(workflow, "publish-release", "Wait for the build matrix")["run"]
    assert f"'Build {arm64[0]['label']}'" in wait


def test_linux_deb_architectures_stage_distinct_assets_and_validate_together(tmp_path):
    workflow = _workflow()
    _stage_assets(tmp_path)
    arm64_asset = tmp_path / "desktop-release-assets" / "Unsloth-Desktop-Ubuntu-ARM64.deb"
    arm64_asset.unlink(missing_ok = True)
    for artifact, arch, target in (
        ("linux-x64", "amd64", "Unsloth-Desktop-Ubuntu.deb"),
        ("linux-arm64", "arm64", "Unsloth-Desktop-Ubuntu-ARM64.deb"),
    ):
        source = tmp_path / f"Unsloth_0.1.50_{arch}.deb"
        source.write_bytes(arch.encode())
        destination = tmp_path / "desktop-release-assets" / target
        destination.unlink(missing_ok = True)
        result, _ = _run_step(
            workflow,
            "build",
            "Stage release assets",
            tmp_path,
            extra_env = {"ARTIFACT_PATHS": json.dumps([str(source)]), "MATRIX_ARTIFACT": artifact},
        )
        assert result.returncode == 0, result.stderr
        assert destination.read_bytes() == arch.encode()
    result, _ = _run_step(workflow, "publish-release", "Validate release asset set", tmp_path)
    assert result.returncode == 0, result.stderr
    arm64_asset.unlink()
    result, _ = _run_step(workflow, "publish-release", "Validate release asset set", tmp_path)
    assert result.returncode != 0
    assert "Unsloth-Desktop-Ubuntu-ARM64.deb" in result.stderr


def test_linux_clean_machine_downloads_only_the_runner_architecture(tmp_path):
    workflow = yaml.safe_load(
        (REPO_ROOT / ".github/workflows/desktop-app-clean-machine-ci.yml").read_text(
            encoding = "utf-8"
        )
    )
    job = workflow["jobs"]["linux"]
    assert job["runs-on"] == "${{ matrix.os }}"
    legs = job["strategy"]["matrix"]["include"]
    assert {leg["arch"] for leg in legs if leg["kind"] == "deb"} == {"x64", "arm64"}
    download = _step(workflow, "linux", "Download the shipped bundle")
    original = download["run"]
    for leg in legs:
        download["run"] = original.replace("${{ matrix.asset }}", leg["asset"])
        result, commands = _run_step(
            workflow,
            "linux",
            "Download the shipped bundle",
            tmp_path,
            target_has_desktop_assets = True,
            extra_env = {"REL_TAG": RELEASE_TAG, "REL_REPO": "unslothai/unsloth"},
        )
        assert result.returncode == 0, result.stderr
        expected = (
            "Unsloth-Desktop-Ubuntu-ARM64.deb"
            if leg["arch"] == "arm64"
            else (
                "Unsloth-Desktop-Ubuntu.deb"
                if leg["kind"] == "deb"
                else "Unsloth-Desktop-Linux.AppImage"
            )
        )
        command = next(
            command for command in commands if command.startswith("gh release download ")
        )
        arguments = shlex.split(command)
        pattern = arguments[arguments.index("--pattern") + 1]
        for prefix in ("Unsloth-Desktop-", "Unsloth-Desktop-0_1_803_beta-"):
            assets = [
                prefix + suffix for suffix in ("Ubuntu.deb", "Ubuntu-ARM64.deb", "Linux.AppImage")
            ]
            assert fnmatch.filter(assets, pattern) == [
                prefix + expected.removeprefix("Unsloth-Desktop-")
            ]
        if leg["arch"] == "arm64":
            assert leg["os"] == "ubuntu-24.04-arm"