| 1 | using AgentForge.Services; |
| 2 | using LibGit2Sharp; |
| 3 | using MsOptions = Microsoft.Extensions.Options; |
| 4 | |
| 5 | namespace AgentForge.Tests; |
| 6 | |
| 7 | public sealed class ForgeGitTransportTests(ForgeWebApplicationFactory factory) : IClassFixture<ForgeWebApplicationFactory> |
| 8 | { |
| 9 | [Fact] |
| 10 | public void BuildCloneUrl_uses_scp_style_for_ssh_base() |
| 11 | { |
| 12 | var git = new GitRepoService(MsOptions.Options.Create(new AgentForge.Options.ForgeOptions |
| 13 | { |
| 14 | GitBaseUrl = "ssh://git@test.local", |
| 15 | GitSshPort = 2222, |
| 16 | })); |
| 17 | Assert.Equal("git@test.local:cad-tools.git", git.BuildCloneUrl("cad-tools")); |
| 18 | } |
| 19 | |
| 20 | [Fact] |
| 21 | public void Bare_repo_commit_and_clone_roundtrip() |
| 22 | { |
| 23 | const string slug = "transport-pilot"; |
| 24 | factory.Git.CreateBareRepository(slug); |
| 25 | factory.Git.CreateCommitOnBranch( |
| 26 | slug, |
| 27 | branch: "main", |
| 28 | filePath: "README.md", |
| 29 | content: "# pilot\n", |
| 30 | message: "seed"); |
| 31 | |
| 32 | var barePath = factory.Git.GetBareRepoPath(slug); |
| 33 | using (var bare = new Repository(barePath)) |
| 34 | { |
| 35 | var main = bare.Branches["main"]; |
| 36 | Assert.NotNull(main?.Tip); |
| 37 | var entry = main!.Tip!.Tree["README.md"]; |
| 38 | Assert.NotNull(entry); |
| 39 | var blob = entry!.Target as Blob; |
| 40 | Assert.NotNull(blob); |
| 41 | Assert.Contains("pilot", blob!.GetContentText()); |
| 42 | } |
| 43 | |
| 44 | var workDir = Path.Combine(Path.GetTempPath(), $"forge-clone-{Guid.NewGuid():N}"); |
| 45 | try |
| 46 | { |
| 47 | var fileUri = new Uri(barePath).AbsoluteUri; |
| 48 | Repository.Clone(fileUri, workDir, new CloneOptions { BranchName = "main" }); |
| 49 | Assert.Contains("pilot", File.ReadAllText(Path.Combine(workDir, "README.md"))); |
| 50 | } |
| 51 | finally |
| 52 | { |
| 53 | try |
| 54 | { |
| 55 | if (Directory.Exists(workDir)) |
| 56 | Directory.Delete(workDir, recursive: true); |
| 57 | } |
| 58 | catch |
| 59 | { |
| 60 | // Windows may lock .git briefly. |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |