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.

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

from __future__ import annotations

import asyncio
import hashlib
import io
import json
import math
import os
import time
import wave
from urllib.parse import quote

import av
import numpy as np
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient

import core.inference.audio_gallery as gallery
import utils.upload_limits as limits
from auth.authentication import get_current_subject
from core.inference import audio_inputs
from utils.api_errors import install_api_error_handlers

INPUTS = "/api/inference/audio/inputs"


@pytest.fixture(autouse = True)
def _tmp_gallery(monkeypatch, tmp_path):
    monkeypatch.setattr(gallery, "studio_root", lambda: tmp_path)


def encode(
    fmt: str,
    codec: str,
    rate: int,
    layout: str,
    seconds: float,
    freq: float = 440.0,
) -> bytes:
    channels = {"stereo": 2, "5.1": 6}.get(layout, 1)
    count = int(round(rate * seconds))
    tone = (0.3 * np.sin(2 * math.pi * freq * np.arange(count) / rate) * 32767).astype(np.int16)
    packed = np.repeat(tone, channels).reshape(1, -1)
    buf = io.BytesIO()
    with av.open(buf, mode = "w", format = fmt) as out:
        stream = out.add_stream(codec, rate = rate, layout = layout)
        step = rate // 50
        for start in range(0, count, step):
            chunk = packed[:, start * channels : min(count, start + step) * channels]
            arr = np.ascontiguousarray(chunk)
            frame = av.AudioFrame.from_ndarray(arr, format = "s16", layout = layout)
            frame.sample_rate, frame.pts = rate, start
            for packet in stream.encode(frame):
                out.mux(packet)
        for packet in stream.encode(None):
            out.mux(packet)
    return buf.getvalue()


def wav_bytes(seconds = 1.0, rate = 16000) -> bytes:
    return encode("wav", "pcm_s16le", rate, "mono", seconds)


async def _chunks(parts, seen = None):
    for part in parts:
        if seen is not None:
            seen.append(len(part))
        yield part


def _save(data: bytes, name = "clip.wav") -> dict:
    return asyncio.run(audio_inputs.save_stream(_chunks([data]), name))[0]


def _clip(rate = 16000, audio_type = "snac") -> dict:
    meta = {"prompt": "p", "model": "m", "audio_type": audio_type, "sample_rate": rate}
    meta.update(duration_s = 1.0, created_at = "2026-10-02T00:00:00Z")
    return gallery.save(wav_bytes(rate = rate), meta)


def _frames(path) -> tuple[int, int, int]:
    with wave.open(str(path)) as w:
        return w.getframerate(), w.getnchannels(), w.getnframes()


def test_transcode_writes_the_wav_header_sizes_once_at_close(tmp_path, monkeypatch):
    src = tmp_path / "clip.wav"
    src.write_bytes(encode("wav", "pcm_s16le", 44100, "stereo", 1.5))
    patches = []
    real = wave.Wave_write._patchheader

    def patch(self):
        patches.append(self._datawritten)
        return real(self)

    monkeypatch.setattr(wave.Wave_write, "_patchheader", patch)
    dst = tmp_path / "out.wav"
    info = audio_inputs.transcode(src, dst)
    assert patches == [os.path.getsize(dst) - 44]
    assert _frames(dst) == (44100, 2, int(round(44100 * 1.5)))
    assert info == {"duration_s": 1.5, "sample_rate": 44100, "channels": 2}


@pytest.fixture
def client():
    from routes import inference

    app = FastAPI()
    install_api_error_handlers(app)
    app.dependency_overrides[get_current_subject] = lambda: "tester"
    app.include_router(inference.router, prefix = "/api/inference")
    app.include_router(inference.studio_router, prefix = "/api/inference")
    app.include_router(inference.router, prefix = "/v1")
    yield TestClient(app)


def _upload(client, data: bytes, name: str):
    headers = {"Content-Type": "application/octet-stream"}
    return client.post(INPUTS, params = {"name": name}, content = data, headers = headers)


