tests/test_offline.py
9.1 KB| 1 | # -*- coding: utf-8 -*- |
| 2 | """离线测试:不连游戏,只喂一份观测文本,验证解析 / 记忆 / 选点。 |
| 3 | |
| 4 | python -m unittest discover -s tests -v # 在包根目录跑 |
| 5 | """ |
| 6 | import os |
| 7 | import sys |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | |
| 11 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 12 | |
| 13 | from forage_memory import readmap, spec # noqa: E402 |
| 14 | from forage_memory.bwf import OfflineClient # noqa: E402 |
| 15 | from forage_memory.detour import Ranger # noqa: E402 |
| 16 | from forage_memory.memory import Memory # noqa: E402 |
| 17 | |
| 18 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 19 | FIXTURE = os.path.join(HERE, "fixture_state.txt") |
| 20 | |
| 21 | |
| 22 | def fixture_text(): |
| 23 | with open(FIXTURE, encoding="utf-8") as f: |
| 24 | return f.read() |
| 25 | |
| 26 | |
| 27 | class TestParse(unittest.TestCase): |
| 28 | def setUp(self): |
| 29 | self.st = readmap.parse(fixture_text()) |
| 30 | |
| 31 | def test_date_and_clock(self): |
| 32 | self.assertEqual((self.st.year, self.st.season, self.st.day), (1, "春", 16)) |
| 33 | self.assertEqual(self.st.clock, (10, 20)) |
| 34 | self.assertEqual(self.st.abs_day, 112 + 16) # 第1年春16日 |
| 35 | |
| 36 | def test_loc_and_pos(self): |
| 37 | self.assertEqual(self.st.loc, "Town") |
| 38 | self.assertEqual(self.st.pos, (2, 4)) |
| 39 | |
| 40 | def test_bag(self): |
| 41 | self.assertEqual(self.st.bag_free, 2) |
| 42 | names = [i.name for i in self.st.items] |
| 43 | self.assertIn("Wood", names) |
| 44 | self.assertIn("Stone", names) |
| 45 | wood = [i for i in self.st.items if i.name == "Wood"][0] |
| 46 | self.assertEqual(wood.stack, 99) |
| 47 | stone = [i for i in self.st.items if i.name == "Stone"][0] |
| 48 | self.assertTrue(stone.keep, "带【留着】的物品必须被标成 keep") |
| 49 | |
| 50 | def test_grid_aligned(self): |
| 51 | # 地图原点对了,@ 必须正好落在「你站在」的坐标上 |
| 52 | self.assertEqual(self.st.grid.get((2, 4)), "@") |
| 53 | self.assertEqual(self.st.grid.get((3, 4)), "b") |
| 54 | self.assertEqual(self.st.grid.get((1, 2)), "*") |
| 55 | |
| 56 | def test_trash(self): |
| 57 | self.assertEqual(sorted(self.st.trash), [(5, 5), (14, 6)]) |
| 58 | |
| 59 | def test_npcs(self): |
| 60 | self.assertEqual([n for n, _ in self.st.npcs], ["Alex", "Evelyn"]) |
| 61 | |
| 62 | def test_berry_season(self): |
| 63 | self.assertEqual(self.st.berry_item(), "Salmonberry") # 春16 在莓子季里 |
| 64 | self.assertEqual(self.st.berry_count(), 12) |
| 65 | |
| 66 | def test_nearby_only_bushes_in_season(self): |
| 67 | kinds = {(k, p) for _d, k, p in self.st.nearby(20)} |
| 68 | self.assertIn(("forage", (1, 2)), kinds) |
| 69 | self.assertIn(("bush", (3, 4)), kinds) |
| 70 | # 非莓子季的灌木不该出现在目标里 |
| 71 | txt = fixture_text().replace("第1年春16日", "第1年春20日") |
| 72 | st2 = readmap.parse(txt) |
| 73 | self.assertIsNone(st2.berry_item()) |
| 74 | kinds2 = {(k, p) for _d, k, p in st2.nearby(20)} |
| 75 | self.assertNotIn(("bush", (3, 4)), kinds2) |
| 76 | |
| 77 | |
| 78 | class TestMemory(unittest.TestCase): |
| 79 | def setUp(self): |
| 80 | self.dir = tempfile.mkdtemp() |
| 81 | self.path = os.path.join(self.dir, "memory.json") |
| 82 | self.m = Memory(self.path) |
| 83 | |
| 84 | def test_note_only_once(self): |
| 85 | self.assertTrue(self.m.note("Town", "trash", (5, 5), 128)) |
| 86 | self.assertFalse(self.m.note("Town", "trash", (5, 5), 129)) |
| 87 | self.assertEqual(len(self.m.entries("Town", "trash")), 1) |
| 88 | |
| 89 | def test_mark_and_candidates(self): |
| 90 | self.m.note("Town", "trash", (5, 5), 128) |
| 91 | self.m.note("Town", "trash", (14, 6), 128) |
| 92 | cands = self.m.candidates("Town", "trash", (2, 4), 128, 6) |
| 93 | self.assertEqual([p for _d, p in cands], [(5, 5)]) # (14,6) 太远 |
| 94 | self.m.mark("Town", "trash", (5, 5), 128, True) |
| 95 | self.assertEqual(self.m.candidates("Town", "trash", (2, 4), 128, 6), []) # 今天翻过了 |
| 96 | # 换一天又能翻 |
| 97 | self.assertEqual(len(self.m.candidates("Town", "trash", (2, 4), 129, 6)), 1) |
| 98 | |
| 99 | def test_atomic_and_reload(self): |
| 100 | self.m.note("Town", "trash", (5, 5), 128) |
| 101 | self.m.save() |
| 102 | m2 = Memory(self.path) |
| 103 | self.assertEqual(len(m2.entries("Town", "trash")), 1) |
| 104 | self.assertIn("schema", m2.data) |
| 105 | |
| 106 | def test_survives_corrupt_file(self): |
| 107 | with open(self.path, "w", encoding="utf-8") as f: |
| 108 | f.write("{ this is not json") |
| 109 | m = Memory(self.path) # 不能抛异常 |
| 110 | self.assertEqual(m.entries("Town", "trash"), []) |
| 111 | |
| 112 | def test_prune_forgets_never_worked(self): |
| 113 | self.m.note("Town", "bush", (3, 4), 100) |
| 114 | self.m.note("Town", "bush", (4, 4), 100) |
| 115 | self.m.mark("Town", "bush", (4, 4), 101, True) # 这一丛真的结果了 |
| 116 | self.m.save() |
| 117 | self.assertEqual(self.m.prune(100 + 41), 1) |
| 118 | left = [(x, y) for x, y, _l, _c, _f in self.m.entries("Town", "bush")] |
| 119 | self.assertEqual(left, [(4, 4)]) |
| 120 | |
| 121 | |
| 122 | class TestRanger(unittest.TestCase): |
| 123 | def setUp(self): |
| 124 | self.dir = tempfile.mkdtemp() |
| 125 | self.mem = Memory(os.path.join(self.dir, "memory.json")) |
| 126 | self.txt = fixture_text() |
| 127 | self.client = OfflineClient(self.txt) |
| 128 | self.r = Ranger(self.client, self.mem, {"trash_safe_radius": 10}) |
| 129 | |
| 130 | def test_scan_learns_trash_and_bushes(self): |
| 131 | st = readmap.parse(self.txt) |
| 132 | self.r.scan(st) |
| 133 | self.assertEqual(len(self.mem.entries("Town", "trash")), 2) |
| 134 | self.assertEqual(len(self.mem.entries("Town", "bush")), 2) |
| 135 | |
| 136 | def test_no_scan_bush_out_of_season(self): |
| 137 | txt = self.txt.replace("第1年春16日", "第1年春22日") |
| 138 | st = readmap.parse(txt) |
| 139 | Ranger(OfflineClient(txt), self.mem, {}).scan(st) |
| 140 | self.assertEqual(len(self.mem.entries("Town", "bush")), 0) |
| 141 | |
| 142 | def test_trash_skipped_when_villager_close(self): |
| 143 | st = readmap.parse(self.txt) |
| 144 | r = Ranger(self.client, self.mem, {"trash_safe_radius": 10}) |
| 145 | r.trash(st, radius=16) |
| 146 | self.assertEqual(self.client.log, [], "有人在场时不能有任何动作") |
| 147 | self.assertTrue(any("有人" in l for l in r.log)) |
| 148 | |
| 149 | def test_trash_digs_when_alone(self): |
| 150 | txt = self.txt.replace("- Alex(Alex)在 (8,7),正朝左\n", "") \ |
| 151 | .replace("- Evelyn(Evelyn)在 (20,20),正朝上\n", "") |
| 152 | client = OfflineClient(txt) |
| 153 | r = Ranger(client, self.mem, {"trash_safe_radius": 10}) |
| 154 | got = r.trash(readmap.parse(txt), radius=6) |
| 155 | self.assertEqual(got, 1) # 只有 (5,5) 在 6 格内 |
| 156 | self.assertTrue(any(a[0] == "interact" for a in client.log)) |
| 157 | |
| 158 | def test_bag_full_blocks_picking(self): |
| 159 | txt = self.txt.replace("空位:2 个", "空位:0 个") |
| 160 | client = OfflineClient(txt) |
| 161 | r = Ranger(client, self.mem, {}) |
| 162 | st = readmap.parse(txt) |
| 163 | self.assertEqual(r.bush(st, radius=16), 0) |
| 164 | self.assertEqual(r.forage(st, radius=16), 0) |
| 165 | self.assertTrue(any("背包" in l for l in r.log)) |
| 166 | self.assertEqual(client.log, [], "背包满时不该发任何 interact") |
| 167 | |
| 168 | def test_bush_not_picked_out_of_season(self): |
| 169 | txt = self.txt.replace("第1年春16日", "第1年春22日") |
| 170 | client = OfflineClient(txt) |
| 171 | r = Ranger(client, self.mem, {}) |
| 172 | self.assertEqual(r.bush(readmap.parse(txt), radius=16), 0) |
| 173 | |
| 174 | |
| 175 | class TestSpec(unittest.TestCase): |
| 176 | def test_berry_seasons_match_game(self): |
| 177 | seasons = {(s["season"], s["start"], s["end"]) for s in spec.BERRY_SEASONS} |
| 178 | self.assertEqual(seasons, {("春", 15, 18), ("秋", 8, 11)}) |
| 179 | |
| 180 | |
| 181 | class TestNetworkGuard(unittest.TestCase): |
| 182 | """只允许连本机 —— 这条是安全声明,必须有回归保护(灵盾会看)。""" |
| 183 | |
| 184 | def test_loopback_ok(self): |
| 185 | from forage_memory.bwf import _assert_loopback |
| 186 | for u in ("http://127.0.0.1:18080/mcp", "http://localhost:18080/mcp", |
| 187 | "http://[::1]:18080/mcp"): |
| 188 | _assert_loopback(u) # 不抛异常即通过 |
| 189 | |
| 190 | def test_rejects_remote(self): |
| 191 | from forage_memory.bwf import _assert_loopback |
| 192 | # 203.0.113.x / 198.51.100.x 是 RFC 5737 留给文档的测试网段,不是真实内网 |
| 193 | for u in ("http://evil.example.com/mcp", "http://203.0.113.9:18080/mcp", |
| 194 | "http://198.51.100.7:18080/mcp", "https://api.example.com/x"): |
| 195 | with self.assertRaises(ValueError): |
| 196 | _assert_loopback(u) |
| 197 | |
| 198 | def test_client_constructor_guards(self): |
| 199 | from forage_memory.bwf import Client |
| 200 | with self.assertRaises(ValueError): |
| 201 | Client("http://evil.example.com/mcp") |
| 202 | |
| 203 | def test_no_shell_exec_in_package(self): |
| 204 | """包内不得出现执行命令 / 动态求值的调用。""" |
| 205 | import pathlib |
| 206 | banned = ("subprocess", "os.system", "os.popen", "eval(", "exec(") |
| 207 | pkg = pathlib.Path(__file__).resolve().parent.parent / "forage_memory" |
| 208 | for f in pkg.glob("*.py"): |
| 209 | src = f.read_text(encoding="utf-8") |
| 210 | for word in banned: |
| 211 | self.assertNotIn(word, src, f"{f.name} 里出现了 {word}") |
| 212 | |
| 213 | |
| 214 | if __name__ == "__main__": |
| 215 | unittest.main(verbosity=2) |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

