summaryrefslogtreecommitdiff
path: root/tests/FundLab.Api.Tests/StockSipTests.fs
blob: 2dae473e60248f0a13c90eb468f0cfddf93df69f (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
namespace FundLab.Api.Tests

open System
open Xunit
open FundLab.Api
open FundLab.Domain

module StockSipSupport =
    let quoteWith (code: string) (price: decimal option) (suspended: bool option) : StockQuote =
        {
            Code = code
            Name = Some "贵州茅台"
            Price = price
            Currency = "CNY"
            Market = Some "sh"
            Adjust = Some "none"
            Suspended = suspended
        }

    let stubQuoteProbe (quote: StockQuote) =
        { new IStockQuoteProbe with
            member _.GetQuote(_, _) = Ok quote }

    let probes (quote: StockQuote) =
        { NavDates = NavDateProbeSupport.stubNavDateProbe
          NavSeries = NavDateProbeSupport.stubNavSeriesProbe
          BondQuotes = NavDateProbeSupport.stubBondQuoteProbe
          StockQuotes = stubQuoteProbe quote
          StockDaily = NavDateProbeSupport.stubStockDailyProbe }

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

    let repository () = sharedRepository.Value

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

        let key = fixture.Key(sprintf "stock-sip-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 createStockPlan fundId code amount frequency anchor =
        let key = fixture.Key(sprintf "stock-sip-plan-%s" (Guid.NewGuid().ToString("N")))

        match
            repository().CreateSipPlan(
                key,
                fundId,
                { InstrumentCode = code; Amount = amount; Frequency = frequency },
                anchor,
                "stock"
            )
        with
        | SipPlanWriteResult.SipPlanCreated plan -> plan.Id
        | other -> failwithf "unexpected sip plan result: %A" other

    let app (quote: StockQuote) =
        App.createApplicationWithProbes (repository ()) (StockSipSupport.probes quote)

    let advance (quote: StockQuote) fundId body =
        PersistenceTestHelpers.invoke
            (app quote)
            "POST"
            (sprintf "/api/funds/%O/sip/advance" fundId)
            [ "Authorization", "Bearer test-token" ]
            body

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

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

    [<Fact>]
    member _.``a stock sip period buys whole lots and debits cash through the shared pipeline``() =
        let fundId = createFund 10000.00m
        let quote = StockSipSupport.quoteWith "600519" (Some 10.00m) (Some false)
        let _ = createStockPlan fundId "600519" 2000.00m SipFrequency.Weekly (DateOnly(2026, 9, 7))

        let status, body = advance quote fundId """{"endDate":"2026-09-14"}"""

        Assert.Equal(200, status)
        Assert.Contains("\"assetClass\":\"stock\"", body)
        Assert.Contains("\"status\":\"succeeded\"", body)

        Assert.Equal(200m, stockQuantity fundId "600519")
        Assert.Equal(8000.00m, availableCash fundId)

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

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

    [<Fact>]
    member _.``a suspended stock sip period is rejected without any trade``() =
        let fundId = createFund 10000.00m
        let quote = StockSipSupport.quoteWith "600519" (Some 10.00m) (Some true)
        let _ = createStockPlan fundId "600519" 2000.00m SipFrequency.Weekly (DateOnly(2026, 9, 7))

        let status, body = advance quote fundId """{"endDate":"2026-09-14"}"""

        Assert.Equal(200, status)
        Assert.Contains("\"status\":\"suspended\"", body)
        Assert.Equal(0m, stockQuantity fundId "600519")
        Assert.Equal(10000.00m, availableCash fundId)

    [<Fact>]
    member _.``a stock sip period with insufficient cash is rejected and never drives cash negative``() =
        let fundId = createFund 500.00m
        let quote = StockSipSupport.quoteWith "600519" (Some 10.00m) (Some false)
        let _ = createStockPlan fundId "600519" 2000.00m SipFrequency.Weekly (DateOnly(2026, 9, 7))

        let status, body = advance quote fundId """{"endDate":"2026-09-14"}"""

        Assert.Equal(200, status)
        Assert.Contains("\"status\":\"insufficient_cash\"", body)
        Assert.Equal(0m, stockQuantity fundId "600519")
        Assert.Equal(500.00m, availableCash fundId)

    [<Fact>]
    member _.``re-running the same stock sip window does not double the position``() =
        let fundId = createFund 10000.00m
        let quote = StockSipSupport.quoteWith "600519" (Some 10.00m) (Some false)
        let _ = createStockPlan fundId "600519" 2000.00m SipFrequency.Weekly (DateOnly(2026, 9, 7))

        let firstStatus, _ = advance quote fundId """{"endDate":"2026-09-14"}"""
        Assert.Equal(200, firstStatus)

        let secondStatus, secondBody = advance quote fundId """{"endDate":"2026-09-14"}"""
        Assert.Equal(200, secondStatus)
        Assert.Contains("\"status\":\"succeeded\"", secondBody)

        Assert.Equal(200m, stockQuantity fundId "600519")
        Assert.Equal(8000.00m, availableCash fundId)