okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1631a9fde
main
1/** The notice for chats the host could not write (owner wording 11-04): shown once, with a copy button, in every language, gone when the chats are saved. */ 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { build } from 'esbuild' ; 5import { fileURLToPath } from 'node:url' ; 6import { mkdtempSync , rmSync , readFileSync } from 'node:fs' ; 7import { tmpdir } from 'node:os' ; 8import { join } from 'node:path' ; 9import { createChatStore , realIo } from '../server/chat-store.mjs' ; 10import { installFakeHostFetch } from './helpers/fake-host-chats.mjs' ; 11 12const here = ( p ) => fileURLToPath ( new URL ( p , import . meta. url )); 13async function bundle ( entry , plugins = []) { 14const result = await build ({ 15entryPoints :[ here ( entry )], bundle :true , format :'esm' , platform :'node' , write :false , logLevel :'silent' , 16alias :{ react :here ( './helpers/react-stub.mjs' ) }, loader :{ '.json' :'json' }, plugins, 17}); 18return import ( `data:text/javascript;base64, ${ Buffer . from ( result . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 19} 20 21const TITLE = 'Some chats could not be saved' ; 22const BODY = "Strata's chat storage is full, so the newest changes to {0} chat(s) are only in this window. Delete old chats to make room; they then save by themselves." ; 23const BUTTON = 'Copy unsaved chats' ; 24 25test ( 'the notice exists in all nine languages, with the placeholder kept' , () => { 26for ( const lang of [ 'en' , 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]) { 27const file = JSON . parse ( readFileSync ( here ( `../src/locales/sav- ${ lang } .json` ), 'utf8' )); 28assert . deepEqual ( Object . keys ( file ), [ 'sav01' , 'sav02' , 'sav03' ], lang ); 29assert . ok ( / \{0\} / . test ( file . sav02 ), ` ${ lang } keeps {0}` ); 30assert . ok ( Object . values ( file ). every (( t ) => t . trim (). length > 3 ), lang ); 31if ( lang === 'en' ) assert . deepEqual ( Object . values ( file ), [ TITLE , BODY , BUTTON ], 'the owner\'s exact wording' ); 32else assert . notEqual ( file . sav01 , TITLE , ` ${ lang } is translated` ); 33} 34}); 35 36test ( 'the catalog finds the notice: the German page shows it in German, with the number filled in' , async () => { 37const i18n = await bundle ( '../src/lib/i18n.ts' ); 38await i18n . setTranslationLanguage ( 'de' ); 39assert . equal ( i18n . tr ( TITLE ), 'Einige Chats konnten nicht gespeichert werden' ); 40assert . equal ( i18n . tr ( BUTTON ), 'Nicht gespeicherte Chats kopieren' ); 41assert . match ( i18n . tr ( BODY , [ 3 ]), / ^Der Chat-Speicher von Strata ist voll, daher gibt es die neuesten Änderungen an 3 Chat\(s\) nur in diesem Fenster / ); 42i18n . setTranslationLanguage ( 'en' ); 43assert . equal ( i18n . tr ( BODY , [ 3 ]), "Strata's chat storage is full, so the newest changes to 3 chat(s) are only in this window. Delete old chats to make room; they then save by themselves." ); 44}); 45 46test ( 'a full disk raises the notice once with a working copy button; offline raises nothing; saving by itself takes the notice away' , async () => { 47const dir = mkdtempSync ( join ( tmpdir (), 'strata-notice-' )); 48let full = false ; 49const chatStore = createChatStore ({ dir, io :{ ...realIo , async writeFile ( path , data ) { if ( full ) throw Object . assign ( new Error ( 'no space left on device' ), { code :'ENOSPC' }); return realIo . writeFile ( path , data ); } } }); 50const fake = installFakeHostFetch ( chatStore ); 51try { 52globalThis . window ??= globalThis ; 53globalThis . document ??= { addEventListener () {}, hasFocus :() => true , visibilityState :'visible' , documentElement :{ dataset :{}, style :{ setProperty () {} }, classList :{ toggle () {}, add () {}, remove () {} } } }; 54const memory = new Map (); 55globalThis . localStorage = { get length () { return memory . size ; }, key :( i ) => [ ...memory . keys ()][ i ] ?? null , getItem :( k ) => memory . get ( k ) ?? null , setItem :( k , v ) => { memory . set ( k , v ); }, removeItem :( k ) => memory . delete ( k ) }; 56globalThis . navigator ??= { language :'en' }; 57const copied = []; 58Object . defineProperty ( globalThis , 'navigator' , { value :{ language :'en' , clipboard :{ writeText :async ( t ) => { copied . push ( t ); } } }, configurable :true }); 59globalThis . matchMedia ??= () => ({ matches :false , addEventListener () {}, removeEventListener () {}, addListener () {}, removeListener () {} }); 60const mockClient = { 61name :'mock-client' , 62setup ( b ) { 63b . onResolve ({ filter :/ \/client$ / }, () => ({ path :'mock-client' , namespace :'mock' })); 64b . onLoad ({ filter :/ . */ , namespace :'mock' }, () => ({ loader :'js' , contents :` 65export class ApiError extends Error {} 66const reject = async () => { throw new Error('offline'); }; 67export const api = { get: reject, post: async () => ({}), put: reject, patch: reject, del: reject, delete: reject }; 68export function onEvent() { return () => {}; } 69export async function streamPost(path, body, onFrame) { onFrame({ type: 'start', model: 'm', contextMax: 8192 }); onFrame({ type: 'delta', text: 'an answer' }); onFrame({ type: 'done', tokensIn: 1, tokensOut: 2, durationMs: 1, usageSource: 'reported' }); }` })); 70}, 71}; 72const store = await bundle ( '../src/lib/store.ts' , [ mockClient ]); 73await store . loadChatsFromHost (); 74const toasts = () => store . useStore (( s ) => s . toasts ); 75const notice = () => store . useStore (( s ) => s . sessionSave ); 76 77store . newSession (); 78await store . flushSessions (); 79assert . equal ( notice (), null ); 80full = true ; 81await store . sendMessage ( 'a question that cannot be saved' ); 82await store . flushSessions (); 83assert . equal ( notice (). reason , 'disk-full' ); 84assert . equal ( notice (). failedIds . length , 1 ); 85const shown = toasts (). filter (( t ) => t . title === TITLE ); 86assert . equal ( shown . length , 1 , 'shown once' ); 87assert . equal ( shown [ 0 ]. tone , 'error' ); 88assert . equal ( shown [ 0 ]. body , "Strata's chat storage is full, so the newest changes to 1 chat(s) are only in this window. Delete old chats to make room; they then save by themselves." ); 89assert . equal ( shown [ 0 ]. action . label , BUTTON ); 90await store . flushSessions (); 91assert . equal ( toasts (). filter (( t ) => t . title === TITLE ). length , 1 , 'a second failed save does not show it again' ); 92 93// the button copies the unsaved chats whole 94assert . ok ( store . unsavedSessionsJson (). includes ( 'a question that cannot be saved' )); 95shown [ 0 ]. action . run (); 96for ( let i = 0 ; i < 20 && ! copied . length ; i ++ ) await new Promise (( r ) => setTimeout ( r , 10 )); 97assert . equal ( copied . length , 1 ); 98const copy = JSON . parse ( copied [ 0 ]); 99assert . equal ( copy [ 0 ]. messages . some (( m ) => m . content === 'a question that cannot be saved' ), true ); 100 101// a host that does not answer is not a full disk: no new notice, nothing claimed 102full = false ; 103store . dismissToast ( shown [ 0 ]. id ); 104await store . flushSessions (); // there is room now: the chat saves and the notice goes 105assert . equal ( notice (), null ); 106assert . equal ( toasts (). filter (( t ) => t . title === TITLE && ! t . leaving ). length , 0 , 'the notice is taken away when the chats are saved' ); 107const onDisk = JSON . parse ( await chatStore . read ( copy [ 0 ]. id )). session ; 108assert . equal ( onDisk . messages . some (( m ) => m . content === 'a question that cannot be saved' ), true , 'and the chat is on disk after all' ); 109} finally { fake . restore (); rmSync ( dir , { recursive :true , force :true }); } 110});