def _video_without_audio() -> bytes:
    buf = io.BytesIO()
    with av.open(buf, mode = "w", format = "matroska") as out:
        stream = out.add_stream("ffv1", rate = 1)
        stream.width = stream.height = 16
        stream.pix_fmt = "yuv420p"
        frame = av.VideoFrame.from_ndarray(np.zeros((16, 16, 3), dtype = np.uint8), format = "rgb24")
        for packet in [*stream.encode(frame), *stream.encode(None)]:
            out.mux(packet)
    return buf.getvalue()


@pytest.mark.parametrize(
    "fmt, codec, rate, layout, name, channels",
    [
        ("webm", "libopus", 48000, "stereo", "recording.webm", 2),
        ("mp3", "libmp3lame", 44100, "mono", "voice.mp3", 1),
        ("wav", "pcm_s16le", 16000, "mono", "voice.wav", 1),
        ("wav", "pcm_s16le", 24000, "5.1", "surround.wav", 2),
        ("flac", "flac", 96000, "stereo", "hires.flac", 2),
    ],
)
def test_an_upload_decodes_dedupes_and_deletes(client, fmt, codec, rate, layout, name, channels):
    data = encode(fmt, codec, rate, layout, 2.0)
    response = _upload(client, data, f"C:\\Users\\me\\{name}")
    assert response.status_code == 201, response.text
    record = response.json()
    assert set(record) == set("id name duration_s sample_rate channels url expires_at".split())
    got = (record["name"], record["sample_rate"], record["channels"], round(record["duration_s"]))
    rate = min(rate, audio_inputs.MAX_RATE)
    assert got == (name, rate, channels, 2)
    assert record["url"] == f"{INPUTS}/{record['id']}/file"
    served = client.get(record["url"])
    assert served.status_code == 200 and served.headers["content-type"] == "audio/wav"
    with wave.open(io.BytesIO(served.content)) as w:
        assert (w.getframerate(), w.getnchannels(), w.getsampwidth()) == (rate, channels, 2)
    assert (directory := audio_inputs.inputs_dir()) == gallery.gallery_dir() / "inputs"
    again = _upload(client, data, "other.wav")
    assert again.status_code == 200
    # A re-upload refreshes the expiry, which can cross a second boundary.
    reused = again.json()
    assert reused.pop("expires_at") >= record["expires_at"]
    assert reused == {k: v for k, v in record.items() if k != "expires_at"}
    assert len(list(directory.glob("*.json"))) == 1
    assert audio_inputs.prepare_reference({"input_id": record["id"]})[1].is_file()
    assert client.delete(f"{INPUTS}/{record['id']}").json() == {"removed": True}
    assert client.get(record["url"]).status_code == 404
    assert client.delete(f"{INPUTS}/{record['id']}").status_code == 404
    assert not list(directory.iterdir())


def test_an_upload_with_a_damaged_frame_keeps_the_audio_after_it(client):
    data = bytearray(encode("mp3", "libmp3lame", 44100, "mono", 20.0))
    middle = len(data) // 2
    data[middle : middle + 2048] = bytes(range(256)) * 8
    response = _upload(client, bytes(data), "damaged.mp3")
    assert response.status_code == 201, response.text
    assert response.json()["duration_s"] > 19


def test_an_oversize_body_is_refused_while_streaming():
    seen: list[int] = []
    with pytest.raises(audio_inputs.AudioInputError) as refused:
        body = _chunks([b"\x00" * 1024] * 100, seen)
        asyncio.run(audio_inputs.save_stream(body, "big.wav", max_bytes = 4096))
    assert (refused.value.status, refused.value.detail) == (413, "Audio is too large.")
    assert len(seen) == 5
    assert not list(audio_inputs.inputs_dir().iterdir())


@pytest.mark.parametrize(
    "patches, make, status, detail",
    [
        (
            {"audio_input_max_bytes": lambda: 2048},
            lambda: wav_bytes(2.0),
            413,
            "Audio is too large.",
        ),
        (
            {"transcribe_max_seconds": lambda: 1},
            lambda: wav_bytes(2.0),
            413,
            "Audio is longer than 0 minutes.",
        ),
        ({}, lambda: b"", 400, "Audio is empty."),
        ({}, lambda: b"not audio " * 120, 400, "This file is not audio Unsloth can read."),
        ({}, _video_without_audio, 400, "This file has no audio stream."),
    ],
)
def test_bad_or_over_cap_uploads_are_refused(client, monkeypatch, patches, make, status, detail):
    for attr, value in patches.items():
        monkeypatch.setattr(audio_inputs, attr, value)
        monkeypatch.setattr(limits, attr, value, raising = False)
    response = _upload(client, make(), "clip.mkv")
    assert response.status_code == status and response.json()["detail"] == detail
    assert not list(audio_inputs.inputs_dir().glob("*.wav"))


