|
| 1 | +// @ts-check |
| 2 | +const { test, expect } = require("@playwright/test"); |
| 3 | +const { login } = require("../helpers/auth"); |
| 4 | +const { navigateToAtomic } = require("../helpers/navigation"); |
| 5 | + |
| 6 | +test.describe("Atomic plugin - error states", () => { |
| 7 | + test.beforeEach(async ({ page }) => { |
| 8 | + await login(page); |
| 9 | + }); |
| 10 | + |
| 11 | + test("should show placeholder count when abilities API fails", async ({ page }) => { |
| 12 | + // Intercept abilities API to simulate failure |
| 13 | + await page.route("**/api/v2/abilities", (route) => { |
| 14 | + route.fulfill({ |
| 15 | + status: 500, |
| 16 | + contentType: "application/json", |
| 17 | + body: JSON.stringify({ error: "Internal Server Error" }), |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + await navigateToAtomic(page); |
| 22 | + |
| 23 | + // The count should show "---" since no abilities loaded |
| 24 | + const countText = page.locator(".is-size-1, h1.is-size-1").first(); |
| 25 | + await expect(countText).toBeVisible({ timeout: 15_000 }); |
| 26 | + const text = await countText.textContent(); |
| 27 | + expect(text?.trim()).toBe("---"); |
| 28 | + }); |
| 29 | + |
| 30 | + test("page should remain functional when abilities API returns empty array", async ({ page }) => { |
| 31 | + await page.route("**/api/v2/abilities", (route) => { |
| 32 | + route.fulfill({ |
| 33 | + status: 200, |
| 34 | + contentType: "application/json", |
| 35 | + body: JSON.stringify([]), |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + await navigateToAtomic(page); |
| 40 | + |
| 41 | + // Count should show "---" for zero abilities |
| 42 | + const countText = page.locator(".is-size-1, h1.is-size-1").first(); |
| 43 | + await expect(countText).toBeVisible({ timeout: 15_000 }); |
| 44 | + const text = await countText.textContent(); |
| 45 | + expect(text?.trim()).toBe("---"); |
| 46 | + |
| 47 | + // Page heading should still be visible |
| 48 | + await expect(page.locator("h2:has-text('Atomic')").first()).toBeVisible(); |
| 49 | + }); |
| 50 | + |
| 51 | + test("page should remain functional when adversaries API fails", async ({ page }) => { |
| 52 | + await page.route("**/api/v2/adversaries", (route) => { |
| 53 | + route.fulfill({ |
| 54 | + status: 500, |
| 55 | + contentType: "application/json", |
| 56 | + body: JSON.stringify({ error: "Internal Server Error" }), |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + await navigateToAtomic(page); |
| 61 | + |
| 62 | + // The page heading and description should still render |
| 63 | + await expect(page.locator("h2:has-text('Atomic')").first()).toBeVisible(); |
| 64 | + await expect(page.locator("p:has-text('Red Canary Atomic')").first()).toBeVisible(); |
| 65 | + }); |
| 66 | + |
| 67 | + test("View Abilities button should still be present when no abilities loaded", async ({ page }) => { |
| 68 | + await page.route("**/api/v2/abilities", (route) => { |
| 69 | + route.fulfill({ |
| 70 | + status: 200, |
| 71 | + contentType: "application/json", |
| 72 | + body: JSON.stringify([]), |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + await navigateToAtomic(page); |
| 77 | + |
| 78 | + const viewBtn = page.locator( |
| 79 | + 'a:has-text("View Abilities"), .button:has-text("View Abilities")' |
| 80 | + ).first(); |
| 81 | + await expect(viewBtn).toBeVisible({ timeout: 15_000 }); |
| 82 | + }); |
| 83 | + |
| 84 | + test("page should handle slow API responses gracefully", async ({ page }) => { |
| 85 | + // Simulate slow response |
| 86 | + await page.route("**/api/v2/abilities", async (route) => { |
| 87 | + await new Promise((r) => setTimeout(r, 5_000)); |
| 88 | + route.fulfill({ |
| 89 | + status: 200, |
| 90 | + contentType: "application/json", |
| 91 | + body: JSON.stringify([]), |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + await navigateToAtomic(page); |
| 96 | + |
| 97 | + // While loading, the count should show "---" |
| 98 | + const countText = page.locator(".is-size-1, h1.is-size-1").first(); |
| 99 | + await expect(countText).toBeVisible({ timeout: 15_000 }); |
| 100 | + const initialText = await countText.textContent(); |
| 101 | + expect(initialText?.trim()).toBe("---"); |
| 102 | + |
| 103 | + // Heading should be visible during loading |
| 104 | + await expect(page.locator("h2:has-text('Atomic')").first()).toBeVisible(); |
| 105 | + }); |
| 106 | + |
| 107 | + test("page should not crash with malformed API response", async ({ page }) => { |
| 108 | + await page.route("**/api/v2/abilities", (route) => { |
| 109 | + route.fulfill({ |
| 110 | + status: 200, |
| 111 | + contentType: "application/json", |
| 112 | + body: "not valid json", |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + await navigateToAtomic(page); |
| 117 | + |
| 118 | + // Page should still show the heading even if parsing fails |
| 119 | + await expect(page.locator("h2:has-text('Atomic')").first()).toBeVisible({ timeout: 15_000 }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments