|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +test.describe("Map loading", () => { |
| 5 | + test.beforeEach(async ({ context, page }) => { |
| 6 | + await context.clearCookies(); |
| 7 | + |
| 8 | + await page.goto("/"); |
| 9 | + await page.evaluate(() => { |
| 10 | + localStorage.clear(); |
| 11 | + sessionStorage.clear(); |
| 12 | + }); |
| 13 | + |
| 14 | + // Wait for the hidden file input to be available |
| 15 | + await page.waitForSelector("#mapToLoad", { state: "attached" }); |
| 16 | + }); |
| 17 | + |
| 18 | + test("should load a saved map file", async ({ page }) => { |
| 19 | + // Track errors during map loading |
| 20 | + const errors: string[] = []; |
| 21 | + page.on("pageerror", (error) => errors.push(`pageerror: ${error.message}`)); |
| 22 | + page.on("console", (msg) => { |
| 23 | + if (msg.type() === "error") { |
| 24 | + errors.push(`console.error: ${msg.text()}`); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + // Get the file input element and upload the map file |
| 29 | + const fileInput = page.locator("#mapToLoad"); |
| 30 | + const mapFilePath = path.join(__dirname, "../fixtures/demo.map"); |
| 31 | + await fileInput.setInputFiles(mapFilePath); |
| 32 | + |
| 33 | + // Wait for map to be fully loaded |
| 34 | + // mapId is set at the very end of map loading in showStatistics() |
| 35 | + await page.waitForFunction(() => (window as any).mapId !== undefined, { |
| 36 | + timeout: 120000, |
| 37 | + }); |
| 38 | + |
| 39 | + // Additional wait for rendering to settle |
| 40 | + await page.waitForTimeout(500); |
| 41 | + |
| 42 | + // Verify map data is loaded |
| 43 | + const mapData = await page.evaluate(() => { |
| 44 | + const pack = (window as any).pack; |
| 45 | + return { |
| 46 | + hasStates: pack.states && pack.states.length > 1, |
| 47 | + hasBurgs: pack.burgs && pack.burgs.length > 1, |
| 48 | + hasCells: pack.cells && pack.cells.i && pack.cells.i.length > 0, |
| 49 | + hasRivers: pack.rivers && pack.rivers.length > 0, |
| 50 | + mapId: (window as any).mapId, |
| 51 | + }; |
| 52 | + }); |
| 53 | + |
| 54 | + expect(mapData.hasStates).toBe(true); |
| 55 | + expect(mapData.hasBurgs).toBe(true); |
| 56 | + expect(mapData.hasCells).toBe(true); |
| 57 | + expect(mapData.hasRivers).toBe(true); |
| 58 | + expect(mapData.mapId).toBeDefined(); |
| 59 | + |
| 60 | + // Ensure no JavaScript errors occurred during loading |
| 61 | + // Filter out expected errors (external resources like Google Analytics, fonts) |
| 62 | + const criticalErrors = errors.filter( |
| 63 | + (e) => |
| 64 | + !e.includes("fonts.googleapis.com") && |
| 65 | + !e.includes("google-analytics") && |
| 66 | + !e.includes("googletagmanager") && |
| 67 | + !e.includes("Failed to load resource") |
| 68 | + ); |
| 69 | + expect(criticalErrors).toEqual([]); |
| 70 | + }); |
| 71 | + |
| 72 | + test("loaded map should have correct SVG structure", async ({ page }) => { |
| 73 | + const errors: string[] = []; |
| 74 | + page.on("pageerror", (error) => errors.push(`pageerror: ${error.message}`)); |
| 75 | + page.on("console", (msg) => { |
| 76 | + if (msg.type() === "error") { |
| 77 | + errors.push(`console.error: ${msg.text()}`); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + const fileInput = page.locator("#mapToLoad"); |
| 82 | + const mapFilePath = path.join(__dirname, "../fixtures/demo.map"); |
| 83 | + await fileInput.setInputFiles(mapFilePath); |
| 84 | + |
| 85 | + await page.waitForFunction(() => (window as any).mapId !== undefined, { |
| 86 | + timeout: 120000, |
| 87 | + }); |
| 88 | + await page.waitForTimeout(500); |
| 89 | + |
| 90 | + // Check essential SVG layers exist |
| 91 | + const layers = await page.evaluate(() => { |
| 92 | + return { |
| 93 | + ocean: !!document.getElementById("ocean"), |
| 94 | + lakes: !!document.getElementById("lakes"), |
| 95 | + coastline: !!document.getElementById("coastline"), |
| 96 | + rivers: !!document.getElementById("rivers"), |
| 97 | + borders: !!document.getElementById("borders"), |
| 98 | + burgs: !!document.getElementById("burgIcons"), |
| 99 | + labels: !!document.getElementById("labels"), |
| 100 | + }; |
| 101 | + }); |
| 102 | + |
| 103 | + expect(layers.ocean).toBe(true); |
| 104 | + expect(layers.lakes).toBe(true); |
| 105 | + expect(layers.coastline).toBe(true); |
| 106 | + expect(layers.rivers).toBe(true); |
| 107 | + expect(layers.borders).toBe(true); |
| 108 | + expect(layers.burgs).toBe(true); |
| 109 | + expect(layers.labels).toBe(true); |
| 110 | + |
| 111 | + const criticalErrors = errors.filter( |
| 112 | + (e) => |
| 113 | + !e.includes("fonts.googleapis.com") && |
| 114 | + !e.includes("google-analytics") && |
| 115 | + !e.includes("googletagmanager") && |
| 116 | + !e.includes("Failed to load resource") |
| 117 | + ); |
| 118 | + expect(criticalErrors).toEqual([]); |
| 119 | + }); |
| 120 | + |
| 121 | + test("loaded map should preserve state data", async ({ page }) => { |
| 122 | + const errors: string[] = []; |
| 123 | + page.on("pageerror", (error) => errors.push(`pageerror: ${error.message}`)); |
| 124 | + page.on("console", (msg) => { |
| 125 | + if (msg.type() === "error") { |
| 126 | + errors.push(`console.error: ${msg.text()}`); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + const fileInput = page.locator("#mapToLoad"); |
| 131 | + const mapFilePath = path.join(__dirname, "../fixtures/demo.map"); |
| 132 | + await fileInput.setInputFiles(mapFilePath); |
| 133 | + |
| 134 | + await page.waitForFunction(() => (window as any).mapId !== undefined, { |
| 135 | + timeout: 120000, |
| 136 | + }); |
| 137 | + await page.waitForTimeout(500); |
| 138 | + |
| 139 | + // Verify states have proper structure |
| 140 | + const statesData = await page.evaluate(() => { |
| 141 | + const pack = (window as any).pack; |
| 142 | + const states = pack.states.filter((s: any) => s.i !== 0); // exclude neutral |
| 143 | + |
| 144 | + return { |
| 145 | + count: states.length, |
| 146 | + allHaveNames: states.every((s: any) => s.name && s.name.length > 0), |
| 147 | + allHaveCells: states.every((s: any) => s.cells > 0), |
| 148 | + allHaveArea: states.every((s: any) => s.area > 0), |
| 149 | + }; |
| 150 | + }); |
| 151 | + |
| 152 | + expect(statesData.count).toBeGreaterThan(0); |
| 153 | + expect(statesData.allHaveNames).toBe(true); |
| 154 | + expect(statesData.allHaveCells).toBe(true); |
| 155 | + expect(statesData.allHaveArea).toBe(true); |
| 156 | + |
| 157 | + const criticalErrors = errors.filter( |
| 158 | + (e) => |
| 159 | + !e.includes("fonts.googleapis.com") && |
| 160 | + !e.includes("google-analytics") && |
| 161 | + !e.includes("googletagmanager") && |
| 162 | + !e.includes("Failed to load resource") |
| 163 | + ); |
| 164 | + expect(criticalErrors).toEqual([]); |
| 165 | + }); |
| 166 | + |
| 167 | + test("loaded map should preserve burg data", async ({ page }) => { |
| 168 | + const errors: string[] = []; |
| 169 | + page.on("pageerror", (error) => errors.push(`pageerror: ${error.message}`)); |
| 170 | + page.on("console", (msg) => { |
| 171 | + if (msg.type() === "error") { |
| 172 | + errors.push(`console.error: ${msg.text()}`); |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + const fileInput = page.locator("#mapToLoad"); |
| 177 | + const mapFilePath = path.join(__dirname, "../fixtures/demo.map"); |
| 178 | + await fileInput.setInputFiles(mapFilePath); |
| 179 | + |
| 180 | + await page.waitForFunction(() => (window as any).mapId !== undefined, { |
| 181 | + timeout: 120000, |
| 182 | + }); |
| 183 | + await page.waitForTimeout(500); |
| 184 | + |
| 185 | + // Verify burgs have proper structure |
| 186 | + const burgsData = await page.evaluate(() => { |
| 187 | + const pack = (window as any).pack; |
| 188 | + // Filter out placeholder (i=0) and removed burgs (removed=true or no name) |
| 189 | + const activeBurgs = pack.burgs.filter( |
| 190 | + (b: any) => b.i !== 0 && !b.removed && b.name |
| 191 | + ); |
| 192 | + |
| 193 | + return { |
| 194 | + count: activeBurgs.length, |
| 195 | + allHaveNames: activeBurgs.every( |
| 196 | + (b: any) => b.name && b.name.length > 0 |
| 197 | + ), |
| 198 | + allHaveCoords: activeBurgs.every( |
| 199 | + (b: any) => typeof b.x === "number" && typeof b.y === "number" |
| 200 | + ), |
| 201 | + allHaveCells: activeBurgs.every( |
| 202 | + (b: any) => typeof b.cell === "number" |
| 203 | + ), |
| 204 | + }; |
| 205 | + }); |
| 206 | + |
| 207 | + expect(burgsData.count).toBeGreaterThan(0); |
| 208 | + expect(burgsData.allHaveNames).toBe(true); |
| 209 | + expect(burgsData.allHaveCoords).toBe(true); |
| 210 | + expect(burgsData.allHaveCells).toBe(true); |
| 211 | + |
| 212 | + const criticalErrors = errors.filter( |
| 213 | + (e) => |
| 214 | + !e.includes("fonts.googleapis.com") && |
| 215 | + !e.includes("google-analytics") && |
| 216 | + !e.includes("googletagmanager") && |
| 217 | + !e.includes("Failed to load resource") |
| 218 | + ); |
| 219 | + expect(criticalErrors).toEqual([]); |
| 220 | + }); |
| 221 | +}); |
0 commit comments