import sys from pathlib import Path # Add src to path sys.path.append(str(Path.cwd() / 'src')) from onyx import OnyxCLI import shutil def test_extension_logic(): # Setup test_dir = Path('reproduce_test_vault') if test_dir.exists(): shutil.rmtree(test_dir) test_dir.mkdir() # We need to set a default vault for the CLI to work without error # Since we can't easily inject config without mocking, we'll rely on the fact that # create_note calls _get_vault_and_path. # We will mock the config file path or just use the class methods that don't require it if possible, # but create_note DOES require it. # Let's just instantiate CLI and manually set the vault path in the internal config if needed, # or better, just use the public method set_default_vault to configure our test vault. cli = OnyxCLI() try: cli.set_default_vault('reproduce_vault', str(test_dir.resolve())) except Exception as e: print(f"Setup warning: {e}") # Test 1: No extension print("Test 1: 'note1' -> should be 'note1.md'") try: cli.create_note('note1', 'content') if (test_dir / 'note1.md').exists(): print("PASS: note1.md created") else: print("FAIL: note1.md NOT created") except Exception as e: print(f"FAIL: Test 1 error: {e}") # Test 2: With .md extension print("\nTest 2: 'note2.md' -> should be 'note2.md'") try: cli.create_note('note2.md', 'content') if (test_dir / 'note2.md').exists() and not (test_dir / 'note2.md.md').exists(): print("PASS: note2.md created correctly") else: print("FAIL: note2.md handling incorrect") except Exception as e: print(f"FAIL: Test 2 error: {e}") # Test 3: With other extension (potential issue) print("\nTest 3: 'note3.txt' -> should be 'note3.txt.md' (assuming user intent)") try: cli.create_note('note3.txt', 'content') if (test_dir / 'note3.txt.md').exists(): print("PASS: note3.txt.md created") elif (test_dir / 'note3.txt').exists(): print("FAIL: created note3.txt instead of note3.txt.md") else: print("FAIL: unexpected result for note3.txt") except Exception as e: print(f"FAIL: Test 3 error: {e}") # Test 4: Nested folders print("\nTest 4: 'folder/nested/note' -> should create folders") try: cli.create_note('folder/nested/note', 'content') if (test_dir / 'folder' / 'nested' / 'note.md').exists(): print("PASS: Nested folders created") else: print("FAIL: Nested folders NOT created") except Exception as e: print(f"FAIL: Test 4 error: {e}") # Cleanup try: shutil.rmtree(test_dir) except: pass if __name__ == "__main__": test_extension_logic()