|
1 | 1 | # Copyright (c) Microsoft. All rights reserved.
|
2 | 2 |
|
3 | 3 | import os
|
| 4 | +import tempfile |
4 | 5 | from unittest.mock import patch
|
5 | 6 |
|
6 | 7 | import pytest
|
@@ -418,3 +419,210 @@ def test_function_model_dump_json():
|
418 | 419 | model_dump_json = function.model_dump_json()
|
419 | 420 | assert isinstance(model_dump_json, str)
|
420 | 421 | assert "test" in model_dump_json
|
| 422 | + |
| 423 | + |
| 424 | +def test_from_directory_utf8_encoding_default(): |
| 425 | + """Test loading plugin with default UTF-8 encoding.""" |
| 426 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 427 | + prompt_path = os.path.join(temp_dir, "skprompt.txt") |
| 428 | + config_path = os.path.join(temp_dir, "config.json") |
| 429 | + |
| 430 | + # UTF-8 content with international characters |
| 431 | + prompt_content = """Hello! I can help with questions in multiple languages: |
| 432 | + English: Hello world! |
| 433 | + Spanish: ¡Hola mundo! |
| 434 | + Chinese: 你好世界! |
| 435 | + Japanese: こんにちは世界! |
| 436 | + |
| 437 | + Question: {{$input}} |
| 438 | + """ |
| 439 | + |
| 440 | + config_content = """{ |
| 441 | + "schema": 1, |
| 442 | + "description": "A multilingual assistant function", |
| 443 | + "input_variables": [ |
| 444 | + { |
| 445 | + "name": "input", |
| 446 | + "description": "User's question", |
| 447 | + "required": true |
| 448 | + } |
| 449 | + ] |
| 450 | +}""" |
| 451 | + |
| 452 | + # Write files with UTF-8 encoding |
| 453 | + with open(prompt_path, "w", encoding="utf-8") as f: |
| 454 | + f.write(prompt_content) |
| 455 | + with open(config_path, "w", encoding="utf-8") as f: |
| 456 | + f.write(config_content) |
| 457 | + |
| 458 | + # Test default behavior (should use UTF-8) |
| 459 | + function = KernelFunctionFromPrompt.from_directory(temp_dir) |
| 460 | + assert function.name == os.path.basename(temp_dir) |
| 461 | + assert function.description == "A multilingual assistant function" |
| 462 | + assert "你好世界" in function.prompt_template.prompt_template_config.template |
| 463 | + assert "こんにちは世界" in function.prompt_template.prompt_template_config.template |
| 464 | + |
| 465 | + |
| 466 | +def test_from_directory_explicit_utf8_encoding(): |
| 467 | + """Test loading plugin with explicit UTF-8 encoding.""" |
| 468 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 469 | + prompt_path = os.path.join(temp_dir, "skprompt.txt") |
| 470 | + config_path = os.path.join(temp_dir, "config.json") |
| 471 | + |
| 472 | + prompt_content = "Hello with UTF-8 characters: ñáéíóú {{$input}}" |
| 473 | + config_content = '{"schema": 1, "description": "Test with UTF-8 characters"}' |
| 474 | + |
| 475 | + with open(prompt_path, "w", encoding="utf-8") as f: |
| 476 | + f.write(prompt_content) |
| 477 | + with open(config_path, "w", encoding="utf-8") as f: |
| 478 | + f.write(config_content) |
| 479 | + |
| 480 | + # Test explicit UTF-8 encoding |
| 481 | + function = KernelFunctionFromPrompt.from_directory(temp_dir, encoding="utf-8") |
| 482 | + assert function.description == "Test with UTF-8 characters" |
| 483 | + assert "ñáéíóú" in function.prompt_template.prompt_template_config.template |
| 484 | + |
| 485 | + |
| 486 | +def test_from_directory_latin1_encoding(): |
| 487 | + """Test loading plugin with Latin-1 encoding.""" |
| 488 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 489 | + prompt_path = os.path.join(temp_dir, "skprompt.txt") |
| 490 | + config_path = os.path.join(temp_dir, "config.json") |
| 491 | + |
| 492 | + # Content with Latin-1 characters (Western European) |
| 493 | + prompt_content = """Assistant for Western European languages: |
| 494 | + French: café, naïve, résumé |
| 495 | + German: Müller, Größe, weiß |
| 496 | + Spanish: niño, señora, años |
| 497 | + |
| 498 | + Question: {{$input}} |
| 499 | + """ |
| 500 | + |
| 501 | + config_content = """{ |
| 502 | + "schema": 1, |
| 503 | + "description": "Western European language assistant", |
| 504 | + "input_variables": [ |
| 505 | + { |
| 506 | + "name": "input", |
| 507 | + "description": "User's question", |
| 508 | + "required": true |
| 509 | + } |
| 510 | + ] |
| 511 | +}""" |
| 512 | + |
| 513 | + # Write files with Latin-1 encoding |
| 514 | + with open(prompt_path, "w", encoding="latin-1") as f: |
| 515 | + f.write(prompt_content) |
| 516 | + with open(config_path, "w", encoding="latin-1") as f: |
| 517 | + f.write(config_content) |
| 518 | + |
| 519 | + # Load with Latin-1 encoding |
| 520 | + function = KernelFunctionFromPrompt.from_directory(temp_dir, encoding="latin-1") |
| 521 | + assert function.description == "Western European language assistant" |
| 522 | + assert "café" in function.prompt_template.prompt_template_config.template |
| 523 | + assert "Müller" in function.prompt_template.prompt_template_config.template |
| 524 | + assert "niño" in function.prompt_template.prompt_template_config.template |
| 525 | + |
| 526 | + |
| 527 | +def test_from_directory_cp1252_encoding(): |
| 528 | + """Test loading plugin with Windows-1252 encoding.""" |
| 529 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 530 | + prompt_path = os.path.join(temp_dir, "skprompt.txt") |
| 531 | + config_path = os.path.join(temp_dir, "config.json") |
| 532 | + |
| 533 | + # Content with Windows-1252 specific characters |
| 534 | + prompt_content = """Windows text processing assistant: |
| 535 | + Smart quotes: "Hello" and 'world' |
| 536 | + Em dash: Yes—absolutely! |
| 537 | + Ellipsis: Wait… |
| 538 | + |
| 539 | + Question: {{$input}} |
| 540 | + """ |
| 541 | + |
| 542 | + config_content = """{ |
| 543 | + "schema": 1, |
| 544 | + "description": "Windows text processing assistant", |
| 545 | + "input_variables": [ |
| 546 | + { |
| 547 | + "name": "input", |
| 548 | + "description": "User's question about text processing", |
| 549 | + "required": true |
| 550 | + } |
| 551 | + ] |
| 552 | +}""" |
| 553 | + |
| 554 | + # Write files with Windows-1252 encoding |
| 555 | + with open(prompt_path, "w", encoding="cp1252") as f: |
| 556 | + f.write(prompt_content) |
| 557 | + with open(config_path, "w", encoding="cp1252") as f: |
| 558 | + f.write(config_content) |
| 559 | + |
| 560 | + # Load with Windows-1252 encoding |
| 561 | + function = KernelFunctionFromPrompt.from_directory(temp_dir, encoding="cp1252") |
| 562 | + assert function.description == "Windows text processing assistant" |
| 563 | + assert '"Hello"' in function.prompt_template.prompt_template_config.template |
| 564 | + assert "Yes—absolutely" in function.prompt_template.prompt_template_config.template |
| 565 | + assert "Wait…" in function.prompt_template.prompt_template_config.template |
| 566 | + |
| 567 | + |
| 568 | +def test_from_directory_with_plugin_name_and_encoding(): |
| 569 | + """Test loading plugin with both plugin name and encoding specified.""" |
| 570 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 571 | + prompt_path = os.path.join(temp_dir, "skprompt.txt") |
| 572 | + config_path = os.path.join(temp_dir, "config.json") |
| 573 | + |
| 574 | + prompt_content = "Simple assistant: {{$input}}" |
| 575 | + config_content = '{"schema": 1, "description": "Simple assistant"}' |
| 576 | + |
| 577 | + with open(prompt_path, "w", encoding="utf-8") as f: |
| 578 | + f.write(prompt_content) |
| 579 | + with open(config_path, "w", encoding="utf-8") as f: |
| 580 | + f.write(config_content) |
| 581 | + |
| 582 | + # Load with both plugin name and encoding specified |
| 583 | + function = KernelFunctionFromPrompt.from_directory( |
| 584 | + path=temp_dir, plugin_name="MyCustomPlugin", encoding="utf-8" |
| 585 | + ) |
| 586 | + assert function.metadata.plugin_name == "MyCustomPlugin" |
| 587 | + assert function.description == "Simple assistant" |
| 588 | + assert function.prompt_template.prompt_template_config.template == "Simple assistant: {{$input}}" |
| 589 | + |
| 590 | + |
| 591 | +def test_from_directory_encoding_error_handling(): |
| 592 | + """Test that incorrect encoding raises appropriate error.""" |
| 593 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 594 | + prompt_path = os.path.join(temp_dir, "skprompt.txt") |
| 595 | + config_path = os.path.join(temp_dir, "config.json") |
| 596 | + |
| 597 | + # Write UTF-8 content |
| 598 | + prompt_content = "Hello with UTF-8: 你好世界 {{$input}}" |
| 599 | + config_content = '{"schema": 1, "description": "UTF-8 content"}' |
| 600 | + |
| 601 | + with open(prompt_path, "w", encoding="utf-8") as f: |
| 602 | + f.write(prompt_content) |
| 603 | + with open(config_path, "w", encoding="utf-8") as f: |
| 604 | + f.write(config_content) |
| 605 | + |
| 606 | + # Try to read with ASCII encoding - should fail |
| 607 | + with pytest.raises(UnicodeDecodeError): |
| 608 | + KernelFunctionFromPrompt.from_directory(temp_dir, encoding="ascii") |
| 609 | + |
| 610 | + |
| 611 | +def test_from_directory_backward_compatibility(): |
| 612 | + """Test that existing code without encoding parameter still works.""" |
| 613 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 614 | + prompt_path = os.path.join(temp_dir, "skprompt.txt") |
| 615 | + config_path = os.path.join(temp_dir, "config.json") |
| 616 | + |
| 617 | + prompt_content = "Basic ASCII content: {{$input}}" |
| 618 | + config_content = '{"schema": 1, "description": "Basic function"}' |
| 619 | + |
| 620 | + with open(prompt_path, "w", encoding="utf-8") as f: |
| 621 | + f.write(prompt_content) |
| 622 | + with open(config_path, "w", encoding="utf-8") as f: |
| 623 | + f.write(config_content) |
| 624 | + |
| 625 | + # Test that old calling style still works |
| 626 | + function = KernelFunctionFromPrompt.from_directory(temp_dir) |
| 627 | + assert function.description == "Basic function" |
| 628 | + assert function.prompt_template.prompt_template_config.template == "Basic ASCII content: {{$input}}" |
0 commit comments