okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5d9f47922
main
1"""The web page's chat history: saved chats in the browser (#1787), edit / regenerate / rewind with an Undo (#2615), the rewind 2choice between "here" and a branch (#3823), and sending while an answer is written (#4004). One design, opt-in, off by default. 3 4The logic runs in Node (serve/test_web_history.js); what the page does in a real browser is in tools/webtest/browser_test.mjs 5(run here when WEBTEST_MODULES is set). These checks read the sources for what must hold whatever the code looks like: 6off is the page from before, and nothing about a chat goes to the server.""" 7from __future__import annotations 8 9import os 10import re 11import shutil 12import subprocess 13import unittest 14import urllib .error 15import urllib .request 16from pathlib import Path 17 18from serve .frontend import ChatTemplate 19from serve .server import ByteTokenizer ,MockEngine ,Service ,serve 20 21ROOT = Path (__file__ ).resolve ().parents [1 ] 22WEB = Path (__file__ ).parent / "web" 23 24 25@ unittest . skipUnless ( shutil . which ( "node" ), "node is not installed" ) 26class HistoryInNode (unittest .TestCase ): 27def test_messages_titles_branches_and_the_browser_store (self ): 28r = subprocess .run ([shutil .which ("node" ),str (Path (__file__ ).with_name ("test_web_history.js" ))], 29capture_output = True ,text = True ,timeout = 60 ) 30self .assertEqual (r .returncode ,0 ,r .stdout + r .stderr ) 31 32 33class OffIsThePageFromBefore (unittest .TestCase ): 34@classmethod 35def setUpClass (cls ): 36cls .html = (WEB / "index.html" ).read_text (encoding = "utf-8" ) 37cls .app = (WEB / "app.js" ).read_text (encoding = "utf-8" ) 38cls .ui = (WEB / "chats-ui.js" ).read_text (encoding = "utf-8" ) 39cls .store = (WEB / "chatstore.js" ).read_text (encoding = "utf-8" ) 40 41def test_the_three_switches_start_off (self ): 42self .assertIn ('chatList: false, chatEdit: false, whileBusy: "wait"' ,self .app ) 43for el in ("s-list" ,"s-edit" ): 44self .assertIn ('aria-checked="false"' ,re .search (rf'<button[^>]*id=" { el } "[^>]*>' ,self .html ).group (0 )) 45self .assertIn ('data-v="wait"' ,self .html ) 46self .assertRegex (self .html ,r'<aside class="chat-sidebar"[^>]*\bhidden>' )# no list until it is switched on 47 48def test_the_browser_store_is_opened_only_for_the_list (self ): 49self .assertEqual (self .ui .count ("chatStore.start()" ),1 ) 50self .assertRegex (self .ui ,r"async function openChats\(fromScreen\) \{\s*await chatStore\.start\(\);" ) 51self .assertEqual (len (re .findall (r"\bopenChats\(" ,self .ui )),2 )# its definition and the one call, in setChatList 52self .assertNotIn ("indexedDB" ,self .app ,"the page's main script never touches the store" ) 53self .assertIn ("if (settings.chatList) await setChatList(true, false);" ,self .app ) 54 55def test_with_the_list_off_one_chat_is_kept_as_before (self ): 56self .assertIn ('if (!listOn) { store.set("chat", H.cleanMessages(messages)); return; }' ,self .ui ) 57self .assertIn ('$("new-btn").hidden = on;' ,self .ui )# only the list's own New chat shows while it is on 58 59def test_pictures_and_files_are_kept_as_names_only (self ): 60self .assertIn ("chatStore.put(H.cleanChat(chat))" ,self .ui ) 61self .assertNotIn ("chatStore.put(chat)" ,self .ui ) 62 63def test_nothing_about_a_chat_goes_to_the_server (self ): 64for name ,src in (("app.js" ,self .app ), ("chats-ui.js" ,self .ui ), ("chatstore.js" ,self .store )): 65for call in re .findall (r'fetch\(\s*([^,)]+)' ,src ): 66if call != '"v1/chat/completions"' :# the question itself, as ever 67self .assertNotRegex (call ,r"chat|histor" ,f" { name } : { call } " ) 68 69def test_the_edit_controls_wait_for_a_finished_answer (self ): 70self .assertIn ("const historyLocked = () => !!busy || !!panel || !!pending;" ,self .ui ) 71self .assertIn ("$(\"s-list\").disabled = $(\"s-edit\").disabled = !!busy || !!panel;" ,self .app ) 72 73def test_an_interrupted_answer_keeps_what_was_written (self ): 74self .assertIn ("if (cut) m.interrupted = true;" ,self .app ) 75self .assertIn ("busy.cutAfterTool = true; return; } // let the tool finish" ,self .app ) 76self .assertIn ("settlePending(cut, !m.stopped && !m.error);" ,self .app ) 77 78def test_sending_while_busy_parks_nothing_unless_asked (self ): 79self .assertIn ('const parksWhileBusy = () => settings.whileBusy === "queue" || settings.whileBusy === "interject";' ,self .app ) 80self .assertIn ("if (!parksWhileBusy()) return;" ,self .app ) 81 82 83class NothingOnTheServer (unittest .TestCase ): 84@classmethod 85def setUpClass (cls ): 86tok = ByteTokenizer () 87cls .svc = Service (MockEngine (tok ,"ok" ),tok ,ChatTemplate (ROOT / "serve" / "chat_template.jinja" )) 88cls .httpd = serve (cls .svc ,port = 0 ) 89cls .base = f"http://127.0.0.1: { cls . httpd . server_address [ 1 ] } " 90 91@classmethod 92def tearDownClass (cls ): 93cls .httpd .shutdown () 94cls .httpd .server_close () 95 96def test_the_scripts_are_served_and_there_is_no_chat_endpoint (self ): 97for name in ("history.js" ,"chatstore.js" ,"chats-ui.js" ): 98with urllib .request .urlopen (f" { self . base } /web/ { name } " ,timeout = 10 )as r : 99self .assertEqual (r .status ,200 ) 100for path in ("/chats" ,"/api/chats" ,"/history" ,"/api/history" ): 101with self .subTest (path = path ): 102with self .assertRaises (urllib .error .HTTPError )as e : 103urllib .request .urlopen (self .base + path ,timeout = 10 ) 104self .assertEqual (e .exception .code ,404 ) 105 106 107@ unittest . skipUnless ( os . environ . get ( "WEBTEST_MODULES" ) and shutil . which ( "node" ), 108"set WEBTEST_MODULES (a folder with node_modules/puppeteer-core) to run the browser tests" ) 109class InABrowser (unittest .TestCase ): 110def test_history_in_chrome (self ): 111for name in ("history" ,"list" ,"rewind" ,"branch" ,"regenerate" ,"edit answer" ,"while answering" ): 112with self .subTest (name = name ): 113r = subprocess .run ([shutil .which ("node" ),str (ROOT / "tools" / "webtest" / "browser_test.mjs" ),name ], 114capture_output = True ,text = True ,timeout = 300 ) 115self .assertEqual (r .returncode ,0 ,r .stdout + r .stderr ) 116 117 118if __name__ == "__main__" : 119unittest .main ()