Fix flaky RSpec: include local_hcb_code.ledger_item in calculated_ledger_item#14535
Conversation
…er_item Co-authored-by: garyhtou <20099646+garyhtou@users.noreply.github.com>
|
@copilot My intuition tells me that this is not the right fix because we do not want CanonicalTransaction to depend on HCBCode. We plan to deprecate HCBCode since Ledger::Item is replacing it. |
… is present Co-authored-by: garyhtou <20099646+garyhtou@users.noreply.github.com>
Good point. I've reverted the |
Summary of the problem
bad_settled_mapping_spec.rbfails non-deterministically (reproducible with--seed 17199) with:CanonicalTransactionandCanonicalPendingTransactioneach useHCB-000-{id}as their hcb_code when no linked object exists. Because they draw from independent PostgreSQL sequences, they can coincidentally receive the sameid. When that happens, the CPT'sassign_ledger_itemcreates aLedger::Itemand attaches it to the sharedHcbCode. When the CT is then created, itsassign_ledger_itemcheck seescalculated_ledger_item = nilbutlocal_hcb_code.ledger_item = <CPT's item>and raises viaRails.error.unexpected.Describe your changes
Added
local_hcb_code&.ledger_itemas a third fallback inCanonicalTransaction#calculated_ledger_item:This makes CT consistent with
CanonicalPendingTransaction#assign_ledger_item, which already useslocal_hcb_code.ledger_itemas its primary source. TheRails.error.unexpectedcheck is preserved and still fires for genuine mismatches—whenshort_codeorlinked_object_v2resolves to a specific ledger item that disagrees with what the HcbCode stores. The false positive (nil vs. an already-assigned item from a co-grouped record) is eliminated.