From 9e70bb2245dc68390464969686e4f325e3f4e20e Mon Sep 17 00:00:00 2001 From: Jeffery Date: Fri, 3 Jul 2026 09:12:17 +0800 Subject: [PATCH] =?UTF-8?q?test(llm):=20=E8=A3=9C=20extractMeaningfulError?= =?UTF-8?q?=20=E6=B8=AC=E8=A9=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/llm.test.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/test/llm.test.js b/src/test/llm.test.js index 0864089..efcf0bf 100644 --- a/src/test/llm.test.js +++ b/src/test/llm.test.js @@ -3,7 +3,7 @@ import assert from 'node:assert/strict'; import { mkdtemp, writeFile, chmod, rm, readFile } from 'fs/promises'; import { tmpdir } from 'os'; import { join } from 'path'; -import { extractBalancedJSON, extractJSONText } from '../llm.js'; +import { extractBalancedJSON, extractJSONText, extractMeaningfulError } from '../llm.js'; const ENV_KEYS = [ 'AI_ASSISTANT_CLI', 'MODEL', 'OPENCODE_MODEL', 'PATH', 'AI_ASSISTANT_TIMEOUT_MS', 'AI_ASSISTANT_MAX_BUFFER', @@ -237,3 +237,39 @@ describe('extractJSONText', () => { assert.equal(result, 'not json at all'); }); }); + +describe('extractMeaningfulError', () => { + it('抽出尾端真正的錯誤,而非開頭的 codex banner/回顯 prompt', () => { + const raw = [ + 'OpenAI Codex v0.142.5', + '--------', + 'workdir: /workspace/actions/ai-code-review', + 'model: gpt-5.4-mini', + 'reasoning effort: none', + '--------', + 'user', + '請依照以下系統指示處理使用者內容,並只輸出要求的最終結果。', + 'ERROR codex_api::endpoint::responses_websocket: failed to connect to websocket: HTTP error: 401 Unauthorized', + 'ERROR: Your access token could not be refreshed because your refresh token was revoked. Please log out and sign in again.', + ].join('\n'); + + const result = extractMeaningfulError(raw); + + assert.match(result, /401 Unauthorized/); + assert.match(result, /refresh token was revoked/); + assert.doesNotMatch(result, /workdir:/); + assert.doesNotMatch(result, /請依照以下系統指示/); + }); + + it('抽不到錯誤行時退取尾段(不取開頭)', () => { + const raw = 'A'.repeat(1200) + '\nTAIL-CONTENT'; + const result = extractMeaningfulError(raw, 100); + assert.ok(result.length <= 100); + assert.match(result, /TAIL-CONTENT$/); + }); + + it('容錯處理空輸入', () => { + assert.equal(extractMeaningfulError(''), ''); + assert.equal(extractMeaningfulError(null), ''); + }); +});