Mercurial
diff mrjunejune/inference/mock_sidecar_test.py @ 261:b401627fc49e
Add JRPG mock flows and interactive previews
Add scripted mock SSE commands, custom event forwarding, animated chat turns, full-height message navigation, and a cyberpunk resume dossier.
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <mrjunejune@users.noreply.github.com> |
|---|---|
| date | Wed, 05 Aug 2026 20:38:32 -0700 |
| parents | |
| children | 056790c4fb0d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mrjunejune/inference/mock_sidecar_test.py Wed Aug 05 20:38:32 2026 -0700 @@ -0,0 +1,228 @@ +import asyncio +import json +import pathlib +import tempfile +import unittest + +from mrjunejune.inference.mock_sidecar import ( + MockConfig, + MockSidecar, + load_mock_config, +) + + +class MockSidecarTest(unittest.IsolatedAsyncioTestCase): + async def asyncSetUp(self): + self.events = [] + + async def emit(payload): + self.events.append(payload) + + packaged = load_mock_config( + pathlib.Path(__file__).with_name("mock_responses.json") + ) + self.config = MockConfig( + delay_ms=0, + fallback=packaged.fallback, + commands=packaged.commands, + ) + self.sidecar = MockSidecar(emit, self.config) + + def test_packaged_commands_are_selectable(self): + self.assertEqual(self.config.select("!hello").name, "!hello") + self.assertEqual(self.config.select("!response please").name, "!response") + self.assertEqual(self.config.select("ordinary prompt").name, "!response") + self.assertEqual(self.config.select("!tool").name, "!tool") + self.assertEqual(self.config.select("!plan").name, "!plan") + + def test_response_file_rejects_invalid_scripts(self): + invalid_configs = ( + { + "fallback": "!hello", + "commands": { + "!hello": { + "events": [ + { + "type": "assistant.completed", + "content": "Hello", + } + ], + "unknown": True, + } + }, + }, + { + "fallback": "hello", + "commands": { + "hello": { + "events": [ + { + "type": "assistant.completed", + "content": "Hello", + } + ] + } + }, + }, + { + "fallback": "!hello", + "commands": { + "!hello": { + "events": [ + { + "type": "assistant.delta", + "delta": "Different", + }, + { + "type": "assistant.completed", + "content": "Hello", + }, + ] + } + }, + }, + { + "delay_ms": 60001, + "fallback": "!hello", + "commands": { + "!hello": { + "events": [ + { + "type": "assistant.completed", + "content": "Hello", + } + ] + } + }, + }, + { + "fallback": "!hello", + "commands": { + "!hello": { + "events": [ + { + "type": "assistant.completed", + "content": "Hello", + }, + { + "type": "assistant.delta", + "delta": "Hello", + }, + ] + } + }, + }, + ) + for payload in invalid_configs: + with self.subTest(payload=payload): + with tempfile.TemporaryDirectory() as root: + path = pathlib.Path(root) / "responses.json" + path.write_text(json.dumps(payload), encoding="utf-8") + with self.assertRaises(ValueError): + load_mock_config(path) + + async def test_hello_command_streams_scripted_events(self): + await self.sidecar.dispatch( + { + "command": "turn.start", + "request_id": "request-1", + "conversation_id": "conversation-1", + "prompt": "!hello", + } + ) + await self.sidecar.wait_for_idle() + + self.assertEqual( + [event["type"] for event in self.events], + [ + "turn.accepted", + "assistant.delta", + "assistant.delta", + "assistant.completed", + "turn.done", + ], + ) + deltas = "".join( + event["delta"] + for event in self.events + if event["type"] == "assistant.delta" + ) + completed = next( + event["content"] + for event in self.events + if event["type"] == "assistant.completed" + ) + self.assertEqual(deltas, completed) + self.assertEqual(self.events[-1]["mock_command"], "!hello") + + async def test_tool_command_preserves_custom_event_payloads(self): + await self.sidecar.dispatch( + { + "command": "turn.start", + "request_id": "request-2", + "conversation_id": "conversation-2", + "prompt": "!tool", + } + ) + await self.sidecar.wait_for_idle() + + started = next( + event for event in self.events if event["type"] == "tool.started" + ) + completed = next( + event for event in self.events if event["type"] == "tool.completed" + ) + self.assertEqual(started["tool"]["name"], "site_search") + self.assertEqual(completed["tool"]["result"]["matches"], 3) + + async def test_error_command_emits_failed_turn(self): + await self.sidecar.dispatch( + { + "command": "turn.start", + "request_id": "request-3", + "conversation_id": "conversation-3", + "prompt": "!error", + } + ) + await self.sidecar.wait_for_idle() + + self.assertEqual( + [event["type"] for event in self.events], + ["turn.accepted", "turn.error", "turn.done"], + ) + self.assertTrue(self.events[-1]["failed"]) + + async def test_abort_stops_active_command(self): + self.sidecar._config = MockConfig( + delay_ms=50, + fallback=self.config.fallback, + commands=self.config.commands, + ) + await self.sidecar.dispatch( + { + "command": "turn.start", + "request_id": "request-4", + "conversation_id": "conversation-4", + "prompt": "!response", + } + ) + await asyncio.sleep(0.01) + await self.sidecar.dispatch( + { + "command": "turn.abort", + "request_id": "abort-4", + "conversation_id": "conversation-4", + } + ) + + target_done = next( + event + for event in self.events + if event["type"] == "turn.done" + and event["request_id"] == "request-4" + ) + self.assertTrue(target_done["aborted"]) + + +if __name__ == "__main__": + unittest.main()