|
30 | 30 | from multilingualprogramming.keyword.language_pack_validator import ( |
31 | 31 | LanguagePackValidator, |
32 | 32 | ) |
| 33 | +from multilingualprogramming.runtime.ai_runtime import AIRuntime, MockProvider |
33 | 34 |
|
34 | 35 |
|
35 | 36 | def _parse(source, lang="en"): |
@@ -420,6 +421,54 @@ def test_cmd_run_show_backend_writes_report_to_stderr(self): |
420 | 421 | self.assertEqual(stdout.getvalue(), "hello\n") |
421 | 422 | self.assertIn("[backend] python (python-codegen-exec)", stderr.getvalue()) |
422 | 423 |
|
| 424 | + def test_cmd_run_registers_default_mock_provider_for_ai_programs(self): |
| 425 | + with tempfile.NamedTemporaryFile( |
| 426 | + "w", suffix=".multi", delete=False, encoding="utf-8" |
| 427 | + ) as tmp: |
| 428 | + tmp.write( |
| 429 | + 'fn main() uses ai:\n' |
| 430 | + ' print(prompt @claude-sonnet: "hello")\n' |
| 431 | + 'main()\n' |
| 432 | + ) |
| 433 | + tmp_path = tmp.name |
| 434 | + |
| 435 | + args = Namespace(file=tmp_path, lang="en", show_backend=False) |
| 436 | + stdout = io.StringIO() |
| 437 | + try: |
| 438 | + AIRuntime.reset() |
| 439 | + with patch("sys.stdout", stdout): |
| 440 | + cmd_run(args) |
| 441 | + finally: |
| 442 | + AIRuntime.reset() |
| 443 | + Path(tmp_path).unlink(missing_ok=True) |
| 444 | + |
| 445 | + self.assertIn("mock response to:", stdout.getvalue()) |
| 446 | + |
| 447 | + def test_cmd_run_preserves_existing_registered_provider(self): |
| 448 | + with tempfile.NamedTemporaryFile( |
| 449 | + "w", suffix=".multi", delete=False, encoding="utf-8" |
| 450 | + ) as tmp: |
| 451 | + tmp.write( |
| 452 | + 'fn main() uses ai:\n' |
| 453 | + ' print(prompt @claude-sonnet: "hello")\n' |
| 454 | + 'main()\n' |
| 455 | + ) |
| 456 | + tmp_path = tmp.name |
| 457 | + |
| 458 | + args = Namespace(file=tmp_path, lang="en", show_backend=False) |
| 459 | + stdout = io.StringIO() |
| 460 | + provider = MockProvider().add_response("custom provider response") |
| 461 | + try: |
| 462 | + AIRuntime.reset() |
| 463 | + AIRuntime.register(provider) |
| 464 | + with patch("sys.stdout", stdout): |
| 465 | + cmd_run(args) |
| 466 | + finally: |
| 467 | + AIRuntime.reset() |
| 468 | + Path(tmp_path).unlink(missing_ok=True) |
| 469 | + |
| 470 | + self.assertEqual(stdout.getvalue().strip(), "custom provider response") |
| 471 | + |
423 | 472 |
|
424 | 473 | # --------------------------------------------------------------- |
425 | 474 | # WS7: REPL improvements |
|
0 commit comments