def test_the_body_middleware_passes_the_upload_through_at_its_own_cap(monkeypatch):
    import main

    monkeypatch.delenv(limits.AUDIO_INPUT_MAX_UPLOAD_MB_ENV, raising = False)
    assert limits.AUDIO_INPUT_MAX_BYTES == 200 * 1024 * 1024
    for path in (INPUTS, f"{INPUTS}/"):
        cap = main._get_upload_passthrough_request_max_bytes(path)
        assert cap == limits.audio_input_max_bytes()
    assert f"{INPUTS}/x/transcribe" not in main._BODY_UPLOAD_PASSTHROUGH_EXACT_PATHS


@pytest.fixture
def kib_limits(monkeypatch):
    """Upload limits in KiB rather than MiB, so a few seconds of WAV crosses them."""
    monkeypatch.delenv(limits.AUDIO_INPUT_MAX_UPLOAD_MB_ENV, raising = False)
    monkeypatch.setattr(limits, "_BYTES_PER_MB", 1024)
    monkeypatch.setattr(limits, "AUDIO_INPUT_MAX_BYTES", 200 * 1024)
    setting = {"mb": 300}
    monkeypatch.setattr(limits, "get_upload_limit_mb", lambda: setting["mb"])
    return setting


@pytest.mark.parametrize("setting, cap", [(300, 300 * 1024), (100, 200 * 1024)])
def test_the_audio_cap_follows_the_upload_setting_above_its_floor(client, kib_limits, setting, cap):
    import main

    kib_limits["mb"] = setting
    assert limits.audio_input_max_bytes() == cap
    assert main._get_upload_passthrough_request_max_bytes(INPUTS) == cap
    fits, over_floor = wav_bytes(6.0), wav_bytes(9.0)  # ~188 KiB and ~281 KiB
    assert len(fits) < 200 * 1024 < len(over_floor) < 300 * 1024
    assert _upload(client, fits, "a.wav").status_code == 201
    response = _upload(client, over_floor, "b.wav")
    assert response.status_code == (201 if setting == 300 else 413), response.text
    with pytest.raises(audio_inputs.AudioInputError) as refused:
        asyncio.run(audio_inputs.save_stream(_chunks([b"\x00" * (cap + 1)]), "d.wav"))
    assert (refused.value.status, refused.value.detail) == (413, "Audio is too large.")


@pytest.mark.parametrize(
    "env, cap",
    [("0", None), ("2", 2 * 1024), ("abc", 300 * 1024), ("-1", 300 * 1024), ("", 300 * 1024)],
)
def test_the_audio_cap_env_overrides_the_setting_exactly(kib_limits, monkeypatch, env, cap):
    import sys

    import main
    from routes.settings import _upload_limit_response

    monkeypatch.setenv(limits.AUDIO_INPUT_MAX_UPLOAD_MB_ENV, env)
    assert limits.audio_input_max_bytes() == cap
    passthrough = main._get_upload_passthrough_request_max_bytes(INPUTS)
    assert passthrough == (sys.maxsize if cap is None else cap)
    assert _upload_limit_response(300).audio_input_max_bytes == cap


def test_no_audio_cap_takes_any_size(client, kib_limits, monkeypatch):
    monkeypatch.setenv(limits.AUDIO_INPUT_MAX_UPLOAD_MB_ENV, "0")
    big = wav_bytes(20.0)
    assert len(big) > 300 * 1024
    assert _upload(client, big, "big.wav").status_code == 201


