Repository navigation
Expand file tree
/
Copy pathSessionFsE2ETests.cs
More file actions
779 lines (664 loc) · 31.2 KB
/
Copy pathSessionFsE2ETests.cs
File metadata and controls
779 lines (664 loc) · 31.2 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using GitHub.Copilot.Rpc;
using GitHub.Copilot.Test.Harness;
using Microsoft.Extensions.AI;
using Xunit;
using Xunit.Abstractions;
namespace GitHub.Copilot.Test.E2E;
public class SessionFsE2ETests(E2ETestFixture fixture, ITestOutputHelper output)
: E2ETestBase(fixture, "session_fs", output)
{
private static readonly SessionFsConfig SessionFsConfig = new()
{
InitialWorkingDirectory = "/",
SessionStatePath = CreateSessionStatePath(),
Conventions = SessionFsSetProviderConventions.Posix,
};
[Fact]
public async Task Should_Route_File_Operations_Through_The_Session_Fs_Provider()
{
var providerRoot = CreateProviderRoot();
try
{
await using var client = CreateSessionFsClient(providerRoot);
var session = await Ctx.CreateSessionAsync(client, new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsProvider = s => new TestSessionFsHandler(s.SessionId, providerRoot),
});
var msg = await session.SendAndWaitAsync(new MessageOptions { Prompt = "What is 100 + 200?" });
Assert.Contains("300", msg?.Data.Content ?? string.Empty);
await session.DisposeAsync();
var eventsPath = GetStoredPath(providerRoot, session.SessionId, $"{SessionFsConfig.SessionStatePath}/events.jsonl");
await WaitForConditionAsync(() => File.Exists(eventsPath));
var content = await ReadAllTextSharedAsync(eventsPath);
Assert.Contains("300", content);
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task Should_Load_Session_Data_From_Fs_Provider_On_Resume()
{
var providerRoot = CreateProviderRoot();
try
{
await using var client = CreateSessionFsClient(providerRoot);
Func<CopilotSession, SessionFsProvider> createSessionFsHandler = s => new TestSessionFsHandler(s.SessionId, providerRoot);
var session1 = await Ctx.CreateSessionAsync(client, new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsProvider = createSessionFsHandler,
});
var sessionId = session1.SessionId;
var msg = await session1.SendAndWaitAsync(new MessageOptions { Prompt = "What is 50 + 50?" });
Assert.Contains("100", msg?.Data.Content ?? string.Empty);
await session1.DisposeAsync();
var eventsPath = GetStoredPath(providerRoot, sessionId, $"{SessionFsConfig.SessionStatePath}/events.jsonl");
await WaitForConditionAsync(() => File.Exists(eventsPath));
var session2 = await Ctx.ResumeSessionAsync(client, sessionId, new ResumeSessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsProvider = createSessionFsHandler,
});
var msg2 = await session2.SendAndWaitAsync(new MessageOptions { Prompt = "What is that times 3?" });
Assert.Contains("300", msg2?.Data.Content ?? string.Empty);
await session2.DisposeAsync();
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task Should_Reject_SetProvider_When_Sessions_Already_Exist()
{
var providerRoot = CreateProviderRoot();
try
{
await using var client1 = CreateSessionFsClient(providerRoot, useStdio: false, tcpConnectionToken: "session-fs-shared-token");
var createSessionFsHandler = (Func<CopilotSession, SessionFsProvider>)(s => new TestSessionFsHandler(s.SessionId, providerRoot));
_ = await Ctx.CreateSessionAsync(client1, new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsProvider = createSessionFsHandler,
});
var port = client1.RuntimePort
?? throw new InvalidOperationException("Client1 is not using TCP mode; RuntimePort is null");
var client2 = Ctx.CreateClient(
options: new CopilotClientOptions
{
LogLevel = CopilotLogLevel.Error,
SessionFs = SessionFsConfig,
Connection = RuntimeConnection.ForUri($"localhost:{port}", connectionToken: "session-fs-shared-token"),
});
try
{
await Assert.ThrowsAnyAsync<Exception>(() => client2.StartAsync());
}
finally
{
try
{
await client2.ForceStopAsync();
}
catch (IOException ex)
{
Console.Error.WriteLine($"Ignoring expected teardown IOException from ForceStopAsync: {ex.Message}");
}
finally
{
Ctx.UntrackClient(client2);
}
}
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task Should_Map_All_SessionFs_Handler_Operations()
{
var providerRoot = CreateProviderRoot();
var sessionId = "handler-session";
try
{
Directory.CreateDirectory(providerRoot);
ISessionFsHandler handler = new TestSessionFsHandler(sessionId, providerRoot);
var mkdirError = await handler.MkdirAsync(new SessionFsMkdirRequest
{
SessionId = sessionId,
Path = "/workspace/nested",
Recursive = true,
});
Assert.Null(mkdirError);
var writeError = await handler.WriteFileAsync(new SessionFsWriteFileRequest
{
SessionId = sessionId,
Path = "/workspace/nested/file.txt",
Content = "hello",
});
Assert.Null(writeError);
var appendError = await handler.AppendFileAsync(new SessionFsAppendFileRequest
{
SessionId = sessionId,
Path = "/workspace/nested/file.txt",
Content = " world",
});
Assert.Null(appendError);
var exists = await handler.ExistsAsync(new SessionFsExistsRequest
{
SessionId = sessionId,
Path = "/workspace/nested/file.txt",
});
Assert.True(exists.Exists);
var stat = await handler.StatAsync(new SessionFsStatRequest
{
SessionId = sessionId,
Path = "/workspace/nested/file.txt",
});
Assert.True(stat.IsFile);
Assert.False(stat.IsDirectory);
Assert.Equal("hello world".Length, stat.Size);
Assert.Null(stat.Error);
var content = await handler.ReadFileAsync(new SessionFsReadFileRequest
{
SessionId = sessionId,
Path = "/workspace/nested/file.txt",
});
Assert.Equal("hello world", content.Content);
Assert.Null(content.Error);
var entries = await handler.ReaddirAsync(new SessionFsReaddirRequest
{
SessionId = sessionId,
Path = "/workspace/nested",
});
Assert.Contains("file.txt", entries.Entries);
Assert.Null(entries.Error);
var typedEntries = await handler.ReaddirWithTypesAsync(new SessionFsReaddirWithTypesRequest
{
SessionId = sessionId,
Path = "/workspace/nested",
});
Assert.Contains(
typedEntries.Entries,
entry => entry.Name == "file.txt" && entry.Type == SessionFsReaddirWithTypesEntryType.File);
Assert.Null(typedEntries.Error);
var renameError = await handler.RenameAsync(new SessionFsRenameRequest
{
SessionId = sessionId,
Src = "/workspace/nested/file.txt",
Dest = "/workspace/nested/renamed.txt",
});
Assert.Null(renameError);
var oldPath = await handler.ExistsAsync(new SessionFsExistsRequest
{
SessionId = sessionId,
Path = "/workspace/nested/file.txt",
});
Assert.False(oldPath.Exists);
var renamedPath = await handler.ReadFileAsync(new SessionFsReadFileRequest
{
SessionId = sessionId,
Path = "/workspace/nested/renamed.txt",
});
Assert.Equal("hello world", renamedPath.Content);
var rmError = await handler.RmAsync(new SessionFsRmRequest
{
SessionId = sessionId,
Path = "/workspace/nested/renamed.txt",
});
Assert.Null(rmError);
var removed = await handler.ExistsAsync(new SessionFsExistsRequest
{
SessionId = sessionId,
Path = "/workspace/nested/renamed.txt",
});
Assert.False(removed.Exists);
var forcedRmError = await handler.RmAsync(new SessionFsRmRequest
{
SessionId = sessionId,
Path = "/workspace/nested/missing.txt",
Force = true,
});
Assert.Null(forcedRmError);
var missing = await handler.StatAsync(new SessionFsStatRequest
{
SessionId = sessionId,
Path = "/workspace/nested/missing.txt",
});
Assert.Equal(SessionFsErrorCode.ENOENT, missing.Error?.Code);
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task SessionFsProvider_Converts_Exceptions_To_Rpc_Errors()
{
var handler = (ISessionFsHandler)new ThrowingSessionFsProvider(new FileNotFoundException("missing"));
AssertFsError((await handler.ReadFileAsync(new SessionFsReadFileRequest { Path = "missing.txt" })).Error);
AssertFsError(await handler.WriteFileAsync(new SessionFsWriteFileRequest { Path = "missing.txt", Content = "content" }));
AssertFsError(await handler.AppendFileAsync(new SessionFsAppendFileRequest { Path = "missing.txt", Content = "content" }));
var exists = await handler.ExistsAsync(new SessionFsExistsRequest { Path = "missing.txt" });
Assert.False(exists.Exists);
AssertFsError((await handler.StatAsync(new SessionFsStatRequest { Path = "missing.txt" })).Error);
AssertFsError(await handler.MkdirAsync(new SessionFsMkdirRequest { Path = "missing-dir" }));
AssertFsError((await handler.ReaddirAsync(new SessionFsReaddirRequest { Path = "missing-dir" })).Error);
AssertFsError((await handler.ReaddirWithTypesAsync(new SessionFsReaddirWithTypesRequest { Path = "missing-dir" })).Error);
AssertFsError(await handler.RmAsync(new SessionFsRmRequest { Path = "missing.txt" }));
AssertFsError(await handler.RenameAsync(new SessionFsRenameRequest { Src = "missing.txt", Dest = "dest.txt" }));
AssertFsError((await handler.SqliteQueryAsync(new SessionFsSqliteQueryRequest { Query = "select 1" })).Error);
var sqliteExists = await handler.SqliteExistsAsync(new SessionFsSqliteExistsRequest());
Assert.False(sqliteExists.Exists);
var unknown = (ISessionFsHandler)new ThrowingSessionFsProvider(new InvalidOperationException("bad path"));
var unknownError = await unknown.WriteFileAsync(new SessionFsWriteFileRequest { Path = "bad.txt", Content = "content" });
Assert.Equal(SessionFsErrorCode.UNKNOWN, unknownError!.Code);
static void AssertFsError(SessionFsError? error)
{
Assert.NotNull(error);
Assert.Equal(SessionFsErrorCode.ENOENT, error.Code);
Assert.Contains("missing", error.Message, StringComparison.OrdinalIgnoreCase);
}
}
[Fact]
public async Task Should_Map_Large_Output_Handling_Into_SessionFs()
{
var providerRoot = CreateProviderRoot();
try
{
const int largeContentSize = 100_000;
var suppliedFileContent = new string('x', largeContentSize);
await using var client = CreateSessionFsClient(providerRoot);
var session = await Ctx.CreateSessionAsync(client, new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsProvider = s => new TestSessionFsHandler(s.SessionId, providerRoot),
Tools =
[
AIFunctionFactory.Create(() => suppliedFileContent, "get_big_string", "Returns a large string")
],
});
await session.SendAndWaitAsync(new MessageOptions
{
Prompt = "Call the get_big_string tool and reply with the word DONE only.",
});
var messages = await session.GetEventsAsync();
var toolResult = FindToolCallResult(messages, "get_big_string");
Assert.NotNull(toolResult);
// Windows reports the spilled file path with backslash separators.
toolResult = toolResult!.Replace('\\', '/');
Assert.Contains($"{SessionFsConfig.SessionStatePath}/temp/", toolResult);
var match = System.Text.RegularExpressions.Regex.Match(
toolResult!,
$"({System.Text.RegularExpressions.Regex.Escape(SessionFsConfig.SessionStatePath)}/temp/[^\\s]+)");
Assert.True(match.Success);
var fileContent = await ReadAllTextSharedAsync(GetStoredPath(providerRoot, session.SessionId, match.Groups[1].Value));
Assert.Equal(suppliedFileContent, fileContent);
await session.DisposeAsync();
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task Should_Succeed_With_Compaction_While_Using_SessionFs()
{
var providerRoot = CreateProviderRoot();
try
{
await using var client = CreateSessionFsClient(providerRoot);
var session = await Ctx.CreateSessionAsync(client, new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsProvider = s => new TestSessionFsHandler(s.SessionId, providerRoot),
});
SessionCompactionCompleteEvent? compactionEvent = null;
using var _ = session.On<SessionEvent>(evt =>
{
if (evt is SessionCompactionCompleteEvent complete)
{
compactionEvent = complete;
}
});
await session.SendAndWaitAsync(new MessageOptions { Prompt = "What is 2+2?" });
var eventsPath = GetStoredPath(providerRoot, session.SessionId, $"{SessionFsConfig.SessionStatePath}/events.jsonl");
await WaitForConditionAsync(() => File.Exists(eventsPath), TimeSpan.FromSeconds(30));
var contentBefore = await ReadAllTextSharedAsync(eventsPath);
Assert.DoesNotContain("checkpointNumber", contentBefore);
await session.Rpc.History.CompactAsync();
await WaitForConditionAsync(() => compactionEvent != null, TimeSpan.FromSeconds(30));
Assert.NotNull(compactionEvent);
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task Should_Write_Workspace_Metadata_Via_SessionFs()
{
var providerRoot = CreateProviderRoot();
try
{
await using var client = CreateSessionFsClient(providerRoot);
var session = await Ctx.CreateSessionAsync(client, new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsProvider = s => new TestSessionFsHandler(s.SessionId, providerRoot),
});
var msg = await session.SendAndWaitAsync(new MessageOptions { Prompt = "What is 7 * 8?" });
Assert.Contains("56", msg?.Data.Content ?? string.Empty);
var workspaceYamlPath = GetStoredPath(providerRoot, session.SessionId, $"{SessionFsConfig.SessionStatePath}/workspace.yaml");
await WaitForConditionAsync(
async () => File.Exists(workspaceYamlPath)
&& (await ReadAllTextSharedAsync(workspaceYamlPath)).Contains(session.SessionId),
TimeSpan.FromSeconds(30));
var indexPath = GetStoredPath(providerRoot, session.SessionId, $"{SessionFsConfig.SessionStatePath}/checkpoints/index.md");
await WaitForConditionAsync(() => File.Exists(indexPath), TimeSpan.FromSeconds(30));
await session.DisposeAsync();
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task Should_Persist_Plan_Md_Via_SessionFs()
{
var providerRoot = CreateProviderRoot();
try
{
await using var client = CreateSessionFsClient(providerRoot);
var session = await Ctx.CreateSessionAsync(client, new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsProvider = s => new TestSessionFsHandler(s.SessionId, providerRoot),
});
// Write a plan via the session RPC
await session.SendAndWaitAsync(new MessageOptions { Prompt = "What is 2 + 3?" });
await session.Rpc.Plan.UpdateAsync("# Test Plan\n\nThis is a test.");
var planPath = GetStoredPath(providerRoot, session.SessionId, $"{SessionFsConfig.SessionStatePath}/plan.md");
await WaitForConditionAsync(
async () => File.Exists(planPath)
&& (await ReadAllTextSharedAsync(planPath)).Contains("This is a test."),
TimeSpan.FromSeconds(30));
await session.DisposeAsync();
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
private CopilotClient CreateSessionFsClient(string providerRoot, bool useStdio = true, string? tcpConnectionToken = null)
{
RuntimeConnection connection = useStdio
? RuntimeConnection.ForStdio()
: RuntimeConnection.ForTcp(connectionToken: tcpConnectionToken);
Directory.CreateDirectory(providerRoot);
return Ctx.CreateClient(
options: new CopilotClientOptions
{
SessionFs = SessionFsConfig,
Connection = connection,
});
}
private static string? FindToolCallResult(IReadOnlyList<SessionEvent> messages, string toolName)
{
var callId = messages
.OfType<ToolExecutionStartEvent>()
.FirstOrDefault(m => string.Equals(m.Data.ToolName, toolName, StringComparison.Ordinal))
?.Data.ToolCallId;
if (callId is null)
{
return null;
}
return messages
.OfType<ToolExecutionCompleteEvent>()
.FirstOrDefault(m => string.Equals(m.Data.ToolCallId, callId, StringComparison.Ordinal))
?.Data.Result?.Content;
}
private static string CreateProviderRoot()
=> Path.Join(Path.GetTempPath(), $"copilot-sessionfs-{Guid.NewGuid():N}");
private static string CreateSessionStatePath()
{
if (OperatingSystem.IsWindows())
{
return "/session-state";
}
return Path.Join(Path.GetTempPath(), $"copilot-sessionfs-state-{Guid.NewGuid():N}", "session-state")
.Replace(Path.DirectorySeparatorChar, '/');
}
private static string GetStoredPath(string providerRoot, string sessionId, string sessionPath)
{
var safeSessionId = NormalizeRelativePathSegment(sessionId, nameof(sessionId));
var relativeSegments = sessionPath
.TrimStart('/', '\\')
.Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries)
.Select(segment => NormalizeRelativePathSegment(segment, nameof(sessionPath)))
.ToArray();
return Path.Join([providerRoot, safeSessionId, .. relativeSegments]);
}
private static async Task WaitForConditionAsync(Func<bool> condition, TimeSpan? timeout = null)
{
await TestHelper.WaitForConditionAsync(
condition,
timeout: timeout ?? TimeSpan.FromSeconds(30),
timeoutMessage: "Timed out waiting for the session_fs test condition.");
}
private static async Task WaitForConditionAsync(Func<Task<bool>> condition, TimeSpan? timeout = null)
{
await TestHelper.WaitForConditionAsync(
condition,
timeout: timeout ?? TimeSpan.FromSeconds(30),
timeoutMessage: "Timed out waiting for the session_fs test condition.",
transientExceptionFilter: TestHelper.IsTransientFileSystemException);
}
private static async Task<string> ReadAllTextSharedAsync(string path, CancellationToken cancellationToken = default)
{
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using var reader = new StreamReader(stream);
return await reader.ReadToEndAsync(cancellationToken);
}
private static async Task TryDeleteDirectoryAsync(string path)
{
if (!Directory.Exists(path))
{
return;
}
await TestHelper.WaitForConditionAsync(
() => Task.FromResult(DeleteDirectoryIfPresent(path)),
timeout: TimeSpan.FromSeconds(5),
timeoutMessage: $"Timed out deleting directory '{path}'.",
transientExceptionFilter: TestHelper.IsTransientFileSystemException);
static bool DeleteDirectoryIfPresent(string path)
{
if (!Directory.Exists(path))
{
return true;
}
Directory.Delete(path, recursive: true);
return !Directory.Exists(path);
}
}
private static string NormalizeRelativePathSegment(string segment, string paramName)
{
if (string.IsNullOrWhiteSpace(segment))
{
throw new InvalidOperationException($"{paramName} must not be empty.");
}
var normalized = segment.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
if (Path.IsPathRooted(normalized) || normalized.Contains(Path.VolumeSeparatorChar))
{
throw new InvalidOperationException($"{paramName} must be a relative path segment: {segment}");
}
return normalized;
}
private sealed class ThrowingSessionFsProvider(Exception exception)
: SessionFsProvider, ISessionFsSqliteProvider, ISessionFsSqliteTransactionProvider
{
protected override Task<string> ReadFileAsync(string path, CancellationToken cancellationToken) =>
Task.FromException<string>(exception);
protected override Task WriteFileAsync(string path, string content, int? mode, CancellationToken cancellationToken) =>
Task.FromException(exception);
protected override Task AppendFileAsync(string path, string content, int? mode, CancellationToken cancellationToken) =>
Task.FromException(exception);
protected override Task<bool> ExistsAsync(string path, CancellationToken cancellationToken) =>
Task.FromException<bool>(exception);
protected override Task<SessionFsStatResult> StatAsync(string path, CancellationToken cancellationToken) =>
Task.FromException<SessionFsStatResult>(exception);
protected override Task MakeDirectoryAsync(string path, bool recursive, int? mode, CancellationToken cancellationToken) =>
Task.FromException(exception);
protected override Task<IList<string>> ReadDirectoryAsync(string path, CancellationToken cancellationToken) =>
Task.FromException<IList<string>>(exception);
protected override Task<IList<SessionFsReaddirWithTypesEntry>> ReadDirectoryWithTypesAsync(string path, CancellationToken cancellationToken) =>
Task.FromException<IList<SessionFsReaddirWithTypesEntry>>(exception);
protected override Task RemoveAsync(string path, bool recursive, bool force, CancellationToken cancellationToken) =>
Task.FromException(exception);
protected override Task RenameAsync(string src, string dest, CancellationToken cancellationToken) =>
Task.FromException(exception);
Task<SessionFsSqliteResult?> ISessionFsSqliteProvider.QueryAsync(SessionFsSqliteQueryType queryType, string query, IDictionary<string, object?>? bindParams, CancellationToken cancellationToken) =>
Task.FromException<SessionFsSqliteResult?>(exception);
Task<IList<SessionFsSqliteResult>> ISessionFsSqliteTransactionProvider.TransactionAsync(IList<SessionFsSqliteStatement> statements, CancellationToken cancellationToken) =>
Task.FromException<IList<SessionFsSqliteResult>>(exception);
Task<bool> ISessionFsSqliteProvider.ExistsAsync(CancellationToken cancellationToken) =>
Task.FromException<bool>(exception);
}
private sealed class TestSessionFsHandler(string sessionId, string rootDir) : SessionFsProvider
{
protected override async Task<string> ReadFileAsync(string path, CancellationToken cancellationToken)
{
return await File.ReadAllTextAsync(ResolvePath(path), cancellationToken);
}
protected override async Task WriteFileAsync(string path, string content, int? mode, CancellationToken cancellationToken)
{
var fullPath = ResolvePath(path);
Directory.CreateDirectory(Path.GetDirectoryName(fullPath)!);
await File.WriteAllTextAsync(fullPath, content, cancellationToken);
}
protected override async Task AppendFileAsync(string path, string content, int? mode, CancellationToken cancellationToken)
{
var fullPath = ResolvePath(path);
Directory.CreateDirectory(Path.GetDirectoryName(fullPath)!);
await File.AppendAllTextAsync(fullPath, content, cancellationToken);
}
protected override Task<bool> ExistsAsync(string path, CancellationToken cancellationToken)
{
var fullPath = ResolvePath(path);
return Task.FromResult(File.Exists(fullPath) || Directory.Exists(fullPath));
}
protected override Task<SessionFsStatResult> StatAsync(string path, CancellationToken cancellationToken)
{
var fullPath = ResolvePath(path);
if (File.Exists(fullPath))
{
var info = new FileInfo(fullPath);
return Task.FromResult(new SessionFsStatResult
{
IsFile = true,
IsDirectory = false,
Size = info.Length,
Mtime = info.LastWriteTimeUtc,
Birthtime = info.CreationTimeUtc,
});
}
var dirInfo = new DirectoryInfo(fullPath);
if (!dirInfo.Exists)
{
throw new DirectoryNotFoundException($"Path does not exist: {path}");
}
return Task.FromResult(new SessionFsStatResult
{
IsFile = false,
IsDirectory = true,
Size = 0,
Mtime = dirInfo.LastWriteTimeUtc,
Birthtime = dirInfo.CreationTimeUtc,
});
}
protected override Task MakeDirectoryAsync(string path, bool recursive, int? mode, CancellationToken cancellationToken)
{
Directory.CreateDirectory(ResolvePath(path));
return Task.CompletedTask;
}
protected override Task<IList<string>> ReadDirectoryAsync(string path, CancellationToken cancellationToken)
{
IList<string> entries = Directory
.EnumerateFileSystemEntries(ResolvePath(path))
.Select(Path.GetFileName)
.Where(name => name is not null)
.Cast<string>()
.ToList();
return Task.FromResult(entries);
}
protected override Task<IList<SessionFsReaddirWithTypesEntry>> ReadDirectoryWithTypesAsync(string path, CancellationToken cancellationToken)
{
IList<SessionFsReaddirWithTypesEntry> entries = Directory
.EnumerateFileSystemEntries(ResolvePath(path))
.Select(p => new SessionFsReaddirWithTypesEntry
{
Name = Path.GetFileName(p),
Type = Directory.Exists(p) ? SessionFsReaddirWithTypesEntryType.Directory : SessionFsReaddirWithTypesEntryType.File,
})
.ToList();
return Task.FromResult(entries);
}
protected override Task RemoveAsync(string path, bool recursive, bool force, CancellationToken cancellationToken)
{
var fullPath = ResolvePath(path);
if (File.Exists(fullPath))
{
File.Delete(fullPath);
return Task.CompletedTask;
}
if (Directory.Exists(fullPath))
{
Directory.Delete(fullPath, recursive);
return Task.CompletedTask;
}
if (force)
{
return Task.CompletedTask;
}
throw new FileNotFoundException($"Path does not exist: {path}");
}
protected override Task RenameAsync(string src, string dest, CancellationToken cancellationToken)
{
var srcPath = ResolvePath(src);
var destPath = ResolvePath(dest);
Directory.CreateDirectory(Path.GetDirectoryName(destPath)!);
if (Directory.Exists(srcPath))
{
Directory.Move(srcPath, destPath);
}
else
{
File.Move(srcPath, destPath, overwrite: true);
}
return Task.CompletedTask;
}
private string ResolvePath(string sessionPath)
{
var normalizedSessionId = NormalizeRelativePathSegment(sessionId, nameof(sessionId));
var sessionRoot = Path.GetFullPath(Path.Join(rootDir, normalizedSessionId));
var relativeSegments = sessionPath
.TrimStart('/', '\\')
.Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries)
.Select(segment => NormalizeRelativePathSegment(segment, nameof(sessionPath)))
.ToArray();
var fullPath = Path.GetFullPath(Path.Join([sessionRoot, .. relativeSegments]));
if (!fullPath.StartsWith(sessionRoot, StringComparison.Ordinal))
{
throw new InvalidOperationException($"Path escapes session root: {sessionPath}");
}
return fullPath;
}
}
}