test: add remote-signing-service coverage#279
Conversation
|
👋 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 ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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 |
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 significantly enhances the robustness of the 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. 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. Footnotes
|
| it("should expire pending approvals", async () => { | ||
| const request: SigningRequest = { | ||
| requestId: "req-5", | ||
| chainId: 1, | ||
| to: "0x123", | ||
| value: "20000000000000000", | ||
| data: "0x", | ||
| createdAt: Date.now(), | ||
| }; | ||
|
|
||
| await service.submitSigningRequest(request); | ||
|
|
||
| // Simulate time passing beyond timeout | ||
| vi.spyOn(Date, "now").mockReturnValue(Date.now() + 2000); | ||
|
|
||
| const result = await service.approveRequest("req-5"); | ||
| expect(result.success).toBe(false); | ||
| expect(result.error).toBe("Approval expired"); | ||
| expect(service.getPendingApprovals()).toHaveLength(0); | ||
|
|
||
| vi.restoreAllMocks(); | ||
| }); |
There was a problem hiding this comment.
The test for expiring pending approvals manipulates Date.now() after the createdAt field is set using the real time. This can lead to non-deterministic test results if there is a delay between these calls. To ensure deterministic behavior, mock Date.now() before constructing the request and setting createdAt, so both the request and the approval logic use the same reference time.
Recommended solution:
vi.spyOn(Date, "now").mockReturnValue(1000000);
const request: SigningRequest = { ... createdAt: Date.now(), ... };
// ...
vi.spyOn(Date, "now").mockReturnValue(1000000 + 2000);This ensures both the request and the approval use the mocked time, making the test reliable.
| it("should update policy", () => { | ||
| const newPolicy = service.getPolicy(); | ||
| newPolicy.requireHumanConfirmation = true; | ||
|
|
||
| service.updatePolicy(newPolicy); | ||
|
|
||
| const updatedPolicy = service.getPolicy(); | ||
| expect(updatedPolicy.requireHumanConfirmation).toBe(true); | ||
| expect(mockAuditLog.record).toHaveBeenCalledWith( | ||
| expect.objectContaining({ type: "policy_decision" }) | ||
| ); | ||
| }); |
There was a problem hiding this comment.
The test for updating the policy mutates the object returned by service.getPolicy() directly. If getPolicy() returns a reference to the internal policy object, this could cause unintended side effects, especially if other tests or code rely on the original policy. To avoid this, always clone the policy object before mutating it in tests.
Recommended solution:
const newPolicy = { ...service.getPolicy() };
newPolicy.requireHumanConfirmation = true;
service.updatePolicy(newPolicy);This ensures test isolation and prevents accidental mutation of shared state.
There was a problem hiding this comment.
Code Review
This pull request adds much-needed test coverage for the RemoteSigningService, which is critical for security and reliability. The tests cover a good range of scenarios, including successful signing, policy enforcement, human approval flows, and error handling. I've identified a couple of areas for improvement in the tests to make them more robust and complete. Specifically, I've suggested using fake timers for time-dependent tests and adding a test case for the cleanup logic in getPendingApprovals. Overall, this is a valuable addition.
| it("should expire pending approvals", async () => { | ||
| const request: SigningRequest = { | ||
| requestId: "req-5", | ||
| chainId: 1, | ||
| to: "0x123", | ||
| value: "20000000000000000", | ||
| data: "0x", | ||
| createdAt: Date.now(), | ||
| }; | ||
|
|
||
| await service.submitSigningRequest(request); | ||
|
|
||
| // Simulate time passing beyond timeout | ||
| vi.spyOn(Date, "now").mockReturnValue(Date.now() + 2000); | ||
|
|
||
| const result = await service.approveRequest("req-5"); | ||
| expect(result.success).toBe(false); | ||
| expect(result.error).toBe("Approval expired"); | ||
| expect(service.getPendingApprovals()).toHaveLength(0); | ||
|
|
||
| vi.restoreAllMocks(); | ||
| }); |
There was a problem hiding this comment.
While this test works, using vi.spyOn(Date, 'now') for time-based tests can be fragile and less readable. The recommended approach in vitest is to use fake timers. This provides more deterministic control over time and makes the test's intent clearer. Consider refactoring this test to use vi.useFakeTimers() and vi.advanceTimersByTime().
it("should expire pending approvals", async () => {
vi.useFakeTimers();
const request: SigningRequest = {
requestId: "req-5",
chainId: 1,
to: "0x123",
value: "20000000000000000",
data: "0x",
createdAt: Date.now(),
};
await service.submitSigningRequest(request);
// Simulate time passing beyond timeout
vi.advanceTimersByTime(2000);
const result = await service.approveRequest("req-5");
expect(result.success).toBe(false);
expect(result.error).toBe("Approval expired");
expect(service.getPendingApprovals()).toHaveLength(0);
vi.useRealTimers();
});| expect.objectContaining({ type: "policy_decision" }) | ||
| ); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
The test coverage for RemoteSigningService is great! One small improvement would be to add a test case specifically for the cleanup logic within getPendingApprovals(). The current expiration test (should expire pending approvals) covers the expiration check inside approveRequest, but not the lazy cleanup that happens when getPendingApprovals() is called. Adding a test for this would ensure all paths of the expiration logic are covered.
What:
Added missing test file
src/services/remote-signing-service.test.tsto coverRemoteSigningService.Why:
The service handles critical security operations like transaction signing, audit logging, and human approval flow. Having missing tests here is a security and reliability risk.
Verification:
Ran
bun test src/services/remote-signing-service.test.tsand all 10 tests passed successfully.Result:
Increased confidence in
RemoteSigningServicebehavior without altering any production code.PR created automatically by Jules for task 6223555680832473089 started by @Dexploarer