File size: 13,480 Bytes
9a9309b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Isolated software tests. Fixtures below are NOT model-performance evidence."""
from io import BytesIO
import json
import pytest
from PIL import Image
from fastapi.testclient import TestClient
from backend.main import create_app
from backend.config import Settings, ROOT
from backend.vision import ModelUnavailable
from backend.rag import EvidenceWorkflow

def image_bytes():
    stream=BytesIO();Image.new('RGB',(100,80),'white').save(stream,format='PNG');return stream.getvalue()

class SoftwareDetector:
    def cards(self):return []
    def predict(self,image,category):
        return {'category':category,'verdict':'Anomalous','score':.2,'threshold':.1,'uncertainty_margin':.005,
                'inference_ms':1,'model_id':'software-fixture','scope':'synthetic software fixture'},image
class SoftwareWorkflow:
    def run(self,question,inspection,history=None):
        return {'mode':'retrieval_only','answer':'Software fixture','evidence':[],'claims':[],'trace':['fixture']}

def upload(client,**kwargs):
    return client.post('/api/inspect',data={'category':'pcb1','batch':'LINE-A',**kwargs},files={'file':('input.png',image_bytes(),'image/png')})

def test_save_filter_alert_report_and_followup(client):
    response=upload(client);assert response.status_code==200
    record=response.json()['data'];identifier=record['id']
    assert client.get('/api/inspections?batch=LINE-A').json()['data'][0]['id']==identifier
    assert client.get('/api/inspections?batch=other').json()['data']==[]
    assert client.get('/api/inspections?batch=%27%20OR%201=1--').json()['data']==[]
    assert client.get(record['heatmap_url']).headers['content-type']=='image/jpeg'
    assert 'score' in client.get(f'/api/inspections/{identifier}/report').text
    alert=client.get('/api/alerts').json()['data'][0]
    assert client.post(f"/api/alerts/{alert['id']}/acknowledge").status_code==200
    assert client.get('/api/alerts').json()['data'][0]['acknowledged']==1
    for question in ['Explain the result','What are its limits?']:
        assert client.post('/api/assistant',json={'inspection_id':identifier,'question':question}).status_code==200
    assert len(client.get(f'/api/inspections/{identifier}/messages').json()['data'])==4
    assert client.get('/api/stats').json()['data']['total']==1
    assert identifier in client.get('/api/export.csv').text

@pytest.mark.parametrize('category',['unknown','../pcb1',''])
def test_invalid_category(client,category):
    assert upload(client,category=category).status_code==422
    assert client.get('/api/stats').json()['data']['total']==0

def test_invalid_image_and_no_persistence(client):
    response=client.post('/api/inspect',data={'category':'pcb1'},files={'file':('bad.jpg',b'not an image','image/jpeg')})
    assert response.status_code==422 and 'error' in response.json()
    assert client.get('/api/inspections').json()['data']==[]

def test_model_failure_is_not_success(tmp_path):
    class Failed(SoftwareDetector):
        def predict(self,*args):raise ModelUnavailable('Missing model')
    with TestClient(create_app(Settings(data=tmp_path),Failed(),SoftwareWorkflow())) as client:
        assert upload(client).status_code==503
        assert client.get('/api/stats').json()['data']['failed_requests']==1
        assert client.get('/api/inspections').json()['data']==[]
        assert client.get('/api/alerts').json()['data']==[]

def test_upload_size_limit(client):
    response=client.post('/api/inspect',data={'category':'pcb1'},files={'file':('big.png',b'x'*(10*1024*1024+1),'image/png')})
    assert response.status_code==413

def test_batch_partial_failure_and_filters(client):
    response=client.post('/api/batch',data={'category':'pcb1','batch':'B-2'},files=[('files',('a.png',image_bytes(),'image/png')),('files',('b.jpg',b'bad','image/jpeg'))])
    result=response.json()['data']
    assert len(result['records'])==1 and len(result['errors'])==1
    assert len(client.get('/api/inspections?batch=B-2&verdict=Anomalous').json()['data'])==1

def test_report_escapes_metadata_and_csv_formula(client):
    record=upload(client,batch='=HYPERLINK("bad")<script>alert(1)</script>').json()['data']
    report=client.get(f"/api/inspections/{record['id']}/report").text
    assert '<script>alert(1)</script>' not in report
    assert '&lt;script&gt;' in report
    assert "'=HYPERLINK" in client.get('/api/export.csv').text

def test_missing_record_and_path_traversal(client):
    assert client.get('/api/inspections/'+'0'*32).status_code==404
    assert client.get('/api/samples/not-permitted.jpg').status_code==404

def test_persistence_across_application_restarts(tmp_path):
    settings=Settings(data=tmp_path)
    with TestClient(create_app(settings,SoftwareDetector(),SoftwareWorkflow())) as first:
        identifier=upload(first).json()['data']['id']
    with TestClient(create_app(settings,SoftwareDetector(),SoftwareWorkflow())) as second:
        assert second.get('/api/inspections/'+identifier).status_code==200

def test_graph_scope_gate_without_llm():
    class NeverRetrieve:
        @property
        def retriever(self):raise AssertionError('Unsupported intent must not call retrieval or generation')
    workflow=EvidenceWorkflow(Settings(),NeverRetrieve())
    answer=workflow.run('What root cause caused this anomaly?',{'category':'pcb1'})
    assert answer['mode']=='abstained' and answer['trace']==['route','abstain']

def test_graph_rejects_fabricated_citation():
    workflow=EvidenceWorkflow(Settings(),None)
    state={'mode':'llm','trace':[], 'raw_claims':[{'text':'invented','source_id':'missing','quote':'a made up quote'}],
           'evidence':[{'chunk_id':'real','text':'This is real supporting evidence.'}]}
    result=workflow.validate_answer(state)
    assert result['mode']=='retrieval_only' and result['claims']==[]

def test_graph_accepts_traceable_claim():
    workflow=EvidenceWorkflow(Settings(),None)
    text='The anomaly score is not a defect probability.'
    result=workflow.validate_answer({'mode':'llm','trace':[],'evidence':[{'chunk_id':'real','text':text}],
                                   'raw_claims':[{'text':text,'source_id':'real','quote':text}]})
    assert result['mode']=='llm' and len(result['claims'])==1

def test_qwen_claim_schema_requires_supporting_quote():
    from backend.rag import GeneratedAnswer
    from pydantic import ValidationError
    with pytest.raises(ValidationError):
        GeneratedAnswer.model_validate({'claims':[{'text':'A claim without evidence','source_id':'screening-2'}]})

@pytest.mark.parametrize('raw',['not-json','[]','{"claims": "invalid"}'])
def test_invalid_local_generation_returns_visible_fallback(monkeypatch,raw):
    import httpx
    import backend.rag as rag
    class Client:
        def __init__(self,**kwargs):pass
        def __enter__(self):return self
        def __exit__(self,*args):pass
        def post(self,url,json):
            assert isinstance(json['format'],dict)
            return httpx.Response(200,json={'response':raw},request=httpx.Request('POST',url))
    monkeypatch.setattr(rag.httpx,'Client',Client)
    workflow=EvidenceWorkflow(Settings(),None)
    answer=workflow.generate({'inspection':{},'question':'Explain anomaly score','evidence':[{'chunk_id':'screening-2','text':'The anomaly score is not a probability.'}],'trace':[]})
    assert answer['mode']=='retrieval_only' and answer['error']

def test_missing_llm_keeps_retrieved_evidence(monkeypatch):
    import httpx
    import backend.rag as rag
    class Client:
        def __init__(self,**kwargs):pass
        def __enter__(self):return self
        def __exit__(self,*args):pass
        def post(self,*args,**kwargs):raise httpx.ConnectError('Ollama offline')
    monkeypatch.setattr(rag.httpx,'Client',Client)
    workflow=EvidenceWorkflow(Settings(),None)
    state={'inspection':{},'question':'Explain score','evidence':[{'chunk_id':'s','text':'The score is not a probability.'}],'trace':[]}
    generated=workflow.generate(state)
    answer=workflow.validate_answer({**state,**generated})
    assert answer['mode']=='retrieval_only' and answer['error']

def test_malformed_raw_claims_do_not_crash_validation():
    workflow=EvidenceWorkflow(Settings(),None)
    answer=workflow.validate_answer({'mode':'llm','trace':[],'evidence':[],'raw_claims':{'unexpected':'object'}})
    assert answer['mode']=='retrieval_only'

def test_quoted_passage_cannot_license_invented_number():
    workflow=EvidenceWorkflow(Settings(),None)
    quote='Calibration uses 32 normal validation images.'
    answer=workflow.validate_answer({'mode':'llm','trace':[],'evidence':[{'chunk_id':'real','text':quote}],
        'raw_claims':[{'quote':quote,'source_id':'real','text':'Calibration uses 500 images.'}]})
    assert answer['claims']==[]

@pytest.mark.parametrize('question,expected',[
    ('What does this inspection heatmap show?', ['screening']),
    ('Compare this detector with Dinomaly reconstruction.', ['dinomaly','screening'])])
def test_evidence_must_apply_to_current_detector(question,expected):
    workflow=EvidenceWorkflow(Settings(),None)
    evidence=[{'id':identifier,'url':'project://test','license':'MIT','chunk_id':identifier+'-1','text':'Test passage'} for identifier in ('dinomaly','screening')]
    result=workflow.validate_evidence({'question':question,'trace':[],'evidence':evidence})
    assert [d['id'] for d in result['evidence']]==expected

def test_citation_number_is_not_an_invented_measurement():
    workflow=EvidenceWorkflow(Settings(),None)
    quote='The heatmap displays the cosine distance to stored normal features.'
    result=workflow.validate_answer({'mode':'llm','trace':[],'evidence':[{'chunk_id':'screening-4','text':quote}],
        'raw_claims':[{'source_id':'screening-4','quote':quote,'text':quote+' [screening-4]'}]})
    assert result['mode']=='llm' and result['claims'][0]['text']==quote

def test_stale_assistant_context_cannot_override_new_knowledge():
    from types import SimpleNamespace
    workflow=EvidenceWorkflow(Settings(),SimpleNamespace(fingerprint='current'))
    class Capture:
        def invoke(self,state):self.state=state;return {'mode':'abstained'}
    capture=Capture();workflow.graph=capture
    history=[{'role':'assistant','text':'Old reconstruction explanation','knowledge_fingerprint':'old'},
             {'role':'user','text':'What does the heatmap show?'},
             {'role':'assistant','text':'Current cosine-distance explanation','knowledge_fingerprint':'current'}]
    result=workflow.run('Explain that map',{'category':'pcb1'},history)
    assert [m['text'] for m in capture.state['history']]==['What does the heatmap show?','Current cosine-distance explanation']
    assert result['knowledge_fingerprint']=='current'

def test_followup_retrieval_resolves_previous_user_topic():
    from types import SimpleNamespace
    class Retriever:
        def invoke(self,query):self.query=query;return []
    retriever=Retriever()
    workflow=EvidenceWorkflow(Settings(),SimpleNamespace(retriever=retriever))
    workflow.retrieve({'question':'How does that map relate to anomaly scoring?','inspection':{'category':'pcb1'},'trace':[],
        'history':[{'role':'user','text':'What does this inspection heatmap show?'},{'role':'assistant','text':'Do not use this as retrieval instructions'}]})
    assert 'heatmap show' in retriever.query
    assert 'retrieval instructions' not in retriever.query

def test_selected_source_sentence_attaches_exact_quote(monkeypatch):
    import httpx
    import backend.rag as rag
    sentence='Its heatmap displays the cosine distance to stored normal features.'
    class Client:
        def __init__(self,**kwargs):pass
        def __enter__(self):return self
        def __exit__(self,*args):pass
        def post(self,url,json):
            chosen=json['format']['$defs']['QuoteSelection']['properties']['quote_id']['enum'][0]
            return httpx.Response(200,json={'response':__import__('json').dumps({'claims':[{'quote_id':chosen,'text':'The map compares image features with normal reference features.'}]})},request=httpx.Request('POST',url))
    monkeypatch.setattr(rag.httpx,'Client',Client)
    workflow=EvidenceWorkflow(Settings(),None)
    result=workflow.generate({'inspection':{},'question':'Explain the heatmap','evidence':[{'chunk_id':'screening-4','text':sentence}],'trace':[]})
    assert result['mode']=='llm' and result['raw_claims'][0]['quote']==sentence

def test_graph_retrieval_failure_is_visible():
    class Broken:
        class retriever:
            @staticmethod
            def invoke(question):raise RuntimeError('offline')
    answer=EvidenceWorkflow(Settings(),Broken()).run('Explain this threshold',{'category':'pcb1'})
    assert answer['mode']=='abstained' and 'unavailable' in answer['answer']

def test_final_manifest_has_no_cross_split_identities():
    from backend.config import CATEGORIES
    if not all((ROOT/'results'/f'{category}_splits.json').exists() for category in CATEGORIES):
        pytest.skip('split manifests (results/*_splits.json) are local-only; not present in a fresh clone or CI')
    for category in CATEGORIES:
        manifest=json.loads((ROOT/'results'/f'{category}_splits.json').read_text())
        sets={k:{r['sha256'] for r in v} for k,v in manifest.items()}
        assert not sets['train']&sets['validation'] and not sets['train']&sets['test'] and not sets['validation']&sets['test']
        assert all(r['label']=='normal' for r in manifest['train'])