Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
28 changes: 28 additions & 0 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
Loading