# 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: CDN (Simplest) ```html ``` ### Model 2: npm Package ```bash npm install testdrive-jsui ``` ```javascript import TestDriveJSUI from 'testdrive-jsui'; ``` ### Model 3: With Language Adapter ```bash # Python projects pip install testdrive-jsui-python # Ruby projects gem install testdrive-jsui # Java projects maven: com.testdrive:testdrive-jsui-java ``` **Important**: Language adapters are separate packages that bundle/reference the core JS library. --- ## π§ͺ Testing Architecture ### JavaScript Tests (Core) **Framework**: Jest with jsdom **Location**: `/js/tests/` **What**: Tests the JavaScript library itself ```javascript describe('TestDriveJSUI', () => { test('renders markdown to HTML', () => { const ui = new TestDriveJSUI({...}); expect(ui.render('# Hello')).toContain('