# tushar-81/cph-leetcode architecture

> Architecture diagram of the GitHub repository tushar-81/cph-leetcode, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tushar-81/cph-leetcode
- Repository: https://github.com/tushar-81/cph-leetcode

## Overview

##### Project Type and Purpose
- This is a VS Code extension (see extension.js, package.json, vsc-extension-quickstart.md).
- Purpose: fetch and manage programming-challenge test cases, process user code (e.g. sample.cpp), and display results in a Webview.

##### File Structure
- Top‐level:
- extension.js – VS Code activation entry point
- package.json, jsconfig.json, eslint.config.mjs, .vscodeignore – config & metadata
- sample.cpp – example code file
- test/extension.test.js – unit tests for the extension
- src/basic/ – low-level utilities (processCode.js, transformArray.js, divideTestCases.js)
- src/core/ – core services for fetching, retrieving, storing test cases
- src/webview/ – front-end assets and scripts for the UI panel
- logos/, screenshots/ – assets for marketplace listing

##### (Sparse) README
- Lacks architecture details, so we infer design from code layout and naming.
- Core pattern: extension host triggers services → communicates with Webview → optionally reaches out to an external test‐case API → persists test cases locally.

##### Main Components
a. VS Code Extension Host
- extension.js (activation, command registration)
- Invokes modules in src/basic and src/core
b. Core Services Layer
- fetchTestCases (API client)
- retrieveTestCases (local cache read)
- storeTestCases (local cache write)
c. Utility Layer
- processCode.js (prepares/runs user code)
- transformArray.js, divideTestCases.js (data transforms)
d. Webview UI
- main.js (injected into Webview)
- script.js, theme.js (handles user interactions, theming)
- HTML/CSS assets (bundled in Webview)
e. External Services
- Remote Test‐Case API (HTTP/HTTPS endpoint)
f. Local Environment
- File system (for caching test cases)
- Child process or compiler toolchain (for compiling/running sample.cpp)
g. Tests
- extension.test.js – invokes extension activation in a mock host

##### Relationships and Data Flows
- User triggers command in VS Code (via Command Palette or UI).
- extension.js → calls core.fetchTestCases → external API → returns raw test cases.
- core.storeTestCases writes to local cache → core.retrieveTestCases reads from cache on demand.
- extension.js processes user code: calls basic.processCode → spawns child process (compiler) → obtains execution results.
- Results and test-case data passed to Webview via VS Code’s Webview API.
- Webview main.js renders data, handles UI events (e.g. “Run next test”), sends messages back to extension host.

##### Architectural Patterns and Design Principles
- Layered architecture: Utilities (basic) ← Core services ← Extension host ← Webview UI
- Clear separation of concerns: data fetching vs. data transformation vs. presentation
- Message‐based communication between Extension host and Webview
- Asynchronous flows (network I/O, child‐process I/O)

##### Essential Elements
- Two‐way Webview message channel

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User
    User["User's Editor"]:::external

    %% Extension Host
    subgraph "Extension Host" 
        extension["extension.js"]:::extensionhost
    end

    %% Core Services
    subgraph "Core Services"
        fetch["fetchTestCases"]:::core
        retrieve["retrieveTestCases"]:::core
        store["storeTestCases"]:::core
    end

    %% Utility Layer
    subgraph "Utilities"
        process["processCode"]:::util
        transform["transformArray"]:::util
        divide["divideTestCases"]:::util
    end

    %% Webview UI
    subgraph "Webview UI"
        main["main.js"]:::webview
        script["script.js"]:::webview
        theme["theme.js"]:::webview
    end

    %% External & Local Resources
    API["Remote Test-Case API"]:::external
    cache[".vscode-test.mjs"]:::external
    child["Compiler/Child Process"]:::external

    %% Tests
    test["extension.test.js"]:::test

    %% Config & Metadata
    subgraph "Config & Metadata"
        pkg["package.json"]:::config
        quick["vsc-extension-quickstart.md"]:::config
        jsconfig["jsconfig.json"]:::config
        eslint["eslint.config.mjs"]:::config
        ignore[".vscodeignore"]:::config
    end

    %% Sample Code
    sample["sample.cpp"]:::external

    %% Connections
    User -->|"trigger command"| extension
    extension -->|"fetchTestCases()"| fetch
    fetch -->|"HTTP request"| API
    API -->|"response"| fetch
    fetch -->|"storeTestCases()"| store
    store -->|"write cache"| cache
    extension -->|"retrieveTestCases()"| retrieve
    retrieve -->|"read cache"| cache
    extension -->|"processCode()"| process
    process -->|"compile & run"| child
    child -->|"results"| process
    extension -->|"postMessage()"| main
    main -->|"onDidReceiveMessage"| extension
    main -->|"init & render"| script
    script -->|"apply theme"| theme
    test -->|"activate extension"| extension

    %% Click Events
    click extension "https://github.com/tushar-81/cph-leetcode/blob/master/extension.js"
    click fetch "https://github.com/tushar-81/cph-leetcode/blob/master/src/core/fetchTestCases.js"
    click retrieve "https://github.com/tushar-81/cph-leetcode/blob/master/src/core/retrieveTestCases.js"
    click store "https://github.com/tushar-81/cph-leetcode/blob/master/src/core/storeTestCases.js"
    click process "https://github.com/tushar-81/cph-leetcode/blob/master/src/basic/processCode.js"
    click transform "https://github.com/tushar-81/cph-leetcode/blob/master/src/basic/transformArray.js"
    click divide "https://github.com/tushar-81/cph-leetcode/blob/master/src/basic/divideTestCases.js"
    click main "https://github.com/tushar-81/cph-leetcode/blob/master/src/webview/main.js"
    click script "https://github.com/tushar-81/cph-leetcode/blob/master/src/webview/script.js"
    click theme "https://github.com/tushar-81/cph-leetcode/blob/master/src/webview/theme.js"
    click cache "https://github.com/tushar-81/cph-leetcode/blob/master/.vscode-test.mjs"
    click test "https://github.com/tushar-81/cph-leetcode/blob/master/test/extension.test.js"
    click pkg "https://github.com/tushar-81/cph-leetcode/blob/master/package.json"
    click quick "https://github.com/tushar-81/cph-leetcode/blob/master/vsc-extension-quickstart.md"
    click jsconfig "https://github.com/tushar-81/cph-leetcode/blob/master/jsconfig.json"
    click eslint "https://github.com/tushar-81/cph-leetcode/blob/master/eslint.config.mjs"
    click ignore "https://github.com/tushar-81/cph-leetcode/blob/master/.vscodeignore"
    click sample "https://github.com/tushar-81/cph-leetcode/blob/master/sample.cpp"

    %% Styles
    classDef extensionhost fill:#E0F2FF,stroke:#0288D1,stroke-width:2px
    classDef core fill:#E3FCEC,stroke:#2E7D32,stroke-width:2px
    classDef util fill:#F1F8E9,stroke:#558B2F,stroke-width:2px
    classDef webview fill:#FFF3E0,stroke:#FB8C00,stroke-width:2px
    classDef external fill:#ECEFF1,stroke:#607D8B,stroke-width:2px,stroke-dasharray: 5 5
    classDef test fill:#F5F5F5,stroke:#9E9E9E,stroke-width:2px,stroke-dasharray: 5 5
    classDef config fill:#FAFAFA,stroke:#BDBDBD,stroke-width:2px,stroke-dasharray: 4 2
```
