Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

test(harness): add lightweight scenario runner

cyhd59aab314bc8
diff --git a/standards/scenario-testing/STANDARD.md b/standards/scenario-testing/STANDARD.md
index eaa4c55ba..78c66f237 100644
--- a/standards/scenario-testing/STANDARD.md
+++ b/standards/scenario-testing/STANDARD.md
@@ -126,6 +126,8 @@ Recommended layering:
- fake transport/client
- fake process / completion handle
- base scenario harness
+ - step runner
+ - shared expectation helpers
- capability-specific harness
- auth setup
- attachment delivery
diff --git a/tests/scenario_harness/README.md b/tests/scenario_harness/README.md
index ce48c67b8..9d31e182e 100644
--- a/tests/scenario_harness/README.md
+++ b/tests/scenario_harness/README.md
@@ -12,6 +12,15 @@ Rules:
Current contents:
- `core.py`
- Generic scenario-harness primitives: event probe, fake IM client, base controller, fake process, base harness
+ Generic scenario-harness primitives: event probe, fake IM client, base controller, fake process, base harness, runner, expectation helpers
- `auth_setup.py`
Auth/setup-specific harness built on top of the generic layer
+
+Recommended layering:
+
+1. `core.py`
+ The reusable layer every capability can share.
+2. `<capability>.py`
+ Capability-specific harness or fixtures.
+3. `tests/test_<capability>_scenarios.py`
+ The scenario transcript itself.
diff --git a/tests/scenario_harness/core.py b/tests/scenario_harness/core.py
index d7fe8007d..13c845810 100644
--- a/tests/scenario_harness/core.py
+++ b/tests/scenario_harness/core.py
@@ -1,4 +1,5 @@
import asyncio
+from dataclasses import dataclass
from types import SimpleNamespace
from unittest.mock import AsyncMock
@@ -114,3 +115,54 @@ class BaseScenarioHarness:
def rendered_texts(self) -> list[str]:
return self.controller.im_probe.rendered_texts()
+
+
+@dataclass
+class ScenarioStep:
+ """One named scenario action executed against a harness."""
+
+ name: str
+ action: object
+
+
+class ScenarioRunner:
+ """Run a sequence of named async steps against one harness instance."""
+
+ def __init__(self, harness: BaseScenarioHarness):
+ self.harness = harness
+ self.history: list[str] = []
+
+ async def run(self, *steps: ScenarioStep):
+ for step in steps:
+ self.history.append(step.name)
+ result = step.action(self.harness)
+ if asyncio.iscoroutine(result):
+ await result
+
+
+class ScenarioExpect:
+ """Common assertions for scenario tests."""
+
+ @staticmethod
+ def text_contains(
+ harness: BaseScenarioHarness,
+ needle: str,
+ *,
+ index: int | None = None,
+ case_sensitive: bool = False,
+ ):
+ texts = harness.rendered_texts()
+ haystack = texts[index] if index is not None else "\n".join(texts)
+ expected = needle
+ if not case_sensitive:
+ haystack = haystack.lower()
+ expected = needle.lower()
+ assert expected in haystack, f"Expected to find {needle!r} in scenario output, got: {haystack!r}"
+
+ @staticmethod
+ def flow_missing(harness, flow_key: str):
+ assert flow_key not in harness.service._flows, f"Expected flow {flow_key} to be removed"
+
+ @staticmethod
+ def step_history(runner: ScenarioRunner, expected: list[str]):
+ assert runner.history == expected, f"Expected step history {expected!r}, got {runner.history!r}"
diff --git a/tests/test_agent_auth_setup_scenarios.py b/tests/test_agent_auth_setup_scenarios.py
index 3119d7b3b..5c737cb24 100644
--- a/tests/test_agent_auth_setup_scenarios.py
+++ b/tests/test_agent_auth_setup_scenarios.py
@@ -8,49 +8,61 @@ ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from tests.scenario_harness.auth_setup import AuthSetupScenarioHarness, FakeProcess
+from tests.scenario_harness.core import ScenarioExpect, ScenarioRunner, ScenarioStep
class AgentAuthSetupScenarioTests(unittest.IsolatedAsyncioTestCase):
async def test_codex_device_auth_scenario_reaches_terminal_success(self):
harness = AuthSetupScenarioHarness()
fake_process = FakeProcess()
+ runner = ScenarioRunner(harness)
harness.service._start_codex_process = AsyncMock(return_value=fake_process)
harness.service._read_codex_output = AsyncMock(return_value=None)
harness.service._verify_login = AsyncMock(return_value=(True, "Logged in using ChatGPT"))
harness.service._refresh_backend_runtime = AsyncMock()
- await harness.service.start_setup(harness.context, backend="codex", force_reset=True)
+ await runner.run(
+ ScenarioStep(
+ "start_setup",
+ lambda h: h.service.start_setup(h.context, backend="codex", force_reset=True),
+ ),
+ ScenarioStep(
+ "emit_device_url",
+ lambda h: h.service._handle_process_text(
+ h.context,
+ "codex",
+ "Open this URL to authenticate: https://auth.openai.com/codex/device",
+ ),
+ ),
+ ScenarioStep(
+ "emit_device_code",
+ lambda h: h.service._handle_process_text(
+ h.context,
+ "codex",
+ "Then enter this code: T74L-XU61D",
+ ),
+ ),
+ )
flow = harness.flow("codex")
self.assertFalse(flow.waiter_task.done())
-
- await harness.service._handle_process_text(
- harness.context,
- "codex",
- "Open this URL to authenticate: https://auth.openai.com/codex/device",
- )
- await harness.service._handle_process_text(
- harness.context,
- "codex",
- "Then enter this code: T74L-XU61D",
- )
-
fake_process.finish(0)
await flow.waiter_task
harness.service._refresh_backend_runtime.assert_awaited_once_with("codex")
- rendered = harness.rendered_texts()
- self.assertIn("starting codex", rendered[0].lower())
- self.assertIn("https://auth.openai.com/codex/device", rendered[1])
- self.assertIn("T74L-XU61D", rendered[1])
- self.assertIn("codex login is active again", rendered[-1].lower())
- self.assertNotIn("C1:codex", harness.service._flows)
+ ScenarioExpect.step_history(runner, ["start_setup", "emit_device_url", "emit_device_code"])
+ ScenarioExpect.text_contains(harness, "starting codex", index=0)
+ ScenarioExpect.text_contains(harness, "https://auth.openai.com/codex/device", index=1)
+ ScenarioExpect.text_contains(harness, "T74L-XU61D", index=1)
+ ScenarioExpect.text_contains(harness, "codex login is active again")
+ ScenarioExpect.flow_missing(harness, "C1:codex")
async def test_claude_manual_callback_scenario_accepts_plain_reply_and_completes(self):
harness = AuthSetupScenarioHarness()
fake_client = object()
completion_released = asyncio.Event()
callback_payloads = []
+ runner = ScenarioRunner(harness)
harness.service._start_claude_control_flow = AsyncMock(
return_value=(fake_client, "https://platform.claude.com/oauth/code/callback")
@@ -74,58 +86,72 @@ class AgentAuthSetupScenarioTests(unittest.IsolatedAsyncioTestCase):
harness.service._refresh_backend_runtime = AsyncMock()
harness.service._disconnect_claude_client = AsyncMock()
- await harness.service.start_setup(
- harness.context,
- backend="claude",
- force_reset=True,
- claude_login_method="console",
+ await runner.run(
+ ScenarioStep(
+ "start_setup",
+ lambda h: h.service.start_setup(
+ h.context,
+ backend="claude",
+ force_reset=True,
+ claude_login_method="console",
+ ),
+ ),
+ ScenarioStep(
+ "submit_callback_reply",
+ lambda h: h.service.maybe_consume_setup_reply(h.context, "auth-code#oauth-state"),
+ ),
)
flow = harness.flow("claude")
self.assertFalse(flow.waiter_task.done())
-
- consumed = await harness.service.maybe_consume_setup_reply(harness.context, "auth-code#oauth-state")
- self.assertTrue(consumed)
-
await flow.waiter_task
self.assertEqual(callback_payloads, [("auth-code", "oauth-state")])
harness.service._refresh_backend_runtime.assert_awaited_once_with("claude")
harness.service._disconnect_claude_client.assert_awaited_once_with(fake_client)
- rendered = harness.rendered_texts()
- self.assertIn("starting claude", rendered[0].lower())
- self.assertIn("https://platform.claude.com/oauth/code/callback", rendered[1])
- self.assertIn("submitted", rendered[2].lower())
- self.assertIn("claude login is active again", rendered[-1].lower())
- self.assertNotIn("C1:claude", harness.service._flows)
+ ScenarioExpect.step_history(runner, ["start_setup", "submit_callback_reply"])
+ ScenarioExpect.text_contains(harness, "starting claude", index=0)
+ ScenarioExpect.text_contains(harness, "https://platform.claude.com/oauth/code/callback", index=1)
+ ScenarioExpect.text_contains(harness, "submitted", index=2)
+ ScenarioExpect.text_contains(harness, "claude login is active again")
+ ScenarioExpect.flow_missing(harness, "C1:claude")
async def test_opencode_direct_key_scenario_installs_key_and_refreshes_runtime(self):
harness = AuthSetupScenarioHarness()
+ runner = ScenarioRunner(harness)
harness.service._resolve_opencode_provider = AsyncMock(return_value="opencode")
harness.service._install_opencode_api_key = AsyncMock()
harness.service._refresh_backend_runtime = AsyncMock()
harness.service._clear_backend_sessions_for_context = AsyncMock()
- await harness.service.start_setup(harness.context, backend="opencode", force_reset=True)
-
+ await runner.run(
+ ScenarioStep(
+ "start_setup",
+ lambda h: h.service.start_setup(h.context, backend="opencode", force_reset=True),
+ ),
+ )
flow = harness.flow("opencode")
self.assertTrue(flow.awaiting_code)
self.assertEqual(flow.url, "https://opencode.ai/auth")
- consumed = await harness.service.maybe_consume_setup_reply(
- harness.context,
- "oc_live_Abcdef1234567890",
+ await runner.run(
+ ScenarioStep(
+ "submit_direct_credential",
+ lambda h: h.service.maybe_consume_setup_reply(
+ h.context,
+ "oc_live_Abcdef1234567890",
+ ),
+ ),
)
- self.assertTrue(consumed)
harness.service._install_opencode_api_key.assert_awaited_once_with("opencode", "oc_live_Abcdef1234567890")
harness.service._refresh_backend_runtime.assert_awaited_once_with("opencode")
harness.service._clear_backend_sessions_for_context.assert_awaited_once_with("opencode", harness.context)
- rendered = harness.rendered_texts()
- self.assertIn("starting opencode", rendered[0].lower())
- self.assertIn("https://opencode.ai/auth", rendered[1])
- self.assertIn("opencode login is active again", rendered[-1].lower())
- self.assertNotIn("C1:opencode", harness.service._flows)
+ ScenarioExpect.step_history(runner, ["start_setup", "submit_direct_credential"])
+ ScenarioExpect.text_contains(harness, "starting opencode", index=0)
+ ScenarioExpect.text_contains(harness, "https://opencode.ai/auth", index=1)
+ ScenarioExpect.text_contains(harness, "opencode login is active again")
+ ScenarioExpect.flow_missing(harness, "C1:opencode")
if __name__ == "__main__":