File size: 2,012 Bytes
e61da91
 
281998d
 
 
 
 
e61da91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281998d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from pathlib import Path
from typing import cast

import gradio as gr

from src.ui import runtime


def test_embedding_warmup_is_disabled_by_default(monkeypatch) -> None:
    monkeypatch.setattr(runtime.settings, "embedding_warmup_enabled", False)
    monkeypatch.setattr(
        runtime,
        "warm_up_embeddings",
        lambda: (_ for _ in ()).throw(AssertionError("unexpected warmup")),
    )

    assert runtime.warm_up_if_enabled() is False


def test_embedding_warmup_runs_when_enabled(monkeypatch) -> None:
    monkeypatch.setattr(runtime.settings, "embedding_warmup_enabled", True)
    monkeypatch.setattr(runtime, "warm_up_embeddings", lambda: 12.5)

    assert runtime.warm_up_if_enabled() is True


def test_embedding_warmup_failure_does_not_block_startup(monkeypatch) -> None:
    monkeypatch.setattr(runtime.settings, "embedding_warmup_enabled", True)
    monkeypatch.setattr(
        runtime,
        "warm_up_embeddings",
        lambda: (_ for _ in ()).throw(RuntimeError("model unavailable")),
    )

    assert runtime.warm_up_if_enabled() is False


def test_launch_preserves_the_app_with_custom_routes(monkeypatch, tmp_path: Path) -> None:
    class DemoStub:
        def __init__(self) -> None:
            self.app = object()
            self.queued_app = object()
            self.launch_kwargs: dict[str, object] = {}

        def queue(self, **_: object) -> DemoStub:
            self.app = self.queued_app
            return self

        def launch(self, **kwargs: object) -> None:
            self.launch_kwargs = kwargs

    demo = DemoStub()
    routed_apps: list[object] = []
    monkeypatch.setattr(runtime.settings, "export_dir", tmp_path)
    monkeypatch.setattr(runtime, "warm_up_if_enabled", lambda: False)
    monkeypatch.setattr(runtime, "add_monitoring_route", routed_apps.append)

    runtime.launch_demo(cast(gr.Blocks, demo))

    assert routed_apps == [demo.queued_app]
    assert demo.launch_kwargs["_app"] is demo.queued_app