From 67350cc25bd7fd2eb5467402fbc16a090d3995d6 Mon Sep 17 00:00:00 2001 From: shum Date: Wed, 26 Aug 2026 14:34:10 +0000 Subject: [PATCH] tests, plan: cover every unstorable badge entry type --- .../2026-08-21-badges-web-checkout.md | 6 +- tests/Bots/BadgeServiceTests.hs | 82 ++++++++++++++----- tests/ChatTests/Utils.hs | 13 ++- 3 files changed, 79 insertions(+), 22 deletions(-) diff --git a/plans/badges-codes/2026-08-21-badges-web-checkout.md b/plans/badges-codes/2026-08-21-badges-web-checkout.md index baa5191b25..e2b0a53d90 100644 --- a/plans/badges-codes/2026-08-21-badges-web-checkout.md +++ b/plans/badges-codes/2026-08-21-badges-web-checkout.md @@ -1219,7 +1219,7 @@ The redemption path runs over service RPC and has no IP; B5's per-signer throttl - No expiry sweeper. Expiry is evaluated at redemption time by B3's classifier against `expires_at`, so there is no expired state at rest to maintain. - `codes status --order ` and `codes status --ref `. Both print the order status, badge type, months and redemption state. With `--reveal` they additionally derive and print the plaintext code and the `?order=` resume URL, which is the support path E6 promises. `--reveal` requires an operator reason, is refused for a redeemed or revoked code, and its use is logged with the reason and the `shortRef`, never the code or the `orderId` (H4). - Authorisation is possession of the database file and `[codes] secret_file`; there is no in-band authentication and no HTTP surface (decision 3). The operator runs it on the service host as the service user. -- `codes unredeem --order | --ref | --code ` calls B1's `unredeemCode`, so the user can retry the same code and E4 discloses it again for a further `codeDisclosureDays`. A batch code has no order and no `shortRef`, so `--code` is its only selector; the operator holds the plaintext from `codes issue`. This covers C4's lost-response case. Reissuing a *different* code **for the same order** is impossible by construction: the code is a pure function of `orderId` (decision 9), so unredeeming is the only coherent recovery for that order. Compensation outside an order uses a batch code from `codes issue` (B8, H3). +- `codes unredeem --order | --ref | --code ` calls B1's `unredeemCode`, so the user can retry the same code and E4 discloses it again for a further `codeDisclosureDays`. A batch code has no order and no `shortRef`, so `--code` is its only selector; the operator holds the plaintext from `codes issue`. This covers C4's lost-response case. **Re-point C5's `codes unredeem` example at this command when it lands**: `testC5SameCodeAgainThenUnredeemed` (`tests/Bots/BadgeServiceTests.hs`) calls `unredeemCode` directly today, because this command did not exist when C5 was written (§9). Reissuing a *different* code **for the same order** is impossible by construction: the code is a pure function of `orderId` (decision 9), so unredeeming is the only coherent recovery for that order. Compensation outside an order uses a batch code from `codes issue` (B8, H3). - Order of operations for a lost response, which is the case support meets: `codes status --ref` confirms the redemption, `codes unredeem --ref` clears it, then `codes status --ref --reveal` prints the code, which `--reveal` now permits because the code is unredeemed again. Support holds only the `shortRef` (E6), so both support subcommands, `status` and `unredeem`, accept `--ref`. - Every admin action logged with a timestamp and the operator-supplied reason. @@ -1443,6 +1443,10 @@ Append here when a step contradicts this plan: the step id, what was wrong, and - **C5 — five production mutations were run to prove the load-bearing assertions can fail.** Appending a suffix to `entry_uuid` in `insertLedgerEntries` fails the ledger parity comparison; deleting `verifyUserBadge` from `storeRedeemedBadge`, and deleting the pre-transaction `checkStatementEntries`, each turn their "writes nothing" example into a redemption that succeeds or raises the wrong error; rotating the master key in `issueDueBadgePeriod`'s request fails the master-key stability assertion with the two keys printed; and reverting the `activeUserId == Just userId` guard to an unconditional `chatWriteVar` fails the profile-switch example with `("active profile", 1)` where 2 was expected. A sixth, writing the `payments` row before the request is sent, fails the row counts of all three "writes nothing" examples with their error messages unchanged — which is what proves the counts themselves are load-bearing and not decoration on a message assertion. All reverted. - **C5 — what remains untested.** The lock discipline (C3's and C4's own gap: the read-send-write scope and the release before `presentUserBadgeToContacts` still hold by source shape alone); `CTName`/`CTDomain` address forms, above; and the Postgres cross-check, which was not run for this step — it adds no SQL of its own, every write goes through C1's and B1's store functions, and the two `#if !defined(dbPostgres)` guards in the module are B10's, untouched. +- **C5 fix round — the unstorable-statement case covers all four entry types and asserts the whole message.** It stubbed `credit(charge)` only and stopped its assertion before `tshow e`, so a build that refused the WRONG entry type would still have passed. `unstorableEntryCases` now drives `credit(charge)`, `credit(transferIn)`, `debit(upgrade)` and `debit(transferOut)` — every type `storedEntryType` refuses — and each is compared as a complete line including the reason, spelled out literally rather than derived from the function under test. Verified live: swapping one expected reason for another's fails the example naming both. +- **C5 fix round — `drainEcho` eats blank lines, and now says so.** `""` is a prefix of every command, so up to 8 blank rows arriving before an echo are consumed silently, and a blank line a later `<## ""` expected would go missing there instead of failing here. Left as it is deliberately, and documented: tightening it to "the fragment must have added something" would reject a bare `"> "` row, which is one of the two shapes the artifact this tolerance exists for might have — and C5 could not reproduce it to find out which. A blank line before a command's own echo has never been observed. +- **C5 fix round — three comments corrected, one plan cross-reference added.** The happy-path example claimed that verifying the stored credential proved the client had checked it before storing; it does not (the real service only signs valid credentials), and the comment now points at `testC5UnverifiableCredentialWritesNothing` and its mutation, which is the actual proof. The catalog example now names the six-line comparison as what catches `catalogRequest` starting to sign. And H2's **Do** now carries the instruction to re-point `testC5SameCodeAgainThenUnredeemed` at `codes unredeem` when that command lands, instead of that depending on H2's author reading §9. + ## 10. End-to-end verification After F5: diff --git a/tests/Bots/BadgeServiceTests.hs b/tests/Bots/BadgeServiceTests.hs index 538fca4985..17842f7f43 100644 --- a/tests/Bots/BadgeServiceTests.hs +++ b/tests/Bots/BadgeServiceTests.hs @@ -95,8 +95,8 @@ import Simplex.Chat.Badges.Service -- 'BadgeUpgrade' imports only its constructor: its 'receipt' and 'balance' fields collide -- with 'BSPBadgeCredential''s and 'BSCIssueBadge''s, both imported with '(..)' above. BadgeUpgrade (BadgeUpgrade), - StatementCreditType (SCCharge, SCOpening, SCPayment), - StatementDebitType (SDBadge, SDLapse), + StatementCreditType (SCCharge, SCOpening, SCPayment, SCTransferIn), + StatementDebitType (SDBadge, SDLapse, SDTransferOut, SDUpgrade), StatementEntry (StatementEntry), StatementEntryType (SECredit, SEDebit), pattern VersionBadgeService, @@ -2670,8 +2670,11 @@ testC5RedeemCodeShowsBadge ps = do alice ##> "/_badge state 1" expectBadgeState alice threeMonthBadgeState reportsNoBadgeEvent alice - -- the credential verifies under the key the service actually loaded, so it went through - -- 'verifyUserBadge' rather than being stored on the service's word + -- the stored credential verifies under the key the service actually loaded -- which says the + -- credential is genuine, NOT that the client checked it: a client that stored blindly would + -- hold this same validly-signed credential, because the real service only signs valid ones. + -- What proves the check happens is 'testC5UnverifiableCredentialWritesNothing', where the + -- configured key is not the service's and nothing at all is written. issuerPub <- readTestIssuerPublicKey (tmpPath ps) cred <- clientIssuedCredentials alice >>= \case @@ -2925,7 +2928,12 @@ testC5CatalogFromFreshProfile ps = do clock <- newTestClock testClockStart withBadgeClient ps clock (pure ()) $ \alice -> do alice ##> "/_badge catalog 1" - -- neither catalog query orders its rows, so the six lines are compared as a set + -- neither catalog query orders its rows, so the six lines are compared as a set. + -- + -- This comparison is also what would catch 'catalogRequest' starting to sign: a signed + -- getBadgeCatalog from this never-purchased key is answered with unknown_purchase_key, so + -- there would be no six lines to compare -- the example would fail here, on a + -- "badge service error: unknown_purchase_key" line, rather than passing quietly. catalogLines <- replicateM 6 (getTermLine alice) sort catalogLines `shouldBe` sort @@ -3217,10 +3225,12 @@ stubCredentialResponse credential statement = testC5UnrecordableResponsesWriteNothing :: HasCallStack => TestParams -> IO () testC5UnrecordableResponsesWriteNothing ps = withBadgeStub ps $ \alice stub sk -> do + -- the whole line, not a prefix: every message below is complete, and a prefix assertion on + -- the unstorable cases would pass for a build that refused the WRONG entry type let redeemAnsweredWith label body message = do alice ##> purchaseCodeCmd "C5-STUB-CODE" answerStubRequest stub body - alice <##. message + alice <## message noClientBadgeRows label alice -- no credential at all: the exhausted balance of an issueBadge, which a redemption cannot be redeemAnsweredWith @@ -3244,19 +3254,53 @@ testC5UnrecordableResponsesWriteNothing ps = "after a credential with no expiry" (stubCredentialResponse (Just noExpiryCred) stubRedeemStatement) "badge service error: internal, issued badge credential carries no expiry" - -- an entry type this build cannot store, refused BEFORE the transaction opens - let unstorable = - BadgeStatement - { entries = - [ stubEntry "c5-stub-charge" 3 3 testClockStart (SECredit (SCCharge "c5-charge")), - stubEntry "c5-stub-debit" (-1) 2 firstPeriodEnd (SEDebit SDBadge) - ], - previousEntryId = Nothing - } - redeemAnsweredWith - "after an unstorable statement" - (stubCredentialResponse (Just cred) unstorable) - "badge service error: internal, badge service statement cannot be stored: " + -- EVERY entry type this build cannot store, each refused BEFORE the transaction opens and each + -- named in the message it is refused with. All four go through the same + -- 'storedEntryType'/'encodeLedgerEntryType' pair, so one of them would have exercised the + -- code path -- but only naming each in its own assertion catches a build that rejects the + -- wrong one, and only the debit(badge) after it keeps 'issuedBadgePeriod' satisfied so the + -- storability check is what the redemption fails on. + (otherKey, _otherPriv) <- mkTestKeyPair + forM_ (unstorableEntryCases otherKey) $ \(label, entryType, reason) -> do + let unstorable = + BadgeStatement + { entries = + [ stubEntry "c5-stub-unstorable" 3 3 testClockStart entryType, + stubEntry "c5-stub-debit" (-1) 2 firstPeriodEnd (SEDebit SDBadge) + ], + previousEntryId = Nothing + } + redeemAnsweredWith + label + (stubCredentialResponse (Just cred) unstorable) + ("badge service error: internal, badge service statement cannot be stored: SEInternalError {message = \"" <> reason <> "\"}") + +-- | The four statement entry types this build cannot store, with the label of the case and the +-- reason 'Simplex.Chat.Store.Badges.storedEntryType' refuses each with. +-- +-- The reasons are spelled out rather than derived from that function: a test that asked production +-- for the message it expects production to produce would pass whichever message production +-- produced. They carry a purchase KEY where the stored columns hold a purchase id, or a TEXT +-- charge id against an 'Int64' one, which is why none of them can be replicated today. +unstorableEntryCases :: C.PublicKeyEd25519 -> [(String, StatementEntryType, String)] +unstorableEntryCases otherKey = + [ ( "after a statement carrying credit(charge)", + SECredit (SCCharge "c5-charge"), + "cannot store badge ledger credit(charge): the stored LedgerCreditType.CTCharge types chargeId as Int64 while the wire and the charge_id column are both TEXT; subscriptions are out of scope" + ), + ( "after a statement carrying credit(transferIn)", + SECredit (SCTransferIn otherKey), + "cannot store badge ledger credit(transferIn): carries a purchase key where from_purchase_id holds a purchase id; transfers are out of scope" + ), + ( "after a statement carrying debit(upgrade)", + SEDebit (SDUpgrade otherKey), + "cannot store badge ledger debit(upgrade): carries a purchase key where to_purchase_id holds a purchase id; upgrades are out of scope" + ), + ( "after a statement carrying debit(transferOut)", + SEDebit (SDTransferOut otherKey), + "cannot store badge ledger debit(transferOut): carries a purchase key where to_purchase_id holds a purchase id; transfers are out of scope" + ) + ] -- | A response of the wrong shape for the command it answers, and one that is not a -- 'BadgeServiceResponse' at all, on both commands that send one. diff --git a/tests/ChatTests/Utils.hs b/tests/ChatTests/Utils.hs index 82e446e7a3..f88d8b135f 100644 --- a/tests/ChatTests/Utils.hs +++ b/tests/ChatTests/Utils.hs @@ -184,8 +184,17 @@ cc ##> cmd = do -- do with the failure (C5). -- -- What is tolerated is narrow: a leading @\"> \"@ prompt prefix, and fragments that spell the --- command out in order. Anything else — an event line, a response line, an unrelated command's --- echo — is not a prefix of what was sent and is reported as the same mismatch as before. +-- command out in order. A response line, an event line or an unrelated command's echo is not a +-- prefix of what was sent and is reported as the same mismatch as before. +-- +-- __It does eat blank lines, and that is a known limitation, not an oversight.__ @\"\"@ is a +-- prefix of every command, so up to 8 blank rows arriving before the echo are consumed silently, +-- and a blank line that some later assertion in the same example expected would then be missing — +-- failing that assertion instead of this one. It is left this way deliberately: tightening the +-- test to "the fragment must have added something" would reject a bare @\"> \"@ row, and a bare +-- @\"> \"@ row is one of the two shapes the artifact this exists for might have (C5 could not +-- reproduce it to find out which). A blank line before a command's own echo has never been +-- observed; a lost echo has. drainEcho :: HasCallStack => TestCC -> String -> Expectation drainEcho cc cmd = drain (0 :: Int) "" where