|
| 1 | +import { test, expect } from "../fixtures/caldera-auth"; |
| 2 | + |
| 3 | +test.describe("Emu plugin — error states and edge cases", () => { |
| 4 | + test("should handle abilities API failure gracefully", async ({ |
| 5 | + authenticatedPage: page, |
| 6 | + }) => { |
| 7 | + // Intercept abilities API and return error |
| 8 | + await page.route("**/api/v2/abilities", (route) => |
| 9 | + route.fulfill({ |
| 10 | + status: 500, |
| 11 | + contentType: "application/json", |
| 12 | + body: JSON.stringify({ error: "Internal Server Error" }), |
| 13 | + }) |
| 14 | + ); |
| 15 | + |
| 16 | + await page.goto("/#/plugins/emu"); |
| 17 | + await page.waitForLoadState("networkidle"); |
| 18 | + |
| 19 | + // The page should still render the heading |
| 20 | + const heading = page.locator("h2:has-text('Emu')"); |
| 21 | + await expect(heading).toBeVisible({ timeout: 15_000 }); |
| 22 | + |
| 23 | + // Abilities count should show placeholder "---" since API failed |
| 24 | + const abilitiesCount = page.locator("h1.is-size-1").first(); |
| 25 | + await expect(abilitiesCount).toBeVisible({ timeout: 10_000 }); |
| 26 | + const text = await abilitiesCount.textContent(); |
| 27 | + expect(text?.trim()).toBe("---"); |
| 28 | + }); |
| 29 | + |
| 30 | + test("should handle adversaries API failure gracefully", async ({ |
| 31 | + authenticatedPage: page, |
| 32 | + }) => { |
| 33 | + // Intercept adversaries API and return error |
| 34 | + await page.route("**/api/v2/adversaries", (route) => |
| 35 | + route.fulfill({ |
| 36 | + status: 500, |
| 37 | + contentType: "application/json", |
| 38 | + body: JSON.stringify({ error: "Internal Server Error" }), |
| 39 | + }) |
| 40 | + ); |
| 41 | + |
| 42 | + await page.goto("/#/plugins/emu"); |
| 43 | + await page.waitForLoadState("networkidle"); |
| 44 | + |
| 45 | + const heading = page.locator("h2:has-text('Emu')"); |
| 46 | + await expect(heading).toBeVisible({ timeout: 15_000 }); |
| 47 | + |
| 48 | + // Adversaries count should show placeholder |
| 49 | + const adversariesCount = page.locator("h1.is-size-1").nth(1); |
| 50 | + await expect(adversariesCount).toBeVisible({ timeout: 10_000 }); |
| 51 | + const text = await adversariesCount.textContent(); |
| 52 | + expect(text?.trim()).toBe("---"); |
| 53 | + }); |
| 54 | + |
| 55 | + test("should handle network timeout for abilities API gracefully", async ({ |
| 56 | + authenticatedPage: page, |
| 57 | + }) => { |
| 58 | + await page.route("**/api/v2/abilities", (route) => route.abort()); |
| 59 | + |
| 60 | + await page.goto("/#/plugins/emu"); |
| 61 | + await page.waitForLoadState("domcontentloaded"); |
| 62 | + |
| 63 | + // Page should still render |
| 64 | + const heading = page.locator("h2:has-text('Emu')"); |
| 65 | + await expect(heading).toBeVisible({ timeout: 15_000 }); |
| 66 | + }); |
| 67 | + |
| 68 | + test("should handle network timeout for adversaries API gracefully", async ({ |
| 69 | + authenticatedPage: page, |
| 70 | + }) => { |
| 71 | + await page.route("**/api/v2/adversaries", (route) => route.abort()); |
| 72 | + |
| 73 | + await page.goto("/#/plugins/emu"); |
| 74 | + await page.waitForLoadState("domcontentloaded"); |
| 75 | + |
| 76 | + const heading = page.locator("h2:has-text('Emu')"); |
| 77 | + await expect(heading).toBeVisible({ timeout: 15_000 }); |
| 78 | + }); |
| 79 | + |
| 80 | + test("should handle both APIs failing simultaneously", async ({ |
| 81 | + authenticatedPage: page, |
| 82 | + }) => { |
| 83 | + await page.route("**/api/v2/abilities", (route) => |
| 84 | + route.fulfill({ |
| 85 | + status: 500, |
| 86 | + body: "error", |
| 87 | + }) |
| 88 | + ); |
| 89 | + await page.route("**/api/v2/adversaries", (route) => |
| 90 | + route.fulfill({ |
| 91 | + status: 500, |
| 92 | + body: "error", |
| 93 | + }) |
| 94 | + ); |
| 95 | + |
| 96 | + await page.goto("/#/plugins/emu"); |
| 97 | + await page.waitForLoadState("domcontentloaded"); |
| 98 | + |
| 99 | + // Page should render without crashing |
| 100 | + const heading = page.locator("h2:has-text('Emu')"); |
| 101 | + await expect(heading).toBeVisible({ timeout: 15_000 }); |
| 102 | + |
| 103 | + // Both counts should show placeholder |
| 104 | + const counts = page.locator("h1.is-size-1"); |
| 105 | + const count = await counts.count(); |
| 106 | + expect(count).toBeGreaterThanOrEqual(2); |
| 107 | + }); |
| 108 | + |
| 109 | + test("should display abilities count as 0 when API returns empty array", async ({ |
| 110 | + authenticatedPage: page, |
| 111 | + }) => { |
| 112 | + // Return empty arrays (no emu abilities) |
| 113 | + await page.route("**/api/v2/abilities", (route) => |
| 114 | + route.fulfill({ |
| 115 | + status: 200, |
| 116 | + contentType: "application/json", |
| 117 | + body: JSON.stringify([]), |
| 118 | + }) |
| 119 | + ); |
| 120 | + await page.route("**/api/v2/adversaries", (route) => |
| 121 | + route.fulfill({ |
| 122 | + status: 200, |
| 123 | + contentType: "application/json", |
| 124 | + body: JSON.stringify([]), |
| 125 | + }) |
| 126 | + ); |
| 127 | + |
| 128 | + await page.goto("/#/plugins/emu"); |
| 129 | + await page.waitForLoadState("networkidle"); |
| 130 | + |
| 131 | + const heading = page.locator("h2:has-text('Emu')"); |
| 132 | + await expect(heading).toBeVisible({ timeout: 15_000 }); |
| 133 | + |
| 134 | + // With empty arrays filtered for emu, counts should show "---" (falsy 0) |
| 135 | + const abilitiesCount = page.locator("h1.is-size-1").first(); |
| 136 | + await expect(abilitiesCount).toBeVisible({ timeout: 10_000 }); |
| 137 | + const text = await abilitiesCount.textContent(); |
| 138 | + // 0 is falsy so the template shows "---" |
| 139 | + expect(text?.trim()).toMatch(/^(0|---)$/); |
| 140 | + }); |
| 141 | + |
| 142 | + test("should not crash when API returns non-emu abilities only", async ({ |
| 143 | + authenticatedPage: page, |
| 144 | + }) => { |
| 145 | + // Return abilities that belong to a different plugin |
| 146 | + await page.route("**/api/v2/abilities", (route) => |
| 147 | + route.fulfill({ |
| 148 | + status: 200, |
| 149 | + contentType: "application/json", |
| 150 | + body: JSON.stringify([ |
| 151 | + { |
| 152 | + ability_id: "test-1", |
| 153 | + name: "Test Ability", |
| 154 | + plugin: "stockpile", |
| 155 | + }, |
| 156 | + ]), |
| 157 | + }) |
| 158 | + ); |
| 159 | + await page.route("**/api/v2/adversaries", (route) => |
| 160 | + route.fulfill({ |
| 161 | + status: 200, |
| 162 | + contentType: "application/json", |
| 163 | + body: JSON.stringify([ |
| 164 | + { |
| 165 | + adversary_id: "test-1", |
| 166 | + name: "Test Adversary", |
| 167 | + plugin: "stockpile", |
| 168 | + }, |
| 169 | + ]), |
| 170 | + }) |
| 171 | + ); |
| 172 | + |
| 173 | + await page.goto("/#/plugins/emu"); |
| 174 | + await page.waitForLoadState("networkidle"); |
| 175 | + |
| 176 | + const heading = page.locator("h2:has-text('Emu')"); |
| 177 | + await expect(heading).toBeVisible({ timeout: 15_000 }); |
| 178 | + |
| 179 | + // Emu-filtered counts should be 0 (shown as "---") |
| 180 | + const abilitiesCount = page.locator("h1.is-size-1").first(); |
| 181 | + await expect(abilitiesCount).toBeVisible({ timeout: 10_000 }); |
| 182 | + const text = await abilitiesCount.textContent(); |
| 183 | + expect(text?.trim()).toMatch(/^(0|---)$/); |
| 184 | + }); |
| 185 | +}); |
0 commit comments