election-game/web/test/geometry-rings.test.ts

31 lines
1.7 KiB
TypeScript
Raw Normal View History

/// <reference types="node" />
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, type StagePolygons } from "../src/game/stage/geometry.ts";
const packs = join(import.meta.dirname, "..", "..", "backend", "data", "build", "packs");
const read = <T>(path: string): T => JSON.parse(readFileSync(path, "utf-8")) as T;
const context = read<FeatureCollection<Geometry, { name: string; county: string }>>(join(import.meta.dirname, "..", "public", "maps", "taiwan-counties.json"));
const has = (pack: string) => existsSync(join(packs, pack, "topology.json"));
const topo = (pack: string) => read<Topology>(join(packs, pack, "topology.json"));
const points = (polys: StagePolygons) => polys.flat(2);
const inside = (polys: StagePolygons, pad = 1) => points(polys).every(([x, y]) => x >= -pad && x <= STAGE_WIDTH + pad && y >= -pad && y <= STAGE_HEIGHT + pad);
describe("立體盤面用的多邊形", () => {
it.skipIf(!has("68000-before-2022-11-26"))("桃園:每區都有外圈,鄰近縣市裁在盤面內", () => {
const g = buildGeometry(topo("68000-before-2022-11-26"), context, { county: "68000", level: "town", villages: null, names: new Map() });
expect(g.towns.length).toBe(13);
for (const t of g.towns) {
expect(t.rings.length).toBeGreaterThan(0);
expect(t.rings[0]![0]!.length).toBeGreaterThanOrEqual(3);
expect(inside(t.rings)).toBe(true);
}
expect(g.context.length).toBeGreaterThan(0);
for (const c of g.context) expect(inside(c.rings, 0.01)).toBe(true);
});
});