okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
538acd098
main
1"""Offline tests of HTTP smoke-test orchestration and safety defaults.""" 2from collections import OrderedDict 3import io 4import json 5from pathlib import Path 6import subprocess 7import sys 8import tempfile 9import unittest 10from unittest .mock import Mock 11 12sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ]))# runnable as tools/test_x.py from the repo root too 13 14from tools .conversation_cache_http_smoke import run 15 16 17class FakeClient : 18def __init__ (self ): 19self .history = [] 20self .cache = OrderedDict () 21 22def metrics (self ): 23return {'live' : {'state' :'idle' ,'queued' :0 }, 24'engine' : {'model' :'fake' ,'conversation_cache_slots' :4 ,'conversation_cache_mib' :8192 }, 25'requests' :self .history [::- 1 ]} 26 27def request (self ,model ,content ,stream = False ,count = 8 ): 28reused = 100 if content in self .cache else 0 29self .cache .pop (content ,None ) 30self .cache [content ]= True 31while len (self .cache )> 5 :# active plus four parked entries 32self .cache .popitem (last = False ) 33self .history .append ({'time' :len (self .history )+ 1 ,'reused' :reused ,'output_tokens' :5 , 34'finish' :'disconnect' if stream else 'stop' }) 35data = {'choices' : [{'delta' : {'content' :'1' }}]}if stream else { 36'choices' : [{'message' : {'role' :'assistant' ,'content' :'blue' }}]} 37return io .BytesIO ((('data: ' if stream else '' )+ json .dumps (data )+ '\n' ).encode ()) 38 39 40class HttpSmoke (unittest .TestCase ): 41def test_orchestration_offline (self ): 42result = run (FakeClient ()) 43self .assertEqual (len (result ['records' ]),12 ) 44self .assertEqual (result ['records' ][- 1 ]['name' ],'cancelled-A-restored' ) 45self .assertGreater (result ['records' ][- 1 ]['reused' ],0 ) 46 47def test_busy_server_receives_no_requests (self ): 48client = Mock () 49client .metrics .return_value = {'live' : {'state' :'generating' ,'queued' :0 }} 50with self .assertRaisesRegex (RuntimeError ,'busy' ): 51run (client ) 52client .request .assert_not_called () 53 54def test_disabled_cache_receives_no_requests (self ): 55client = Mock () 56client .metrics .return_value = {'live' : {'state' :'idle' ,'queued' :0 },'engine' : {}} 57with self .assertRaisesRegex (RuntimeError ,'enabled cache' ): 58run (client ) 59client .request .assert_not_called () 60 61def test_dry_run_does_not_connect_or_create_output (self ): 62with tempfile .TemporaryDirectory (prefix = 'strata-http-dry-' )as directory : 63output = Path (directory )/ 'not-created.json' 64result = subprocess .run ([sys .executable ,str (Path (__file__ ).with_name ('conversation_cache_http_smoke.py' )), 65'--url' ,'invalid://not-a-server' ,'--output' ,str (output )], 66capture_output = True ,text = True ,timeout = 10 ) 67self .assertEqual (result .returncode ,0 ,result .stderr ) 68self .assertIn ('No network traffic' ,result .stdout ) 69self .assertFalse (output .exists ()) 70 71 72if __name__ == '__main__' : 73unittest .main ()