File size: 3,148 Bytes
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
import argparse
import importlib.util
import os
from pathlib import Path


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


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

    values = run_b2_live_proof.apply_env_file(env_file)

    assert values["PROOFFRAME_STORAGE_BACKEND"] == "b2"
    assert values["PROOFFRAME_GENERATION_BACKEND"] == "mock"
    assert os.environ["B2_BUCKET"] == "proofframe-demo-a6b4e49"


def test_wait_for_b2_app_accepts_only_b2_and_mock():
    calls = []

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

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

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


def test_preflight_only_fails_closed_without_b2_key(monkeypatch, tmp_path):
    env_file = tmp_path / ".env.final.local"
    env_file.write_text(
        "\n".join(
            [
                "PROOFFRAME_STORAGE_BACKEND=b2",
                "PROOFFRAME_GENERATION_BACKEND=mock",
                "B2_ENDPOINT_URL=s3.us-west-004.backblazeb2.com",
                "B2_BUCKET=proofframe-demo-a6b4e49",
            ]
        ),
        encoding="utf-8",
    )
    for key in [
        "PROOFFRAME_STORAGE_BACKEND",
        "PROOFFRAME_GENERATION_BACKEND",
        "B2_ENDPOINT_URL",
        "B2_BUCKET",
        "B2_KEY_ID",
        "B2_APPLICATION_KEY",
        "B2_APP_KEY",
    ]:
        monkeypatch.delenv(key, raising=False)

    args = argparse.Namespace(
        env_file=env_file,
        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_b2_live_proof.run_b2_live_proof(args) == 2
    assert not args.evidence_out.exists()