Skip to content

test(discussion): add httpmock unit tests for DiscussionClient#13252

Merged
babakks merged 6 commits into
cli:feature/discussionfrom
maxbeizer:tests/discussion-client-httpmock
Apr 24, 2026
Merged

test(discussion): add httpmock unit tests for DiscussionClient#13252
babakks merged 6 commits into
cli:feature/discussionfrom
maxbeizer:tests/discussion-client-httpmock

Conversation

@maxbeizer

Copy link
Copy Markdown
Contributor

Summary

Adds comprehensive httpmock-based unit tests for pkg/cmd/discussion/client/ as requested in PR #13084 review.

Tests added

List

  • Success path with full Discussion round-trip (author actor mapping, category, labels, dates)
  • Discussions disabled → error
  • Limit ≤ 0 → error
  • Invalid orderBy / direction / state → errors
  • Pagination: fetches multiple pages until limit is reached; sets NextCursor when more pages remain; does not set NextCursor when fully fetched
  • Filter variants: state open/closed, label, category ID, orderBy/direction combos

Search

  • Success path with flat inline-fragment JSON (shurcooL-graphql decoder convention)
  • Limit ≤ 0 → error
  • Invalid orderBy / direction / state → errors
  • Pagination across two pages
  • Filter variants

ListCategories

  • Success path
  • Discussions disabled → error

Technical note

The shurcooL-graphql decoder expects inline-fragment fields (graphql:"... on Type") to be flattened in the JSON response at the same level as sibling fields — not nested under a "... on Type" key. Tests use this correct format.

Add comprehensive httpmock-based unit tests for the client package covering:
- List: success path, discussions disabled, limit/filter validation, pagination
- Search: success path, filter validation, pagination
- ListCategories: success path, discussions disabled

Tests use httpmock.Registry with defer Verify(t) to ensure all stubs
are exercised, following the established testing pattern in this repo.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@maxbeizer maxbeizer requested a review from a team as a code owner April 21, 2026 14:13
@maxbeizer maxbeizer requested review from williammartin and removed request for a team April 21, 2026 14:13
@github-actions github-actions Bot added external pull request originating outside of the CLI core team needs-triage needs to be reviewed unmet-requirements and removed needs-triage needs to be reviewed labels Apr 21, 2026
@github-actions

Copy link
Copy Markdown

Thanks for your pull request! Unfortunately, it doesn't meet the minimum requirements for review:

  • None of the referenced issues have the help wanted label

Please update your PR to address the above. Requirements:

  1. Include a detailed description of what this PR does
  2. Link to an issue with the help wanted label (use Fixes #123 or Closes #123 if it resolves the issue)

This PR will be automatically closed in 7 days if these requirements are not met.

@maxbeizer maxbeizer marked this pull request as draft April 21, 2026 14:14

@babakks babakks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I know it's still in draft, but I did a quick review.

Comment thread pkg/cmd/discussion/client/client_impl_test.go Outdated
Comment thread pkg/cmd/discussion/client/client_impl_test.go Outdated
Comment thread pkg/cmd/discussion/client/client_impl_test.go Outdated
Comment thread pkg/cmd/discussion/client/client_impl_test.go Outdated
Comment thread pkg/cmd/discussion/client/client_impl_test.go Outdated
Comment thread pkg/cmd/discussion/client/client_impl_test.go Outdated
Comment thread pkg/cmd/discussion/client/client_impl_test.go Outdated
Addresses babakks' review on PR cli#13252:
- Convert 18 individual test functions to 3 table-driven functions
- Replace ptr helper with new(value) syntax throughout
- Assert all Discussion struct fields in success cases
- Add after-cursor test cases for pagination
- Fold pagination tests into table structure

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@maxbeizer maxbeizer marked this pull request as ready for review April 22, 2026 20:05

@maxbeizer maxbeizer left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the thorough review @babakks! Addressed all 7 points in the latest commit:

  • Converted all 18 individual test functions to 3 table-driven test functions (TestList, TestSearch, TestListCategories)
  • Removed the ptr generic helper; replaced all uses with new(value) syntax
  • Added full-field assertions for all Discussion struct fields in success cases
  • Added after-cursor test cases to cover pagination continuation
  • Folded pagination tests into the table structure

All tests pass.

maxbeizer and others added 4 commits April 22, 2026 16:36
…ases

- Replace false-positive filter tests with GraphQLQuery responders that
  assert on actual GQL variables (first, after, states, answered, orderBy,
  categoryId in List; query string qualifiers in Search)
- Add Bot actor test case (Bot.ID maps to DiscussionActor.ID, Name is empty)
- Add limit>100 test cases for both List and Search to verify the
  per-iteration first variable is set correctly (100 on page 1, remainder
  on page 2)
- Fix limit>100 bug in client_impl.go: move variables["first"] assignment
  inside the loop so each iteration caps at min(remaining, 100)
- Remove intStr helper; use strconv.Itoa directly

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace responses/checkVarsFns with httpStubs func per test case in
TestList, TestSearch, and TestListCategories, matching the pattern used
in agent-task/capi tests. Expand inline JSON to heredoc, remove flower
box comments, and add "empty list" and "exact fit" test cases.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The fetch loop already assigns "first" on each iteration, so the
initial assignment in the variables map is dead code. Remove it from
both List and Search.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@babakks

babakks commented Apr 24, 2026

Copy link
Copy Markdown
Member

Thanks for applying the changes, @maxbeizer! 🙏

I made some changes mostly refactoring the tests. The main is converting all three test functions (TestList, TestSearch, TestListCategories) to use an httpStubs func(*testing.T, *httpmock.Registry) callback per test case, instead of the responses/checkVarsFns slices + a generic registration loop. This matches the pattern we use in agent-task/capi/sessions_test.go and makes each test case fully self-contained in how it sets up its HTTP mocks.

I'll merge this once CI finished.

@babakks

babakks commented Apr 24, 2026

Copy link
Copy Markdown
Member

Merging as the CI fail is already fixed in trunk.

@babakks babakks merged commit d7276c7 into cli:feature/discussion Apr 24, 2026
7 of 8 checks passed
@maxbeizer maxbeizer deleted the tests/discussion-client-httpmock branch May 22, 2026 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team unmet-requirements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants