DamagedConfigTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Microsoft.Playwright;
  2. using Tests.E2e.Infra;
  3. using Xunit.Abstractions;
  4. namespace Tests.E2e;
  5. /// <summary>
  6. /// Web half of https://github.com/Timmoth/RackPeek/issues/337. A config that
  7. /// exists but cannot be read must not leave the app stuck on "Loading…" — the
  8. /// YAML editor is how someone repairs it — and must never be reported as an
  9. /// empty inventory. These tests stage a damaged config in the container, then
  10. /// repair it through the UI.
  11. /// </summary>
  12. public class DamagedConfigTests(
  13. PlaywrightFixture fixture,
  14. ITestOutputHelper output) : E2ETestBase(fixture, output) {
  15. private readonly PlaywrightFixture _fixture = fixture;
  16. private readonly ITestOutputHelper _output = output;
  17. // Cut mid-token, exactly as an interrupted in-place write would leave it.
  18. private const string _damaged = """
  19. version: 4
  20. resources:
  21. - kind: Server
  22. name: srv-a
  23. - ki
  24. """;
  25. private const string _healthy = """
  26. version: 4
  27. resources:
  28. - kind: Server
  29. name: repaired-srv
  30. connections: []
  31. """;
  32. [Fact]
  33. public async Task The_App_Still_Loads_And_Can_Repair_A_Damaged_Config() {
  34. (IBrowserContext context, IPage page) = await CreatePageAsync();
  35. try {
  36. await _fixture.WriteConfigAsync(_damaged);
  37. // 1. The app renders rather than hanging on "Loading…".
  38. await page.GotoAsync($"{_fixture.BaseUrl}/yaml");
  39. await Assertions.Expect(page.GetByTestId("circuit-probe"))
  40. .ToHaveAttributeAsync("data-circuit-ready", "true");
  41. // 2. The editor shows the damaged file, so it can be fixed in place.
  42. ILocator content = page.GetByTestId("yaml-file-content");
  43. await Assertions.Expect(content).ToBeVisibleAsync();
  44. await Assertions.Expect(content).ToContainTextAsync("srv-a");
  45. // 3. Repair it through the editor.
  46. await page.GetByRole(AriaRole.Button, new() { Name = "Edit" }).ClickAsync();
  47. ILocator textarea = page.Locator("textarea");
  48. await Assertions.Expect(textarea).ToBeVisibleAsync();
  49. await textarea.FillAsync(_healthy);
  50. await page.GetByRole(AriaRole.Button, new() { Name = "Save" }).ClickAsync();
  51. await Assertions.Expect(page.GetByTestId("yaml-file-error")).ToHaveCountAsync(0);
  52. // 4. The inventory reads correctly again.
  53. await page.GotoAsync($"{_fixture.BaseUrl}/servers/list");
  54. await Assertions.Expect(page.GetByText("repaired-srv").First).ToBeVisibleAsync();
  55. Assert.Contains("repaired-srv", await _fixture.ReadConfigAsync());
  56. }
  57. catch (Exception) {
  58. _output.WriteLine($"TEST FAILED — URL: {page.Url}");
  59. _output.WriteLine(await page.ContentAsync());
  60. throw;
  61. }
  62. finally {
  63. // Leave the container usable for any other test in this class.
  64. await _fixture.WriteConfigAsync(_healthy);
  65. await context.CloseAsync();
  66. }
  67. }
  68. [Fact]
  69. public async Task A_Damaged_Config_Is_Never_Reported_As_An_Empty_Inventory() {
  70. (IBrowserContext context, IPage page) = await CreatePageAsync();
  71. try {
  72. await _fixture.WriteConfigAsync(_damaged);
  73. await page.GotoAsync($"{_fixture.BaseUrl}/servers/list");
  74. // The inventory pages read through the collection, which now refuses a
  75. // config it could not parse. What must never happen is the page
  76. // rendering a confident, empty list over a recoverable file.
  77. var body = await page.InnerTextAsync("body");
  78. Assert.DoesNotContain("srv-a", body);
  79. Assert.DoesNotContain("No servers", body, StringComparison.OrdinalIgnoreCase);
  80. // And the damaged file is still on disk, untouched by the failed read.
  81. Assert.Equal(_damaged, (await _fixture.ReadConfigAsync()).TrimEnd('\n'));
  82. }
  83. catch (Exception) {
  84. _output.WriteLine($"TEST FAILED — URL: {page.Url}");
  85. _output.WriteLine(await page.ContentAsync());
  86. throw;
  87. }
  88. finally {
  89. await _fixture.WriteConfigAsync(_healthy);
  90. await context.CloseAsync();
  91. }
  92. }
  93. }