okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-i18n: French as the ninth app language and the Polish README52ba0b781

main
6.0 KiB92 linesraw
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', () => {
13  assert.equal(MCP_NAME_MAX, 32);
14  for (const ok of ['files', 'my_server-1', 'A', 'x'.repeat(32)]) assert.ok(validMcpName(ok), ok);
15  for (const bad of ['', 'my.server', 'x'.repeat(33), 'x'.repeat(40), 'a b', 'ä', 'a/b']) assert.ok(!validMcpName(bad), bad);
16  assert.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 () => {
20  const text = await readFile(new URL('../src/lib/mcp-names.ts', import.meta.url), 'utf8');
21  assert.ok(text.includes(String(MCP_NAME_RE)), 'same regular expression');
22  assert.ok(text.includes(`MCP_NAME_MAX = ${MCP_NAME_MAX}`));
23  assert.ok(text.includes(MCP_NAME_HELP), 'same help sentence');
24  const editor = await readFile(new URL('../src/components/NativeMcpEditor.tsx', import.meta.url), 'utf8');
25  assert.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', () => {
29  const long = 'x'.repeat(40);
30  assert.throws(() => validateMcpServers({ [long]: cmd }, {}), /up to 32/);
31  assert.throws(() => validateMcpServers({ 'my.server': cmd }, {}), /up to 32/);
32  assert.deepEqual(Object.keys(validateMcpServers({ [long]: cmd }, { [long]: cmd })), [long]);
33  assert.deepEqual(Object.keys(validateMcpServers({ files: cmd }, {})), ['files']);
34});
35
36test('a server that cannot be wired is reported with its reason, not dropped silently', () => {
37  const skipped = [];
38  const rows = mcpRows({
39    files: cmd, [`${'y'.repeat(40)}`]: cmd, 'my.server': cmd, empty: {}, off: { ...cmd, disabled: true },
40    Files2: cmd, 'files2': cmd,
41  }, {}, skipped);
42  assert.deepEqual(rows.map(r => r.config.serverName), ['files', 'Files2']);
43  assert.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
45  assert.ok(!skipped.some(s => s.name === 'off'));
46});
47
48test('the notices name the servers, one sentence per reason, in the catalog wording', async () => {
49  const notices = mcpSkippedNotices([{ name: 'a.b', why: 'name' }, { name: 'c'.repeat(33), why: 'name' }, { name: 'e', why: 'address' }]);
50  assert.equal(notices.length, 2);
51  assert.match(notices[0].message, /^These tool servers were left out because their names are not allowed: a\.b, c{33}\./);
52  assert.match(notices[1].message, /no program or web address: e\.$/);
53  assert.notEqual(notices[0].code, notices[1].code);
54  assert.deepEqual(mcpSkippedNotices([]), []);
55  // every sentence is in the catalog of every language (src/locales/tools-<lang>.json)
56  for (const lang of ['en', 'de', 'es', 'pt', 'ru', 'ja', 'ko', 'fr', 'zh']) {
57    const catalog = JSON.parse(await readFile(new URL(`../src/locales/tools-${lang}.json`, import.meta.url), 'utf8'));
58    const english = JSON.parse(await readFile(new URL('../src/locales/tools-en.json', import.meta.url), 'utf8'));
59    for (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}.']) {
62      const id = Object.keys(english).find(k => english[k] === text);
63      assert.ok(id, text);
64      assert.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', () => {
70  const 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  ];
74  const names = profile => profile.patch.match(/"serverName": "([^"]+)"/g)?.map(s => s.slice(15, -1)) ?? [];
75  assert.deepEqual(names(buildProfile({ local, defaultLocalId: 'second' })), ['beta']);
76  assert.deepEqual(names(buildProfile({ local, defaultLocalId: 'first' })), ['alpha']);
77  assert.deepEqual(names(buildProfile({ local })), ['alpha'], 'no default named: the first, as before');
78  assert.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
80  assert.equal(buildProfile({ local, defaultLocalId: 'second' }).defaultRoute.instanceId, 'second');
81  assert.notEqual(buildProfile({ local, defaultLocalId: 'second' }).toolsSignature, buildProfile({ local, defaultLocalId: 'first' }).toolsSignature);
82  // the Settings servers are added for every default
83  const withSettings = buildProfile({ local, defaultLocalId: 'second', tools: { off: new Set(), search: { provider: 'builtin', key: '' }, servers: { gamma: cmd } } });
84  assert.deepEqual(names(withSettings).sort(), ['beta', 'gamma']);
85});
86
87test('buildProfile hands the skipped servers on for the chat to report', () => {
88  const local = [{ id: 'a', modelId: 'm', baseUrl: 'http://127.0.0.1:1/v1', mcpServers: { ['z'.repeat(33)]: cmd, ok: cmd } }];
89  const profile = buildProfile({ local, defaultLocalId: 'a' });
90  assert.deepEqual(profile.mcpSkipped.map(s => s.why), ['name']);
91  assert.match(profile.patch, /"serverName": "ok"/);
92});