okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8667a3dbf
main
1// Connect an app: Anthropic-style apps get the server root (they add /v1/messages themselves), OpenAI-style apps keep /v1, 2// and the host accepts /v1/v1/messages as the same request for people who pasted the old address. 3import test from 'node:test' ; 4import assert from 'node:assert/strict' ; 5import { readFile } from 'node:fs/promises' ; 6import http from 'node:http' ; 7import { build } from 'esbuild' ; 8import { apiPath } from '../server/api-paths.mjs' ; 9import { createLocalShare } from '../server/local-share.mjs' ; 10 11const bundled = await build ({ entryPoints :[ 'src/lib/connect-urls.ts' ], bundle :true , write :false , format :'esm' , platform :'node' }); 12const { serverRoot, appBaseUrl} = await import ( `data:text/javascript;base64, ${ Buffer . from ( bundled . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 13 14test ( 'the Anthropic tab shows the address without /v1, the other tabs keep it' , () => { 15const base = 'http://127.0.0.1:8792/v1' ; 16assert . equal ( appBaseUrl ( base , 'anthropic' ), 'http://127.0.0.1:8792' ); 17assert . equal ( appBaseUrl ( base , 'openai' ), base ); 18assert . equal ( appBaseUrl ( base , 'responses' ), base ); 19assert . equal ( serverRoot ( 'https://abc.trycloudflare.com/v1/' ), 'https://abc.trycloudflare.com' ); 20assert . equal ( serverRoot ( 'http://192.168.1.5:8792' ), 'http://192.168.1.5:8792' ); 21assert . equal ( serverRoot ( 'http://host/api/v1' ), 'http://host/api' ); 22}); 23 24test ( 'the Connect dialog gives the Anthropic tab the root address and says why in plain words' , async () => { 25const screen = await readFile ( 'src/screens/Serving.tsx' , 'utf8' ); 26assert . match ( screen , / appBaseUrl\(serving\.baseUrl, protocol\) / ); 27assert . match ( screen , / ANTHROPIC_BASE_URL / ); 28}); 29 30test ( '/v1/v1/messages is read as /v1/messages, and nothing else is aliased' , () => { 31assert . equal ( apiPath ( '/v1/v1/messages' ), '/v1/messages' ); 32assert . equal ( apiPath ( '/v1/v1/messages/' ), '/v1/messages' ); 33assert . equal ( apiPath ( '/v1/v1/messages/count_tokens' ), '/v1/messages/count_tokens' ); 34assert . equal ( apiPath ( '/v1/messages' ), '/v1/messages' ); 35for ( const other of [ '/v1/v1/chat/completions' , '/v1/v1/models' , '/v1/v1/responses' , '/v1/v1/v1/messages' ]) assert . equal ( apiPath ( other ), other ); 36}); 37 38test ( 'the shared (key-protected) listener serves /v1/v1/messages as /v1/messages' , async () => { 39const seen = []; 40const share = createLocalShare ({ handle :async ( req , res , path ) => { seen . push ( path ); res . writeHead ( 200 ); res . end ( 'ok' ); }, hasKey :() => true , host :'127.0.0.1' , port :0 }); 41const { url} = await share . start (); 42try { 43const root = url . replace ( / \/v1$ / , '' ); 44for ( const path of [ '/v1/messages' , '/v1/v1/messages' ]) { 45const res = await fetch ( root + path , { method :'POST' , body :'{}' }); 46assert . equal ( res . status , 200 , path ); 47} 48assert . deepEqual ( seen , [ '/v1/messages' , '/v1/messages' ]); 49assert . equal (( await fetch ( root + '/v1/v1/chat/completions' , { method :'POST' , body :'{}' })). status , 404 ); 50} finally { await share . stop (); } 51});