| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import pytest
- from cq2_sdk.tasks.base import Task, TaskResult
- from cq2_sdk.tasks.registry import TaskRegistry
- from cq2_sdk.tasks.context import TaskContext
- from cq2_sdk.tasks.stubs import (
- T01CreateRole, T02DismissHint, T13Activity, _REGISTRY,
- )
- class FakeCtx:
- """Minimal stand-in for TaskContext (we only need it to construct the registry)."""
- pass
- @pytest.mark.asyncio
- async def test_task_subclass_runs():
- class T(Task):
- id = "fake"
- name = "Fake"
- async def run(self, ctx):
- return TaskResult(success=True, elapsed=0.123, error=None)
- t = T()
- r = await t.run(ctx=None)
- assert r.success
- assert r.elapsed == 0.123
- assert r.error is None
- @pytest.mark.asyncio
- async def test_task_failure_reported():
- class T(Task):
- id = "fail"
- name = "Fail"
- async def run(self, ctx):
- return TaskResult(success=False, elapsed=0.5, error="boom")
- t = T()
- r = await t.run(ctx=None)
- assert not r.success
- assert r.error == "boom"
- def test_all_13_tasks_registered():
- # All 13 in-game task IDs must be present
- for i in range(1, 14):
- tid = f"t{i:02d}"
- assert tid in _REGISTRY, f"missing {tid}"
- def test_task_ids_and_names():
- assert T01CreateRole.id == "t01"
- assert T01CreateRole.name == "Create role"
- assert T02DismissHint.id == "t02"
- assert T13Activity.id == "t13"
- def test_registry_lazy_instantiation():
- ctx = FakeCtx() # type: ignore[arg-type]
- reg = TaskRegistry(ctx) # type: ignore[arg-type]
- t1a = reg.t01
- t1b = reg.t01
- # Same instance returned (cached)
- assert t1a is t1b
- # Different tasks are different instances
- t2 = reg.t02
- assert t1a is not t2
- def test_registry_unknown_id_raises():
- ctx = FakeCtx() # type: ignore[arg-type]
- reg = TaskRegistry(ctx) # type: ignore[arg-type]
- with pytest.raises(AttributeError, match="No task"):
- reg.t99
- def test_registry_private_attr_raises():
- ctx = FakeCtx() # type: ignore[arg-type]
- reg = TaskRegistry(ctx) # type: ignore[arg-type]
- with pytest.raises(AttributeError):
- reg.__internal # double underscore triggers __getattr__
- def test_registry_all_ids_returns_13():
- ctx = FakeCtx() # type: ignore[arg-type]
- reg = TaskRegistry(ctx) # type: ignore[arg-type]
- assert len(reg.all_ids()) == 13
- assert "t01" in reg.all_ids()
- assert "t13" in reg.all_ids()
|