# TestDrive-JSUI Architecture **Last Updated**: 2025-12-16 **Status**: Refactoring to JavaScript-first architecture --- ## 🎯 Core Principle **TestDrive-JSUI is a JavaScript library, not a Python package.** The JavaScript code provides all functionality. Python, Ruby, Java, or other language bindings are **integration adapters** that help serve, configure, and test the JavaScript library - they do not provide core functionality. --- ## πŸ“ Architectural Layers ### Layer 1: Core JavaScript Library (Functionality) **Location**: `/js/` **Purpose**: Complete standalone JavaScript UI framework **Dependencies**: Only `marked.js` (markdown parsing) **Language**: Pure JavaScript (ES6+) ``` js/ β”œβ”€β”€ testdrive-jsui.js # Main library bundle (future) β”œβ”€β”€ core/ # Core systems β”‚ β”œβ”€β”€ debug-system.js # Debug infrastructure β”‚ └── section-manager.js # Document section management β”œβ”€β”€ components/ # UI components β”‚ β”œβ”€β”€ dom-renderer.js # DOM rendering engine β”‚ β”œβ”€β”€ debug-panel.js # Debug UI β”‚ └── document-controls.js # Document control UI β”œβ”€β”€ controls/ # Interactive control panels β”‚ β”œβ”€β”€ control-base.js # Base control class β”‚ β”œβ”€β”€ edit-control.js # Edit mode controls β”‚ β”œβ”€β”€ debug-control.js # Debug controls β”‚ β”œβ”€β”€ status-control.js # Status indicator β”‚ └── contents-control.js # Table of contents β”œβ”€β”€ widgets/ # Reusable UI widgets β”œβ”€β”€ plugins/ # Extension system └── utils/ # Shared utilities ``` **Key Characteristics**: - βœ… Works standalone in any browser - βœ… No backend required (can load from file://) - βœ… Self-contained markdown rendering - βœ… All UI logic in JavaScript - βœ… Configuration via JSON **Standalone Usage**: ```html
``` --- ### Layer 2: Language Adapters (Integration) **Purpose**: Integration helpers for different backend languages **Function**: Serve assets, provide testing, backend integration **NOT**: Core functionality implementation #### Python Adapter **Location**: `/src/testdrive_jsui/` (optional, for Python projects) **Purpose**: Python project integration ```python # Python is just a helper to serve/test the JS library from testdrive_jsui import TestDriveJSUIAdapter adapter = TestDriveJSUIAdapter() # Adapter helps with: # 1. Asset serving assets = adapter.get_asset_urls() # 2. HTML template generation html = adapter.generate_html( markdown="# Content", mode="edit", config={...} ) # 3. Testing JavaScript test_result = adapter.run_js_tests() ``` **Python Adapter Responsibilities**: - βœ… Asset path resolution - βœ… HTML template generation - βœ… Configuration serialization (Python dict β†’ JSON) - βœ… JavaScript test runner integration (pytest ↔ Jest) - βœ… Development server (optional) - ❌ **NOT** markdown rendering (JS does this) - ❌ **NOT** UI logic (JS does this) - ❌ **NOT** content transformation (JS does this) #### Ruby Adapter (Future) ```ruby # Similar concept for Ruby/Rails projects adapter = TestDriveJSUI::Adapter.new # Generate view helper <%= testdrive_jsui_editor( markdown: @document.content, mode: :edit ) %> ``` #### Java Adapter (Future) ```java // Similar concept for Java/Spring projects TestDriveJSUIAdapter adapter = new TestDriveJSUIAdapter(); String html = adapter.generateHtml(markdown, "edit", config); ``` --- ## πŸ”„ Data Flow ### Standalone Mode (No Backend) ``` 1. Browser loads HTML 2. HTML includes testdrive-jsui.js 3. JS initializes with config 4. JS fetches/parses markdown β†’ marked.js 5. JS renders UI β†’ DOM 6. User interacts β†’ JS handles everything 7. Save via JS (localStorage, download, etc.) ``` ### With Backend Adapter (Python/Ruby/Java) ``` 1. Backend serves HTML template 2. Backend injects config JSON 3. Backend includes JS/CSS assets 4. Browser runs JavaScript (same as standalone) 5. JS may call backend APIs for save/load ``` **Key Point**: Backend is optional and only for convenience! --- ## 🎨 Current vs Target Architecture ### Current (Hybrid - Needs Refactoring) ``` ❌ Python generates HTML structure ❌ Python does markdown fallback rendering ❌ JavaScript enhances the Python-generated HTML ❌ Tight coupling between Python and JavaScript ``` ### Target (Clean Separation) ``` βœ… JavaScript is the complete UI library βœ… JavaScript handles all rendering (markdown β†’ HTML β†’ DOM) βœ… Python/Ruby/Java are thin adapters βœ… Zero coupling - JS works without any backend ``` --- ## πŸ“¦ Distribution Models ### Model 1: Self-hosted file (simplest) Copy `dist/testdrive-jsui.min.js` and `dist/testdrive-jsui.css` from the package (or a release build) next to your page and load them with `