Skip to content

test(plugin): test matrices for one_commodity, check_closing, check_commodity - #1038

Merged
robcohen merged 5 commits into
mainfrom
test/plugin-tier2-validators
May 8, 2026
Merged

robcohen merged 5 commits into
mainfrom
test/plugin-tier2-validators

Conversation

@robcohen

@robcohen robcohen commented May 8, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes the remaining Tier 2 gaps identified in the #1002 plugin testing matrix audit. Each plugin had only 2 happy-path tests in native_plugins_test.rs and no inline tests, leaving most code branches uncovered.

This PR follows the established pattern of #1007, #1008, and #1012 — one bundled PR adding full test matrices for a small group of related plugins.

Plugins covered

  • one_commodity (51 LOC validator) — 5 new tests
    • Empty input, auto-balanced posting (units = None) skip, per-account independence, three-currency cascade asserting both error pairings (USD↔EUR, USD↔GBP), non-Transaction directive ignore.
  • check_closing (78 LOC mutator) — 8 new tests
    • Empty input, closing: false → no emission, non-Bool metadata value (String) → no emission, units = None falling back to default "USD" (with NOTE about the hardcoded fallback not respecting operating_currencies), multiple closing postings in one transaction → multiple balances, non-Transaction passthrough, unrelated metadata key (note) not triggering emission, malformed date (2024-13-01) → defensive increment_date() None skip.
  • check_commodity (75 LOC validator) — 9 new tests
    • Empty input, severity is Warning (not Error), input passthrough unchanged, undeclared cost currency (posting.cost.currency), cost.currency = None inner skip branch, undeclared currency in Balance directive, undeclared currency in Price directive (both price.currency and price.amount.currency), HashSet dedup of repeated undeclared usage, mixed declared/undeclared with only the undeclared ones warned about.

Why these three

Initial gap analysis only counted tests in tests/native_plugins_test.rs, which over-flagged plugins like valuation (6 inline) and currency_accounts (6 inline) as undertested. After counting inline + central tests, exactly these three remained at the 2-test, no-inline floor.

Review hardening

This PR was self-reviewed and a reviewer (Copilot) flagged that the original test_onecommodity_skips_auto_balanced_posting used make_transaction (which always sets units: Some(...)) and never actually traversed the units = None skip branch. Rewritten in 910e0b84 to construct an explicit transaction with a units = None posting and probe the skip via a follow-up currency mismatch.

A subsequent deep review tightened assertions and closed remaining gaps in 53cb0b57: the cascade test now asserts the error pairings not just the count, the cost.currency = None skip branch is exercised, and the increment_date() None branch is pinned.

Test plan

  • cargo test -p rustledger-plugin — all 165 tests pass
  • cargo clippy -p rustledger-plugin --all-features --all-targets -- -D warnings — clean
  • cargo fmt -p rustledger-plugin -- --check — clean
  • scripts/check-plugin-test-quality.sh — passes (no weak count assertions, no (partial) labels)

🤖 Generated with Claude Code

…ommodity

Closes the remaining Tier 2 gaps identified in the #1002 plugin testing
matrix audit. Each plugin had only 2 happy-path tests in the central test
file and no inline tests, leaving most code branches uncovered.

one_commodity: empty input, auto-balanced posting skip, per-account
independence, three-currency cascade, non-Transaction directive ignore.

check_closing: empty input, `closing: false`, non-Bool metadata value,
`units = None` defaulting to USD, multiple closing postings in one
transaction, non-Transaction directive passthrough, unrelated metadata
key not triggering emission.

check_commodity: empty input, warning severity (not error), input
passthrough unchanged, undeclared cost currency, undeclared currency in
Balance directive, undeclared currency in Price directive (both fields),
HashSet dedup of repeated undeclared usage, mixed declared/undeclared.

