Files
JWeBot/tests/test_release_065.py
JWeBot Contributors a74147e7f1
check / local-gate (push) Failing after 40s
Release alpha 0.6.6 autochess flow
2026-10-15 17:10:28 +08:00

103 lines
3.6 KiB
Python

"""Behavioral coverage for AI team enrollment in CS autochess."""
from __future__ import annotations
import json
import tempfile
import unittest
from pathlib import Path
from test_jwebot_edge import MODULE, config
from jwebot_official_extensions import cs_autochess as AUTOCHESS
class Release065AiEnrollmentTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.engine = MODULE.EdgeApplication(config(Path(self.temp.name)))
self.counter = 0
def tearDown(self):
self.engine.close()
self.temp.cleanup()
def command(self, question, *, sender="Alice", event_id=None):
self.counter += 1
response = self.engine.accept(
{
"event_id": event_id or f"two-ai-{self.counter}",
"group_fingerprint": "group-a",
"sender": sender,
"text": "@JWeBot " + question,
"mention_bot": True,
"confidence": 1.0,
"sender_confidence": 1.0,
"body_confidence": 1.0,
}
)
self.assertEqual(response["status"], "reply")
return list(response.get("texts") or [response["text"]])
def state(self):
row = self.engine.ledger.connection.execute(
"""
SELECT state_json FROM autochess_runs
WHERE group_fingerprint = 'group-a'
ORDER BY season_number DESC
LIMIT 1
"""
).fetchone()
self.assertIsNotNone(row)
return json.loads(row[0])
def test_two_humans_add_two_ai_and_lock_registration(self):
self.command("/自走棋开始")
self.command("/自走棋报名", sender="Bob")
added = "\n".join(self.command("/自走棋加AI战队"))
self.assertIn("AI 战队 A、AI 战队 B", added)
self.assertIn("2 人+2 AI", added)
self.assertIn(
"已经锁定",
"\n".join(self.command("/自走棋报名", sender="Carol")),
)
launch = "\n".join(self.command("/自走棋开赛"))
state = self.state()
self.assertIn("2 人+2 AI 测试赛", launch)
self.assertEqual(state["ai_test_variant"], "two_plus_two")
self.assertEqual(
state["seed_order"][2:],
[AUTOCHESS.AI_PERSON_KEY, AUTOCHESS.AI_SECOND_PERSON_KEY],
)
def test_three_humans_add_one_ai(self):
self.command("/自走棋开始")
self.command("/自走棋报名", sender="Bob")
self.command("/自走棋报名", sender="Carol")
added = "\n".join(self.command("/自走棋加AI战队"))
self.assertIn("3 人+1 AI", added)
self.command("/自走棋开赛")
state = self.state()
self.assertEqual(state["ai_test_variant"], "three_plus_one")
self.assertEqual(state["seed_order"][-1], AUTOCHESS.AI_PERSON_KEY)
self.assertTrue(state["players"][AUTOCHESS.AI_PERSON_KEY]["is_ai"])
def test_add_ai_replay_is_idempotent(self):
self.command("/自走棋开始")
self.command("/自走棋报名", sender="Bob")
first = self.command("/自走棋加AI战队", event_id="same-add-two-ai")
replay = self.command("/自走棋加AI战队", event_id="same-add-two-ai")
self.assertEqual(first, replay)
self.assertIn("已经加入", "\n".join(self.command("/自走棋加AI战队")))
state = self.state()
self.assertEqual(
sum(bool(player.get("is_ai")) for player in state["players"].values()),
2,
)
if __name__ == "__main__":
unittest.main()