|
| 1 | +using OpenDeepWiki.Services.Repositories; |
| 2 | +using Xunit; |
| 3 | + |
| 4 | +namespace OpenDeepWiki.Tests.Services.Repositories; |
| 5 | + |
| 6 | +public class RepositoryWorkspacePathTests |
| 7 | +{ |
| 8 | + [Fact] |
| 9 | + public void ForBranch_ShouldUseBranchScopedLayout() |
| 10 | + { |
| 11 | + var options = CreateOptions(CreateTempDirectory()); |
| 12 | + |
| 13 | + var path = RepositoryWorkspacePath.ForBranch(options, "acme", "widgets", "main"); |
| 14 | + |
| 15 | + Assert.Equal( |
| 16 | + Path.Combine(options.RepositoriesDirectory, "acme", "widgets", "branches", "main", "tree"), |
| 17 | + path); |
| 18 | + } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void ForBranch_ShouldStripPathTraversalFromComponents() |
| 22 | + { |
| 23 | + var options = CreateOptions(CreateTempDirectory()); |
| 24 | + |
| 25 | + var path = RepositoryWorkspacePath.ForBranch(options, "../etc", "wid/gets", @"feature\x"); |
| 26 | + |
| 27 | + // Separators become underscores first, so "../etc" ends up as "__etc". |
| 28 | + Assert.Equal( |
| 29 | + Path.Combine(options.RepositoriesDirectory, "__etc", "wid_gets", "branches", "feature_x", "tree"), |
| 30 | + path); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void Resolve_ShouldReturnRequestedBranchWorkspace() |
| 35 | + { |
| 36 | + var repositoriesRoot = CreateTempDirectory(); |
| 37 | + var options = CreateOptions(repositoriesRoot); |
| 38 | + var expected = CreateWorkspace(repositoriesRoot, "acme", "widgets", "main"); |
| 39 | + CreateWorkspace(repositoriesRoot, "acme", "widgets", "develop"); |
| 40 | + |
| 41 | + Assert.Equal(expected, RepositoryWorkspacePath.Resolve(options, "acme", "widgets", "main")); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void Resolve_WithoutBranch_ShouldReturnMostRecentlyWrittenBranchWorkspace() |
| 46 | + { |
| 47 | + var repositoriesRoot = CreateTempDirectory(); |
| 48 | + var options = CreateOptions(repositoriesRoot); |
| 49 | + var stale = CreateWorkspace(repositoriesRoot, "acme", "widgets", "develop"); |
| 50 | + var recent = CreateWorkspace(repositoriesRoot, "acme", "widgets", "main"); |
| 51 | + Directory.SetLastWriteTimeUtc(stale, DateTime.UtcNow.AddDays(-1)); |
| 52 | + Directory.SetLastWriteTimeUtc(recent, DateTime.UtcNow); |
| 53 | + |
| 54 | + Assert.Equal(recent, RepositoryWorkspacePath.Resolve(options, "acme", "widgets", branchName: null)); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Resolve_ShouldFallBackToLegacyFlatWorkspace() |
| 59 | + { |
| 60 | + var repositoriesRoot = CreateTempDirectory(); |
| 61 | + var options = CreateOptions(repositoriesRoot); |
| 62 | + var legacy = Path.Combine(repositoriesRoot, "acme", "widgets", "tree"); |
| 63 | + Directory.CreateDirectory(legacy); |
| 64 | + |
| 65 | + Assert.Equal(legacy, RepositoryWorkspacePath.Resolve(options, "acme", "widgets", "main")); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void Resolve_WhenNothingExists_ShouldReturnBranchWorkspacePath() |
| 70 | + { |
| 71 | + var options = CreateOptions(CreateTempDirectory()); |
| 72 | + |
| 73 | + var path = RepositoryWorkspacePath.Resolve(options, "acme", "widgets", "main"); |
| 74 | + |
| 75 | + Assert.Equal(RepositoryWorkspacePath.ForBranch(options, "acme", "widgets", "main"), path); |
| 76 | + Assert.False(Directory.Exists(path)); |
| 77 | + } |
| 78 | + |
| 79 | + private static RepositoryAnalyzerOptions CreateOptions(string repositoriesRoot) |
| 80 | + { |
| 81 | + return new RepositoryAnalyzerOptions { RepositoriesDirectory = repositoriesRoot }; |
| 82 | + } |
| 83 | + |
| 84 | + private static string CreateWorkspace(string repositoriesRoot, string organization, string repository, string branch) |
| 85 | + { |
| 86 | + var path = Path.Combine(repositoriesRoot, organization, repository, "branches", branch, "tree"); |
| 87 | + Directory.CreateDirectory(path); |
| 88 | + return path; |
| 89 | + } |
| 90 | + |
| 91 | + private static string CreateTempDirectory() |
| 92 | + { |
| 93 | + var path = Path.Combine(Path.GetTempPath(), "OpenDeepWiki.Tests", Guid.NewGuid().ToString("N")); |
| 94 | + Directory.CreateDirectory(path); |
| 95 | + return path; |
| 96 | + } |
| 97 | +} |
0 commit comments