All 275 rustledger-plugin tests pass; clippy and fmt clean; plugin-test
quality lint passes.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 8, 2026 20:01
@robcohen
robcohen requested a review from a team as a code owner May 8, 2026 20:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 the integration test coverage in rustledger-plugin by adding matrix-style tests for three native plugins (one_commodity, check_closing, check_commodity) to close remaining coverage gaps identified in the plugin testing audit (#1002).

Changes:

  • Add additional OneCommodityPlugin tests covering empty input, per-account behavior, multi-currency cascades, and non-transaction directive handling.
  • Add CheckCommodityPlugin tests covering severity level, passthrough behavior, and detection of undeclared commodities across Transactions (units/cost), Balance, and Price directives (with deduplication).
  • Add CheckClosingPlugin tests covering empty input, closing metadata value/type gating, default currency behavior when units=None, multiple closing postings, and non-transaction passthrough.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented May 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 25.7ms ± 0.6ms 21.9 MB
beancount 586.3ms ± 2.7ms 50.4 MB
Comparison 22.8x faster 2.3x less

vs main branch (2026-05-08)

Metric main this PR Change
Time 31ms 25.7ms -17.1% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

github-actions Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 74%
Full AST Match 99%
Total Files 786
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

robcohen and others added 4 commits May 8, 2026 15:08
Address Copilot inline feedback on PR #1038. The original
`test_onecommodity_skips_auto_balanced_posting` used `make_transaction`,
which always sets `units: Some(...)`, so the test never actually traversed
the `if let Some(units) = &posting.units` skip path it claimed to cover.

Rewrite the test to construct an explicit transaction with one
`units = None` posting alongside an explicit-USD posting, then follow up
with a different currency on the same account in a later transaction.
With the skip working correctly, the later currency is the first
recorded for that account → no error. If the None posting had been
tracked (in any form), the follow-up would mismatch and produce an
error, so this design directly probes the branch.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…d bad date

Address findings from the deep PR review:

- one_commodity cascade test now asserts the *pairings* (USD↔EUR and
  USD↔GBP), not just the count. Previously a bug that recorded EUR or
  GBP as the "first" currency would still produce 2 errors with wrong
  pairings and pass the test.

- check_commodity now exercises the cost.currency=None inner skip
  branch — the plugin's `if let Some(ref currency) = cost.currency`
  guard must drop postings whose Cost has no currency, not insert a
  spurious empty-string into used_commodities.

- check_closing now pins the increment_date None branch via a "2024-13-01"
  date. The branch is unreachable in practice (parser validates dates
  upstream), but the source defensively guards against it.

- check_closing units=None test gains a NOTE explaining that the "USD"
  fallback is hardcoded — it does not consult `options.operating_currencies`,
  so users with a different operating currency get a USD balance assertion.
  The test pins current behavior; flagged for follow-up.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…hrough

Address remaining nitpicks from the second-pass review:

- one_commodity cascade test now matches the literal `"USD and EUR"` and
  `"USD and GBP"` substrings instead of order-insensitive contains checks.
  A bug that emitted `"EUR and USD"` (reversed order) or scrambled the
  format string would now fail.

- check_closing invalid-date test now also asserts that the original
  transaction passes through unchanged. Previously a regression that
  early-returned (dropping the input) would still pass the balance-count
  check. The doc comment is also corrected: the branch is unreachable
  from parser-loaded ledgers, but reachable from any caller constructing
  DirectiveWrapper programmatically.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen enabled auto-merge (squash) May 8, 2026 20:25
@robcohen
robcohen merged commit 0f7b2b4 into main May 8, 2026
30 of 31 checks passed
@robcohen
robcohen deleted the test/plugin-tier2-validators branch May 8, 2026 20:31
robcohen added a commit that referenced this pull request May 8, 2026
* feat(ci): per-plugin mutation survival floor (closes #1003)

Phase 3 of the plugin-testing-quality plan from #992. The aggregate
mutation score in `mutation.yml` was permissive: a few well-tested
plugins could mask many badly-tested ones. This PR adds per-plugin
floor enforcement (default ≤10% survival rate) and inline PR
annotations for surviving mutants.

What's added
- `scripts/per-plugin-mutation-report.sh`: parses `mutants.out/*.txt`,
  groups outcomes by source file, computes per-plugin survival rate
  (`missed / (caught + missed + timeout)`, excluding unviable), prints
  a table, and exits non-zero when any plugin exceeds MUTATION_FLOOR.
  Infrastructure files outside `src/native/plugins/` are reported but
  not enforced.
- `GITHUB_ANNOTATIONS=1` on the script emits `::warning file=…,line=…`
  for every surviving mutant, satisfying the "PR inline comments"
  acceptance criterion. GitHub Actions surfaces these on the diff.
- `scripts/test-per-plugin-mutation-report.sh`: 9-case self-test for
  the report script — covers empty, all-caught, plugin-over-floor,
  infrastructure-only-over-floor, MUTATION_FLOOR=0 disable, missing
  inputs (exit 2), GitHub annotations, utils/mod bucketing, and the
  exact-10% boundary. Wired into the existing `Plugin Test Quality`
  CI job.
- `.cargo/mutants.toml`: skips `<impl NativePlugin for …>::name` and
  `<impl NativePlugin for …>::description` mutations. These metadata
  methods return fixed strings used only for registry lookup; cargo-
  mutants tries `""` and `"xyzzy"` replacements that survive because
  no behavior depends on the literal text. Filtering at config level
  keeps the report honest about real coverage gaps.
- `.github/workflows/mutation.yml`: new `Per-plugin mutation floor
  check` step that runs the report script with `MUTATION_FLOOR=10`
  and `GITHUB_ANNOTATIONS=1`, gated on the plugin crate being in the
  package list.
- `CONTRIBUTING.md`: documents the floor, the local invocation, and
  the `#[mutants::skip]` annotation pattern with a required reason.

How to handle a missed mutant (when CI fails)
1. Tighten the relevant test to fail under the mutated code, OR
2. Annotate the function with `#[mutants::skip]` and a leading
   `// reason:` comment explaining why mutation testing isn't useful.

Local validation
The report script was driven through 9 synthetic scenarios via the
self-test (all pass). A partial real-data run on the 3 plugins from
\#1038 surfaced 4 surviving mutants on `description()` methods —
addressed by the `.cargo/mutants.toml` exclusion above. The CI run on
this PR validates the workflow change, but the per-plugin enforcement
won't actually fire until a future PR touches `crates/rustledger-
plugin/src/native/plugins/*.rs` (the `mutation.yml` trigger paths).

Closes #1003

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* test(ci): address PR #1041 review (truncation, doc, churn, guidance)

Five Copilot inline findings, all real and actionable:

1. Truncation bug in floor enforcement. `survival_pct = m * 100 / total`
   loses precision: 11/101 = 10.89% truncates to 10 and incorrectly
   passes the `-gt 10` check. Replace with cross-multiplication
   `m * 100 > floor * total`, which is exact integer math and strictly
   greater. Pinned by a new self-test case (case 10: 11 missed out of
   101 total → must fail).

2. Header doc mismatch. The "Usage" section called the bare invocation
   "report only" — actually wrong, since `MUTATION_FLOOR` defaults to
   10 and enforcement is active unless explicitly set to 0. Also
   referenced `::error::` annotations when the script emits
   `::warning::`. Both fixed.

3. Process churn in GitHub annotation emission. Each missed mutant
   spawned 4 sed subprocesses + an echo for parsing — 4000 fork/exec
   pairs on a 1000-line missed.txt. Replace with a single bash
   `[[ $line =~ $regex ]]` capture; in-process, deterministic, faster.

4. Annotation guidance conflict with CONTRIBUTING.md. The script's
   failure message recommended `#[cfg_attr(test, mutants::skip)]`
   but CONTRIBUTING.md documents `#[mutants::skip]` (the canonical
   form, since cargo-mutants reads the AST without compiling). Align
   the script with the docs.

5. Test-coverage gap on the truncation boundary. Case 9 covered
   exactly-10% but didn't cover just-over-10%, which is the case
   the truncation bug would silently pass. New case 10 fills that
   gap and would have caught the original bug.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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.

2 participants