|
2 | 2 |
|
3 | 3 | using System; |
4 | 4 | using System.IO; |
| 5 | +using System.Linq; |
5 | 6 | using System.Text.Json; |
6 | 7 | using System.Threading.Tasks; |
7 | 8 | using FluentAssertions; |
@@ -197,4 +198,131 @@ public async Task RetrieveCheckpointAsync_ShouldReturnPersistedDataAsync() |
197 | 198 | retrieved.GetProperty("name").GetString().Should().Be("test"); |
198 | 199 | retrieved.GetProperty("value").GetInt32().Should().Be(42); |
199 | 200 | } |
| 201 | + |
| 202 | + [Fact] |
| 203 | + public async Task RetrieveIndexAsync_ShouldOnlyReturnCheckpointsForRequestedSessionAsync() |
| 204 | + { |
| 205 | + // Arrange |
| 206 | + using TempDirectory tempDirectory = new(); |
| 207 | + string firstSessionId = Guid.NewGuid().ToString("N"); |
| 208 | + string secondSessionId = Guid.NewGuid().ToString("N"); |
| 209 | + CheckpointInfo firstCheckpoint; |
| 210 | + CheckpointInfo secondCheckpoint; |
| 211 | + |
| 212 | + using (FileSystemJsonCheckpointStore store = new(tempDirectory)) |
| 213 | + { |
| 214 | + firstCheckpoint = await store.CreateCheckpointAsync(firstSessionId, TestData); |
| 215 | + secondCheckpoint = await store.CreateCheckpointAsync(secondSessionId, TestData); |
| 216 | + |
| 217 | + // Act |
| 218 | + CheckpointInfo[] firstSessionIndex = (await store.RetrieveIndexAsync(firstSessionId)).ToArray(); |
| 219 | + |
| 220 | + // Assert |
| 221 | + firstSessionIndex.Should().ContainSingle().Which.Should().Be(firstCheckpoint); |
| 222 | + firstSessionIndex.Should().NotContain(secondCheckpoint); |
| 223 | + } |
| 224 | + |
| 225 | + using (FileSystemJsonCheckpointStore reopenedStore = new(tempDirectory)) |
| 226 | + { |
| 227 | + CheckpointInfo[] secondSessionIndex = (await reopenedStore.RetrieveIndexAsync(secondSessionId)).ToArray(); |
| 228 | + |
| 229 | + secondSessionIndex.Should().ContainSingle().Which.Should().Be(secondCheckpoint); |
| 230 | + secondSessionIndex.Should().NotContain(firstCheckpoint); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + [Fact] |
| 235 | + public async Task RetrieveIndexAsync_ShouldFilterByParentCheckpointAsync() |
| 236 | + { |
| 237 | + // Arrange |
| 238 | + using TempDirectory tempDirectory = new(); |
| 239 | + string sessionId = Guid.NewGuid().ToString("N"); |
| 240 | + CheckpointInfo parentCheckpoint; |
| 241 | + CheckpointInfo childCheckpoint; |
| 242 | + CheckpointInfo unrelatedCheckpoint; |
| 243 | + |
| 244 | + using (FileSystemJsonCheckpointStore store = new(tempDirectory)) |
| 245 | + { |
| 246 | + parentCheckpoint = await store.CreateCheckpointAsync(sessionId, TestData); |
| 247 | + childCheckpoint = await store.CreateCheckpointAsync(sessionId, TestData, parentCheckpoint); |
| 248 | + unrelatedCheckpoint = await store.CreateCheckpointAsync(sessionId, TestData); |
| 249 | + |
| 250 | + // Act |
| 251 | + CheckpointInfo[] childIndex = (await store.RetrieveIndexAsync(sessionId, parentCheckpoint)).ToArray(); |
| 252 | + |
| 253 | + // Assert |
| 254 | + childIndex.Should().ContainSingle().Which.Should().Be(childCheckpoint); |
| 255 | + childIndex.Should().NotContain(parentCheckpoint); |
| 256 | + childIndex.Should().NotContain(unrelatedCheckpoint); |
| 257 | + } |
| 258 | + |
| 259 | + using (FileSystemJsonCheckpointStore reopenedStore = new(tempDirectory)) |
| 260 | + { |
| 261 | + CheckpointInfo[] childIndex = (await reopenedStore.RetrieveIndexAsync(sessionId, parentCheckpoint)).ToArray(); |
| 262 | + |
| 263 | + childIndex.Should().ContainSingle().Which.Should().Be(childCheckpoint); |
| 264 | + childIndex.Should().NotContain(parentCheckpoint); |
| 265 | + childIndex.Should().NotContain(unrelatedCheckpoint); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + [Fact] |
| 270 | + public async Task RetrieveIndexAsync_ShouldKeepLegacyEntriesDiscoverableWithParentFilterAsync() |
| 271 | + { |
| 272 | + // Arrange |
| 273 | + using TempDirectory tempDirectory = new(); |
| 274 | + string sessionId = Guid.NewGuid().ToString("N"); |
| 275 | + CheckpointInfo parentCheckpoint; |
| 276 | + CheckpointInfo childCheckpoint; |
| 277 | + string childFileName; |
| 278 | + |
| 279 | + using (FileSystemJsonCheckpointStore store = new(tempDirectory)) |
| 280 | + { |
| 281 | + parentCheckpoint = await store.CreateCheckpointAsync(sessionId, TestData); |
| 282 | + childCheckpoint = await store.CreateCheckpointAsync(sessionId, TestData, parentCheckpoint); |
| 283 | + childFileName = store.GetFileNameForCheckpoint(sessionId, childCheckpoint); |
| 284 | + } |
| 285 | + |
| 286 | + string indexPath = Path.Combine(tempDirectory.FullName, "index.jsonl"); |
| 287 | + string legacyEntry = JsonSerializer.Serialize(new CheckpointFileIndexEntry(childCheckpoint, childFileName)); |
| 288 | + File.WriteAllText(indexPath, legacyEntry + Environment.NewLine); |
| 289 | + |
| 290 | + // Act |
| 291 | + using FileSystemJsonCheckpointStore reopenedStore = new(tempDirectory); |
| 292 | + CheckpointInfo[] childIndex = (await reopenedStore.RetrieveIndexAsync(sessionId, parentCheckpoint)).ToArray(); |
| 293 | + |
| 294 | + // Assert |
| 295 | + childIndex.Should().ContainSingle().Which.Should().Be(childCheckpoint); |
| 296 | + } |
| 297 | + |
| 298 | + [Fact] |
| 299 | + public async Task RetrieveIndexAsync_ShouldKeepLegacyChildDiscoverableWithUnrelatedParentFilterAsync() |
| 300 | + { |
| 301 | + // Arrange |
| 302 | + using TempDirectory tempDirectory = new(); |
| 303 | + string sessionId = Guid.NewGuid().ToString("N"); |
| 304 | + CheckpointInfo parentCheckpoint; |
| 305 | + CheckpointInfo childCheckpoint; |
| 306 | + CheckpointInfo unrelatedCheckpoint; |
| 307 | + string childFileName; |
| 308 | + |
| 309 | + using (FileSystemJsonCheckpointStore store = new(tempDirectory)) |
| 310 | + { |
| 311 | + parentCheckpoint = await store.CreateCheckpointAsync(sessionId, TestData); |
| 312 | + childCheckpoint = await store.CreateCheckpointAsync(sessionId, TestData, parentCheckpoint); |
| 313 | + unrelatedCheckpoint = await store.CreateCheckpointAsync(sessionId, TestData); |
| 314 | + childFileName = store.GetFileNameForCheckpoint(sessionId, childCheckpoint); |
| 315 | + } |
| 316 | + |
| 317 | + string indexPath = Path.Combine(tempDirectory.FullName, "index.jsonl"); |
| 318 | + string legacyEntry = JsonSerializer.Serialize(new CheckpointFileIndexEntry(childCheckpoint, childFileName)); |
| 319 | + File.WriteAllText(indexPath, legacyEntry + Environment.NewLine); |
| 320 | + |
| 321 | + // Act |
| 322 | + using FileSystemJsonCheckpointStore reopenedStore = new(tempDirectory); |
| 323 | + CheckpointInfo[] childIndex = (await reopenedStore.RetrieveIndexAsync(sessionId, unrelatedCheckpoint)).ToArray(); |
| 324 | + |
| 325 | + // Assert |
| 326 | + childIndex.Should().ContainSingle().Which.Should().Be(childCheckpoint); |
| 327 | + } |
200 | 328 | } |
0 commit comments