def test_the_upload_limit_settings_name_the_audio_cap(monkeypatch):
    from routes.settings import _upload_limit_response

    monkeypatch.delenv(limits.AUDIO_INPUT_MAX_UPLOAD_MB_ENV, raising = False)
    assert _upload_limit_response(1000).audio_input_max_bytes == 1000 * 1024 * 1024
    assert _upload_limit_response(100).audio_input_max_bytes == 200 * 1024 * 1024


@pytest.mark.parametrize(
    "env, seconds", [("90", 5400), ("10", 1800), ("abc", 14400), ("0", 14400), ("-5", 14400)]
)
def test_the_longest_upload_reads_its_env_in_minutes(monkeypatch, env, seconds):
    monkeypatch.setenv(audio_inputs.TRANSCRIBE_MAX_MINUTES_ENV, env)
    assert audio_inputs.transcribe_max_seconds() == seconds


@pytest.mark.parametrize(
    "seconds, words",
    [
        (14400, "4 hours"),
        (3600, "1 hour"),
        (5400, "90 minutes"),
        (1800, "30 minutes"),
        (1, "0 minutes"),
    ],
)
def test_lengths_read_in_hours_or_minutes(seconds, words):
    assert audio_inputs.length_words(seconds) == words


def test_an_upload_past_the_old_30_minutes_is_kept(client, monkeypatch):
    monkeypatch.delenv(audio_inputs.TRANSCRIBE_MAX_MINUTES_ENV, raising = False)
    monkeypatch.setattr(audio_inputs, "MAX_SECONDS", 1)
    response = _upload(client, wav_bytes(2.0), "long.wav")
    assert response.status_code == 201, response.text
    assert response.json()["duration_s"] == pytest.approx(2.0)


def test_an_upload_whose_wav_would_pass_the_store_cap_is_refused(client, monkeypatch):
    monkeypatch.setattr(audio_inputs, "BYTE_CAP", 20000)
    response = _upload(client, wav_bytes(1.0), "big.wav")
    assert response.status_code == 413
    assert response.json()["detail"].startswith("This audio is too long to keep: its WAV copy")
    assert not list(audio_inputs.inputs_dir().iterdir())
    monkeypatch.setattr(audio_inputs, "BYTE_CAP", 64000)
    assert _upload(client, wav_bytes(1.0), "fits.wav").status_code == 201


def test_the_store_cap_reads_in_gb():
    assert audio_inputs._size_words(audio_inputs.BYTE_CAP) == "2 GB"


def test_media_audio_keeps_the_30_minute_cap(tmp_path, monkeypatch):
    monkeypatch.setattr(audio_inputs, "MAX_SECONDS", 1)
    src = tmp_path / "clip.wav"
    src.write_bytes(wav_bytes(2.0))
    with pytest.raises(audio_inputs.AudioInputError) as refused:
        audio_inputs.save_from_media(src, "clip")
    assert refused.value.status == 413


def test_the_sweep_drops_the_oldest_past_the_byte_cap_then_all_past_the_ttl(client, monkeypatch):
    clock = [1_000_000.0]
    monkeypatch.setattr(audio_inputs, "_now", lambda: clock[0])
    ids = []
    for freq in (300.0, 500.0, 700.0):
        ids.append(_save(encode("wav", "pcm_s16le", 16000, "mono", 0.5, freq), "a.wav")["id"])
        clock[0] += 10
    one = (audio_inputs.inputs_dir() / f"{ids[0]}.wav").stat().st_size
    assert audio_inputs.sweep(byte_cap = int(one * 2.5)) == 1
    assert [audio_inputs.input_path(i) is not None for i in ids] == [False, True, True]
    assert audio_inputs.prepare_reference({"input_id": ids[1]})[1].is_file()
    clock[0] += audio_inputs.TTL_SECONDS + 1
    assert client.get(f"{INPUTS}/{ids[2]}/file").status_code == 404
    assert audio_inputs.sweep() == 2
    assert not list(audio_inputs.inputs_dir().iterdir())


