Skip to content

fix: validate project and test-code responses before use - #725

Merged
zeshi-du merged 9 commits into
mainfrom
takeover/pr-340
Nov 10, 2026
Merged

zeshi-du merged 9 commits into
mainfrom
takeover/pr-340

Conversation

@zeshi-du

@zeshi-du zeshi-du commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

This takes over #340 by @jerrybate-sketch (its branch doesn't allow maintainer edits). It carries the three original commits unchanged and adds one maintainer commit on top. project get/list and test code get / code put now check the response shape before using it.

Part of #277. Supersedes #340.

flowchart LR
  R["API response"] --> V{"matches schema?"}
  V -- "yes (extra fields kept)" --> U["render / write --out / send If-Match"]
  V -- "no" --> E["exit 1: Response shape mismatch + requestId"]
Loading

Before and after, against a local stub API whose GET /projects/:id returns {"id":"prj_demo","name":17,"type":"frontend"}:

$ testsprite project get prj_demo          # public main (v0.16.0)
id:          prj_demo
name:        17
type:        frontend
createdFrom: undefined
createdAt:   undefined
updatedAt:   undefined
exit=0

$ testsprite project get prj_demo          # this branch
Error: Response shape mismatch from /projects/prj_demo.
Retry; if it persists, report this requestId (the server returned an unexpected shape).
requestId: cli_920c5e4e-8849-40b2-b21f-6e5bbcb1cd7b
exit=1

Maintainer changes on top of the original commits:

  • The project schemas moved into src/lib/response-schemas.ts, using the shared open-string helper.
  • An omitted nextToken is treated as the last page.
  • defaultEnvironment, environmentCount and originMode are type-checked, because the text renderer reads them.
  • A null or empty code is caught before the presigned-URL check on both the dry-run and real paths, so in text mode --out leaves an existing file untouched.
  • test code get --output json is byte-identical for rows without a codeVersion.
  • CHANGELOG entry.

Tests: the targeted specs for project, test code, response schemas and the p4 contract pass locally, and tsc --noEmit is clean.

Summary by CodeRabbit

  • Bug Fixes
    • Project and test-code commands now reject malformed API responses with an error that includes the request ID and mismatched fields, before displaying invalid results.
    • Null or empty code is handled safely in text and dry-run output. Invalid or null code no longer overwrites an existing output file.
    • Project listings handle omitted pagination tokens. Additional response fields and new project, language, and framework values remain compatible.
    • Legacy JSON output for test code get remains unchanged.

jerrybate-sketch and others added 5 commits September 6, 2026 00:31
Move the project schemas into response-schemas.ts, give fetchSinglePage an
optional schema, validate renderer-consumed project fields and nextToken,
keep codeVersion as received, and check for a null or empty code before
isPresignedCodeUrl on both the dry-run and real code get paths.

Part of #277. Follow-up to #340.
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Project and test-code read responses now undergo schema validation. The changes also handle omitted pagination cursors and nullable or empty test code. Tests cover malformed responses, additional fields, and output behavior.

Changes

Project and test-code response validation

Layer / File(s) Summary
Response schemas and read types
src/lib/response-schemas.ts, src/lib/response-schemas.code.test.ts
Adds schemas for project and test-code responses. Tests cover omitted and explicit null fields, and reject incompatible response shapes.
Project read validation
src/lib/pagination.ts, src/commands/project.ts, src/commands/project.test.ts
Project list and get requests validate responses. Single-page pagination accepts an optional schema. Tests cover malformed project rows and cursors, omitted cursors, and additional response fields.
Test-code reads and code handling
src/commands/test.ts, src/commands/test.test.ts, test/contract/p4-schema.test.ts, CHANGELOG.md
Test-code reads and automatic code-version lookup validate responses. Null or empty code is handled before presigned-URL detection in text and dry-run paths. Tests cover malformed responses, output-file preservation, additional fields, and invalid code versions.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Suggested reviewers: davidson3556

Merge Risk: Minimal · previous review fefb9

Omitted project-list cursors are handled as the final page as intended. No actionable merge-blocking risk remains after normal checks.

Pre-merge checks: Passed 4 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Docstring Coverage Inconclusive Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly summarizes the main change: validating project and test-code responses before use.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 7 files. (2 skipped: 1 unsupported, 1 too large.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 609e402 and fefb92d.

Files selected for processing: 4

  • CHANGELOG.md
  • src/commands/project.test.ts
  • src/commands/project.ts
  • src/commands/test.ts
Files skipped from review as they are similar to previous changes: 1

  • CHANGELOG.md

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@coderabbitai coderabbitai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Fix comments: Fix CodeRabbit comments on this PR

Comment thread CHANGELOG.md Resolved
Comment thread src/lib/response-schemas.ts Resolved
@github-actions

Copy link
Copy Markdown

Test Coverage Report

Metric Coverage
Lines 92.76%
Statements 90.63%
Functions 84.34%
Branches 85.75%

@zeshi-du
zeshi-du merged commit 29f2979 into main Nov 10, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants