okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd0e3766a
main
1"""Tester report T07: `strata connect pi --check --output json` skipped the checks and said "ok": true with exit 0 even when nothing was 2listening. It now runs the same two checks as the plain-words output, returns each result, and exits 1 when one failed or the server cannot be 3reached.""" 4 5from __future__import annotations 6 7import json 8import socket 9import threading 10import unittest 11from http .server import BaseHTTPRequestHandler ,ThreadingHTTPServer 12 13from tests .terminal .launch_support import Home ,run_cli 14 15 16class Handler (BaseHTTPRequestHandler ): 17mode = "good" # good | no-text | no-tool 18 19def log_message (self ,* a )-> None : 20pass 21 22def do_POST (self )-> None : 23body = json .loads (self .rfile .read (int (self .headers .get ("Content-Length" )or 0 ))or b"{}" ) 24wants_tool = bool (body .get ("tools" )) 25message = {"role" :"assistant" ,"content" :"pong" } 26if Handler .mode == "no-text" and not wants_tool : 27message = {"role" :"assistant" ,"content" :None } 28if wants_tool : 29message = ({"role" :"assistant" ,"content" :"I cannot." }if Handler .mode == "no-tool" else 30 {"role" :"assistant" ,"content" :None ,"tool_calls" : [ 31 {"id" :"1" ,"type" :"function" ,"function" : {"name" :"get_weather" ,"arguments" :json .dumps ({"city" :"Paris" })}}]}) 32data = json .dumps ({"choices" : [{"message" :message }]}).encode () 33self .send_response (200 ) 34self .send_header ("Content-Type" ,"application/json" ) 35self .send_header ("Content-Length" ,str (len (data ))) 36self .end_headers () 37self .wfile .write (data ) 38 39 40class ConnectCheckJson (unittest .TestCase ): 41def setUp (self ): 42self .home = Home ().__enter__ () 43self .addCleanup (self .home .__exit__ ,None ,None ,None ) 44self .server = ThreadingHTTPServer (("127.0.0.1" ,0 ),Handler ) 45self .port = self .server .server_address [1 ] 46threading .Thread (target = self .server .serve_forever ,daemon = True ).start () 47self .addCleanup (self .server .shutdown ) 48Handler .mode = "good" 49 50def free_port (self )-> int : 51with socket .socket ()as s : 52s .bind (("127.0.0.1" ,0 )) 53return s .getsockname ()[1 ] 54 55def run_json (self ,port :int ,* extra :str ): 56code ,out ,err = run_cli (["connect" ,"pi" ,"--url" ,f"http://127.0.0.1: { port } " ,"--context" ,"128k" ,"--check" ,"--output" ,"json" ,* extra ]) 57return code ,json .loads (out ),err 58 59def run_human (self ,port :int ): 60return run_cli (["connect" ,"pi" ,"--url" ,f"http://127.0.0.1: { port } " ,"--context" ,"128k" ,"--check" ]) 61 62def test_a_working_server_passes_both_checks_with_ok_true_and_exit_zero (self ): 63code ,data ,_ = self .run_json (self .port ) 64self .assertEqual (code ,0 ) 65self .assertIs (data ["ok" ],True ) 66self .assertEqual ([c ["name" ]for c in data ["checks" ]], ["prompt" ,"tool call" ]) 67self .assertTrue (all (c ["ok" ]for c in data ["checks" ])) 68self .assertIn ("get_weather" ,data ["checks" ][1 ]["detail" ]) 69self .assertEqual (data ["checked_server" ],f"http://127.0.0.1: { self . port } " ) 70self .assertEqual (data ["vendor_tested" ],"pi 0.74.2" ) 71self .assertIn ("files" ,data )# the rendered setup is still there 72 73def test_a_server_that_answers_wrongly_is_false_with_exit_one_and_says_which_check (self ): 74for mode ,failing in (("no-text" ,"prompt" ), ("no-tool" ,"tool call" )): 75Handler .mode = mode 76code ,data ,_ = self .run_json (self .port ) 77self .assertEqual (code ,1 ,mode ) 78self .assertIs (data ["ok" ],False ) 79bad = [c ["name" ]for c in data ["checks" ]if not c ["ok" ]] 80self .assertEqual (bad , [failing ],mode ) 81self .assertIn (failing ,data ["error" ]["message" ]) 82self .assertIn ("answered" ,data ["error" ]["message" ]) 83 84def test_nothing_listening_is_false_with_exit_one_and_says_it_could_not_be_reached (self ): 85port = self .free_port () 86code ,data ,_ = self .run_json (port ) 87self .assertEqual (code ,1 ) 88self .assertIs (data ["ok" ],False ) 89self .assertEqual ([c ["ok" ]for c in data ["checks" ]], [False ,False ]) 90self .assertIn ("could not be reached" ,data ["error" ]["message" ]) 91 92def test_json_and_human_agree_on_the_exit_code (self ): 93port = self .free_port () 94self .assertEqual (self .run_json (port )[0 ],self .run_human (port )[0 ]) 95Handler .mode = "no-tool" 96self .assertEqual (self .run_json (self .port )[0 ],self .run_human (self .port )[0 ]) 97Handler .mode = "good" 98self .assertEqual (self .run_json (self .port )[0 ],self .run_human (self .port )[0 ],0 ) 99 100def test_without_check_json_is_unchanged_and_asks_nothing_of_the_server (self ): 101code ,out ,_ = run_cli (["connect" ,"pi" ,"--url" ,f"http://127.0.0.1: { self . free_port () } " ,"--output" ,"json" ]) 102data = json .loads (out ) 103self .assertEqual (code ,0 ) 104self .assertTrue (data ["ok" ]) 105self .assertNotIn ("checks" ,data ) 106 107def test_the_list_says_vendor_tested_and_not_a_check_of_your_server (self ): 108code ,out ,_ = run_cli (["connect" ]) 109self .assertIn ("Vendor tested" ,out ) 110self .assertIn ("not a check of your server" ,out ) 111code ,out ,_ = run_cli (["connect" ,"--output" ,"json" ]) 112rows = json .loads (out )["harnesses" ] 113self .assertEqual (next (r for r in rows if r ["id" ]== "pi" )["vendor_tested" ],"pi 0.74.2" ) 114 115 116if __name__ == "__main__" : 117unittest .main ()