From b05e728ae23d088ca6c9ecccf6ed00d2ab6f3839 Mon Sep 17 00:00:00 2001 From: "Somhairle H. Marisol" Date: Tue, 22 Sep 2026 08:07:00 +0800 Subject: Add bond full milestone: profile probe, coupon/accrual rules, ledger and point-in-time valuation (3d-29) --- tests/FundLab.Api.Tests/BondQuoteProbeTests.fs | 11 ++ tests/FundLab.Api.Tests/BondTradeTests.fs | 139 ++++++++++++++++++++++++- tests/FundLab.Api.Tests/FundValuationTests.fs | 77 +++++++++++++- 3 files changed, 221 insertions(+), 6 deletions(-) (limited to 'tests/FundLab.Api.Tests') diff --git a/tests/FundLab.Api.Tests/BondQuoteProbeTests.fs b/tests/FundLab.Api.Tests/BondQuoteProbeTests.fs index 1e19f77..e8194fa 100644 --- a/tests/FundLab.Api.Tests/BondQuoteProbeTests.fs +++ b/tests/FundLab.Api.Tests/BondQuoteProbeTests.fs @@ -225,6 +225,17 @@ type BondQuoteProbeTests(fixture: PostgresFixture) = AccruedInterest = None Date = None MaturityDate = Some(DateOnly(2026, 6, 3)) + ParValue = None + IssuePrice = None + ValueDate = None + ListingDate = None + PublishDate = None + PayInterestDay = None + CouponRate = None + CouponRateExplain = None + BondExpireYears = None + Rating = None + DataStatus = None }) let status, body = diff --git a/tests/FundLab.Api.Tests/BondTradeTests.fs b/tests/FundLab.Api.Tests/BondTradeTests.fs index 76827f5..b176aa8 100644 --- a/tests/FundLab.Api.Tests/BondTradeTests.fs +++ b/tests/FundLab.Api.Tests/BondTradeTests.fs @@ -24,6 +24,17 @@ module BondTradeSupport = AccruedInterest = None Date = None MaturityDate = Some(DateOnly(2026, 10, 15)) + ParValue = Some 100m + IssuePrice = Some 100m + ValueDate = Some(DateOnly(2020, 10, 15)) + ListingDate = Some(DateOnly(2020, 11, 3)) + PublishDate = None + PayInterestDay = Some "10-15" + CouponRate = Some 0.02m + CouponRateExplain = None + BondExpireYears = Some "6" + Rating = Some "AAA" + DataStatus = Some "complete" } let appWithQuote (probe: IBondQuoteProbe) = @@ -78,25 +89,143 @@ type BondTradeTests(fixture: PostgresFixture) = [ "Authorization", "Bearer test-token" ] "" + let recordCashflow (probe: IBondQuoteProbe) fundId body idempotencyKey = + PersistenceTestHelpers.invoke + (app probe) + "POST" + (sprintf "/api/funds/%O/bond-cashflows" fundId) + [ "Authorization", "Bearer test-token"; "Idempotency-Key", idempotencyKey ] + body + + let readCashflows (probe: IBondQuoteProbe) fundId = + PersistenceTestHelpers.invoke + (app probe) + "GET" + (sprintf "/api/funds/%O/bond-cashflows" fundId) + [ "Authorization", "Bearer test-token" ] + "" + [] - member _.``buying a bond with a live quote records a trade and a position``() = + member _.``buying a bond records clean price, accrued interest, dirty price and settlement``() = let fundId = createFund () let probe = BondTradeSupport.stubQuoteProbe BondTradeSupport.quote110075 let status, body = - buy probe fundId """{"instrumentCode":"110075","quantity":"10"}""" (fixture.Key "bond-trade-buy-ok") + buy + probe + fundId + """{"instrumentCode":"110075","quantity":"10","tradeDate":"2026-09-21"}""" + (fixture.Key "bond-trade-buy-ok") Assert.Equal(201, status) Assert.Contains("\"instrumentCode\":\"110075\"", body) Assert.Contains("南航转债", body) - Assert.Contains("106.368", body) - Assert.Contains("1063.68", body) + Assert.Contains("\"cleanPrice\":\"106.36800000\"", body) + Assert.Contains("\"accruedInterest\":\"1.86849300\"", body) + Assert.Contains("\"price\":\"108.23649300\"", body) + Assert.Contains("\"parValue\":\"100.00000000\"", body) + Assert.Contains("\"couponRate\":\"0.02000000\"", body) + Assert.Contains("\"valueDate\":\"2020-10-15\"", body) + Assert.Contains("\"maturityDate\":\"2026-10-15\"", body) + Assert.Contains("\"settlementDate\":\"2026-09-21\"", body) + Assert.Contains("\"tradeDate\":\"2026-09-21\"", body) + Assert.Contains("1082.36", body) let status, positionsBody = readPositions probe fundId Assert.Equal(200, status) Assert.Contains("\"instrumentCode\":\"110075\"", positionsBody) Assert.Contains("\"quantity\":\"10.00000000\"", positionsBody) - Assert.Contains("1063.68", positionsBody) + Assert.Contains("1082.36", positionsBody) + + [] + member _.``buying a quantity below the minimum unit is rejected``() = + let fundId = createFund () + let probe = BondTradeSupport.stubQuoteProbe BondTradeSupport.quote110075 + + let status, body = + buy + probe + fundId + """{"instrumentCode":"110075","quantity":"5","tradeDate":"2026-09-21"}""" + (fixture.Key "bond-trade-min-unit") + + Assert.Equal(400, status) + Assert.Contains("INVALID_BOND_TRADE_REQUEST", body) + Assert.Contains("multiple", body) + + [] + member _.``a coupon event credits cash and is recorded idempotently``() = + let fundId = createFund () + let probe = BondTradeSupport.stubQuoteProbe BondTradeSupport.quote110075 + + let buyStatus, _ = + buy + probe + fundId + """{"instrumentCode":"110075","quantity":"10","tradeDate":"2026-09-21"}""" + (fixture.Key "bond-coupon-buy") + + Assert.Equal(201, buyStatus) + + let key = fixture.Key "bond-coupon-event" + + let body = + """{"instrumentCode":"110075","eventType":"coupon","eventDate":"2026-10-15","quantity":"10","amount":"20.00"}""" + + let status, response = recordCashflow probe fundId body key + Assert.Equal(201, status) + Assert.Contains("\"eventType\":\"coupon\"", response) + Assert.Contains("\"amount\":\"20.00\"", response) + + let replayStatus, _ = recordCashflow probe fundId body key + Assert.Equal(200, replayStatus) + + let status, events = readCashflows probe fundId + Assert.Equal(200, status) + Assert.Contains("\"eventType\":\"coupon\"", events) + Assert.DoesNotContain("\"eventType\":\"maturity\"", events) + + [] + member _.``a maturity event settles the position and records the cashflow``() = + let fundId = createFund () + let probe = BondTradeSupport.stubQuoteProbe BondTradeSupport.quote110075 + + let buyStatus, _ = + buy + probe + fundId + """{"instrumentCode":"110075","quantity":"10","tradeDate":"2026-09-21"}""" + (fixture.Key "bond-maturity-buy") + + Assert.Equal(201, buyStatus) + + let status, response = + recordCashflow + probe + fundId + """{"instrumentCode":"110075","eventType":"maturity","eventDate":"2026-10-15","quantity":"10","amount":"1000.00"}""" + (fixture.Key "bond-maturity-event") + + Assert.Equal(201, status) + Assert.Contains("\"eventType\":\"maturity\"", response) + + let _, positionsBody = readPositions probe fundId + Assert.Contains("\"positions\":[]", positionsBody) + + [] + member _.``a cashflow for a bond the fund does not hold answers 404``() = + let fundId = createFund () + let probe = BondTradeSupport.stubQuoteProbe BondTradeSupport.quote110075 + + let status, body = + recordCashflow + probe + fundId + """{"instrumentCode":"110075","eventType":"coupon","eventDate":"2026-10-15","quantity":"10","amount":"20.00"}""" + (fixture.Key "bond-coupon-no-position") + + Assert.Equal(404, status) + Assert.Contains("BOND_POSITION_NOT_FOUND", body) [] member _.``buying an unknown code with an unavailable quote does not create a position``() = diff --git a/tests/FundLab.Api.Tests/FundValuationTests.fs b/tests/FundLab.Api.Tests/FundValuationTests.fs index 1897fd3..197686d 100644 --- a/tests/FundLab.Api.Tests/FundValuationTests.fs +++ b/tests/FundLab.Api.Tests/FundValuationTests.fs @@ -19,6 +19,17 @@ module FundValuationSupport = AccruedInterest = None Date = None MaturityDate = None + ParValue = None + IssuePrice = None + ValueDate = None + ListingDate = None + PublishDate = None + PayInterestDay = None + CouponRate = None + CouponRateExplain = None + BondExpireYears = None + Rating = None + DataStatus = None } let stockProbeReturning (prices: Map) = @@ -109,12 +120,52 @@ type FundValuationTests(fixture: PostgresFixture) = repository().CreateBondTrade( key, fundId, - { InstrumentCode = code; BondName = Some "南航转债"; Quantity = quantity; Price = price } + { + InstrumentCode = code + BondName = Some "南航转债" + Quantity = quantity + Price = price + CleanPrice = price + AccruedInterest = 0m + ParValue = 100m + SettlementDate = DateOnly(2026, 9, 21) + CouponRate = None + ValueDate = None + MaturityDate = None + TradeDate = Some(DateOnly(2026, 9, 21)) + } ) with | BondTradeWriteResult.BondTradeCreated _ -> () | other -> failwithf "unexpected bond trade result: %A" other + let buyBondWithTerms fundId code quantity cleanPrice = + let key = fixture.Key(sprintf "valuation-bond-terms-%s" (Guid.NewGuid().ToString("N"))) + + match + repository().CreateBondTrade( + key, + fundId, + { + InstrumentCode = code + BondName = Some "南航转债" + Quantity = quantity + Price = cleanPrice + CleanPrice = cleanPrice + AccruedInterest = 0m + ParValue = 100m + SettlementDate = DateOnly(2026, 9, 21) + CouponRate = Some 0.02m + ValueDate = Some(DateOnly(2020, 10, 15)) + MaturityDate = Some(DateOnly(2026, 10, 15)) + TradeDate = Some(DateOnly(2026, 9, 21)) + } + ) + with + | BondTradeWriteResult.BondTradeCreated _ -> () + | other -> failwithf "unexpected bond trade result: %A" other + + let app stockPrices bondPrices = App.createApplicationWithProbes (repository ()) (FundValuationSupport.probes stockPrices bondPrices) @@ -142,6 +193,30 @@ type FundValuationTests(fixture: PostgresFixture) = Assert.Contains("\"pricedPositions\":2", body) Assert.Contains("\"unavailablePositions\":0", body) + [] + member _.``bond valuation accrues interest point-in-time and values on a dirty basis``() = + let fundId = createFund 10000.00m + buyBondWithTerms fundId "110075" 10m 106.368m + + let valuationAt asOf = + PersistenceTestHelpers.invoke + (app Map.empty (Map.ofList [ "110075", 106.368m ])) + "GET" + (sprintf "/api/funds/%O/valuation?asOfDate=%s" fundId asOf) + [ "Authorization", "Bearer test-token" ] + "" + + let status, early = valuationAt "2026-09-21" + Assert.Equal(200, status) + Assert.Contains("\"accruedInterest\":\"1.86849300\"", early) + Assert.Contains("\"dirtyPrice\":\"108.23649300\"", early) + Assert.Contains("\"valueBasis\":\"dirty\"", early) + Assert.Contains("\"marketValue\":\"1082.36\"", early) + + let _, later = valuationAt "2026-10-01" + Assert.Contains("\"accruedInterest\":\"1.92328800\"", later) + Assert.Contains("\"marketValue\":\"1082.91\"", later) + [] member _.``all-unavailable valuation keeps cash intact and marks positions unavailable without zeroing``() = let fundId = createFund 5000.00m -- cgit v1.2.3