|
| 1 | +// @ts-check |
| 2 | +// The words a failed turn shows the user, and the keys behind them. |
| 3 | +// |
| 4 | +// A turn that died for a reason the plugin cannot read used to say "try again shortly": no |
| 5 | +// cause, no next step, nothing the user could act on. The copy here replaces that with three |
| 6 | +// different sentences for three different situations — a code the plugin knows, an error the |
| 7 | +// plugin raised itself, and a failure whose cause only the Host knows — so the user can tell a |
| 8 | +// busy model from a session that never started. |
| 9 | +// |
| 10 | +// The page localises the codes it knows from its own dictionary, so a code the server and the |
| 11 | +// page spell differently is a sentence the user reads twice, in two languages, with two |
| 12 | +// meanings. The dictionaries are checked against each other here rather than by eye. |
| 13 | + |
| 14 | +import assert from 'node:assert/strict'; |
| 15 | +import { test } from 'node:test'; |
| 16 | +import { mkdtemp, rm } from 'node:fs/promises'; |
| 17 | +import { tmpdir } from 'node:os'; |
| 18 | +import { join } from 'node:path'; |
| 19 | + |
| 20 | +import SERVER from '../miniapp/node/strings.mjs'; |
| 21 | +import CLIENT from '../miniapp/client/strings-app.js'; |
| 22 | +import { t, setLocale } from '../miniapp/node/i18n.mjs'; |
| 23 | +import { describeAgentError, Engine, EngineError } from '../miniapp/node/engine.mjs'; |
| 24 | +import { MemoryError, MemoryStore } from '../miniapp/node/memory.mjs'; |
| 25 | +import { Store } from '../miniapp/node/store.mjs'; |
| 26 | + |
| 27 | +/** The sentence for a turn whose Agent never started. */ |
| 28 | +const UNAVAILABLE = 'agentError.AGENT_UNAVAILABLE'; |
| 29 | +/** The sentence for a failure whose cause the plugin cannot read. */ |
| 30 | +const HOST_ERROR = 'agentError.hostError'; |
| 31 | +/** The sentence for a model that cannot be used; it names a setting, so it is not a fallback. */ |
| 32 | +const MODEL_UNAVAILABLE = 'agentError.MODEL_UNAVAILABLE'; |
| 33 | + |
| 34 | +/** The model sentence, written out: it may not drift while the other copy is being changed. */ |
| 35 | +const MODEL_TEXT = { |
| 36 | + zh: '这个 Bot 设置的模型现在用不了(可能已停用或下线),请在 Bot 设置里换一个模型或改回默认', |
| 37 | + en: "This Bot's model can't be used right now (it may have been disabled or removed). Pick another model, or go back to the default, in the Bot's settings", |
| 38 | +}; |
| 39 | + |
| 40 | +/** Words that belong to the model sentence and would mislead a reader of the other one. */ |
| 41 | +const MODEL_WORDS = ['模型', 'model', 'settings', 'default', 'disabled', '停用', '下线']; |
| 42 | + |
| 43 | +/** The codes the plugin knows by name, in the order the page shows them. */ |
| 44 | +const KNOWN_CODES = [ |
| 45 | + 'BUSY', |
| 46 | + 'NOT_PERMITTED', |
| 47 | + 'SESSION_NOT_FOUND', |
| 48 | + 'INVALID_ARGUMENTS', |
| 49 | + 'AGENT_OUTCOME_UNKNOWN', |
| 50 | + 'REQUEST_CANCELLED', |
| 51 | +]; |
| 52 | + |
| 53 | +/** |
| 54 | + * A turn that fails once, with the Agent replaced by one that throws. |
| 55 | + * @param {import('node:test').TestContext} ctx |
| 56 | + * @param {unknown} thrown |
| 57 | + */ |
| 58 | +async function failedTurn(ctx, thrown) { |
| 59 | + const dir = await mkdtemp(join(tmpdir(), 'bot-agent-error-')); |
| 60 | + const store = new Store(dir); |
| 61 | + const memory = new MemoryStore(dir); |
| 62 | + await store.init(); |
| 63 | + await memory.init(); |
| 64 | + const engine = new Engine({ |
| 65 | + store, |
| 66 | + memory, |
| 67 | + logger: { warn() {} }, |
| 68 | + broadcast() {}, |
| 69 | + retryDelaysMs: [], |
| 70 | + }); |
| 71 | + engine.agent = { |
| 72 | + async run() { |
| 73 | + throw thrown; |
| 74 | + }, |
| 75 | + }; |
| 76 | + ctx.after(async () => { |
| 77 | + engine.dispose(); |
| 78 | + await store.writeChain; |
| 79 | + await rm(dir, { recursive: true, force: true }); |
| 80 | + }); |
| 81 | + const bot = store.state.bots[0]; |
| 82 | + const chatId = `bot:${bot.id}`; |
| 83 | + await engine.runBotTurn({ bot, chatId, text: 'Hello', kind: 'private' }); |
| 84 | + const messages = await store.getMessages(chatId); |
| 85 | + return messages.at(-1); |
| 86 | +} |
| 87 | +test('the page and the server spell the unavailable sentence the same way', () => { |
| 88 | + assert.equal(SERVER.zh[UNAVAILABLE], CLIENT.zh[UNAVAILABLE]); |
| 89 | + assert.equal(SERVER.en[UNAVAILABLE], CLIENT.en[UNAVAILABLE]); |
| 90 | +}); |
| 91 | + |
| 92 | +test('every dictionary carries the same keys in both languages', () => { |
| 93 | + assert.deepEqual(Object.keys(SERVER.zh).sort(), Object.keys(SERVER.en).sort()); |
| 94 | + assert.deepEqual(Object.keys(CLIENT.zh).sort(), Object.keys(CLIENT.en).sort()); |
| 95 | +}); |
| 96 | + |
| 97 | +test('the page reads no error key the server does not have', () => { |
| 98 | + const server = new Set(Object.keys(SERVER.zh).filter((key) => key.startsWith('agentError.'))); |
| 99 | + const page = Object.keys(CLIENT.zh).filter((key) => key.startsWith('agentError.')); |
| 100 | + assert.ok(page.length > 0); |
| 101 | + for (const key of page) assert.ok(server.has(key), `the page has ${key} and the server does not`); |
| 102 | +}); |
| 103 | + |
| 104 | +test('the server has a sentence for a cause it cannot read, in both languages', () => { |
| 105 | + for (const lang of ['zh', 'en']) { |
| 106 | + const text = SERVER[lang][HOST_ERROR]; |
| 107 | + assert.ok(typeof text === 'string' && text.length > 0, `${lang} has no ${HOST_ERROR}`); |
| 108 | + assert.notEqual(text, HOST_ERROR); |
| 109 | + } |
| 110 | +}); |
| 111 | + |
| 112 | +test('the two new sentences differ between the two languages', () => { |
| 113 | + assert.notEqual(SERVER.zh[UNAVAILABLE], SERVER.en[UNAVAILABLE]); |
| 114 | + assert.notEqual(SERVER.zh[HOST_ERROR], SERVER.en[HOST_ERROR]); |
| 115 | +}); |
| 116 | + |
| 117 | +test('the unavailable sentence does not borrow the model sentence', () => { |
| 118 | + for (const lang of ['zh', 'en']) { |
| 119 | + assert.notEqual(SERVER[lang][UNAVAILABLE], SERVER[lang][MODEL_UNAVAILABLE]); |
| 120 | + const lower = SERVER[lang][UNAVAILABLE].toLowerCase(); |
| 121 | + for (const word of MODEL_WORDS) { |
| 122 | + assert.ok(!lower.includes(word.toLowerCase()), `the ${lang} unavailable sentence mentions ${word}`); |
| 123 | + } |
| 124 | + } |
| 125 | +}); |
| 126 | + |
| 127 | +test('every new sentence stays short, alone and behind the failure prefix', () => { |
| 128 | + for (const lang of ['zh', 'en']) { |
| 129 | + for (const key of [UNAVAILABLE, HOST_ERROR]) { |
| 130 | + const text = SERVER[lang][key]; |
| 131 | + assert.ok(text.length <= 120, `${lang} ${key} is ${text.length} characters`); |
| 132 | + const shown = t('notice.failed', { error: text }, lang); |
| 133 | + assert.ok(shown.length <= 120, `${lang} ${key} under notice.failed is ${shown.length} characters`); |
| 134 | + } |
| 135 | + } |
| 136 | +}); |
| 137 | + |
| 138 | +test('the model sentence is the one that was there before', () => { |
| 139 | + assert.equal(SERVER.zh[MODEL_UNAVAILABLE], MODEL_TEXT.zh); |
| 140 | + assert.equal(SERVER.en[MODEL_UNAVAILABLE], MODEL_TEXT.en); |
| 141 | +}); |
| 142 | + |
| 143 | +test('a Host error with a cause the plugin cannot read is described without its text', () => { |
| 144 | + for (const lang of ['zh', 'en']) { |
| 145 | + setLocale(lang); |
| 146 | + const text = describeAgentError({ code: 'SessionServiceError', message: 'SessionServiceError: svc-abc123 bind failed' }); |
| 147 | + assert.equal(text, t(HOST_ERROR)); |
| 148 | + for (const leak of ['svc-abc123', 'SessionServiceError', 'bind']) { |
| 149 | + assert.ok(!text.includes(leak), `the sentence leaks ${leak}`); |
| 150 | + } |
| 151 | + } |
| 152 | +}); |
| 153 | + |
| 154 | +test('a failure that is not an error at all is described without its text', () => { |
| 155 | + for (const lang of ['zh', 'en']) { |
| 156 | + setLocale(lang); |
| 157 | + for (const thrown of [null, undefined, 'boom', {}]) { |
| 158 | + const text = describeAgentError(thrown); |
| 159 | + assert.equal(text, t(HOST_ERROR)); |
| 160 | + for (const leak of ['null', 'undefined', 'boom']) { |
| 161 | + assert.ok(!text.includes(leak), `the sentence leaks ${leak}`); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +}); |
| 166 | + |
| 167 | +test("the plugin's own errors keep the message they were built with", () => { |
| 168 | + for (const lang of ['zh', 'en']) { |
| 169 | + setLocale(lang); |
| 170 | + const expected = t('err.noAgentSupport', undefined, lang); |
| 171 | + assert.equal(describeAgentError(new EngineError('agent_unavailable', expected)), expected); |
| 172 | + assert.equal(describeAgentError(new MemoryError(expected)), expected); |
| 173 | + } |
| 174 | +}); |
| 175 | + |
| 176 | +test('a known code keeps its own sentence in both languages', () => { |
| 177 | + for (const lang of ['zh', 'en']) { |
| 178 | + setLocale(lang); |
| 179 | + for (const code of KNOWN_CODES) { |
| 180 | + const key = `agentError.${code}`; |
| 181 | + assert.equal(describeAgentError({ code }), t(key)); |
| 182 | + } |
| 183 | + } |
| 184 | +}); |
| 185 | + |
| 186 | +test('a turn whose Agent never started shows the unavailable sentence and hides the cause', async (ctx) => { |
| 187 | + setLocale('en'); |
| 188 | + const message = await failedTurn(ctx, { code: 'AGENT_UNAVAILABLE', message: 'SessionServiceError: svc-abc123 bind failed' }); |
| 189 | + assert.equal(message.errorCode, 'AGENT_UNAVAILABLE'); |
| 190 | + assert.equal(message.error, t('agentError.AGENT_UNAVAILABLE')); |
| 191 | + assert.ok(!message.error.includes('svc-abc123'), 'the error keeps the cause'); |
| 192 | + assert.ok(!String(message.text ?? '').includes('svc-abc123'), 'the text keeps the cause'); |
| 193 | +}); |
| 194 | + |
| 195 | +test('a turn that failed for a reason the plugin cannot name shows the host sentence', async (ctx) => { |
| 196 | + setLocale('en'); |
| 197 | + const message = await failedTurn(ctx, { code: 'SessionServiceError' }); |
| 198 | + assert.equal(message.errorCode, 'SessionServiceError'); |
| 199 | + assert.equal(message.error, t(HOST_ERROR)); |
| 200 | + assert.ok(!message.error.includes('SessionServiceError'), 'the error keeps the code'); |
| 201 | +}); |
0 commit comments