fix: harden host language switching

This commit is contained in:
xmanrui 2026-08-24 03:55:55 +08:00
parent 3741dd8fd9
commit d4b8666495
23 changed files with 189 additions and 90 deletions

View file

@ -20,6 +20,7 @@ import {
normalizeDiscordMessage,
resolveDiscordMessageRoute,
} from '../../../src/channels/discord/discord-runtime.mjs';
import { setImHostLanguage } from '../../../src/channels/shared/i18n.mjs';
import {
DISCORD_ENDPOINTS,
createDiscordRpcHandler,
@ -387,6 +388,15 @@ test('Discord controller persists a credential reference and exposes only masked
const status = await controller.bindCredentials({ token: TOKEN });
assert.equal(status.totals.connected, 1);
assert.equal(status.bots[0].bot.name, 'Harness Discord');
setImHostLanguage('en');
try {
assert.equal(
controller.status().bots[0].health.summary,
'The Discord Gateway long-lived connection is running normally',
);
} finally {
setImHostLanguage('zh');
}
const identity = deriveDiscordBotIdentity('1234567890123456789');
assert.equal(credentialStore.values.get(identity.tokenRef), TOKEN);
assert.doesNotMatch(await readFile(configPath, 'utf8'), new RegExp(TOKEN.replaceAll('.', '\\.')));

View file

@ -1,7 +1,7 @@
import assert from 'node:assert/strict';
import { readdirSync, readFileSync, statSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { after, test } from 'node:test';
import {
@ -30,7 +30,7 @@ test('setImHostLanguage accepts English spellings and falls back to Chinese', ()
setImHostLanguage(value);
assert.equal(getImHostLanguage(), 'en', `expected ${value} to select English`);
}
for (const value of [undefined, null, '', 'zh', 'zh-CN', 'fr', 42]) {
for (const value of [undefined, null, '', 'zh', 'zh-CN', 'fr', 'enabled', 42]) {
setImHostLanguage(value);
assert.equal(getImHostLanguage(), 'zh', `expected ${value} to select Chinese`);
}
@ -49,11 +49,36 @@ test('t() translates known keys and fills placeholders in English mode', () => {
test('every English dictionary entry is a non-empty translation of a Chinese key', () => {
for (const [key, value] of Object.entries(EN)) {
assert.ok(/[一-鿿]/.test(key), `dictionary key is not Chinese: ${key}`);
assert.ok(/[一-鿿()]/.test(key), `dictionary key is not a Chinese source string: ${key}`);
assert.ok(typeof value === 'string' && value.trim().length > 0, `empty translation for: ${key}`);
const placeholders = (text) => [...new Set(
[...text.matchAll(/\{([a-zA-Z][a-zA-Z0-9_]*)\}/g)].map((match) => match[1]),
)].sort();
assert.deepEqual(
placeholders(value),
placeholders(key),
`placeholder mismatch for: ${key}`,
);
}
});
test('English dictionary source files do not define duplicate keys', async () => {
const dictionaryDir = join(srcChannelsDir, 'shared', 'i18n-en');
const seen = new Map();
const duplicates = [];
const files = readdirSync(dictionaryDir)
.filter((file) => file.endsWith('.mjs'))
.sort();
for (const file of files) {
const { default: entries } = await import(pathToFileURL(join(dictionaryDir, file)).href);
for (const key of Object.keys(entries)) {
if (seen.has(key)) duplicates.push(`${key}: ${seen.get(key)}, ${file}`);
else seen.set(key, file);
}
}
assert.deepEqual(duplicates, []);
});
test('every literal t() key in src/channels has an English dictionary entry', () => {
const files = [];
const walk = (dir) => {

View file

@ -17,8 +17,10 @@ import {
} from '../../../src/channels/slack/slack-api.mjs';
import {
SlackRuntime,
isSlackToolProgress,
normalizeSlackEvent,
} from '../../../src/channels/slack/slack-runtime.mjs';
import { setImHostLanguage } from '../../../src/channels/shared/i18n.mjs';
import { SLACK_APP_MANIFEST_YAML } from '../../../src/channels/slack/manifest.mjs';
import {
SLACK_ENDPOINTS,
@ -57,6 +59,20 @@ function deferred() {
return { promise, resolve, reject };
}
test('Slack recognizes localized tool progress without hiding ordinary text', () => {
assert.equal(isSlackToolProgress('正在使用搜索…'), true);
assert.equal(isSlackToolProgress('正在使用…'), false);
assert.equal(isSlackToolProgress('普通回复'), false);
setImHostLanguage('en');
try {
assert.equal(isSlackToolProgress('Using search…'), true);
assert.equal(isSlackToolProgress('Using …'), false);
assert.equal(isSlackToolProgress('Ordinary reply'), false);
} finally {
setImHostLanguage('zh');
}
});
async function eventually(predicate, timeoutMs = 1_000) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {

View file

@ -9,6 +9,7 @@ import {
OutboundArtifactRegistry,
createOutboundArtifactTool,
} from '../../../src/channels/shared/semantic/artifact.mjs';
import { setImHostLanguage } from '../../../src/channels/shared/i18n.mjs';
import {
TELEGRAM_ACCESS_MODES,
TelegramConfigStore,
@ -28,6 +29,7 @@ import {
TelegramRuntime,
TELEGRAM_COMMAND_MENU,
normalizeTelegramUpdate,
telegramCommandMenu,
telegramInboundAllowed,
} from '../../../src/channels/telegram/telegram-runtime.mjs';
import { TelegramStateStore } from '../../../src/channels/telegram/state-store.mjs';
@ -247,6 +249,17 @@ test('Telegram API registers the command menu and commands-type menu button', as
await assert.rejects(() => api.setChatMenuButton({ menuButton: 'commands' }), /menu button is invalid/);
});
test('Telegram command menu follows the host language at runtime', () => {
assert.equal(TELEGRAM_COMMAND_MENU[0].description, '开启一个全新会话');
setImHostLanguage('en');
try {
assert.equal(telegramCommandMenu()[0].description, 'Start a brand-new Session');
} finally {
setImHostLanguage('zh');
}
assert.deepEqual(telegramCommandMenu(), TELEGRAM_COMMAND_MENU);
});
test('Telegram API preserves the legacy plain send and edit payloads', async () => {
const calls = [];
const api = new TelegramApi({