okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
52ba0b781
main
1// T5 / T6 (tools and images review, 2026-10-22): the agent's tool servers are the default model's, one naming rule is shared by the editor, the 2// save check and the profile, and a server that is left out is reported instead of vanishing. 3import test from 'node:test' ; 4import assert from 'node:assert/strict' ; 5import { readFile } from 'node:fs/promises' ; 6import { mcpRows , mcpSkippedNotices , buildProfile } from '../server/agent/profile.mjs' ; 7import { MCP_NAME_MAX , MCP_NAME_RE , validMcpName , MCP_NAME_HELP } from '../server/mcp-names.mjs' ; 8import { validateMcpServers } from '../server/native-settings.mjs' ; 9 10const cmd = { command :'node' , args :[ 'x.js' ] }; 11 12test ( 'one rule: 32 characters of letters, numbers, hyphens and underscores' , () => { 13assert . equal ( MCP_NAME_MAX , 32 ); 14for ( const ok of [ 'files' , 'my_server-1' , 'A' , 'x' . repeat ( 32 )]) assert . ok ( validMcpName ( ok ), ok ); 15for ( const bad of [ '' , 'my.server' , 'x' . repeat ( 33 ), 'x' . repeat ( 40 ), 'a b' , 'ä' , 'a/b' ]) assert . ok ( ! validMcpName ( bad ), bad ); 16assert . match ( MCP_NAME_HELP , / up to 32 / ); 17}); 18 19test ( 'the editor keeps the same rule as the host (a copy in src/lib/mcp-names.ts)' , async () => { 20const text = await readFile ( new URL ( '../src/lib/mcp-names.ts' , import . meta. url ), 'utf8' ); 21assert . ok ( text . includes ( String ( MCP_NAME_RE )), 'same regular expression' ); 22assert . ok ( text . includes ( `MCP_NAME_MAX = ${ MCP_NAME_MAX } ` )); 23assert . ok ( text . includes ( MCP_NAME_HELP ), 'same help sentence' ); 24const editor = await readFile ( new URL ( '../src/components/NativeMcpEditor.tsx' , import . meta. url ), 'utf8' ); 25assert . ok ( editor . includes ( 'validMcpName(name)' ) && ! editor . includes ( '{1,64}' )); 26}); 27 28test ( 'the save check refuses a new name over 32 but does not block the other settings of a name saved before' , () => { 29const long = 'x' . repeat ( 40 ); 30assert . throws (() => validateMcpServers ({ [ long ] :cmd }, {}), / up to 32 / ); 31assert . throws (() => validateMcpServers ({ 'my.server' :cmd }, {}), / up to 32 / ); 32assert . deepEqual ( Object . keys ( validateMcpServers ({ [ long ] :cmd }, { [ long ] :cmd })), [ long ]); 33assert . deepEqual ( Object . keys ( validateMcpServers ({ files :cmd }, {})), [ 'files' ]); 34}); 35 36test ( 'a server that cannot be wired is reported with its reason, not dropped silently' , () => { 37const skipped = []; 38const rows = mcpRows ({ 39files :cmd , [ ` ${ 'y' . repeat ( 40 )} ` ] :cmd , 'my.server' :cmd , empty :{}, off :{ ...cmd , disabled :true }, 40Files2 :cmd , 'files2' :cmd , 41}, {}, skipped ); 42assert . deepEqual ( rows . map ( r => r . config . serverName ), [ 'files' , 'Files2' ]); 43assert . deepEqual ( skipped . map ( s => [ s . name . slice ( 0 , 6 ), s . why ]), [[ 'yyyyyy' , 'name' ], [ 'my.ser' , 'name' ], [ 'empty' , 'address' ], [ 'files2' , 'duplicate' ]]); 44// switched off on purpose is not an error 45assert . ok ( ! skipped . some ( s => s . name === 'off' )); 46}); 47 48test ( 'the notices name the servers, one sentence per reason, in the catalog wording' , async () => { 49const notices = mcpSkippedNotices ([{ name :'a.b' , why :'name' }, { name :'c' . repeat ( 33 ), why :'name' }, { name :'e' , why :'address' }]); 50assert . equal ( notices . length , 2 ); 51assert . match ( notices [ 0 ]. message , / ^These tool servers were left out because their names are not allowed: a\.b, c { 33 } \. / ); 52assert . match ( notices [ 1 ]. message , / no program or web address: e\.$ / ); 53assert . notEqual ( notices [ 0 ]. code , notices [ 1 ]. code ); 54assert . deepEqual ( mcpSkippedNotices ([]), []); 55// every sentence is in the catalog of every language (src/locales/tools-<lang>.json) 56for ( const lang of [ 'en' , 'de' , 'es' , 'pt' , 'ru' , 'ja' , 'ko' , 'fr' , 'zh' ]) { 57const catalog = JSON . parse ( await readFile ( new URL ( `../src/locales/tools- ${ lang } .json` , import . meta. url ), 'utf8' )); 58const english = JSON . parse ( await readFile ( new URL ( '../src/locales/tools-en.json' , import . meta. url ), 'utf8' )); 59for ( const text of [ 'These tool servers were left out because their names are not allowed: {0}. A name can have up to 32 letters, numbers, hyphens or underscores.' , 60'These tool servers were left out because they have no program or web address: {0}.' , 61'These tool servers were left out because another one has almost the same name: {0}.' ]) { 62const id = Object . keys ( english ). find ( k => english [ k ] === text ); 63assert . ok ( id , text ); 64assert . ok ( catalog [ id ]?. includes ( '{0}' ), ` ${ lang } : ${ id } ` ); 65} 66} 67}); 68 69test ( 'the agent gets the tool servers of the DEFAULT instance, not the first one loaded' , () => { 70const local = [ 71{ id :'first' , modelId :'m1' , baseUrl :'http://127.0.0.1:1/v1' , mcpServers :{ alpha :cmd } }, 72{ id :'second' , modelId :'m2' , baseUrl :'http://127.0.0.1:2/v1' , mcpServers :{ beta :cmd } }, 73]; 74const names = profile => profile . patch . match ( / "serverName": " ([ ^ " ] + ) " / g )?. map ( s => s . slice ( 15 , - 1 )) ?? []; 75assert . deepEqual ( names ( buildProfile ({ local, defaultLocalId :'second' })), [ 'beta' ]); 76assert . deepEqual ( names ( buildProfile ({ local, defaultLocalId :'first' })), [ 'alpha' ]); 77assert . deepEqual ( names ( buildProfile ({ local})), [ 'alpha' ], 'no default named: the first, as before' ); 78assert . deepEqual ( names ( buildProfile ({ local, defaultLocalId :'gone' })), [ 'alpha' ], 'a default that is not loaded: the first' ); 79// the agent's own default model follows the same instance, and a different default changes what a running agent must be restarted for 80assert . equal ( buildProfile ({ local, defaultLocalId :'second' }). defaultRoute . instanceId , 'second' ); 81assert . notEqual ( buildProfile ({ local, defaultLocalId :'second' }). toolsSignature , buildProfile ({ local, defaultLocalId :'first' }). toolsSignature ); 82// the Settings servers are added for every default 83const withSettings = buildProfile ({ local, defaultLocalId :'second' , tools :{ off :new Set (), search :{ provider :'builtin' , key :'' }, servers :{ gamma :cmd } } }); 84assert . deepEqual ( names ( withSettings ). sort (), [ 'beta' , 'gamma' ]); 85}); 86 87test ( 'buildProfile hands the skipped servers on for the chat to report' , () => { 88const local = [{ id :'a' , modelId :'m' , baseUrl :'http://127.0.0.1:1/v1' , mcpServers :{ [ 'z' . repeat ( 33 )] :cmd , ok :cmd } }]; 89const profile = buildProfile ({ local, defaultLocalId :'a' }); 90assert . deepEqual ( profile . mcpSkipped . map ( s => s . why ), [ 'name' ]); 91assert . match ( profile . patch , / "serverName": "ok" / ); 92});