File size: 3,062 Bytes
c810fc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json

from hackathon_advisor.data import Project
from hackathon_advisor.quest_cache import (
    build_quest_cache_identity,
    quest_analyzer_fingerprint_from_env,
    read_quest_cache_entry,
    write_quest_cache_entry,
)


def _project(readme_body: str = "Uses MiniCPM locally.") -> Project:
    return Project(
        id="build-small-hackathon/cache-unit",
        title="Cache Unit",
        summary="A small MiniCPM app.",
        tags=("gradio",),
        models=("openbmb/MiniCPM5-1B",),
        datasets=(),
        likes=0,
        sdk="gradio",
        license="mit",
        created_at="2026-06-01T00:00:00+00:00",
        last_modified="2026-06-08T00:00:00+00:00",
        host="https://cache-unit.hf.space",
        url="https://huggingface.co/spaces/build-small-hackathon/cache-unit",
        app_file="app.py",
        app_file_source="from transformers import AutoModelForCausalLM",
        readme_body=readme_body,
    )


def test_quest_cache_key_changes_when_prompt_changes() -> None:
    fingerprint = quest_analyzer_fingerprint_from_env({"ADVISOR_QUEST_ADAPTER_ID": ""})

    first = build_quest_cache_identity(_project("Uses MiniCPM locally."), fingerprint)
    second = build_quest_cache_identity(_project("Exports a PDF report."), fingerprint)

    assert first.prompt_hash != second.prompt_hash
    assert first.cache_key != second.cache_key


def test_quest_cache_round_trip_validates_cached_matches(tmp_path) -> None:
    project = _project()
    fingerprint = quest_analyzer_fingerprint_from_env({"ADVISOR_QUEST_ADAPTER_ID": ""})
    matches = [
        {
            "quest": "OpenBMB",
            "confidence": 0.91,
            "evidence": "Uses MiniCPM locally",
            "source": "readme",
        }
    ]

    stored = write_quest_cache_entry(
        tmp_path,
        project,
        fingerprint,
        matches,
        source="minicpm-json-quest-analyzer",
    )
    lookup = read_quest_cache_entry(tmp_path, project, fingerprint)

    assert lookup.reason == "hit"
    assert lookup.entry is not None
    assert lookup.entry.path == stored.path
    assert lookup.entry.matches == stored.matches


def test_quest_cache_rejects_corrupt_record(tmp_path) -> None:
    project = _project()
    fingerprint = quest_analyzer_fingerprint_from_env({"ADVISOR_QUEST_ADAPTER_ID": ""})
    stored = write_quest_cache_entry(
        tmp_path,
        project,
        fingerprint,
        [
            {
                "quest": "OpenBMB",
                "confidence": 0.91,
                "evidence": "Uses MiniCPM locally",
                "source": "readme",
            }
        ],
        source="minicpm-json-quest-analyzer",
    )
    payload = json.loads(stored.path.read_text(encoding="utf-8"))
    payload["matches"][0]["quest"] = "Unknown Quest"
    stored.path.write_text(json.dumps(payload), encoding="utf-8")

    lookup = read_quest_cache_entry(tmp_path, project, fingerprint)

    assert lookup.entry is None
    assert lookup.reason.startswith("invalid_schema:")