def test_expired_inputs_are_swept_without_waiting_for_another_upload(monkeypatch, tmp_path):
    from auth import storage

    clock = [1_000_000.0]
    monkeypatch.setattr(audio_inputs, "_now", lambda: clock[0])
    input_id = _save(wav_bytes(0.5), "a.wav")["id"]
    clock[0] += audio_inputs.TTL_SECONDS + 1
    other = {"account_id": "acct-b", "username": "bob", "role": "user"}
    monkeypatch.setattr(storage, "list_accounts", lambda: [other])
    monkeypatch.setattr(audio_inputs, "account_path", lambda rel: tmp_path / "acct-b" / rel)
    assert audio_inputs.sweep_all_accounts() == 1
    assert audio_inputs.input_path(input_id) is None
    assert not (tmp_path / "acct-b").exists()


def test_a_prepared_reference_is_24k_mono_cut_to_thirty_seconds_and_cached():
    record = _save(wav_bytes(seconds = 31.0, rate = 8000), "long.wav")
    source, path = audio_inputs.prepare_reference({"input_id": record["id"]})
    assert (source.kind, source.name) == ("input", "long.wav")
    assert _frames(path) == (24000, 1, 30 * 24000)
    assert path.parent == audio_inputs.inputs_dir() and path.name.startswith(record["id"])
    mtime = path.stat().st_mtime_ns
    assert audio_inputs.prepare_reference({"input_id": record["id"]})[1].stat().st_mtime_ns == mtime
    assert _frames(audio_inputs.prepared_path(source, 24000, max_seconds = 1.0))[2] == 24000


def test_preparing_a_clip_copy_sweeps_expired_copies_without_an_upload(tmp_path):
    directory = audio_inputs.inputs_dir()
    stale = directory / "c-old.24000.mono.m30.wav"
    stale.write_bytes(wav_bytes(0.2))
    old = time.time() - audio_inputs.TTL_SECONDS - 60
    os.utime(stale, (old, old))
    clip = tmp_path / "clip.wav"
    clip.write_bytes(wav_bytes(0.5))
    source = audio_inputs.Source(kind = "clip", id = "fresh", path = clip, name = "clip.wav")
    fresh = audio_inputs.prepared_path(source, 24000, max_seconds = 30.0)
    assert fresh.is_file() and not stale.exists()


@pytest.mark.parametrize("bad", ["../escape", "a/b", "..", "x" * 129, "name.wav", ""])
def test_unsafe_ids_resolve_to_nothing(client, bad):
    assert audio_inputs.input_path(bad) is None
    assert audio_inputs.delete(bad) is False
    with pytest.raises(audio_inputs.AudioInputError) as refused:
        audio_inputs.resolve_source({"input_id": bad or None})
    assert refused.value.status in (400, 404)
    if bad:
        assert client.get(f"{INPUTS}/{quote(bad, safe = '')}/file").status_code == 404
        assert client.delete(f"{INPUTS}/{quote(bad, safe = '')}").status_code == 404


def test_transcribe_sends_a_16k_mono_copy_and_saves_nothing(client, monkeypatch):
    from routes import inference

    calls, model = [], "audio-cpp/audio.cpp-gguf/Qwen3-ASR-0.6B-GGUF"

    async def fake_transcribe(
        raw,
        model,
        language,
        fast,
        engine,
        request,
        device,
        *,
        context_length = 0,
    ):
        with wave.open(io.BytesIO(raw)) as w:
            calls.append(
                (
                    w.getframerate(),
                    w.getnchannels(),
                    model,
                    language,
                    engine,
                    device,
                    context_length,
                )
            )
        return {"text": " Okay, I'm Cemo. ", "language": "English"}

    monkeypatch.setattr(inference, "_transcribe_audio_result", fake_transcribe)
    record = _upload(client, encode("webm", "libopus", 48000, "stereo", 1.0), "r.webm").json()
    url = f"{INPUTS}/{record['id']}/transcribe"
    response = client.post(url, json = {"model": model, "device": "cpu", "context_length": 4096})
    assert response.json() == {"text": "Okay, I'm Cemo.", "language": "English", "model": model}
    assert calls == [(16000, 1, model, None, None, "cpu", 4096)]
    source, clip_q = f"{INPUTS}/source/transcribe", {"clip_id": _clip()["id"]}
    assert client.post(source, params = clip_q, json = {"model": "m"}).status_code == 200
    assert client.post(url, params = clip_q, json = {"model": "m"}).status_code == 400
    assert client.post(source, json = {"model": "m"}).status_code == 400
    assert client.post(url, json = {"model": "m", "path": "/etc/passwd"}).status_code == 422
    assert [r["id"] for r in gallery.list_audio()] == [clip_q["clip_id"]]


def test_a_convert_source_is_transcribed_past_the_clone_reference_cap(client, monkeypatch):
    from routes import inference

    seconds = []

    async def fake_transcribe(raw, *args, **kwargs):
        with wave.open(io.BytesIO(raw)) as w:
            seconds.append(round(w.getnframes() / w.getframerate()))
        return {"text": "words"}

    monkeypatch.setattr(inference, "_transcribe_audio_result", fake_transcribe)
    record = _upload(client, encode("wav", "pcm_s16le", 16000, "mono", 40.0), "long.wav").json()
    url = f"{INPUTS}/{record['id']}/transcribe"
    for purpose in ("reference", "convert"):
        response = client.post(url, json = {"model": "m", "purpose": purpose})
        assert response.status_code == 200, response.text
    assert seconds == [30, 40]


def test_the_byte_cap_counts_only_kept_inputs(client, monkeypatch):
    clock = [1_000_000.0]
    monkeypatch.setattr(audio_inputs, "_now", lambda: clock[0])
    small = _save(encode("wav", "pcm_s16le", 16000, "mono", 0.5, 300.0), "s.wav")["id"]
    clock[0] += 10
    big = _save(encode("wav", "pcm_s16le", 16000, "mono", 2.0, 500.0), "b.wav")["id"]
    clock[0] += 10
    new = _save(encode("wav", "pcm_s16le", 16000, "mono", 0.5, 700.0), "n.wav")["id"]
    size = lambda i: (audio_inputs.inputs_dir() / f"{i}.wav").stat().st_size
    cap = size(new) + size(small) + 10
    assert audio_inputs.sweep(byte_cap = cap) == 1
    assert [audio_inputs.input_path(i) is not None for i in (small, big, new)] == [
        True,
        False,
        True,
    ]


def test_an_upload_prepared_for_audio_cpp_is_mono_at_the_rate_and_leaves_no_copy():
    from routes.inference import _prepared_upload

    path = _prepared_upload(encode("mp3", "libmp3lame", 44100, "stereo", 1.0), 24000)
    assert path.parent == audio_inputs.inputs_dir()
    rate, channels, frames = _frames(path)
    assert (rate, channels) == (24000, 1) and abs(frames - 24000) < 2400
    assert [p.name for p in path.parent.iterdir()] == [path.name]


def test_an_upload_too_long_for_audio_cpp_is_refused_not_cut(monkeypatch):
    from routes.inference import _prepared_upload

    monkeypatch.setattr(audio_inputs, "MAX_SECONDS", 0.5)
    with pytest.raises(audio_inputs.AudioInputError) as refused:
        _prepared_upload(wav_bytes(1.0), 16000)
    assert refused.value.status == 413
    assert list(audio_inputs.inputs_dir().iterdir()) == []


def test_the_sweep_reaps_a_prepared_upload_a_crash_left_behind():
    from routes.inference import _prepared_upload

    stale = _prepared_upload(wav_bytes(0.2), 16000)
    waiting = _prepared_upload(wav_bytes(0.2), 16000)
    for path, age in ((stale, audio_inputs.TTL_SECONDS + 60), (waiting, 2 * 60 * 60)):
        os.utime(path, (time.time() - age, time.time() - age))
    audio_inputs.sweep()
    assert not stale.exists() and waiting.exists()


def _sidecar_for(directory, input_id, touched):
    import json
    meta = {
        "name": "a.wav",
        "duration_s": 1,
        "sample_rate": 16000,
        "channels": 1,
        "sha256": input_id,
        "created_at": "x",
        "touched_at": touched,
    }
    (directory / f"{input_id}.json").write_text(json.dumps(meta), encoding = "utf-8")


