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

open System
open Xunit
open FundLab.Api

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

    let repository () = sharedRepository.Value

    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-archive-test-hash")
        code

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

        let key = fixture.Key(sprintf "rebalance-archive-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 app () = App.createApplication (repository ())

    let createPlan (fundId: Guid) (code: string) =
        let key = fixture.Key(sprintf "rebalance-archive-plan-%s" (Guid.NewGuid().ToString("N")))

        match
            repository().CreateRebalancePlan(key, fundId, { Targets = [ { InstrumentCode = code; TargetPercent = 100.00m } ] })
        with
        | RebalanceWriteResult.RebalancePlanCreated plan -> plan.Id
        | other -> failwithf "unexpected rebalance plan result: %A" other

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

    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 plansViaApi fundId =
        PersistenceTestHelpers.invoke
            (app ())
            "GET"
            (sprintf "/api/funds/%O/rebalance/plans" fundId)
            [ "Authorization", "Bearer test-token" ]
            ""

    [<Fact>]
    member _.``archiving an active plan marks it archived in the plan list``() =
        let fundId = createFund 1000.00m
        let code = seedInstrument ()
        let planId = createPlan fundId code

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

        let _, plansBody = plansViaApi fundId
        Assert.Contains("\"status\":\"archived\"", plansBody)

    [<Fact>]
    member _.``archived plan cannot be previewed``() =
        let fundId = createFund 1000.00m
        let code = seedInstrument ()
        let planId = createPlan fundId code

        let _, _ = archiveViaApi fundId planId
        let status, body = previewViaApi fundId planId

        Assert.Equal(409, status)
        Assert.Contains("PLAN_ARCHIVED", body)

    [<Fact>]
    member _.``archived plan cannot be executed``() =
        let fundId = createFund 1000.00m
        let code = seedInstrument ()
        let planId = createPlan fundId code

        let _, _ = archiveViaApi fundId planId
        let status, body = executeViaApi fundId planId

        Assert.Equal(409, status)
        Assert.Contains("PLAN_ARCHIVED", body)

    [<Fact>]
    member _.``archiving an already archived plan is idempotent``() =
        let fundId = createFund 1000.00m
        let code = seedInstrument ()
        let planId = createPlan fundId code

        let firstStatus, firstBody = archiveViaApi fundId planId
        Assert.Equal(200, firstStatus)
        Assert.Contains("\"status\":\"archived\"", firstBody)

        let secondStatus, secondBody = archiveViaApi fundId planId
        Assert.Equal(200, secondStatus)
        Assert.Contains("\"status\":\"archived\"", secondBody)

    [<Fact>]
    member _.``archiving a plan owned by another fund answers 404``() =
        let fundId = createFund 1000.00m
        let otherFundId = createFund 1000.00m
        let code = seedInstrument ()
        let planId = createPlan fundId code

        let status, body = archiveViaApi otherFundId planId
        Assert.Equal(404, status)
        Assert.Contains("PLAN_NOT_FOUND", body)