Skip to content

Commit 363c82e

Browse files
fix: update port type from string to number and add tests for inland burgs (#1292)
1 parent e938bc7 commit 363c82e

File tree

2 files changed

+115
-6
lines changed

2 files changed

+115
-6
lines changed

src/modules/burgs-generator.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface Burg {
1515
feature?: number;
1616
capital?: number;
1717
lock?: boolean;
18-
port?: string;
18+
port?: number;
1919
removed?: boolean;
2020
population?: number;
2121
type?: string;
@@ -81,7 +81,7 @@ class BurgModule {
8181
Object.entries(featurePortCandidates).forEach(([featureId, burgs]) => {
8282
if (burgs.length < 2) return; // only one port on water body - skip
8383
burgs.forEach((burg) => {
84-
burg.port = featureId;
84+
burg.port = Number(featureId);
8585
const haven = cells.haven[burg.cell];
8686
const [x, y] = getCloseToEdgePoint(burg.cell, haven);
8787
burg.x = x;
@@ -237,7 +237,7 @@ class BurgModule {
237237
}
238238
}
239239

240-
getType(cellId: number, port: string) {
240+
getType(cellId: number, port?: number) {
241241
const { cells, features } = pack;
242242

243243
if (port) return "Naval";
@@ -272,7 +272,7 @@ class BurgModule {
272272
}
273273

274274
private defineEmblem(burg: Burg) {
275-
burg.type = this.getType(burg.cell, burg.port as string);
275+
burg.type = this.getType(burg.cell, burg.port);
276276

277277
const state = pack.states[burg.state as number];
278278
const stateCOA = state.coa;
@@ -485,7 +485,7 @@ class BurgModule {
485485
const population = rn(burgPopulation! * populationRate * urbanization);
486486

487487
const river = cells.r[cell] ? 1 : 0;
488-
const coast = Number(parseInt(burg.port as string, 10) > 0);
488+
const coast = Number((burg.port || 0) > 0);
489489
const sea = (() => {
490490
if (!coast || !cells.haven[cell]) return null;
491491

@@ -672,7 +672,7 @@ class BurgModule {
672672
name,
673673
feature,
674674
capital: 0,
675-
port: "0",
675+
port: 0,
676676
};
677677
this.definePopulation(burg);
678678
this.defineEmblem(burg);

tests/e2e/burgs.spec.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)