import sys from pathlib import Path import subprocess import os import shutil # Clean up previous test vault vault_path = Path("test_vault").resolve() if vault_path.exists(): shutil.rmtree(vault_path) vault_path.mkdir(exist_ok=True) # Create a note with French characters manually note_name = "french_test.md" note_path = vault_path / note_name content = "C'est l'été à Paris. Ça va bien?" # Write with default encoding (likely cp1252 on Windows or utf-8) try: # Write as UTF-8 (no BOM) to simulate a "good" file note_path.write_text(content, encoding="utf-8") print(f"Created {note_name} with utf-8") except Exception as e: print(f"Error creating note: {e}") # Import OnyxCLI sys.path.append(str(Path("src").resolve())) from onyx import OnyxCLI cli = OnyxCLI() # Mock config to point to our test vault cli.config_file = Path("test_config.yaml").resolve() cli.config_file.write_text(f"default_vault: test\nvaults:\n test: {str(vault_path).replace(os.sep, '/')}", encoding="utf-8") print("\n--- Reading note with OnyxCLI ---") try: # This calls read_text() internally. # With our fix (utf-8-sig), it should read utf-8 (no BOM) correctly too. cli.print_note(note_name, vault="test") except Exception as e: print(f"Error reading note: {e}") print("\n--- Creating note with OnyxCLI ---") new_note_name = "french_cli_created.md" new_content = "Hélène et François" try: cli.create_note(new_note_name, content=new_content, vault="test", overwrite=True) # Check how it was saved saved_path = vault_path / new_note_name # Read as bytes to check for BOM raw_bytes = saved_path.read_bytes() print(f"Saved bytes: {raw_bytes}") if raw_bytes.startswith(b'\xef\xbb\xbf'): print("SUCCESS: BOM detected") else: print("FAILURE: No BOM detected") # Try reading it back with utf-8-sig (which handles BOM) print(f"Content read back as utf-8-sig: {saved_path.read_text(encoding='utf-8-sig')}") except Exception as e: print(f"Error creating/checking note: {e}")