okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
85269ca15
main
1/** A stand-in for the host's /api/chats routes that is backed by the real chat store (server/chat-store.mjs), for tests of the page's side. */ 2import { ChatStoreError , storeFailure } from '../../server/chat-store.mjs' ; 3 4/** A transport (the page's ChatTransport) straight over a chat store, with a way to make the host go away. */ 5export function storeTransport ( store , TransportError , state = { offline :false }) { 6const guard = async ( work ) => { 7if ( state . offline ) throw new TransportError ( 'The host did not answer.' , 0 , 'offline' , true ); 8try { return await work (); } 9catch ( e ) { const f = e instanceof TransportError ?e :storeFailure ( e ); throw f instanceof TransportError ?f :new TransportError ( f . message , f . status , f . code ); } 10}; 11return { 12 state, 13list :() => guard ( async () => store . list ()), 14get :( id ) => guard ( async () => { const text = await store . read ( id ); if ( text === null ) return null ; const d = JSON . parse ( text ); return { rev :d . rev , session :d . session }; }), 15put :( id , text , baseRev ) => guard (() => store . save ( id , text , { baseRev})), 16remove :( id ) => guard ( async () => { await store . remove ( id ); }), 17importMany :( sessions ) => guard (() => store . importMany ( sessions )), 18}; 19} 20 21/** Install a global fetch that answers /api/chats* from the store; every other path is refused like an unreachable host. */ 22export function installFakeHostFetch ( store , { events= [] } = {}) { 23const calls = []; 24const previous = globalThis . fetch ; 25globalThis . fetch = async ( url , init = {}) => { 26const path = String ( url ). replace ( / ^https ? :\/\/ [ ^ / ] +/ , '' ); 27const method = init . method ?? 'GET' ; 28const headers = Object . fromEntries ( Object . entries ( init . headers ?? {}). map (([ k , v ]) => [ k . toLowerCase (), v ])); 29calls . push ({ method, path, headers}); 30const reply = ( status , body ) => ({ ok :status < 200 || status >= 300 ?false :true , status, json :async () => ( typeof body === 'string' ?JSON . parse ( body ) :body ), text :async () => ( typeof body === 'string' ?body :JSON . stringify ( body )) }); 31const m = / ^\/api\/chats (?: \/ ([ ^ /? ] + )) ?/ . exec ( path ); 32if ( ! m ) throw new TypeError ( 'fetch failed' ); 33if ( ! headers [ 'x-strata-client' ]) return reply ( 403 , { error :'Not allowed.' }); 34const id = m [ 1 ] ?decodeURIComponent ( m [ 1 ]) :null ; 35try { 36if ( ! id && method === 'GET' ) return reply ( 200 , store . list ()); 37if ( id === 'import' && method === 'POST' ) { const r = await store . importMany ( JSON . parse ( init . body ). sessions ); return reply ( 200 , r ); } 38if ( id && method === 'GET' ) { const t = await store . read ( id ); return t === null ?reply ( 404 , { error :'No such chat.' , code :'not-found' }) :reply ( 200 , t ); } 39if ( id && method === 'PUT' ) { const base = headers [ 'x-strata-base-rev' ]; const r = await store . save ( id , init . body , { baseRev :base !== undefined ?Number ( base ) :null }); events . push ({ id, rev :r . rev , from :headers [ 'x-strata-window' ] }); return reply ( 200 , r ); } 40if ( id && method === 'DELETE' ) { const r = await store . remove ( id ); events . push ({ id, deleted :true , from :headers [ 'x-strata-window' ] }); return reply ( 200 , r ); } 41} catch ( e ) { const f = e instanceof ChatStoreError ?e :storeFailure ( e ); return reply ( f . status , { error :f . message , code :f . code }); } 42return reply ( 405 , { error :'Method not allowed.' }); 43}; 44return { calls, restore () { globalThis . fetch = previous ; } }; 45}