| 1 | using AgentForge.Abstractions; |
| 2 | |
| 3 | namespace AgentForge.Tests; |
| 4 | |
| 5 | public sealed class ForgeImportRegistryTests |
| 6 | { |
| 7 | private sealed class StubRepoImporter(string sourceId, ForgeRepositoryImportScope scope, string pluginId) |
| 8 | : IForgeRepositoryImporter |
| 9 | { |
| 10 | public string SourceId { get; } = sourceId; |
| 11 | public ForgeRepositoryImportScope Scope { get; } = scope; |
| 12 | public string PluginId { get; } = pluginId; |
| 13 | |
| 14 | public Task<ForgeRepositoryImportResponse> ImportAsync( |
| 15 | ForgeRepositoryImportRequest request, |
| 16 | CancellationToken cancellationToken = default) => |
| 17 | Task.FromResult(new ForgeRepositoryImportResponse("o", "e", 0, 0, 0, 0, [])); |
| 18 | } |
| 19 | |
| 20 | [Fact] |
| 21 | public void Repository_registry_resolves_by_source_and_scope() |
| 22 | { |
| 23 | var registry = new ForgeRepositoryImporterRegistry([ |
| 24 | new StubRepoImporter("github", ForgeRepositoryImportScope.Organization, "repository-import-org-github"), |
| 25 | new StubRepoImporter("github", ForgeRepositoryImportScope.User, "repository-import-user-github"), |
| 26 | ]); |
| 27 | |
| 28 | Assert.True(registry.TryGet("github", ForgeRepositoryImportScope.Organization, out var org)); |
| 29 | Assert.Equal("repository-import-org-github", org!.PluginId); |
| 30 | Assert.False(registry.TryGet("github", ForgeRepositoryImportScope.Single, out _)); |
| 31 | } |
| 32 | |
| 33 | [Fact] |
| 34 | public void Repository_registry_throws_on_duplicate_key() |
| 35 | { |
| 36 | Assert.Throws<InvalidOperationException>(() => new ForgeRepositoryImporterRegistry([ |
| 37 | new StubRepoImporter("github", ForgeRepositoryImportScope.Organization, "a"), |
| 38 | new StubRepoImporter("github", ForgeRepositoryImportScope.Organization, "b"), |
| 39 | ])); |
| 40 | } |
| 41 | |
| 42 | private sealed class StubOrgImporter(string sourceId, string pluginId) : IForgeOrgImporter |
| 43 | { |
| 44 | public string SourceId { get; } = sourceId; |
| 45 | public string PluginId { get; } = pluginId; |
| 46 | |
| 47 | public Task<ForgeOrgImportResponse> ImportMembersAsync( |
| 48 | ForgeOrgImportRequest request, |
| 49 | CancellationToken cancellationToken = default) => |
| 50 | Task.FromResult(new ForgeOrgImportResponse("o", "e", 0, 0, 0, 0, [])); |
| 51 | } |
| 52 | |
| 53 | [Fact] |
| 54 | public void Org_registry_resolves_by_source() |
| 55 | { |
| 56 | var registry = new ForgeOrgImporterRegistry([ |
| 57 | new StubOrgImporter("github", "org-import-github"), |
| 58 | ]); |
| 59 | |
| 60 | Assert.True(registry.TryGet("github", out var importer)); |
| 61 | Assert.Equal("org-import-github", importer!.PluginId); |
| 62 | Assert.False(registry.TryGet("gitlab", out _)); |
| 63 | } |
| 64 | } |
| 65 | |