Skip to content

refactor: consolidate paging/print helpers + split buildTagPayload + close test gaps#23

Merged
dgilperez merged 1 commit into
mainfrom
refactor/consolidate-helpers-and-tests
May 23, 2026
Merged

refactor: consolidate paging/print helpers + split buildTagPayload + close test gaps#23
dgilperez merged 1 commit into
mainfrom
refactor/consolidate-helpers-and-tests

Conversation

@dgilperez
Copy link
Copy Markdown
Collaborator

Follow-up to #22. Now that every `print*` helper routes through the same `respond()` path, the per-resource families are byte-identical except for the prefix in the function name. This PR collapses them, splits the `buildTagPayload` flag-argument smell, and closes the orphan-command test gaps flagged in the post-merge review.

Helper consolidation (net −158 LOC)

Deleted Migrated callers to
`printInvestmentGet/Post/Patch/Delete/DryRun` `printGet` / `dispatchWrite`
`printFinancialGet/Post/Patch/DryRun` `printGet` / `dispatchWrite`
`printFamilyExportGet/DryRun` `printGet` / `dispatchWrite`
`printUsersGet/Delete/DryRun` `printGet` / `dispatchWrite`
`addImportPagingFlags/Query`, `importPathWithQuery` `addPagingFlags` / `addPagingQuery` / `pathWithQuery`
`addInvestmentPagingFlags/Query`, `investmentPathWithQuery` same
`addFinancialPagingFlags/Query`, `financialPathWithQuery` same

