aboutsummaryrefslogtreecommitdiff
path: root/lib/project/tests/test_ai.py
blob: e8c587276c0795c27da28101f1180a5f24d0bc6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""Offline AI contract tests; no Torch installation, model download, or GPU needed."""
import hashlib
import io
import json
import os
from pathlib import Path
import sys
import time
from types import SimpleNamespace
from unittest.mock import Mock
import zipfile

import numpy as np
import pytest
import soundfile as sf

from voiceforge import ai, ai_worker, setup


MODEL_BYTES = 7986207
MODEL_HASH = "49c52edc8947ae1f9bf50d81530beaf3a2c3245aeaf34b6f31ff535cd22284d2"


@pytest.mark.parametrize("size,message", [
    (10, "checksum mismatch"),
    (MODEL_BYTES, "checksum mismatch"),
    (MODEL_BYTES + 1, "exceeds the expected size"),
])
def test_model_rejects_truncated_corrupt_and_oversized_downloads(tmp_path, monkeypatch, size, message):
    monkeypatch.setattr(ai_worker.urllib.request, "urlopen", Mock(return_value=io.BytesIO(b"x" * size)))
    progress = Mock()
    monkeypatch.setattr(ai_worker, "report", progress)
    with pytest.raises(RuntimeError, match=message):
        ai_worker.ensure_model(tmp_path / "model")
    assert list(tmp_path.iterdir()) == []
    events = [call.args for call in progress.call_args_list]
    assert events[0] == ("Downloading DeepFilterNet3 model (bytes)", 0, MODEL_BYTES)
    counts = [completed for _, completed, total in events if total == MODEL_BYTES]
    assert counts == sorted(counts)
    assert all(0 <= count <= MODEL_BYTES for count in counts)


@pytest.mark.parametrize("unsafe", [False, True])
def test_mocked_model_archive_progress_extraction_and_reuse(tmp_path, monkeypatch, unsafe):
    # Mock only the release digest for a synthetic archive; corruption tests above
    # exercise the real SHA-256 gate. Keep its real byte count and ZIP extraction.
    def archive(padding):
        stream = io.BytesIO()
        with zipfile.ZipFile(stream, "w") as bundle:
            bundle.writestr("DeepFilterNet3/config.ini", "[df]\nsr=48000\n")
            bundle.writestr("DeepFilterNet3/checkpoints/model_120.ckpt.best", "checkpoint")
            bundle.writestr("../escape" if unsafe else "padding", b"x" * padding)
        return stream.getvalue()

    payload = archive(MODEL_BYTES - len(archive(0)))
    assert len(payload) == MODEL_BYTES
    digest = Mock(wraps=hashlib.sha256())
    digest.hexdigest.return_value = MODEL_HASH
    monkeypatch.setattr(ai_worker.hashlib, "sha256", lambda: digest)
    download = Mock(return_value=io.BytesIO(payload))
    monkeypatch.setattr(ai_worker.urllib.request, "urlopen", download)
    progress = Mock()
    monkeypatch.setattr(ai_worker, "report", progress)
    directory = tmp_path / "model"
    if unsafe:
        with pytest.raises(RuntimeError, match="Unsafe path"):
            ai_worker.ensure_model(directory)
        assert not (tmp_path / "escape").exists()
        assert list(tmp_path.iterdir()) == []
    else:
        model = ai_worker.ensure_model(directory)
        assert (model / "config.ini").is_file()
        assert (model / "checkpoints/model_120.ckpt.best").is_file()
        assert ai_worker.ensure_model(directory) == model
        download.assert_called_once()
    assert b"".join(call.args[0] for call in digest.update.call_args_list) == payload
    events = [call.args for call in progress.call_args_list]
    byte_events = [event for event in events if "(bytes)" in event[0]]
    assert byte_events[0][1:] == (0, MODEL_BYTES)
    assert byte_events[-1][1:] == (MODEL_BYTES, MODEL_BYTES)
    assert len(byte_events) > 2
    assert events[-1] == ("Verifying and extracting DeepFilterNet3 model",)


def test_incomplete_model_fails_without_network(tmp_path, monkeypatch):
    directory = tmp_path / "model"
    directory.mkdir()
    download = Mock(side_effect=AssertionError("Unexpected network call"))
    monkeypatch.setattr(ai_worker.urllib.request, "urlopen", download)
    with pytest.raises(RuntimeError, match="Incomplete model directory"):
        ai_worker.ensure_model(directory)
    download.assert_not_called()


@pytest.mark.parametrize("close_output", [False, True])
def test_subprocess_heartbeat_does_not_block_on_silence_or_closed_output(close_output):
    events = []
    code = "import os,time; "
    if close_output:
        code += "os.close(1); os.close(2); "
    code += "time.sleep(0.8)"
    setup.run_process([sys.executable, "-c", code], "Waiting", lambda *event: events.append(event))
    assert events[0] == ("Waiting", None, None)
    elapsed = [completed for _, completed, total in events[1:] if total is None]
    assert len(elapsed) >= 2
    assert elapsed == sorted(elapsed)


def test_subprocess_parses_split_byte_progress_records():
    events = []
    record = "VOICEFORGE_PROGRESS " + json.dumps(["Download (bytes)", 123, 456]) + "\n"
    code = f"import os,time; os.write(1,{record[:12].encode()!r}); time.sleep(.3); os.write(1,{record[12:].encode()!r})"
    setup.run_process([sys.executable, "-c", code], "Download", lambda *event: events.append(event))
    assert events[-1] == ("Download (bytes)", 123, 456)


@pytest.mark.parametrize("exception", [KeyboardInterrupt, RuntimeError])
def test_callback_cancellation_terminates_and_reaps_child(monkeypatch, exception):
    popen = setup.subprocess.Popen
    children = []

    def launch(*args, **kwargs):
        child = popen(*args, **kwargs)
        children.append(child)
        return child

    def cancel(stage, completed, total):
        if completed is not None:
            raise exception("cancelled")

    monkeypatch.setattr(setup.subprocess, "Popen", launch)
    started = time.monotonic()
    with pytest.raises(exception, match="cancelled"):
        setup.run_process([sys.executable, "-c", "import time; time.sleep(30)"], "Waiting", cancel)
    assert len(children) == 1 and children[0].poll() is not None
    assert time.monotonic() - started < 10


def test_cancellation_kills_descendants_that_outlive_the_leader(tmp_path, monkeypatch):
    sentinel = tmp_path / "grandchild.pid"
    monkeypatch.setenv("GRANDCHILD_PID_FILE", str(sentinel))
    child_code = (
        "import os, subprocess, sys, time\n"
        "subprocess.Popen([sys.executable, '-c', "
        "\"import os, time; open(os.environ['GRANDCHILD_PID_FILE'], 'w')"
        ".write(str(os.getpid())); time.sleep(60)\"])\n"
        "while not os.path.exists(os.environ['GRANDCHILD_PID_FILE']):\n"
        "    time.sleep(0.02)\n"
    )

    def cancel(stage, completed, total):
        if completed is not None and children and children[0].poll() is not None:
            raise KeyboardInterrupt("cancelled")

    popen = setup.subprocess.Popen
    children = []

    def launch(*args, **kwargs):
        child = popen(*args, **kwargs)
        children.append(child)
        return child

    monkeypatch.setattr(setup.subprocess, "Popen", launch)
    with pytest.raises(KeyboardInterrupt, match="cancelled"):
        setup.run_process([sys.executable, "-c", child_code], "Waiting", cancel)

    def alive(pid):
        try:
            for line in Path(f"/proc/{pid}/status").read_text().splitlines():
                if line.startswith("State:"):
                    return "Z" not in line
            return True
        except FileNotFoundError:
            return False

    grandchild = int(sentinel.read_text())
    for _ in range(50):
        if not alive(grandchild):
            break
        time.sleep(0.1)
    else:
        pytest.fail("A descendant survived the cancellation of its process group")


def test_subprocess_failure_keeps_bounded_diagnostics():
    code = "import sys; print('x'*100000); print('specific failure'); sys.exit(7)"
    with pytest.raises(RuntimeError, match="exit 7") as error:
        setup.run_process([sys.executable, "-c", code], "Worker")
    assert "specific failure" in str(error.value)
    assert len(str(error.value)) < 66000


@pytest.mark.parametrize("strength", [-1, 2, float("nan"), float("inf")])
def test_invalid_strength_fails_before_setup(tmp_path, monkeypatch, strength):
    install = Mock(side_effect=AssertionError("Unexpected setup"))
    monkeypatch.setattr(ai, "ensure_ai", install)
    with pytest.raises(ValueError, match="strength"):
        ai.denoise(tmp_path / "input.wav", tmp_path / "output.wav", strength=strength)
    install.assert_not_called()


def _ready_environment(root, flavor="cpu"):
    environment = root / f"ai-df-0.5.6-torch-2.5.1-{flavor}-py311-v1"
    (environment / "bin").mkdir(parents=True)
    (environment / "bin/python").write_text("#!/bin/sh\n")
    (environment / ".voiceforge-ready").touch()
    return environment


def test_ready_environment_skips_the_check_worker(tmp_path, monkeypatch):
    root = tmp_path / "prefix"
    environment = _ready_environment(root)
    monkeypatch.setenv("VOICEFORGE_HOME", str(root))
    monkeypatch.setattr(ai.os, "confstr", lambda *_: "glibc 2.39")
    executed = []
    monkeypatch.setattr(ai, "run_process", lambda args, *rest, **kwargs: executed.append(args))
    python = ai.ensure_ai("cpu")
    assert executed == []
    assert python == environment / "bin/python"


def test_setup_verifies_even_a_ready_environment(tmp_path, monkeypatch):
    root = tmp_path / "prefix"
    _ready_environment(root)
    monkeypatch.setenv("VOICEFORGE_HOME", str(root))
    monkeypatch.setattr(ai.os, "confstr", lambda *_: "glibc 2.39")
    executed = []
    monkeypatch.setattr(ai, "run_process", lambda args, *rest, **kwargs: executed.append(args))
    ai.ensure_ai("cpu", verify=True)
    assert len(executed) == 1
    assert executed[0][-1] == "--check"


def test_broken_environment_is_rebuilt_and_self_tested(tmp_path, monkeypatch):
    root = tmp_path / "prefix"
    environment = _ready_environment(root)
    (environment / "bin/python").unlink()
    (environment / "bin/python").symlink_to("/nonexistent/python3.11")
    leftover = environment / "leftover.txt"
    leftover.write_text("from the previous location")
    monkeypatch.setenv("VOICEFORGE_HOME", str(root))
    monkeypatch.setattr(ai.os, "confstr", lambda *_: "glibc 2.39")
    stages, commands = [], []

    def fake_run(args, stage, progress=None, **kwargs):
        commands.append(args)
        stages.append(stage)
        if stage == "Checking DeepFilterNet3":
            environment.mkdir(parents=True, exist_ok=True)
            (environment / ".voiceforge-ready").touch()

    monkeypatch.setattr(ai, "run_process", fake_run)
    monkeypatch.setattr(ai, "ensure_uv", lambda progress=None: "uv")
    ai.ensure_ai("cpu")
    assert not leftover.exists()
    assert stages == ["Preparing AI Python 3.11", "Installing PyTorch (cpu)",
                      "Installing DeepFilterNet3", "Checking DeepFilterNet3"]
    assert commands[0][-1] == str(environment)
    assert (environment / ".voiceforge-ready").is_file()


def test_copied_environment_is_rebuilt_not_trusted(tmp_path, monkeypatch):
    # Copying an installation while the original remains leaves the copy's
    # interpreter links resolving into the original prefix; that must not
    # count as ready, or the copy breaks when the original is removed.
    root = tmp_path / "prefix"
    environment = _ready_environment(root)
    original = _ready_environment(tmp_path / "other")
    (environment / "bin/python").unlink()
    (environment / "bin/python").symlink_to(original / "bin/python")
    monkeypatch.setenv("VOICEFORGE_HOME", str(root))
    monkeypatch.setattr(ai.os, "confstr", lambda *_: "glibc 2.39")
    stages = []

    def fake_run(args, stage, progress=None, **kwargs):
        stages.append(stage)
        if stage == "Checking DeepFilterNet3":
            environment.mkdir(parents=True, exist_ok=True)
            (environment / ".voiceforge-ready").touch()

    monkeypatch.setattr(ai, "run_process", fake_run)
    monkeypatch.setattr(ai, "ensure_uv", lambda progress=None: "uv")
    ai.ensure_ai("cpu")
    assert stages[0] == "Preparing AI Python 3.11"


@pytest.fixture
def mocked_worker(tmp_path, monkeypatch):
    class Tensor:
        def __init__(self, array):
            self.array = array

        def unsqueeze(self, axis):
            return Tensor(np.expand_dims(self.array, axis))

        def squeeze(self, axis):
            return Tensor(np.squeeze(self.array, axis))

        def numpy(self):
            return self.array

    calls = []

    def enhance(model, state, tensor, pad):
        assert pad is True
        assert tensor.array.shape[1] % 480 == 0
        calls.append(tensor.array.shape[1])
        return Tensor(tensor.array * 0.5)

    state = SimpleNamespace(sr=lambda: 48000)
    backend = SimpleNamespace(enhance=enhance, init_df=lambda *a, **kw: (None, state, None))
    for name, module in {
        "torch": SimpleNamespace(set_num_threads=lambda n: None, from_numpy=Tensor,
                                 cuda=SimpleNamespace(is_available=lambda: False)),
        "df": SimpleNamespace(),
        "df.enhance": backend,
        "df.model": SimpleNamespace(ModelParams=lambda: SimpleNamespace(
            sr=48000, fft_size=960, hop_size=480, nb_erb=32, min_nb_freqs=2)),
        "libdf": SimpleNamespace(DF=lambda **kw: state),
    }.items():
        monkeypatch.setitem(sys.modules, name, module)
    monkeypatch.setattr(ai_worker, "ensure_model", lambda path: path)
    monkeypatch.setattr(ai_worker.signal, "signal", lambda *args: None)
    monkeypatch.setenv("DEVICE", "cpu")
    progress = Mock()
    monkeypatch.setattr(ai_worker, "report", progress)

    def run(source, target, strength):
        monkeypatch.setattr(sys, "argv", ["worker", "--device", "cpu", "--model-dir", str(tmp_path),
                                         "--source", str(source), "--target", str(target),
                                         "--strength", str(strength)])
        ai_worker.main()

    return run, calls, backend, progress


@pytest.mark.parametrize("frames", [0, 1, 479, 481, 480001, 960017])
@pytest.mark.parametrize("strength", [0, 0.85, 1])
def test_mocked_worker_exact_length_mix_and_bounded_windows(tmp_path, wav, mocked_worker, frames, strength):
    run, calls, _, progress = mocked_worker
    audio = np.random.default_rng(42).normal(0, 0.1, frames).astype("float32")
    source = wav(audio)
    target = tmp_path / "clean.wav"
    run(source, target, strength)
    result, rate = sf.read(target, dtype="float32")
    assert len(result) == frames and rate == 48000
    assert sf.info(target).subtype == "FLOAT"
    np.testing.assert_allclose(result, audio * (1 - strength * 0.5), rtol=2e-6, atol=1e-8)
    if strength == 0:
        np.testing.assert_array_equal(result, audio)
        assert calls == []
    assert max(calls, default=0) <= 674400
    assert progress.call_args.args == ("Denoising (cpu)", frames, frames)
    assert not list(tmp_path.glob(".voiceforge-ai-*"))


def test_worker_failure_preserves_existing_target(tmp_path, wav, mocked_worker):
    run, _, backend, _ = mocked_worker
    backend.enhance = Mock(side_effect=RuntimeError("inference failed"))
    source = wav()
    target = tmp_path / "existing.wav"
    target.write_bytes(b"existing output")
    with pytest.raises(RuntimeError, match="inference failed"):
        run(source, target, 0.85)
    assert target.read_bytes() == b"existing output"
    assert not list(tmp_path.glob(".voiceforge-ai-*"))