test(llm): 補 OpenCode 逾時設定與失敗丟例外(不再 process.exit)的測試
CI / AI Code Review (pull_request) Has been cancelled

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeffery
2026-06-26 16:47:28 +08:00
co-authored by Claude Opus 4.8
parent 98da4d4143
commit 14ef273f1a
+23 -3
View File
@@ -85,14 +85,34 @@ describe('chat - OpenCode', async () => {
assert.equal(result, 'hello world');
});
it('calls process.exit(1) when OpenCode fails', async () => {
it('sets a request timeout on OpenCode calls so a stalled server fails fast', async () => {
process.env.OPENCODE_BASE_URL = 'http://opencode.local:4096';
const timeouts = [];
mock.method(axios, 'post', async (url, _payload, opts) => {
timeouts.push(opts.timeout);
if (url.endsWith('/session')) return { data: { id: 'ses_test' } };
return { data: { parts: [{ type: 'text', text: 'ok' }] } };
});
await chat('sys', 'user');
assert.equal(timeouts.length, 2);
for (const t of timeouts) {
assert.equal(typeof t, 'number');
assert.ok(t > 0, 'OpenCode 請求必須帶正數 timeout,避免無限等待');
}
});
it('throws (does not process.exit) when OpenCode fails, so callers can degrade gracefully', async () => {
process.env.OPENCODE_BASE_URL = 'http://opencode.local:4096';
mock.method(axios, 'post', async () => { throw new Error('fail'); });
const exitMock = mock.method(process, 'exit', () => { throw new Error('exit:1'); });
await assert.rejects(() => chat('sys', 'user'), /exit:1/);
// 失敗時向外拋出原始錯誤,而非 process.exit(1),讓 Step5 的 Promise.allSettled
// 與去重/過濾的 try/catch fallback 能各自處理,不會整個流程被砍掉。
await assert.rejects(() => chat('sys', 'user'), /fail/);
assert.equal(exitMock.mock.calls[0].arguments[0], 1);
assert.equal(exitMock.mock.calls.length, 0, 'chat 不應再呼叫 process.exit');
});
});