summaryrefslogtreecommitdiff
path: root/tests/SomhairlesDream.Server.Tests/StoreTests.fs
blob: 4aec16f96bcedc300f9ec5335c32f0005f7b72d8 (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
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
namespace SomhairlesDream.Server.Tests

open System
open Xunit
open SomhairlesDream.Shared
open SomhairlesDream.Server

module StoreTests =
    let private at hour = DateTimeOffset(2026, 9, 20, hour, 0, 0, 0, TimeSpan.Zero)

    let private artifactIds : RunIds =
        { ProjectId = "heritage-001"
          RunId = "run-20260920-001"
          BaseId = "base-000"
          TargetId = "target-003" }

    let private mesh version label =
        { Version = version
          Label = label
          Vertices =
              [| { X = 0.; Y = 0.; Z = 0. }
                 { X = float version; Y = 1.; Z = 0. } |]
          Faces = [||] }

    [<Fact>]
    let ``store publishes the latest state atomically`` () =
        let store = RunStore("heritage-001", "run-001", TimeSpan.FromMinutes 2.)

        let running = store.Start(at 9)
        let published = store.Publish(at 9, mesh running.TargetVersion "主承力骨架")

        Assert.Equal(RunStatus.Succeeded, published.Status)
        Assert.Equal(published, store.Snapshot())
        Assert.Equal(1, published.CurrentVersion)

    [<Fact>]
    let ``store marks an unresponsive run stale when observed`` () =
        let store = RunStore("heritage-001", "run-001", TimeSpan.FromMinutes 2.)
        store.Start(at 9) |> ignore

        let observed = store.Observe(at 10)

        Assert.Equal(RunStatus.Stale, observed.Status)
        Assert.Equal("心跳超时,等待重连", observed.Message)

    [<Fact>]
    let ``store sends an initial snapshot and every transition to subscribers`` () =
        let store = RunStore("heritage-001", "run-001", TimeSpan.FromMinutes 2.)
        let reader = store.Subscribe()

        let initial = reader.ReadAsync().AsTask().GetAwaiter().GetResult()
        let running = store.Start(at 9)
        let update = reader.ReadAsync().AsTask().GetAwaiter().GetResult()

        Assert.Equal(RunStatus.Idle, initial.Status)
        Assert.Equal(running, update)

    [<Fact>]
    let ``artifact store applies lifecycle events to a run snapshot`` () =
        let store = ArtifactRunStore(artifactIds, TimeSpan.FromSeconds 15., at 9)

        let start =
            RunEvent.Start
                { Ids = artifactIds
                  At = at 9
                  TotalSteps = 3 }

        let checkpoint =
            RunEvent.Checkpoint
                { Ids = artifactIds
                  At = at 9
                  StepId = "01-foundation"
                  StepIndex = 0
                  TotalSteps = 3
                  Message = "artifact verified"
                  ArtifactPath = "steps/01-foundation.glb" }

        Assert.True(store.Apply(start) |> Result.isOk)
        Assert.True(store.Apply(checkpoint) |> Result.isOk)

        let snapshot = store.Snapshot()

        Assert.Equal("running", snapshot.Status)
        Assert.Equal(artifactIds.BaseId, snapshot.BaseId)
        Assert.Equal(artifactIds.TargetId, snapshot.TargetId)
        Assert.Equal(Some "01-foundation", snapshot.CurrentStepId)
        Assert.Equal(0, snapshot.CurrentStepIndex)
        Assert.Equal(3, snapshot.TotalSteps)
        Assert.True(snapshot.ManifestPath.IsNone)
        Assert.False(snapshot.IsStale)

    [<Fact>]
    let ``artifact store derives staleness without changing lifecycle status`` () =
        let store = ArtifactRunStore(artifactIds, TimeSpan.FromSeconds 15., at 9)

        store.Apply(
            RunEvent.Start
                { Ids = artifactIds
                  At = at 9
                  TotalSteps = 3 }
        )
        |> ignore

        let snapshot = store.Observe(at 10)

        Assert.Equal("running", snapshot.Status)
        Assert.True(snapshot.IsStale)

    [<Fact>]
    let ``artifact store rejects lifecycle events after completion`` () =
        let store = ArtifactRunStore(artifactIds, TimeSpan.FromSeconds 15., at 9)

        store.Apply(
            RunEvent.Start
                { Ids = artifactIds
                  At = at 9
                  TotalSteps = 3 }
        )
        |> ignore

        for index, stepId in [| 0, "01-foundation"; 1, "02-frame"; 2, "03-cabin" |] do
            store.Apply(
                RunEvent.Checkpoint
                    { Ids = artifactIds
                      At = at 9
                      StepId = stepId
                      StepIndex = index
                      TotalSteps = 3
                      Message = "artifact verified"
                      ArtifactPath = $"steps/{stepId}.glb" }
            )
            |> ignore

        store.Apply(
            RunEvent.Complete
                { Ids = artifactIds
                  At = at 9
                  ManifestPath = "manifest.json" }
        )
        |> ignore

        let result =
            store.Apply(
                RunEvent.Heartbeat
                    { Ids = artifactIds
                      At = at 9
                      Message = "late heartbeat" }
            )

        Assert.True(result |> Result.isError)

    [<Fact>]
    let ``artifact store rejects completion before every checkpoint`` () =
        let store = ArtifactRunStore(artifactIds, TimeSpan.FromSeconds 15., at 9)

        store.Apply(
            RunEvent.Start
                { Ids = artifactIds
                  At = at 9
                  TotalSteps = 3 }
        )
        |> ignore

        let result =
            store.Apply(
                RunEvent.Complete
                    { Ids = artifactIds
                      At = at 9
                      ManifestPath = "manifest.json" }
            )

        Assert.True(result |> Result.isError)
        Assert.Equal("running", store.Snapshot().Status)

    [<Fact>]
    let ``artifact store records a pipeline failure before start`` () =
        let store = ArtifactRunStore(artifactIds, TimeSpan.FromSeconds 15., at 9)

        let result =
            store.Apply(
                RunEvent.Fail
                    { Ids = artifactIds
                      At = at 9
                      StepId = None
                      Message = "run directory already exists" }
            )

        Assert.True(result |> Result.isOk)
        Assert.Equal("failed", store.Snapshot().Status)
        Assert.Equal(Some "run directory already exists", store.Snapshot().Error)

    [<Fact>]
    let ``artifact store accepts completion after ordered checkpoints`` () =
        let store = ArtifactRunStore(artifactIds, TimeSpan.FromSeconds 15., at 9)

        store.Apply(
            RunEvent.Start
                { Ids = artifactIds
                  At = at 9
                  TotalSteps = 3 }
        )
        |> ignore

        for index, stepId in [| 0, "01-foundation"; 1, "02-frame"; 2, "03-cabin" |] do
            store.Apply(
                RunEvent.Checkpoint
                    { Ids = artifactIds
                      At = at 9
                      StepId = stepId
                      StepIndex = index
                      TotalSteps = 3
                      Message = "artifact verified"
                      ArtifactPath = $"steps/{stepId}.glb" }
            )
            |> ignore

        let result =
            store.Apply(
                RunEvent.Complete
                    { Ids = artifactIds
                      At = at 9
                      ManifestPath = "manifest.json" }
            )

        Assert.True(result |> Result.isOk)
        Assert.Equal("complete", store.Snapshot().Status)

    [<Fact>]
    let ``artifact registry atomically creates only one store for a run key`` () =
        let registry = ArtifactRunRegistry(TimeSpan.FromSeconds 15., fun () -> at 9)

        let first = registry.TryCreate(artifactIds)
        let duplicate = registry.TryCreate(artifactIds)

        Assert.True(first.IsSome)
        Assert.True(duplicate.IsNone)