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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
namespace FundLab.Api.Tests
open System
open Xunit
open FundLab.Api
module StockSellSupport =
let quote (code: string) (price: decimal) : StockQuote =
{ Code = code; Name = Some "招商银行"; Price = Some price; Currency = "CNY"; Market = Some "sh"; Adjust = Some "none"; Suspended = Some false }
let stubQuoteProbe (quotes: Map<string, decimal>) =
{ new IStockQuoteProbe with
member _.GetQuote(code, _) =
match quotes.TryFind code with
| Some price -> Ok(quote code price)
| None -> Error(MarketDataCollectorUnavailable(sprintf "no valid quote for %s" code)) }
let probes (quotes: Map<string, decimal>) =
{ NavDates = NavDateProbeSupport.stubNavDateProbe
NavSeries = NavDateProbeSupport.stubNavSeriesProbe
BondQuotes = NavDateProbeSupport.stubBondQuoteProbe
StockQuotes = stubQuoteProbe quotes
StockDaily = NavDateProbeSupport.stubStockDailyProbe }
let snapshot (code: string) (price: decimal) : InstrumentSnapshotRecord =
{
InstrumentCode = code
AssetClass = "stock"
SnapshotDate = DateOnly(2026, 9, 21)
Price = price
Source = "test"
SourceRevision = "test"
SourceCollectedAt = DateTimeOffset.UtcNow
SourcePayloadHash = sprintf "test:%s" code
}
[<Collection("postgres")>]
type StockSellTests(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-sell-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 (quotes: Map<string, decimal>) =
App.createApplicationWithProbes (repository ()) (StockSellSupport.probes quotes)
let buy (quotes: Map<string, decimal>) fundId body idempotencyKey =
PersistenceTestHelpers.invoke
(app quotes)
"POST"
(sprintf "/api/funds/%O/stock-trades" fundId)
[ "Authorization", "Bearer test-token"; "Idempotency-Key", idempotencyKey ]
body
let sell (quotes: Map<string, decimal>) fundId body idempotencyKey =
PersistenceTestHelpers.invoke
(app quotes)
"POST"
(sprintf "/api/funds/%O/stock-sells" fundId)
[ "Authorization", "Bearer test-token"; "Idempotency-Key", idempotencyKey ]
body
let positions (quotes: Map<string, decimal>) fundId =
PersistenceTestHelpers.invoke
(app quotes)
"GET"
(sprintf "/api/funds/%O/stock-positions" fundId)
[ "Authorization", "Bearer test-token" ]
""
let fund (quotes: Map<string, decimal>) fundId =
PersistenceTestHelpers.invoke
(app quotes)
"GET"
(sprintf "/api/funds/%O" fundId)
[ "Authorization", "Bearer test-token" ]
""
let valuation (quotes: Map<string, decimal>) fundId =
PersistenceTestHelpers.invoke
(app quotes)
"GET"
(sprintf "/api/funds/%O/valuation" fundId)
[ "Authorization", "Bearer test-token" ]
""
[<Fact>]
member _.``selling part of a position credits cash and reduces the position``() =
let fundId = createFund 1000.00m
let quotes = Map.ofList [ "600030", 10.00m ]
let status, _ = buy quotes fundId """{"instrumentCode":"600030","quantity":"100"}""" (fixture.Key "stock-sell-buy-1")
Assert.Equal(201, status)
let sellQuotes = Map.ofList [ "600030", 12.00m ]
let status, body =
sell sellQuotes fundId """{"instrumentCode":"600030","quantity":"40"}""" (fixture.Key "stock-sell-partial")
Assert.Equal(201, status)
Assert.Contains("\"instrumentCode\":\"600030\"", body)
Assert.Contains("\"quantity\":\"40.00000000\"", body)
Assert.Contains("\"price\":\"12.00000000\"", body)
Assert.Contains("\"feeAmount\":\"0.00\"", body)
Assert.Contains("\"proceeds\":\"480.00\"", body)
let status, positionsBody = positions sellQuotes fundId
Assert.Equal(200, status)
Assert.Contains("\"quantity\":\"60.00000000\"", positionsBody)
Assert.Contains("\"costCash\":\"600.00\"", positionsBody)
let status, fundBody = fund sellQuotes fundId
Assert.Equal(200, status)
Assert.Contains("\"availableCash\":\"1480.00\"", fundBody)
[<Fact>]
member _.``selling all units removes the position row and credits the proceeds``() =
let fundId = createFund 1000.00m
let quotes = Map.ofList [ "600030", 10.00m ]
let status, _ = buy quotes fundId """{"instrumentCode":"600030","quantity":"100"}""" (fixture.Key "stock-sell-buy-2")
Assert.Equal(201, status)
let sellQuotes = Map.ofList [ "600030", 12.00m ]
let status, _ =
sell sellQuotes fundId """{"instrumentCode":"600030","quantity":"100"}""" (fixture.Key "stock-sell-all")
Assert.Equal(201, status)
let _, positionsBody = positions sellQuotes fundId
Assert.Contains("\"positions\":[]", positionsBody)
let _, fundBody = fund sellQuotes fundId
Assert.Contains("\"availableCash\":\"2200.00\"", fundBody)
[<Fact>]
member _.``overselling is rejected with a reason and leaves cash and position unchanged``() =
let fundId = createFund 1000.00m
let quotes = Map.ofList [ "600030", 10.00m ]
let status, _ = buy quotes fundId """{"instrumentCode":"600030","quantity":"100"}""" (fixture.Key "stock-sell-buy-3")
Assert.Equal(201, status)
let status, body =
sell quotes fundId """{"instrumentCode":"600030","quantity":"150"}""" (fixture.Key "stock-sell-over")
Assert.Equal(400, status)
Assert.Contains("INSUFFICIENT_STOCK_HOLDINGS", body)
Assert.Contains("are not enough", body)
let _, positionsBody = positions quotes fundId
Assert.Contains("\"quantity\":\"100.00000000\"", positionsBody)
let _, fundBody = fund quotes fundId
Assert.Contains("\"availableCash\":\"1000.00\"", fundBody)
[<Fact>]
member _.``valuation after a partial sale prices only the remaining units``() =
let fundId = createFund 1000.00m
let quotes = Map.ofList [ "600030", 10.00m ]
let status, _ = buy quotes fundId """{"instrumentCode":"600030","quantity":"100"}""" (fixture.Key "stock-sell-buy-4")
Assert.Equal(201, status)
let status, _ =
sell quotes fundId """{"instrumentCode":"600030","quantity":"40"}""" (fixture.Key "stock-sell-valuation")
Assert.Equal(201, status)
let valuationQuotes = Map.ofList [ "600030", 15.00m ]
let status, body = valuation valuationQuotes fundId
Assert.Equal(200, status)
Assert.Contains("\"quantity\":\"60.00000000\"", body)
Assert.Contains("\"positionsMarketValue\":\"900.00\"", body)
Assert.Contains("\"pricedPositions\":1", body)
[<Fact>]
member _.``replaying a sale with the same idempotency key does not double-sell or double-credit``() =
let fundId = createFund 1000.00m
let quotes = Map.ofList [ "600030", 10.00m ]
let status, _ = buy quotes fundId """{"instrumentCode":"600030","quantity":"100"}""" (fixture.Key "stock-sell-buy-5")
Assert.Equal(201, status)
let key = fixture.Key "stock-sell-replay"
let status, _ = sell quotes fundId """{"instrumentCode":"600030","quantity":"30"}""" key
Assert.Equal(201, status)
let status, _ = sell quotes fundId """{"instrumentCode":"600030","quantity":"30"}""" key
Assert.Equal(200, status)
let _, positionsBody = positions quotes fundId
Assert.Contains("\"quantity\":\"70.00000000\"", positionsBody)
Assert.DoesNotContain("\"quantity\":\"40.00000000\"", positionsBody)
let _, fundBody = fund quotes fundId
Assert.Contains("\"availableCash\":\"1300.00\"", fundBody)
[<Fact>]
member _.``a persisted snapshot price wins over the live quote when selling``() =
let fundId = createFund 1000.00m
let quotes = Map.ofList [ "600036", 10.00m ]
let status, _ = buy quotes fundId """{"instrumentCode":"600036","quantity":"100"}""" (fixture.Key "stock-sell-buy-6")
Assert.Equal(201, status)
repository().UpsertInstrumentSnapshots [ StockSellSupport.snapshot "600036" 20.00m ]
let liveQuotes = Map.ofList [ "600036", 99.00m ]
let status, body =
sell liveQuotes fundId """{"instrumentCode":"600036","quantity":"50"}""" (fixture.Key "stock-sell-snapshot")
Assert.Equal(201, status)
Assert.Contains("\"price\":\"20.00000000\"", body)
Assert.Contains("\"proceeds\":\"1000.00\"", body)
|