/// import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; import type { FeatureCollection, Geometry } from "geojson"; import type { Topology } from "topojson-specification"; import { describe, expect, it } from "vitest"; import { STAGE_HEIGHT, STAGE_WIDTH, buildGeometry } from "../src/game/stage/geometry.ts"; const packs = join(import.meta.dirname, "..", "..", "backend", "data", "build", "packs"); const read = (path: string): T => JSON.parse(readFileSync(path, "utf-8")) as T; const context = read>(join(import.meta.dirname, "..", "public", "maps", "taiwan-counties.json")); const has = (pack: string) => existsSync(join(packs, pack, "topology.json")); const topo = (pack: string) => read(join(packs, pack, "topology.json")); /** path 字串裡所有座標都在舞台內(允許一點邊界誤差) */ function inStage(d: string): boolean { const nums = d.match(/-?\d+(\.\d+)?/g)?.map(Number) ?? []; for (let i = 0; i + 1 < nums.length; i += 2) if (nums[i]! < -1 || nums[i]! > STAGE_WIDTH + 1 || nums[i + 1]! < -1 || nums[i + 1]! > STAGE_HEIGHT + 1) return false; return true; } describe("地圖舞台", () => { it.skipIf(!has("63000-before-2024-01-13"))("立委選區:只上色選區內的里,選區外的同市各區當底圖", () => { const t = topo("63000-before-2024-01-13"); const all = (t.objects.villages as unknown as { geometries: { properties: { code: string } }[] }).geometries.map((g) => g.properties.code); const villages = new Set(all.filter((c) => c.startsWith("63000120") || c === "63000110001")); const g = buildGeometry(t, context, { county: "63000", level: "town", villages, names: new Map([["63000120", "北投區"], ["63000110", "士林區"]]) }); expect(g.villages.length).toBe(villages.size); expect(g.towns.map((x) => x.name).sort()).toEqual(["北投區", "士林區"]); // 士林區其餘的里、以及大安區等,都畫成底圖 expect(g.context.some((c) => c.name === "士林區")).toBe(true); expect(g.context.some((c) => c.name === "大安區")).toBe(true); expect(g.villages.every((v) => inStage(v.d))).toBe(true); }); it.skipIf(!has("10002-before-2022-11-26"))("宜蘭縣:遠方的釣魚台不算進縮放範圍,本島填滿畫面", () => { const g = buildGeometry(topo("10002-before-2022-11-26"), context, { county: "10002", level: "town", villages: null, names: new Map() }); const yilan = g.towns.find((x) => x.name === "宜蘭市")!; expect(yilan.cx).toBeGreaterThan(0); expect(yilan.cx).toBeLessThan(STAGE_WIDTH); // 宜蘭市在縣的中段:沒被釣魚台擠到畫面左邊 expect(yilan.cx).toBeGreaterThan(STAGE_WIDTH * 0.3); }); it.skipIf(!has("00000-before-2024-01-13"))("全國:22 縣市各是一區,金門、馬祖放進小框", () => { const g = buildGeometry(topo("00000-before-2024-01-13"), context, { county: "00000", level: "county", villages: null, names: new Map() }); expect(g.towns.length).toBe(22); expect(g.insets.map((i) => i.name)).toEqual(["連江縣", "金門縣"]); const matsu = g.towns.find((x) => x.code === "09007000")!; const box = g.insets[0]!; expect(matsu.cx).toBeGreaterThan(box.x); expect(matsu.cx).toBeLessThan(box.x + box.w); expect(g.context).toEqual([]); }); });