File size: 5,727 Bytes
f74bce1
 
74ee21a
f74bce1
 
 
74ee21a
 
f74bce1
 
 
 
 
 
 
74ee21a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f74bce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74ee21a
f74bce1
 
 
 
 
 
 
 
 
 
 
 
 
14a02b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f74bce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74ee21a
 
 
 
 
 
 
 
 
 
 
 
f74bce1
 
 
 
 
 
 
 
 
 
 
74ee21a
f74bce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import importlib.util
import json
import os
from pathlib import Path

import pytest


SCRIPT_PATH = Path(__file__).resolve().parents[1] / "scripts" / "run_final_live_proof.py"
SPEC = importlib.util.spec_from_file_location("run_final_live_proof", SCRIPT_PATH)
run_final_live_proof = importlib.util.module_from_spec(SPEC)
assert SPEC.loader is not None
SPEC.loader.exec_module(run_final_live_proof)

LIVE_ENV_KEYS = [
    "PROOFFRAME_STORAGE_BACKEND",
    "PROOFFRAME_GENERATION_BACKEND",
    "B2_ENDPOINT_URL",
    "B2_BUCKET",
    "B2_KEY_ID",
    "B2_APPLICATION_KEY",
    "B2_APP_KEY",
    "B2_REGION",
    "GENBLAZE_PROVIDER",
    "GENBLAZE_API_KEY",
    "GMI_API_KEY",
    "OPENAI_API_KEY",
    "GENBLAZE_IMAGE_MODEL",
]


@pytest.fixture(autouse=True)
def restore_live_env():
    snapshot = {key: os.environ.get(key) for key in LIVE_ENV_KEYS}
    yield
    for key, value in snapshot.items():
        if value is None:
            os.environ.pop(key, None)
        else:
            os.environ[key] = value


def test_apply_env_file_sets_b2_storage_and_genblaze_generation(tmp_path, monkeypatch):
    env_file = tmp_path / ".env.final.local"
    env_file.write_text(
        "\n".join(
            [
                "PROOFFRAME_STORAGE_BACKEND=local",
                "PROOFFRAME_GENERATION_BACKEND=mock",
                "B2_ENDPOINT_URL=s3.us-west-004.backblazeb2.com",
                "B2_BUCKET=proofframe-demo-a6b4e49",
                "B2_KEY_ID=test-key-id",
                "B2_APPLICATION_KEY=fake",
                "GMI_API_KEY=test-gmi-key",
                "GENBLAZE_IMAGE_MODEL=seedream-5.0-lite",
            ]
        ),
        encoding="utf-8",
    )
    for key in [
        "PROOFFRAME_STORAGE_BACKEND",
        "PROOFFRAME_GENERATION_BACKEND",
        "B2_ENDPOINT_URL",
        "B2_BUCKET",
        "B2_KEY_ID",
        "B2_APPLICATION_KEY",
        "GENBLAZE_PROVIDER",
        "GMI_API_KEY",
        "GENBLAZE_IMAGE_MODEL",
    ]:
        monkeypatch.delenv(key, raising=False)

    values = run_final_live_proof.apply_env_file(env_file)

    assert values["PROOFFRAME_STORAGE_BACKEND"] == "b2"
    assert values["PROOFFRAME_GENERATION_BACKEND"] == "genblaze"
    assert os.environ["B2_BUCKET"] == "proofframe-demo-a6b4e49"
    assert os.environ["GENBLAZE_IMAGE_MODEL"] == "seedream-5.0-lite"


def test_apply_env_file_allows_non_secret_genblaze_overrides(tmp_path, monkeypatch):
    env_file = tmp_path / ".env.final.local"
    env_file.write_text(
        "\n".join(
            [
                "PROOFFRAME_STORAGE_BACKEND=local",
                "PROOFFRAME_GENERATION_BACKEND=mock",
                "GENBLAZE_PROVIDER=gmicloud",
                "GENBLAZE_IMAGE_MODEL=seedream-5.0-lite",
            ]
        ),
        encoding="utf-8",
    )
    for key in [
        "PROOFFRAME_STORAGE_BACKEND",
        "PROOFFRAME_GENERATION_BACKEND",
        "GENBLAZE_PROVIDER",
        "GENBLAZE_IMAGE_MODEL",
    ]:
        monkeypatch.delenv(key, raising=False)

    values = run_final_live_proof.apply_env_file(
        env_file,
        genblaze_provider="openai",
        genblaze_image_model="gpt-image-1",
    )

    assert values["PROOFFRAME_STORAGE_BACKEND"] == "b2"
    assert values["PROOFFRAME_GENERATION_BACKEND"] == "genblaze"
    assert os.environ["GENBLAZE_PROVIDER"] == "openai"
    assert os.environ["GENBLAZE_IMAGE_MODEL"] == "gpt-image-1"


def test_build_uvicorn_command_uses_requested_host_and_port():
    command = run_final_live_proof.build_uvicorn_command("127.0.0.1", 8099)

    assert command[:3] == [run_final_live_proof.sys.executable, "-m", "uvicorn"]
    assert "proofframe.app:app" in command
    assert "127.0.0.1" in command
    assert "8099" in command


def test_wait_for_live_app_accepts_only_b2_and_genblaze():
    calls = []

    def fake_read(url):
        calls.append(url)
        if len(calls) == 1:
            return {"ready": True, "storage_backend": "local", "generation_backend": "mock"}
        return {"ready": True, "storage_backend": "b2", "generation_backend": "genblaze"}

    health = run_final_live_proof.wait_for_live_app(
        "http://127.0.0.1:8088",
        timeout_seconds=1,
        read=fake_read,
    )

    assert health["storage_backend"] == "b2"
    assert health["generation_backend"] == "genblaze"
    assert calls == ["http://127.0.0.1:8088", "http://127.0.0.1:8088"]


def test_annotate_final_evidence_adds_schema_and_mode(tmp_path):
    evidence_path = tmp_path / "evidence.json"
    evidence_path.write_text('{"ok": true, "storage_backend": "b2"}', encoding="utf-8")

    assert run_final_live_proof.annotate_final_evidence(evidence_path) is True

    evidence = json.loads(evidence_path.read_text(encoding="utf-8"))
    assert evidence["schema"] == run_final_live_proof.EVIDENCE_SCHEMA
    assert evidence["mode"] == run_final_live_proof.EVIDENCE_MODE
    assert evidence["ok"] is True


def test_preflight_only_fails_closed_without_live_env(monkeypatch, tmp_path):
    for key in [
        "PROOFFRAME_STORAGE_BACKEND",
        "B2_ENDPOINT_URL",
        "B2_BUCKET",
        "B2_KEY_ID",
        "B2_APPLICATION_KEY",
        "B2_APP_KEY",
        "PROOFFRAME_GENERATION_BACKEND",
        "GENBLAZE_API_KEY",
        "GMI_API_KEY",
        "OPENAI_API_KEY",
        "GENBLAZE_IMAGE_MODEL",
    ]:
        monkeypatch.delenv(key, raising=False)

    args = argparse.Namespace(
        env_file=None,
        host="127.0.0.1",
        port=8088,
        timeout_seconds=0.1,
        evidence_out=tmp_path / "evidence.json",
        log_path=tmp_path / "uvicorn.log",
        preflight_only=True,
    )

    assert run_final_live_proof.run_final_live_proof(args) == 2
    assert not args.evidence_out.exists()