def test_the_sweep_lists_prepared_copies_once_for_every_live_input(monkeypatch):
    directory = audio_inputs.inputs_dir()
    now = time.time()
    for i in range(6):
        input_id = f"{i:032x}"
        (directory / f"{input_id}.wav").write_bytes(b"w" * 10)
        (directory / f"{input_id}.24000.mono.wav").write_bytes(b"d" * 10)
        _sidecar_for(directory, input_id, now)
    globs = []
    real = audio_inputs._derived
    monkeypatch.setattr(audio_inputs, "_derived", lambda d, s: globs.append(s) or real(d, s))
    assert audio_inputs.sweep() == 0
    assert globs == []


def test_the_sweep_sizes_prepared_copies_like_the_per_input_glob(monkeypatch):
    directory = audio_inputs.inputs_dir()
    now = time.time()
    names = {
        "aaaa": ["aaaa.24000.mono.wav", "aaaa..wav", "aaaa.x.y.wav", "aaaa.1.wav.tmp"],
        "aaaab": ["aaaab.1.wav"],
        "bbbb": ["bbbb.16000.stereo.m30.wav"],
        "cc-dd_1": ["cc-dd_1.24000.mono.wav", "cc-dd_1x.1.wav"],
    }
    for age, (input_id, derived) in enumerate(names.items()):
        (directory / f"{input_id}.wav").write_bytes(b"w" * 100)
        for name in derived:
            (directory / name).write_bytes(b"d" * 1000)
        _sidecar_for(directory, input_id, now - age)
    (directory / "zz.notes.wav").write_bytes(b"q")
    index = audio_inputs._derived_index(directory)
    for input_id in names:
        assert sorted(audio_inputs._derived_from(index, directory, input_id)) == sorted(
            audio_inputs._derived(directory, input_id)
        )
    # aaaa = 100 + 3 * 1000 (the .tmp is not a WAV), aaaab = 1100, bbbb = 1100, cc-dd_1 = 1100.
    assert audio_inputs.sweep(byte_cap = 3100 + 1100 + 1100) == 1
    assert not (directory / "cc-dd_1.wav").exists()
    assert (directory / "cc-dd_1x.1.wav").exists()
    assert (directory / "bbbb.wav").exists()


@pytest.mark.parametrize(
    "fmt, codec, rate, layout, start_s, end_s",
    [
        ("mp4", "aac", 44100, "stereo", 0.25, 1.1),
        ("wav", "pcm_s16le", 96000, "mono", 0.0, None),
        ("flac", "flac", 16000, "5.1", 0.5, None),
    ],
)
def test_a_media_cut_is_decoded_once_and_stored_as_cut(
    tmp_path, monkeypatch, fmt, codec, rate, layout, start_s, end_s
):
    src = tmp_path / f"clip.{fmt}"
    src.write_bytes(encode(fmt, codec, rate, layout, 1.5))
    whole = tmp_path / "whole.wav"
    audio_inputs.transcode(src, whole)
    with wave.open(str(whole)) as r:
        out_rate, channels, total = r.getframerate(), r.getnchannels(), r.getnframes()
        first = int(start_s * out_rate)
        last = total if end_s is None else min(total, int(end_s * out_rate))
        r.setpos(first)
        expected = io.BytesIO()
        with wave.open(expected, "wb") as w:
            w.setparams(r.getparams())
            w.writeframes(r.readframes(last - first))

    calls = []
    real = audio_inputs.transcode
    monkeypatch.setattr(audio_inputs, "transcode", lambda *a, **k: calls.append(a) or real(*a, **k))
    record, created = audio_inputs.save_from_media(src, "clip", start_s, end_s)
    assert created and len(calls) == 1
    stored = audio_inputs.inputs_dir() / f"{record['id']}.wav"
    assert stored.read_bytes() == expected.getvalue()
    assert record["sample_rate"] == out_rate and record["channels"] == channels
    assert record["duration_s"] == round((last - first) / out_rate, 3)
    meta = json.loads((audio_inputs.inputs_dir() / f"{record['id']}.json").read_text())
    assert meta["sha256"] == hashlib.sha256(expected.getvalue()).hexdigest()
    assert meta["bytes"] == len(expected.getvalue())

    again, created = audio_inputs.save_from_media(src, "clip", start_s, end_s)
    assert not created and again["id"] == record["id"]
    leftovers = [p.name for p in audio_inputs.inputs_dir().iterdir() if p.name.startswith(".")]
    assert leftovers == []