| 1 | using System.Net.Http.Headers; |
| 2 | using System.Net.Http.Json; |
| 3 | using System.Text.Json; |
| 4 | using AgentForge.Services; |
| 5 | |
| 6 | namespace AgentForge.Tests; |
| 7 | |
| 8 | [Collection("ForgeEnvironment")] |
| 9 | public sealed class ForgePluginReloadTests |
| 10 | { |
| 11 | private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; |
| 12 | |
| 13 | [Fact] |
| 14 | public async Task Plugin_status_lists_bundle_and_plugins() |
| 15 | { |
| 16 | using var factory = CreateFactory(); |
| 17 | using var client = factory.CreateClient(); |
| 18 | |
| 19 | var response = await client.GetAsync("/api/v1/admin/plugins/status"); |
| 20 | response.EnsureSuccessStatusCode(); |
| 21 | var json = await response.Content.ReadFromJsonAsync<JsonElement>(JsonOptions); |
| 22 | Assert.Equal("standard", json.GetProperty("bundle").GetString()); |
| 23 | Assert.True(json.GetProperty("plugins").GetArrayLength() >= 3); |
| 24 | } |
| 25 | |
| 26 | [Fact] |
| 27 | public async Task Plugin_reload_restart_schedules_stop() |
| 28 | { |
| 29 | Environment.SetEnvironmentVariable("FORGE_BOOTSTRAP_TOKEN", "bootstrap-reload"); |
| 30 | try |
| 31 | { |
| 32 | var lifetime = new TestForgeHostLifetime(); |
| 33 | using var factory = CreateFactory(lifetime); |
| 34 | using var client = factory.CreateClient(); |
| 35 | |
| 36 | using var request = new HttpRequestMessage(HttpMethod.Post, "/api/v1/admin/plugins/reload?mode=restart"); |
| 37 | request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "bootstrap-reload"); |
| 38 | |
| 39 | var response = await client.SendAsync(request); |
| 40 | Assert.Equal(System.Net.HttpStatusCode.Accepted, response.StatusCode); |
| 41 | Assert.Equal(TimeSpan.FromSeconds(3), response.Headers.RetryAfter?.Delta); |
| 42 | |
| 43 | var body = await response.Content.ReadFromJsonAsync<JsonElement>(JsonOptions); |
| 44 | Assert.True(body.GetProperty("restartScheduled").GetBoolean()); |
| 45 | Assert.Equal("restart", body.GetProperty("mode").GetString()); |
| 46 | |
| 47 | await Task.Delay(400); |
| 48 | Assert.Equal(1, lifetime.StopCount); |
| 49 | } |
| 50 | finally |
| 51 | { |
| 52 | Environment.SetEnvironmentVariable("FORGE_BOOTSTRAP_TOKEN", null); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | [Fact] |
| 57 | public async Task Plugin_reload_hot_returns_ok_without_restart() |
| 58 | { |
| 59 | Environment.SetEnvironmentVariable("FORGE_BOOTSTRAP_TOKEN", "bootstrap-hot"); |
| 60 | try |
| 61 | { |
| 62 | var lifetime = new TestForgeHostLifetime(); |
| 63 | using var factory = CreateFactory(lifetime); |
| 64 | using var client = factory.CreateClient(); |
| 65 | |
| 66 | using var request = new HttpRequestMessage(HttpMethod.Post, "/api/v1/admin/plugins/reload?mode=hot"); |
| 67 | request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "bootstrap-hot"); |
| 68 | |
| 69 | var response = await client.SendAsync(request); |
| 70 | response.EnsureSuccessStatusCode(); |
| 71 | var body = await response.Content.ReadFromJsonAsync<JsonElement>(JsonOptions); |
| 72 | Assert.Equal("hot", body.GetProperty("mode").GetString()); |
| 73 | Assert.False(body.GetProperty("restartScheduled").GetBoolean()); |
| 74 | Assert.True(body.GetProperty("endpointsStale").GetBoolean()); |
| 75 | Assert.Equal(0, lifetime.StopCount); |
| 76 | } |
| 77 | finally |
| 78 | { |
| 79 | Environment.SetEnvironmentVariable("FORGE_BOOTSTRAP_TOKEN", null); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private static ForgeWebApplicationFactory CreateFactory(TestForgeHostLifetime? lifetime = null) => |
| 84 | new() |
| 85 | { |
| 86 | HostLifetime = lifetime ?? new TestForgeHostLifetime(), |
| 87 | }; |
| 88 | } |
| 89 | |