import { describe, expect, it } from "vitest"; import { runBot, type BotName } from "../src/bots.ts"; import { PRESS, STOP_EVENTS } from "../src/content.ts"; import { CARD_BY_ID, cardDef, PLUS_MULT, REPORTER_PENALTY } from "../src/data.ts"; import { choose, COMBO_STEP, createRun, endWeek, playCard } from "../src/rules.ts"; import { townNeighbors } from "../src/setup.ts"; import { FLAG_LEAD, totals, UNDECIDED_START, type Intent, type RunState } from "../src/state.ts"; import { TUTORIAL_DECK, TUTORIAL_WEEKS, tutorialSetup } from "../src/tutorial.ts"; import { previewsFor, toView } from "../src/view.ts"; import { SETUP, advance, must, toPlan } from "./fixtures.ts"; const SCALE = 0.7; /** 牌的基準票數(直接讀資料,調平衡時測試不用跟著改) */ const cardBase = (id: string, up = false): number => { const e = cardDef(id, up)!.effect as { base?: number }; return e.base ?? 0; }; const effect = (id: string) => CARD_BY_ID.get(id)!.effect as unknown as Record; /** 指定手牌、換掉對手意圖(連同對手已經生效的樁腳),方便算數字 */ function withHand(s: RunState, ids: string[], intents: Intent[] = []): RunState { const copy = structuredClone(s); return { ...copy, phase: "plan", scene: null, towns: copy.towns.map((t) => ({ ...t, shield: 0, rivalShield: 0 })), hand: ids.map((id, i) => ({ uid: `t${i}`, id })), intents, energy: 9, money: 5000, momentum: 0 }; } const ok = (r: ReturnType): RunState => { if ("error" in r) throw new Error(r.error); return r.state; }; describe("開局", () => { it("同一個種子同一局;第一週 3 點體力、5 張手牌、對手 2 個意圖(第一週不批評)", () => { const a = createRun(SETUP, "r", 42); expect(createRun(SETUP, "r", 42)).toEqual(a); expect(createRun(SETUP, "r", 43).hand.map((c) => c.id)).not.toEqual(a.hand.map((c) => c.id)); expect(a.version).toBe(2); expect(a.week).toBe(1); expect(a.phase).toBe("plan"); expect(a.energy).toBe(3); expect(a.hand).toHaveLength(5); expect(a.intents).toHaveLength(2); expect(a.intents.some((i) => i.kind === "attack" || i.kind === "smear")).toBe(false); const t = totals(a.towns); expect(t.undecided / t.voters).toBeCloseTo(UNDECIDED_START); expect(a.scale).toBeCloseTo(SCALE); expect(a.towns[0]!.reach).toBeGreaterThan(1.1); expect(a.towns[2]!.reach).toBeLessThan(0.75); // 存檔:可以轉成 JSON 再讀回來 expect(JSON.parse(JSON.stringify(a))).toEqual(a); }); it("逆風難度:資金 −40%、牌組多人情包袱、對手開局聲勢、開局落後", () => { const easy = createRun(SETUP, "r", 5); const hard = createRun(SETUP, "r", 5, { ascension: 10 }); expect(hard.money).toBe(Math.round(easy.money * 0.6)); expect([...hard.draw, ...hard.hand].some((c) => c.id === "favor")).toBe(true); expect(hard.rivalMomentum).toBe(2); const gap = (s: RunState) => (totals(s.towns).you - totals(s.towns).rival) / (totals(s.towns).voters * (1 - UNDECIDED_START)); expect(gap(easy) - gap(hard)).toBeCloseTo(0.03, 3); expect(hard.startGap).toBeLessThan(easy.startGap); }); }); describe("出牌", () => { const base = createRun(SETUP, "r", 1); it("掃街打在人口密集的區 ×1.3;花體力、牌進棄牌堆、不改原本的狀態", () => { const s = withHand(base, ["canvass"]); const before = structuredClone(s); const after = ok(playCard(s, "t0", "A")); expect(s).toEqual(before); expect(after.towns[0]!.you - s.towns[0]!.you).toBeCloseTo(cardBase("canvass") * SCALE * s.towns[0]!.reach * 1.3); expect(after.energy).toBe(8); expect(after.hand).toHaveLength(0); expect(after.discard.at(-1)!.id).toBe("canvass"); expect(after.towns[0]!.played).toEqual(["canvass"]); }); it("組合拳:同一區第二種牌 +15%,同一種牌不算", () => { const s = withHand(base, ["canvass", "night", "canvass"]); const one = ok(playCard(s, "t0", "A")); const two = ok(playCard(one, "t1", "A")); const reach = s.towns[0]!.reach; expect(two.towns[0]!.you - one.towns[0]!.you).toBeCloseTo(cardBase("night") * SCALE * reach * 1.5 * (1 + COMBO_STEP)); const again = ok(playCard(two, "t2", "A")); expect(again.towns[0]!.you - two.towns[0]!.you).toBeCloseTo(cardBase("canvass") * SCALE * reach * 1.3); }); it("升級:+版數字 ×1.35、名字加+;服務處+少 1 點體力", () => { expect(cardBase("canvass", true)).toBe(Math.round((cardBase("canvass") * PLUS_MULT) / 10) * 10); expect(cardDef("canvass", true)!.name).toBe("掃街拜票+"); expect(cardDef("office", true)!.cost).toBe(1); const s = withHand(base, ["canvass"]); s.hand[0]!.up = true; const after = ok(playCard(s, "t0", "C")); expect(after.towns[2]!.you - s.towns[2]!.you).toBeCloseTo(cardBase("canvass", true) * SCALE * s.towns[2]!.reach); }); it("造勢擴散到隔壁區兩成半,聲勢 +2", () => { const s = withHand(base, ["rally"]); const after = ok(playCard(s, "t0", "B")); const b = s.towns[1]!; // 區的大小係數離 1 不到 0.05 就不算 const reach = Math.abs(b.reach - 1) >= 0.05 ? b.reach : 1; expect(after.towns[1]!.you - b.you).toBeCloseTo(cardBase("rally") * SCALE * reach); expect(after.towns[0]!.you - s.towns[0]!.you).toBeCloseTo(cardBase("rally") * 0.25 * SCALE * s.towns[0]!.reach); expect(after.towns[3]!.you).toBeCloseTo(s.towns[3]!.you); expect(after.momentum).toBe(2); }); it("消耗的牌打完就沒了;能力牌整局有效", () => { const s = withHand(base, ["mediatraining", "tv"]); const one = ok(playCard(s, "t0", null)); expect(one.exhausted.map((c) => c.id)).toEqual(["mediatraining"]); expect(one.discard.some((c) => c.id === "mediatraining")).toBe(false); expect(one.powers).toEqual(["media"]); const plain = ok(playCard(s, "t1", null)); const trained = ok(playCard(one, "t1", null)); const gain = (x: RunState) => totals(x.towns).you; expect((gain(trained) - gain(one)) / (gain(plain) - gain(s))).toBeCloseTo(1.25, 1); }); it("對手的樁腳先擋掉你在那區的票", () => { const s = withHand(base, ["canvass"]); s.towns[2]!.rivalShield = 500; const after = ok(playCard(s, "t0", "C")); expect(after.towns[2]!.you - s.towns[2]!.you).toBeCloseTo(cardBase("canvass") * SCALE * s.towns[2]!.reach - 500); expect(after.towns[2]!.rivalShield).toBe(0); }); it("打對台:對手那區的行動剩四成,你在那區加票;週末對手只拿到四成", () => { const intent: Intent = { id: "i1", kind: "canvass", town: "B", amount: 3000, cut: 1 }; const s = withHand(base, ["counter"], [intent]); const after = ok(playCard(s, "t0", "i1")); expect(after.intents[0]!.cut).toBe(effect("counter").cut); expect(after.towns[1]!.you).toBeGreaterThan(s.towns[1]!.you); const ended = must(endWeek(after)); expect(ended.towns[1]!.rival - after.towns[1]!.rival).toBeCloseTo(3000 * effect("counter").cut!); // 擋下的票列在結算裡,也算進這週的戰果 expect(ended.log.find((l) => l.blocked)?.blocked).toBe(Math.round(3000 * (1 - effect("counter").cut!))); expect(ended.tally!.blocked).toBeGreaterThan(0); expect(ended.tally!.gained).toBeGreaterThan(0); }); it("佈樁:週末擋掉對手在這區的行動", () => { const intent: Intent = { id: "i1", kind: "canvass", town: "D", amount: 1000, cut: 1 }; const s = withHand(base, ["stake"], [intent]); const after = ok(playCard(s, "t0", "D")); expect(after.towns[3]!.shield).toBeCloseTo(cardBase("stake") * SCALE); const ended = must(endWeek(after)); expect(ended.towns[3]!.rival).toBeCloseTo(after.towns[3]!.rival); }); it("對手批評你:澄清剩兩成;沒擋下來會塞一張記者追問", () => { const intent: Intent = { id: "i1", kind: "attack", town: null, amount: 2000, cut: 1 }; const s = withHand(base, ["clarify"], [intent]); const after = ok(playCard(s, "t0", null)); expect(after.intents[0]!.cut).toBeCloseTo(0.2); const ended = must(endWeek(after)); expect(totals(after.towns).you - totals(ended.towns).you).toBeCloseTo(400, 0); expect(ended.discard.some((c) => c.id === "reporter")).toBe(true); // 事實查核:讓這個批評完全失效 const checked = ok(playCard(withHand(base, ["factcheck"], [intent]), "t0", "i1")); expect(checked.intents[0]!.cut).toBe(0); expect(must(endWeek(checked)).discard.some((c) => c.id === "reporter")).toBe(false); }); it("記者追問留在手上過週末會掉票;回應就移除", () => { const s = withHand(base, ["reporter"]); const ended = must(endWeek(s)); expect(totals(s.towns).you - totals(ended.towns).you).toBeCloseTo(REPORTER_PENALTY * SCALE, 0); const answered = ok(playCard(s, "t0", null)); expect(answered.exhausted.map((c) => c.id)).toEqual(["reporter"]); }); it("爆料:有把柄全額、用掉一個;沒把柄效果小還會反傷", () => { const s = withHand(base, ["expose"]); const weak = ok(playCard(s, "t0", null)); expect(totals(s.towns).rival - totals(weak.towns).rival).toBeCloseTo(effect("expose").weak! * SCALE); expect(totals(s.towns).you - totals(weak.towns).you).toBeCloseTo(effect("expose").backlash! * SCALE); const strong = ok(playCard({ ...s, leverage: 1 }, "t0", null)); expect(totals(s.towns).rival - totals(strong.towns).rival).toBeCloseTo(cardBase("expose") * SCALE); expect(strong.leverage).toBe(0); expect(strong.stats.attacks).toBe(1); }); it("插旗與翻轉:把對手插旗的區拉過來,聲勢 +1", () => { const s = withHand(base, ["door", "door", "door"]); const c = s.towns[2]!; const committed = c.you + c.rival + c.others; c.you = committed * 0.42; c.rival = committed * 0.5; c.flag = "rival"; c.was = "rival"; let x = s; for (const uid of ["t0", "t1", "t2"]) x = ok(playCard(x, uid, "C")); expect(x.towns[2]!.flag).toBe("you"); expect(x.stats.flips).toBe(1); expect(x.momentum).toBe(1); expect(x.log.some((l) => l.text.includes("翻轉"))).toBe(true); expect(FLAG_LEAD).toBeGreaterThan(0); }); it("打出去的牌結算完才進棄牌堆:抽牌不會把自己抽回來;加體力的牌每週一次", () => { const s = withHand(base, ["volunteers", "allnighter"]); s.draw = []; s.discard = []; const drew = ok(playCard(s, "t0", null)); expect(drew.hand.map((c) => c.id)).toEqual(["allnighter"]); expect(drew.discard.map((c) => c.id)).toEqual(["volunteers"]); const rushed = ok(playCard(drew, "t1", null)); expect(rushed.energy).toBe(drew.energy + 2); rushed.hand.push({ uid: "again", id: "allnighter" }); expect(playCard(rushed, "again", null)).toEqual({ error: "這週已經打過了" }); }); it("一次打好幾區的牌也算打過:同一週再打一次不算組合拳", () => { const s = withHand(base, ["calls", "calls"]); const one = ok(playCard(s, "t0", null)); const two = ok(playCard(one, "t1", null)); expect(one.towns.filter((t) => t.played.includes("calls"))).toHaveLength(3); const gained = (a: RunState, b: RunState) => totals(b.towns).you - totals(a.towns).you; expect(gained(one, two)).toBeCloseTo(gained(s, one), 0); }); it("佈樁的預估只算還沒被擋住的威脅", () => { const intent: Intent = { id: "i1", kind: "canvass", town: "D", amount: 1000, cut: 1 }; const s = withHand(base, ["stake", "stake"], [intent]); const first = previewsFor(s).t0!.find((p) => p.target === "D")!; expect(first.note).toContain("可擋對手 1,000"); const after = ok(playCard(s, "t0", "D")); const second = previewsFor(after).t1!.find((p) => p.target === "D")!; expect(second.note).not.toContain("可擋對手"); }); it("不能打的牌說明原因:體力、資金、最後一週才能打、不能打的狀態牌", () => { const s = { ...withHand(base, ["rally", "kneel", "favor"]), energy: 1 }; expect(playCard(s, "t0", "A")).toEqual({ error: "體力不夠" }); expect(playCard({ ...s, energy: 3 }, "t1", null)).toEqual({ error: "最後一週才能打" }); expect(playCard({ ...s, energy: 3, money: 100 }, "t0", "A")).toEqual({ error: "資金不夠" }); expect(playCard(s, "t2", null)).toEqual({ error: "這張牌不能打" }); }); }); describe("預估", () => { it("預估數字和真的打出去一樣(不含機率的牌)", () => { const s = withHand(createRun(SETUP, "r", 7), ["canvass", "temple", "tv", "rally"]); const previews = previewsFor(s); for (const [uid, target] of [["t0", "A"], ["t1", "B"], ["t2", null], ["t3", "B"]] as const) { const p = previews[uid]!.find((x) => x.target === (target ?? "city"))!; const after = ok(playCard(s, uid, target)); const swing = totals(after.towns).you - totals(s.towns).you - (totals(after.towns).rival - totals(s.towns).rival); expect(p.votes).toBe(Math.round(swing)); } expect(previews.t1!.find((x) => x.target === "B")!.note).toContain("宮廟多 ×2"); }); }); describe("週與週之間", () => { it("結束這週 → 下一站三選一(門上直接攤開內容,一次點選就拿到);第 2 週有記者堵麥但不擋出牌", () => { const s = createRun(SETUP, "r", 3); const ended = must(endWeek(s)); expect(ended.phase).toBe("route"); expect(ended.route).toHaveLength(3); expect(ended.route).toContain("card"); expect(totals(ended.towns).rival).toBeGreaterThan(totals(s.towns).rival); const door = ended.route!.indexOf("card"); // 門上直接攤開三張牌 const view0 = toView(ended); const preview = view0.route![door]!.preview as { kind: string; options: { id: string }[] }; expect(preview.kind).toBe("card"); const picked = preview.options[1]!.id; // 選門+選牌一次完成 const next = must(choose(ended, door, null, 1)); expect(next.week).toBe(2); expect(next.hand).toHaveLength(5); // 新牌放在抽牌堆最上面:下一週就在手上 expect(next.hand.some((c) => c.id === picked)).toBe(true); // 記者堵麥:出牌階段就可以出牌,但結束這週前要回答 expect(next.phase).toBe("plan"); expect(next.scene!.kind).toBe("press"); expect(toView(next).scene!.kind).toBe("press"); expect("error" in endWeek(next)).toBe(true); const answered = must(choose(next, 0)); expect(answered.phase).toBe("plan"); expect(answered.scene).toBeNull(); // 還是可以先進門再選(舊的兩步) const at = must(choose(ended, door)); expect(at.phase).toBe("stop"); expect((at.stop as { options: string[] }).options[1]).toBe(picked); // 不去下一站:改拿資金 const skipped = must(choose(must(endWeek(answered)), "skip")); expect(skipped.week).toBe(3); expect(skipped.money).toBeGreaterThan(0); }); it("第 4 週民調自動公布,不用點", () => { const s = advance(createRun(SETUP, "r", 12), 4); expect(s.week).toBe(4); expect(s.phase).toBe("plan"); expect(s.scene).toBeNull(); expect(s.poll!.week).toBe(4); expect(toView(s).poll!.week).toBe(4); }); it("第 2 週一定可以找幕僚;商店買東西扣錢,刪牌服務拿掉一張牌", () => { let s = toPlan(must(choose(must(endWeek(createRun(SETUP, "r", 9))), "skip"))); s = must(endWeek(s)); expect(s.week).toBe(2); expect(s.route).toContain("advisor"); const shop = structuredClone(s); shop.phase = "stop"; shop.route = null; shop.stop = { kind: "shop", items: [{ kind: "card", id: "tv", price: 120, sold: false }, { kind: "remove", id: "remove", price: 100, sold: false }] }; shop.money = 1000; const bought = must(choose(shop, 0)); expect(bought.money).toBe(880); expect(bought.phase).toBe("stop"); // 買的牌放在抽牌堆最上面,下週一定抽到 expect(bought.draw[0]!.id).toBe("tv"); const victim = [...bought.draw, ...bought.discard][0]!; const removed = must(choose(bought, 1, victim.uid)); expect([...removed.draw, ...removed.discard, ...removed.hand].some((c) => c.uid === victim.uid)).toBe(false); expect(removed.money).toBe(780); expect(choose(removed, 0)).toEqual({ error: "沒有這個商品" }); expect(must(choose(removed, "skip")).week).toBe(3); }); it("升級一張牌、深入基層三個遭遇之後那區加票", () => { const s = must(endWeek(createRun(SETUP, "r", 4))); const up = structuredClone(s); up.phase = "stop"; up.stop = { kind: "upgrade" }; const target = up.draw[0]!; const done = must(choose(up, 0, target.uid)); expect([...done.draw, ...done.discard, ...done.hand].find((c) => c.uid === target.uid)!.up).toBe(true); const enc = structuredClone(s); enc.phase = "stop"; enc.stop = { kind: "encounter", town: "A", encounters: ["heckler", "selfie_kids", "lost_grandma"], step: 0, score: 0, notes: [] }; let x = must(choose(enc, 0)); x = must(choose(x, 0)); x = must(choose(x, 0)); expect(x.stop!.kind).toBe("encounter"); expect((x.stop as { score: number }).score).toBe(5); expect(x.towns[0]!.you).toBeGreaterThan(enc.towns[0]!.you); expect(must(choose(x, "skip")).week).toBe(2); }); }); describe("劇情", () => { it("第 3 週論點連擊辯論:有辯論教練看得出最好的論點,三連擊加成越來越大、拿到辯論金句", () => { let s = createRun(SETUP, "r", 21); s.advisors = ["debate_coach"]; s = advance(s, 3); expect(s.week).toBe(3); const scene = s.scene as Extract; expect(scene.kind).toBe("debate"); expect(scene.hand.length).toBeGreaterThanOrEqual(5); // 三種論點至少各一張 expect(new Set(scene.hand.map((a) => a.kind)).size).toBe(3); // 確定手上有答得出來的論點(抽論點有運氣) scene.hand = [...scene.needs.map((kind) => ({ kind, text: "測試" })), { kind: "policy", text: "多一張" }]; for (let round = 0; round < 3; round++) { const view = toView(s).scene as Extract["scene"]>, { kind: "debate" }>; expect(view.sure).toBe(true); expect(view.challenge).toBeTruthy(); s = must(choose(s, view.best!)); } const done = s.scene as Extract; expect(done.wins).toBe(3); expect(done.history.map((h) => h.combo)).toEqual([1, 2, 3]); // 第三回合 ×2:大約是第一回合的兩倍 expect(done.history[2]!.votes / done.history[0]!.votes).toBeGreaterThan(1.6); expect(s.hand.some((c) => c.id === "soundbite")).toBe(true); s = must(choose(s, 0)); expect(s.phase).toBe("plan"); }); it("辯論:答非所問會輸、普通的平手,連擊中斷;沒有教練看不到最好的論點", () => { let s = advance(createRun(SETUP, "r", 21), 3); const scene = s.scene as Extract; // 三回合都是數字質疑:拿數據最好、反問普通、講故事最差 scene.needs = ["policy", "policy", "policy"]; scene.hand = [ { kind: "policy", text: "對" }, { kind: "emotion", text: "錯" }, { kind: "question", text: "普通" }, ]; expect((toView(s).scene as { best: number | null }).best).toBeNull(); const before = s.momentum; for (let i = 0; i < 3; i++) s = must(choose(s, 0)); const d = s.scene as Extract; expect(d.history.map((h) => h.result)).toEqual(["win", "lose", "tie"]); expect(d.history[1]!.votes).toBeLessThan(0); expect(d.combo).toBe(0); expect(d.wins).toBe(1); expect(s.momentum).toBeGreaterThanOrEqual(before); }); it("舉債的政策第二回合會被追問;財源扎實的政策變成一張論點", () => { const policies = [ { name: "長照好安心", target: "old", method: "subsidy", funding: "invest" }, { name: "全民蓋公園", target: "families", method: "build", funding: "debt" }, ] as const; let s = createRun(SETUP, "r", 21, { policies, perks: [{ id: "library", level: 1 }] }); s = advance(s, 3); const scene = s.scene as Extract; expect(scene.challenges[1]).toContain("全民蓋公園"); expect(scene.needs[1]).toBe("policy"); expect(scene.hand.some((a) => a.policy === "長照好安心")).toBe(true); expect(scene.hand.some((a) => a.policy === "全民蓋公園")).toBe(false); }); it("第 6 週匿名黑函:公開資料自清沒有損失、這週體力 −1", () => { let s = createRun(SETUP, "r", 33); s = advance(s, 6); expect(s.week).toBe(6); expect(s.scene!.kind).toBe("crisis"); const before = totals(s.towns).you; const after = must(choose(s, 3)); expect(totals(after.towns).you).toBeCloseTo(before); expect(after.energy).toBe(s.energy - 1); expect(after.stats.crisisLoss).toBe(0); const ignored = must(choose(s, 2)); expect(totals(ignored.towns).you).toBeLessThan(before); expect(ignored.momentum).toBeGreaterThan(s.momentum); }); }); describe("一局", () => { it("每種模擬玩家都能打完整場;打得好比什麼都不做好", () => { const bots: BotName[] = ["idle", "random", "naive", "casual", "greedy", "ground", "air", "attack"]; const results = Object.fromEntries(bots.map((b) => [b, runBot(SETUP, 11, b)])); for (const b of bots) expect(results[b]!.phase).toBe("finished"); expect(results.greedy!.result!.youShare).toBeGreaterThan(results.idle!.result!.youShare); expect(results.greedy!.advisors.length).toBeGreaterThan(0); const view = toView(results.greedy!); expect(view.result!.byTown).toHaveLength(4); expect(view.history).toHaveLength(9); expect(Array.isArray(view.result!.feats)).toBe(true); // 逆風 10 級也打得完 expect(runBot(SETUP, 12, "greedy", { ascension: 10 }).phase).toBe("finished"); }); it("新手教學:3 週、照順序發牌、沒有辯論和黑函", () => { const s = createRun(tutorialSetup(), "t", 1, { tutorial: true, weeks: TUTORIAL_WEEKS, deck: TUTORIAL_DECK }); expect(s.hand.map((c) => c.id)).toEqual(TUTORIAL_DECK.slice(0, 5)); expect(toView(s).colors.you).not.toBe(toView(s).colors.rival); const done = runBot(tutorialSetup(), 2, "greedy", { tutorial: true, weeks: TUTORIAL_WEEKS, deck: TUTORIAL_DECK }); expect(done.phase).toBe("finished"); expect(done.history).toHaveLength(TUTORIAL_WEEKS + 1); }); it("堵麥與地方抉擇:牌和狀態牌都存在、每題至少一個不需要資源的選項、不重複", () => { const ids = new Set(); for (const def of [...PRESS, ...STOP_EVENTS]) { expect(ids.has(def.id)).toBe(false); ids.add(def.id); expect(def.choices.some((c) => !c.needs)).toBe(true); for (const c of def.choices) { for (const o of [c.outcome, ...(c.roll ? [c.roll.fail] : [])]) { if (o.card) expect(CARD_BY_ID.has(o.card)).toBe(true); for (const st of o.status ?? []) expect(CARD_BY_ID.get(st)?.rarity).toBe("status"); } } } expect(PRESS.length).toBeGreaterThanOrEqual(20); expect(STOP_EVENTS.length).toBeGreaterThanOrEqual(20); }); it("每張牌的資料完整;+版都算得出來", () => { for (const [id, card] of CARD_BY_ID) { expect(card.id).toBe(id); expect(card.text.length).toBeGreaterThan(0); expect(cardDef(id, true)!.name).toContain("+"); } }); }); describe("相鄰的區", () => { it("共用邊界的區是鄰居;縣市層級用鄉鎮組成縣市", () => { const topology = { objects: { towns: { geometries: [ { type: "Polygon", arcs: [[0, 1]], properties: { townCode: "10001010" } }, { type: "Polygon", arcs: [[~1, 2]], properties: { townCode: "10001020" } }, { type: "Polygon", arcs: [[~2, 3]], properties: { townCode: "10002010" } }, { type: "Polygon", arcs: [[4]], properties: { townCode: "10003010" } }, ], }, }, }; const towns = townNeighbors(topology, ["10001010", "10001020", "10002010", "10003010"]); expect(towns.get("10001010")).toEqual(["10001020"]); expect(towns.get("10001020")).toEqual(["10001010", "10002010"]); expect(towns.get("10003010")).toEqual([]); const counties = townNeighbors(topology, ["10001000", "10002000", "10003000"], "county"); expect(counties.get("10001000")).toEqual(["10002000"]); }); });