summaryrefslogtreecommitdiff
path: root/tests/FundLab.Api.Tests/RebalanceStockTests.fs
blob: 3aed5aea4f1df75eab496b8fca46bce1e1b45a40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
namespace FundLab.Api.Tests

open System
open System.Text.Json
open Xunit
open FundLab.Api
open FundLab.Domain

[<Collection("postgres")>]
type RebalanceStockTests(fixture: PostgresFixture) =
    let sharedRepository =
        lazy
            let value = FundRepository(fixture.ConnectionString)
            value.EnsureSchema()
            value

    let repository () = sharedRepository.Value

    let today = ConfirmationPolicy.tradeDateFor DateTimeOffset.UtcNow

    let createFund (initialCash: decimal) =
        let command =
            {
                Name = "股票再平衡测试 FOF"
                InitialCash = initialCash
                InitialUnitNav = 1.00000000m
                IsSynthetic = true
            }

        let key = fixture.Key(sprintf "rebalance-stock-fund-%s" (Guid.NewGuid().ToString("N")))

        match repository().CreateFund(key, command) with
        | FundWriteResult.Created fund -> fund.Id
        | other -> failwithf "unexpected fund creation result: %A" other

    let seedInstrument () =
        let code = Random.Shared.Next(0, 1000000).ToString("D6")

        let payload =
            {
                Source = "akshare"
                SourceRevision = "akshare-test/eastmoney"
                CollectedAt = DateTimeOffset(2026, 9, 21, 8, 0, 0, TimeSpan.Zero)
                Instruments = [ { Code = code; Name = "再平衡股票测试基金"; FundType = None } ]
            }

        repository().UpsertInstruments(payload, "rebalance-stock-test-hash")
        code

    let seedStockSnapshot (code: string) (price: decimal) =
        let snapshot: InstrumentSnapshotRecord =
            {
                InstrumentCode = code
                AssetClass = "stock"
                SnapshotDate = today
                Price = price
                Source = "akshare"
                SourceRevision = "akshare-test/stock-daily"
                SourceCollectedAt = DateTimeOffset(today.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero)
                SourcePayloadHash = sprintf "rebalance-stock-hash/%s/%O" code (Guid.NewGuid())
            }

        repository().UpsertInstrumentSnapshots [ snapshot ]

    let buyStock fundId code quantity price =
        let key = fixture.Key(sprintf "rebalance-stock-buy-%s" (Guid.NewGuid().ToString("N")))

        match
            repository().CreateStockTrade(
                key,
                fundId,
                { InstrumentCode = code; StockName = Some "贵州茅台"; Quantity = quantity; Price = price }
            )
        with
        | StockTradeWriteResult.StockTradeCreated _ -> ()
        | other -> failwithf "unexpected stock trade result: %A" other

    let app () = App.createApplication (repository ())

    let createPlanId fundId targets =
        let body =
            targets
            |> List.map (fun (code, percent) -> sprintf "{\"instrumentCode\":\"%s\",\"targetPercent\":\"%s\"}" code percent)
            |> String.concat ","
            |> sprintf "{\"targets\":[%s]}"

        let status, response =
            PersistenceTestHelpers.invoke
                (app ())
                "POST"
                (sprintf "/api/funds/%O/rebalance/plans" fundId)
                [
                    "Authorization", "Bearer test-token"
                    "Idempotency-Key", fixture.Key(sprintf "rebalance-stock-plan-%s" (Guid.NewGuid().ToString("N")))
                ]
                body

        match status with
        | 201 ->
            use document = JsonDocument.Parse(response)
            Guid.Parse(document.RootElement.GetProperty("id").GetString())
        | _ -> failwithf "unexpected plan status %d: %s" status response

    let previewViaApi fundId planId =
        PersistenceTestHelpers.invoke
            (app ())
            "GET"
            (sprintf "/api/funds/%O/rebalance/plans/%O/preview" fundId planId)
            [ "Authorization", "Bearer test-token" ]
            ""

    let executeViaApi fundId planId =
        PersistenceTestHelpers.invoke
            (app ())
            "POST"
            (sprintf "/api/funds/%O/rebalance/plans/%O/execute" fundId planId)
            [ "Authorization", "Bearer test-token" ]
            "{}"

    let stockQuantity fundId code =
        repository().GetStockPositions fundId
        |> List.filter (fun position -> position.InstrumentCode = code)
        |> List.sumBy (fun position -> position.Quantity)

    let availableCash fundId =
        match repository().GetFund fundId with
        | Some fund -> fund.AvailableCash
        | None -> failwith "fund was not found"

    [<Fact>]
    member _.``preview counts a stock holding in the rebalance universe``() =
        let fundId = createFund 5000.00m
        buyStock fundId "600519" 100m 100.00m
        seedStockSnapshot "600519" 100.00m
        let planId = createPlanId fundId [ "600519", "100.00" ]

        let status, body = previewViaApi fundId planId
        Assert.Equal(200, status)

        use document = JsonDocument.Parse(body)
        Assert.Equal("15000.00", document.RootElement.GetProperty("equity").GetString())
        Assert.Equal("5000.00", document.RootElement.GetProperty("availableCash").GetString())

        let row =
            document.RootElement.GetProperty("rows").EnumerateArray()
            |> Seq.find (fun row -> row.GetProperty("instrumentCode").GetString() = "600519")

        Assert.Equal("10000.00", row.GetProperty("currentValue").GetString())
        Assert.Equal("66.66", row.GetProperty("currentPercent").GetString())
        Assert.Equal("buy", row.GetProperty("action").GetString())
        Assert.Equal("5000.00", row.GetProperty("deltaAmount").GetString())

    [<Fact>]
    member _.``a stock buy target is executed through the stock trade pipeline and debits cash``() =
        let fundId = createFund 10000.00m
        seedStockSnapshot "600519" 10.00m
        let planId = createPlanId fundId [ "600519", "100.00" ]

        let status, body = executeViaApi fundId planId
        Assert.Equal(200, status)
        Assert.Contains("\"instrumentCode\":\"600519\"", body)
        Assert.Contains("\"action\":\"buy\"", body)
        Assert.Contains("\"status\":\"succeeded\"", body)

        Assert.Equal(1000m, stockQuantity fundId "600519")
        Assert.Equal(0.00m, availableCash fundId)

        let buyEvents =
            repository().GetStockCashflows fundId
            |> List.filter (fun record -> record.EventType = "buy" && record.InstrumentCode = "600519")

        Assert.Single(buyEvents) |> ignore
        Assert.Equal(10000.00m, buyEvents.Head.Amount)

    [<Fact>]
    member _.``an overweight stock holding is sold through the stock sell pipeline``() =
        let fundId = createFund 0.00m
        let fundCode = seedInstrument ()
        buyStock fundId "600519" 100m 100.00m
        seedStockSnapshot "600519" 100.00m
        let planId = createPlanId fundId [ fundCode, "100.00" ]

        let status, body = executeViaApi fundId planId
        Assert.Equal(200, status)
        Assert.Contains("\"instrumentCode\":\"600519\"", body)
        Assert.Contains("\"action\":\"sell\"", body)
        Assert.Contains("\"status\":\"succeeded\"", body)

        Assert.Equal(0m, stockQuantity fundId "600519")

        let sellEvents =
            repository().GetStockCashflows fundId
            |> List.filter (fun record -> record.EventType = "sell" && record.InstrumentCode = "600519")

        Assert.Single(sellEvents) |> ignore
        Assert.Equal(10000.00m, sellEvents.Head.Amount)

    [<Fact>]
    member _.``a stock target below one board lot is skipped without trading``() =
        let fundId = createFund 500.00m
        seedStockSnapshot "600519" 10.00m
        let planId = createPlanId fundId [ "600519", "100.00" ]

        let status, body = executeViaApi fundId planId
        Assert.Equal(200, status)
        Assert.Contains("\"status\":\"skipped_below_lot\"", body)

        Assert.Equal(0m, stockQuantity fundId "600519")
        Assert.Equal(500.00m, availableCash fundId)