Add comprehensive unit tests for http-helpers.ts#158
Add comprehensive unit tests for http-helpers.ts#158Dexploarer wants to merge 1 commit intodevelopfrom
Conversation
Adds a complete unit testing suite for the HTTP utility functions provided in `src/api/http-helpers.ts`. Ensures that stream behavior, JSON parsing, and edge cases (e.g. maximum body limits, invalid JSON, and various error flags) are properly validated without touching any production logic.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| process.nextTick(() => { | ||
| for (const chunk of dataChunks) { | ||
| req.emit("data", Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); | ||
| } | ||
| if (simulateError) { | ||
| req.emit("error", new Error("Simulated stream error")); | ||
| } else { | ||
| req.emit("end"); | ||
| } | ||
| }); |
There was a problem hiding this comment.
The use of process.nextTick to emit 'data', 'error', and 'end' events in createMockReq may lead to race conditions if listeners are not attached synchronously before the next tick. This could result in flaky tests if the event loop timing changes or if listeners are attached asynchronously.
Recommended solution:
Consider refactoring the helper to allow explicit control over when events are emitted, or ensure that all listeners are attached before emitting any events. For example, you could return an object with explicit emitData() and emitEnd() methods, or use a promise to synchronize event emission with listener attachment.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a comprehensive suite of unit tests for the core HTTP helper functions located in Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds a comprehensive suite of unit tests for the http-helpers.ts module. The tests are well-structured and cover a wide range of scenarios, including stream behavior, payload limits, error handling, and various flag combinations. I've identified a couple of opportunities to enhance test coverage for specific error and edge cases. Overall, this is a great addition that significantly improves the robustness of the HTTP helper functions.
|
|
||
| expect(() => writeJsonResponseSafe(res, { a: 1 })).not.toThrow(); | ||
| }); | ||
|
|
There was a problem hiding this comment.
While writeJsonResponseSafe has a test to ensure it doesn't throw on response write errors, the equivalent function writeJsonErrorSafe is missing a similar test. Adding this test would improve coverage for error handling paths.
| it("writeJsonErrorSafe should not throw on response write errors", async () => { | |
| const { res } = createMockRes(); | |
| res.end = vi.fn(() => { throw new Error("Connection closed"); }); | |
| expect(() => writeJsonErrorSafe(res, "Oops")).not.toThrow(); | |
| }); | |
| it("should reject non-object JSON if requireObject is true", async () => { | ||
| const req = createMockReq(['[1, 2, 3]']); | ||
| const { res, getJsonOutput } = createMockRes(); | ||
|
|
||
| const parsed = await readJsonBody(req, res); | ||
| expect(parsed).toBeNull(); | ||
| expect(res.statusCode).toBe(400); | ||
| expect(getJsonOutput()).toBe(JSON.stringify({ error: "Request body must be a JSON object" })); | ||
| }); |
There was a problem hiding this comment.
This test only covers arrays. To make it more comprehensive, you could parameterize it to also test for other non-object JSON types like null, which is a valid JSON value. Using it.each from vitest would be a clean way to cover multiple cases.
it.each([
['array', '[1, 2, 3]'],
['null', 'null'],
])('should reject non-object JSON (%s) if requireObject is true', async (_type, json) => {
const req = createMockReq([json]);
const { res, getJsonOutput } = createMockRes();
const parsed = await readJsonBody(req, res);
expect(parsed).toBeNull();
expect(res.statusCode).toBe(400);
expect(getJsonOutput()).toBe(JSON.stringify({ error: "Request body must be a JSON object" }));
});
Implements unit tests for the core HTTP helpers module (
src/api/http-helpers.ts). Checks stream behavior, request payload limits, flag behavior likereturnNullOnTooLarge, error handling, and JSON serialization defaults. Matches the existing test paradigms and ensures coverage without modifying production code.PR created automatically by Jules for task 3796730622032416101 started by @Dexploarer