The only thing kept from `investment_helpers.go` is `addRepeatedQuery` (renamed from `addRepeatedInvestmentQuery` — it's generic, used by trades + holdings).

Pattern fix — `buildTagPayload`

Split `buildTagPayload(o, requireName bool)` (`reference_cmds.go:233`, flag-argument smell) into separate `buildTagCreatePayload` and `buildTagUpdatePayload`. Matches the chats / valuations / trades / categories pattern of distinct create/update builders.

Test infrastructure

  • New `testhelpers_test.go` with `findSub` + `captureStdout`. Both were defined in `transfers_cmds_test.go` and `dispatch_write_test.go` respectively but used across the whole package — centralizing kills the "where does `findSub` live?" confusion.
  • New `orphan_commands_test.go` with a registration smoke table for the 18 commands that previously had zero test coverage (whoami, sync, refresh, login, status, export, export transactions, transactions delete, all `insights_`, all `plan_`, propose, propose rules). Deleting any `AddCommand` line for these in `root.go` would have silently still compiled. Now it fails fast.
  • Replaced `TestBuildTagPayload` with four focused tests covering required-name on create, optional color, no-fields-on-update, and partial-update payloads.
  • Tightened `TestReferenceCommandsRegistered` to compare resolved `cmd.Name()` to the expected leaf (cobra's loose `Find` pattern that bit `TestBudgetsCommandsRegistered` / `TestTransfersCommandsRegistered` / `TestAuthRegistered` previously).

Coverage

`cmd/sure-cli/root`: 47.6% → 52.9% (+5.3pp). `go test ./... -race -cover` green across the suite.

Scope

14 files changed, +233/-391. Pure mechanical collapse — every migrated site already had behavioural coverage via #22's `respond()` tests or existing command-shape tests. No functional change intended; the dry-run envelope shape for `family-exports create` is explicitly preserved (no `body` key on the dry-run side; `{}` only on `--apply`).

Refs the post-merge code review.

…close test gaps

Follow-up to fix/api-error-envelope-and-safety. Now that every print*
helper goes through the same respond() path, the per-resource families
(investment_helpers, financial_cmds, family_exports, users, imports
paging) are byte-identical except for the prefix in the function name.
Collapse them.

## Helper consolidation

- Delete printInvestmentGet/Post/Patch/Delete/DryRun
  (investment_helpers.go:55-112). All callers in investments_cmds.go
  + trades_cmd.go + holdings_cmd.go migrate to printGet /
  dispatchWrite. The only thing kept from that file is addRepeatedQuery
  (renamed from addRepeatedInvestmentQuery — the helper is generic).
- Delete printFinancialGet/Post/Patch/DryRun (financial_cmds.go:260-310).
  Valuations create/update migrate to dispatchWrite; everything else
  goes through printGet.
- Delete printFamilyExportGet/DryRun (family_exports_cmd.go:90-111).
  list/show migrate to printGet; create migrates to dispatchWrite.
  Preserved the no-body dry-run shape upstream relies on.
- Delete printUsersGet/Delete/DryRun (users_cmd.go:57-86). reset /
  reset status / delete-me migrate to dispatchWrite + printGet.
- Delete addImportPagingFlags/Query, importPathWithQuery
  (imports_cmd.go:266-291) — same shape as the reference_cmds.go
  trio, no behavioural delta.
- Delete addInvestmentPagingFlags/Query, investmentPathWithQuery
  (investment_helpers.go:12-39).
- Delete addFinancialPagingFlags/Query, financialPathWithQuery
  (financial_cmds.go:231-258).

## Pattern fix — buildTagPayload flag argument

Split buildTagPayload(o, requireName bool) at reference_cmds.go:233
into separate buildTagCreatePayload (rejects empty Name) and
buildTagUpdatePayload (rejects "no fields provided to update").
Matches the chats / valuations / trades / categories pattern of
distinct create/update builders. Two 10-line funcs instead of one
12-line func with a magic boolean.

## Test helpers hoisted

- New testhelpers_test.go with findSub + captureStdout. Both were
  hand-rolled in transfers_cmds_test.go and dispatch_write_test.go
  respectively but used across the whole package. Centralizing them
  avoids the "where does findSub live?" confusion called out in the
  refactor review.

## Test gaps closed

- New orphan_commands_test.go with a registration smoke table for
  whoami, sync, refresh, login, status, export[transactions],
  transactions delete, insights[subscriptions/fees/leaks],
  plan[budget/runway/forecast], propose[rules]. Previously each of
  those commands had zero test coverage — deleting the AddCommand
  line in root.go would have silently still compiled. Now it fails
  fast.
- reference_cmds_test.go: replaced the single TestBuildTagPayload
  with four focused tests covering create-required-name,
  create-color-optional, update-needs-at-least-one-field,
  update-partial-ok. Also tightened TestReferenceCommandsRegistered
  to compare resolved cmd.Name() to the expected leaf (cobra's loose
  Find pattern that bit us before).

## Coverage

cmd/sure-cli/root: 47.6% → 52.9% (+5.3pp). go test ./... -race -cover
green across the suite.

## Diff scope

14 files changed, +233/-391 = net **−158 LOC**. Pure mechanical
collapse — every migrated site already had behavioural test coverage
via the previous PR's respond() tests or the existing command-shape
tests, and no functional change is intended.

Refs the post-merge code review.
@superagent-security superagent-security Bot added the contributor:verified Contributor passed trust analysis. label May 23, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 23, 2026

Warning

Review limit reached

@dgilperez, we couldn't start this review because you've used your available PR reviews for now.

Your plan currently allows 1 review/hour. Refill in 52 minutes and 1 second.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more review capacity refills, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than trial, open-source, and free plans. In all cases, review capacity refills continuously over time.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3da2a898-1152-4335-ba9d-4e8a2e114814

📥 Commits

Reviewing files that changed from the base of the PR and between f75c1e2 and 66e3bc4.

📒 Files selected for processing (14)
  • cmd/sure-cli/root/dispatch_write_test.go
  • cmd/sure-cli/root/family_exports_cmd.go
  • cmd/sure-cli/root/financial_cmds.go
  • cmd/sure-cli/root/holdings_cmd.go
  • cmd/sure-cli/root/imports_cmd.go
  • cmd/sure-cli/root/investment_helpers.go
  • cmd/sure-cli/root/investments_cmds.go
  • cmd/sure-cli/root/orphan_commands_test.go
  • cmd/sure-cli/root/reference_cmds.go
  • cmd/sure-cli/root/reference_cmds_test.go
  • cmd/sure-cli/root/testhelpers_test.go
  • cmd/sure-cli/root/trades_cmd.go
  • cmd/sure-cli/root/transfers_cmds_test.go
  • cmd/sure-cli/root/users_cmd.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/consolidate-helpers-and-tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@superagent-security superagent-security Bot added the pr:verified PR passed security analysis. label May 23, 2026
@dgilperez dgilperez merged commit 8eb3455 into main May 23, 2026
5 checks passed
@dgilperez dgilperez deleted the refactor/consolidate-helpers-and-tests branch May 23, 2026 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor:verified Contributor passed trust analysis. pr:verified PR passed security analysis.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant