| 1 | // Run: tsx src/__tests__/inbox-error-localization.test.ts |
| 2 | |
| 3 | import { formatInboxCancelError, formatInboxError } from "../lib/inboxError"; |
| 4 | |
| 5 | let passed = 0; |
| 6 | let failed = 0; |
| 7 | |
| 8 | function eq(actual: unknown, expected: unknown, label: string) { |
| 9 | if (actual === expected) { |
| 10 | process.stdout.write(` PASS ${label}\n`); |
| 11 | passed += 1; |
| 12 | } else { |
| 13 | process.stdout.write(` FAIL ${label}: got ${JSON.stringify(actual)}, want ${JSON.stringify(expected)}\n`); |
| 14 | failed += 1; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | console.log("\ninbox error localization"); |
| 19 | |
| 20 | const stableCases = [ |
| 21 | ["inbox_paused", "收件箱已暂停"], |
| 22 | ["inbox_capacity_items", "收件箱已达到条目上限"], |
| 23 | ["inbox_capacity_bytes", "收件箱已达到容量上限"], |
| 24 | ["inbox_item_too_large", "这条指令太大,无法加入收件箱"], |
| 25 | ["inbox_item_not_found", "这条收件箱指令已不存在"], |
| 26 | ["inbox_invalid_state", "当前状态下无法操作这条收件箱指令"], |
| 27 | ["inbox_schema_readonly", "收件箱由较新版本创建,当前只能读取"], |
| 28 | ["inbox_closed", "收件箱已关闭"], |
| 29 | ["inbox_empty", "不能加入空白指令"], |
| 30 | ["inbox_idempotency_conflict", "这条指令与已提交的请求冲突"], |
| 31 | ["channel_read_only", "当前会话为只读,无法修改收件箱"], |
| 32 | ["workspace_starting", "工作区还在启动,请稍后重试"], |
| 33 | ["workspace_start_failed", "工作区启动失败"], |
| 34 | ["image_attachment_unreadable", "图片读取失败,请重新添加或重试"], |
| 35 | ] as const; |
| 36 | |
| 37 | for (const [code, expected] of stableCases) { |
| 38 | eq(formatInboxError(new Error(`reasonix_error:${code}`), "zh"), expected, `zh maps ${code}`); |
| 39 | } |
| 40 | |
| 41 | eq(formatInboxError(new Error("inbox is paused"), "zh"), "收件箱已暂停", "legacy backend English is localized"); |
| 42 | eq(formatInboxError(new Error("workspace failed to start: internal detail"), "zh"), "工作区启动失败", "legacy startup detail is sanitized and localized"); |
| 43 | eq(formatInboxError("reasonix_error:inbox_paused", "zh-TW"), "收件匣已暫停", "traditional Chinese maps stable code"); |
| 44 | eq(formatInboxError(new Error("reasonix_error:inbox_paused"), "en"), "Inbox is paused", "English maps stable code"); |
| 45 | eq( |
| 46 | formatInboxCancelError(new Error("reasonix_error:inbox_invalid_state"), "zh"), |
| 47 | "取消失败:当前状态下无法操作这条收件箱指令", |
| 48 | "cancel failures localize both context and stable code", |
| 49 | ); |
| 50 | |
| 51 | const diagnostic = "filesystem detail: /private/example"; |
| 52 | eq(formatInboxError(new Error(diagnostic), "zh"), diagnostic, "unknown diagnostic details stay intact"); |
| 53 | eq(formatInboxCancelError(new Error(diagnostic), "zh"), `取消失败:${diagnostic}`, "localized cancel context preserves unknown diagnostics"); |
| 54 | |
| 55 | if (failed > 0) { |
| 56 | process.stderr.write(`\n${failed} failed, ${passed} passed\n`); |
| 57 | process.exit(1); |
| 58 | } |
| 59 | process.stdout.write(`\n${passed} passed\n`); |
| 60 |