-
Notifications
You must be signed in to change notification settings - Fork 73
feat: add jsonrpc prt tests #733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next/2.0
Are you sure you want to change the base?
Conversation
5b26aa7 to
875aa23
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR expands JSON-RPC test coverage for tournament-related (“prt”) methods and adjusts repository/query behavior to make list endpoints deterministic and align empty-list responses with existing “Application not found” semantics.
Changes:
- Add extensive JSON-RPC tests for tournament/commitment/match/match-advanced endpoints.
- Make list endpoints return
RESOURCE_NOT_FOUND: Application not foundwhen the result is empty and the application does not exist. - Stabilize ordering for pagination in Postgres queries (commitments and match advances).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/repository/postgres/match_advanced.go | Changes ordering for ListMatchAdvances to sort by OtherParent (stabilizes pagination within a fixed epoch/tournament/id_hash). |
| internal/repository/postgres/commitment.go | Adds secondary ORDER BY on Commitment to make pagination deterministic when many rows share the same EpochIndex. |
| internal/jsonrpc/jsonrpc.go | Adds “application exists” checks for multiple list handlers; tweaks GetMatchAdvanced parent handling. |
| internal/jsonrpc/util_test.go | Adds helper result structs for JSON unmarshalling in tests. |
| internal/jsonrpc/jsonrpc_test.go | Adds large suite of tests for new/expanded tournament data methods. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
5442605 to
e0b5bff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if len(tournaments) == 0 && !s.applicationExists(w, r, req, params.Application) { | ||
| writeRPCError(w, req.ID, JSONRPC_RESOURCE_NOT_FOUND, "Application not found", nil) | ||
| return | ||
| } |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applicationExists writes an INTERNAL_ERROR response on repository failure and returns false. In this handler, that false result then triggers a second write of a RESOURCE_NOT_FOUND ("Application not found"), which can lead to double-writes and masking real DB errors. Consider changing applicationExists to return (bool, error) (or to never write to w) so the caller can return immediately on error and only emit "Application not found" when the check succeeds and the app truly doesn't exist.
| if len(commitments) == 0 && !s.applicationExists(w, r, req, params.Application) { | ||
| writeRPCError(w, req.ID, JSONRPC_RESOURCE_NOT_FOUND, "Application not found", nil) | ||
| return | ||
| } |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applicationExists can write an INTERNAL_ERROR response and return false; this handler then unconditionally writes "Application not found" when it sees false, which risks double responses and incorrect error codes on transient repository failures. Refactor the existence check to return an error (or avoid writing to the ResponseWriter) and have the caller return immediately when that error occurs.
| writeRPCError(w, req.ID, JSONRPC_INTERNAL_ERROR, "Internal server error", nil) | ||
| return | ||
| } | ||
| if len(matches) == 0 && !s.applicationExists(w, r, req, params.Application) { |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new applicationExists check can emit two responses if GetApplication fails: applicationExists writes INTERNAL_ERROR and returns false, then this branch writes RESOURCE_NOT_FOUND. Please change the helper to return an error (and not write), and handle that error here with an early return.
| if len(matches) == 0 && !s.applicationExists(w, r, req, params.Application) { | |
| if len(matches) == 0 { | |
| if !s.applicationExists(w, r, req, params.Application) { | |
| // applicationExists may have already written an error response; avoid double writes | |
| return | |
| } |
| if len(matchAdvances) == 0 && !s.applicationExists(w, r, req, params.Application) { | ||
| writeRPCError(w, req.ID, JSONRPC_RESOURCE_NOT_FOUND, "Application not found", nil) | ||
| return |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If applicationExists hits a repository error, it currently writes an INTERNAL_ERROR and returns false; this code then also writes "Application not found", resulting in double-writes and hiding the underlying failure. Adjust the helper to return (exists bool, err error) (or never write to w) and handle errors explicitly before emitting RESOURCE_NOT_FOUND.
| if len(matchAdvances) == 0 && !s.applicationExists(w, r, req, params.Application) { | |
| writeRPCError(w, req.ID, JSONRPC_RESOURCE_NOT_FOUND, "Application not found", nil) | |
| return | |
| if len(matchAdvances) == 0 { | |
| exists, err := s.applicationExists(r.Context(), params.Application) | |
| if err != nil { | |
| s.Logger.Error("Unable to check application existence", "err", err) | |
| writeRPCError(w, req.ID, JSONRPC_INTERNAL_ERROR, "Internal server error", nil) | |
| return | |
| } | |
| if !exists { | |
| writeRPCError(w, req.ID, JSONRPC_RESOURCE_NOT_FOUND, "Application not found", nil) | |
| return | |
| } |
| body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ | ||
| "jsonrpc": "2.0", | ||
| "method": "cartesi_getCommitment", | ||
| "params": { | ||
| "application": "%v", | ||
| "epoch_index": "%v", | ||
| "tournament_address": "0x%020x" | ||
| }, | ||
| "id": 0 | ||
| }`, numberToName(app), hexutil.EncodeUint64(nr+1), 0)) |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This "absent" test case for cartesi_getCommitment omits the required commitment parameter. Because the handler's validation currently treats an empty string as valid hex (hex.DecodeString("") succeeds), this test may pass for the wrong reason and doesn't cover the intended contract. Include an explicit commitment value for the not-found scenario, and add a separate test that verifies missing commitment returns JSONRPC_INVALID_PARAMS (if that's the desired API behavior).
e0b5bff to
a13fa56
Compare
a13fa56 to
290a589
Compare
closes #682