From fffcb7ee19fc8023aea33dc46ab4396315e5d3c6 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 19 Mar 2026 08:54:22 +0000 Subject: [PATCH] test: add generic error handler test (500) Co-authored-by: shenald-dev <245350826+shenald-dev@users.noreply.github.com> --- .jules/bolt.md | 8 ++++++++ tests/api.test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/.jules/bolt.md b/.jules/bolt.md index b1ea2de..f3d10b6 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -20,3 +20,11 @@ The `heavyComputation` function was being called repeatedly with identical argum Action: Implemented a `Map`-based cache for `heavyComputation` in `src/index.js`. Subsequent calls with the same `iterations` parameter now return the cached result in $O(1)$ time (~0.01ms), improving performance by several orders of magnitude for repeated inputs. Added unit tests in `tests/heavy_computation.test.js` to verify correctness and timing improvements. + +## 2026-03-20 — Tested Generic Error Handler + +Learning: +The generic error handler in the API was returning a 500 status code but was not covered by any automated tests, leaving a gap in coverage for unexpected server-side failures. + +Action: +Added a test case in `tests/api.test.js` that intentionally mocks a failure in an internal dependency (`crypto.randomUUID`) to trigger the generic error handler. The test asserts that the handler returns a 500 status code and a sanitized JSON response (`{ error: 'Internal server error' }`) without leaking stack traces. Additionally, `console.error` was temporarily suppressed during the test to keep test output clean. \ No newline at end of file diff --git a/tests/api.test.js b/tests/api.test.js index eadeade..e184779 100644 --- a/tests/api.test.js +++ b/tests/api.test.js @@ -45,3 +45,31 @@ test('POST /v1/chat/completions fails with invalid JSON gracefully', async () => assert.strictEqual(res.status, 400); assert.strictEqual(res.body.error, 'Invalid JSON payload'); }); + +test('Generic error handler returns 500 without leaking stack traces', async () => { + const crypto = require('crypto'); + const originalRandomUUID = crypto.randomUUID; + const originalConsoleError = console.error; + + // Mock internal failure + crypto.randomUUID = () => { throw new Error('Mock internal failure'); }; + + // Suppress expected console.error from the error handler + console.error = () => {}; + + try { + const res = await request(app) + .post('/v1/chat/completions') + .send({ + model: 'gpt-4', + messages: [{ role: 'user', content: 'Hello!' }] + }); + + assert.strictEqual(res.status, 500); + assert.deepStrictEqual(res.body, { error: 'Internal server error' }); + } finally { + // Restore originals + crypto.randomUUID = originalRandomUUID; + console.error = originalConsoleError; + } +});