|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | + |
| 3 | +test.describe("Burgs.add", () => { |
| 4 | + test.beforeEach(async ({ context, page }) => { |
| 5 | + await context.clearCookies(); |
| 6 | + |
| 7 | + await page.goto("/"); |
| 8 | + await page.evaluate(() => { |
| 9 | + localStorage.clear(); |
| 10 | + sessionStorage.clear(); |
| 11 | + }); |
| 12 | + |
| 13 | + // Navigate with seed parameter and wait for full load |
| 14 | + await page.goto("/?seed=test-burgs&width=1280&height=720"); |
| 15 | + |
| 16 | + // Wait for map generation to complete |
| 17 | + await page.waitForFunction( |
| 18 | + () => (window as any).mapId !== undefined, |
| 19 | + { timeout: 60000 } |
| 20 | + ); |
| 21 | + |
| 22 | + // Additional wait for any rendering/animations to settle |
| 23 | + await page.waitForTimeout(500); |
| 24 | + }); |
| 25 | + |
| 26 | + test("should create burg with falsy port value when not on coast", async ({ |
| 27 | + page, |
| 28 | + }) => { |
| 29 | + const result = await page.evaluate(() => { |
| 30 | + const { cells, burgs } = (window as any).pack; |
| 31 | + |
| 32 | + // Find a land cell that is not on the coast (no harbor) |
| 33 | + let inlandCellId: number | null = null; |
| 34 | + for (let i = 1; i < cells.i.length; i++) { |
| 35 | + const isLand = cells.h[i] >= 20; |
| 36 | + const hasNoHarbor = !cells.harbor[i]; |
| 37 | + const hasNoBurg = !cells.burg[i]; |
| 38 | + if (isLand && hasNoHarbor && hasNoBurg) { |
| 39 | + inlandCellId = i; |
| 40 | + break; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (!inlandCellId) { |
| 45 | + return { error: "No inland cell found" }; |
| 46 | + } |
| 47 | + |
| 48 | + // Get coordinates for the inland cell |
| 49 | + const [x, y] = cells.p[inlandCellId]; |
| 50 | + |
| 51 | + // Add a new burg at this inland location |
| 52 | + const Burgs = (window as any).Burgs; |
| 53 | + const burgId = Burgs.add([x, y]); |
| 54 | + const burg = burgs[burgId]; |
| 55 | + |
| 56 | + return { |
| 57 | + burgId, |
| 58 | + port: burg.port, |
| 59 | + portType: typeof burg.port, |
| 60 | + portIsFalsy: !burg.port, |
| 61 | + x: burg.x, |
| 62 | + y: burg.y, |
| 63 | + }; |
| 64 | + }); |
| 65 | + |
| 66 | + expect(result.error).toBeUndefined(); |
| 67 | + // Port should be 0 (number), not "0" (string) |
| 68 | + expect(result.port).toBe(0); |
| 69 | + expect(result.portType).toBe("number"); |
| 70 | + expect(result.portIsFalsy).toBe(true); |
| 71 | + // Explicitly verify it's not the buggy string "0" |
| 72 | + expect(result.port).not.toBe("0"); |
| 73 | + }); |
| 74 | + |
| 75 | + test("port toggle button should be inactive for non-coastal burg", async ({ |
| 76 | + page, |
| 77 | + }) => { |
| 78 | + // Add a burg on an inland cell |
| 79 | + const burgId = await page.evaluate(() => { |
| 80 | + const { cells } = (window as any).pack; |
| 81 | + |
| 82 | + // Find a land cell that is not on the coast |
| 83 | + for (let i = 1; i < cells.i.length; i++) { |
| 84 | + const isLand = cells.h[i] >= 20; |
| 85 | + const hasNoHarbor = !cells.harbor[i]; |
| 86 | + const hasNoBurg = !cells.burg[i]; |
| 87 | + if (isLand && hasNoHarbor && hasNoBurg) { |
| 88 | + const [x, y] = cells.p[i]; |
| 89 | + return (window as any).Burgs.add([x, y]); |
| 90 | + } |
| 91 | + } |
| 92 | + return null; |
| 93 | + }); |
| 94 | + |
| 95 | + expect(burgId).not.toBeNull(); |
| 96 | + |
| 97 | + // Open the burg editor |
| 98 | + await page.evaluate((id: number) => { |
| 99 | + (window as any).editBurg(id); |
| 100 | + }, burgId!); |
| 101 | + |
| 102 | + // Wait for the editor dialog to appear |
| 103 | + await page.waitForSelector("#burgEditor", { state: "visible" }); |
| 104 | + |
| 105 | + // The port toggle button should have the "inactive" class |
| 106 | + const portButton = page.locator("#burgPort"); |
| 107 | + await expect(portButton).toHaveClass(/inactive/); |
| 108 | + }); |
| 109 | +}); |
0 commit comments