Unit Test Generator
Run ID: 69cd03883e7fb09ff16a72022026-04-01Development
PantheraHive BOS
BOS Dashboard

This document details the output of Step 2: gemini -> generate_code for the "Unit Test Generator" workflow. In this step, the system leverages advanced AI capabilities to generate comprehensive, production-ready unit tests for your provided codebase.


Step 2: Code Generation - Unit Tests

This section presents the generated unit tests, meticulously crafted to ensure the robustness, correctness, and reliability of your code. We have used pytest, a popular and powerful Python testing framework, known for its simplicity and extensibility.

1. Assumptions and Context

For demonstration purposes, we've assumed an input Python module named calculator.py containing a Calculator class with basic arithmetic operations. In a real-world scenario, this would be your specific code provided in the previous step.

2. Original Code (Example: calculator.py)

This is the hypothetical source code for which the unit tests are generated.

text • 261 chars
### 3. Generated Unit Tests (File: `test_calculator.py`)

Below is the generated `pytest` test suite for the `Calculator` class. Each test function is designed to cover specific aspects of the methods, including happy paths, edge cases, and error conditions.

Sandboxed live preview

Unit Test Generator: Detailed Study Plan

This document outlines a comprehensive four-week study plan designed to equip you with the foundational knowledge and practical skills required to understand, design, and potentially implement a "Unit Test Generator." This plan progresses from core unit testing principles to advanced concepts, including the application of AI/ML for automated test generation.


1. Overall Learning Objectives

By the end of this study plan, you will be able to:

  • Master Unit Testing Fundamentals: Understand the purpose, benefits, and best practices of unit testing, including test doubles, code coverage, and the FIRST principles.
  • Proficiently Use Testing Frameworks: Gain hands-on experience with popular unit testing frameworks in at least one chosen programming language (e.g., Java, Python, JavaScript, C#).
  • Apply Test-Driven Development (TDD): Understand and practice the TDD cycle (Red-Green-Refactor) to build robust and testable code.
  • Analyze Code for Testability: Identify code characteristics that make it difficult or easy to unit test, and learn refactoring techniques to improve testability.
  • Explore Test Generation Strategies: Investigate various approaches to automated test case generation, including property-based testing, symbolic execution, and fuzzing.
  • Understand AI/ML in Test Generation: Grasp how machine learning models, like large language models (LLMs) such as Gemini, can be leveraged to assist in or automate the creation of unit tests.
  • Architect a Unit Test Generator: Develop a high-level architectural understanding of how an automated unit test generator could be designed and implemented.

2. Weekly Schedule & Detailed Objectives

This section breaks down the study plan into a four-week schedule, detailing learning objectives, topics, recommended resources, and practical exercises for each week.

Week 1: Fundamentals of Unit Testing & Best Practices

Learning Objectives:

  • Define unit testing, its purpose, and its benefits.
  • Understand the characteristics of a good unit test (FIRST principles).
  • Differentiate between various types of test doubles (mocks, stubs, spies, fakes, dummies).
  • Learn about code coverage metrics and their interpretation.
  • Apply basic unit testing concepts in a chosen programming language.

Topics:

  • What is Unit Testing? (Definition, Purpose, Benefits, Costs)
  • The Unit Under Test (UUT) and Test Isolation
  • FIRST Principles of Good Unit Tests (Fast, Independent, Repeatable, Self-Validating, Timely)
  • Test Doubles: Mocks, Stubs, Fakes, Spies, Dummies (when and how to use them)
  • Assertions and Test Structure (Arrange-Act-Assert - AAA)
  • Code Coverage: Statement, Branch, Line, Function Coverage
  • Introduction to a Unit Testing Framework (e.g., JUnit, Pytest, NUnit, Jest)

Recommended Resources:

  • Book: The Art of Unit Testing with Examples in C# by Roy Osherove (Concepts are language-agnostic).
  • Online Course: Pluralsight, Udemy, or Coursera courses on "Introduction to Unit Testing" in your preferred language.
  • Documentation: Official documentation for your chosen unit testing framework (e.g., JUnit 5 User Guide, Pytest Documentation).
  • Articles: Martin Fowler's articles on "Test Doubles" and "Mocks Aren't Stubs."

Practical Exercises:

  • Set up a new project in your chosen language with a unit testing framework.
  • Write unit tests for a simple utility class (e.g., a calculator, string manipulator).
  • Refactor existing code to make it more testable.
  • Experiment with different types of test doubles for a class with external dependencies.
  • Integrate a code coverage tool and analyze the reports for your tests.

Week 2: Test-Driven Development (TDD) & Advanced Framework Features

Learning Objectives:

  • Understand and practice the TDD cycle (Red-Green-Refactor).
  • Utilize advanced features of your chosen unit testing framework.
  • Learn to write parameterized tests for efficiency.
  • Explore techniques for testing asynchronous code and exceptions.
  • Understand the importance of test suite organization and naming conventions.

Topics:

  • Test-Driven Development (TDD): The Red-Green-Refactor Cycle
  • Benefits and Challenges of TDD
  • Advanced Test Framework Features:

* Parameterized Tests / Data-Driven Tests

* Setup and Teardown Methods (fixtures)

* Assertions for collections, exceptions, and asynchronous operations

* Test Categories/Tags and Filtering

  • Testing Private Methods (and why you often shouldn't)
  • Organizing Test Suites and Naming Conventions
  • Refactoring for Testability (Dependency Injection, Single Responsibility Principle)

Recommended Resources:

  • Book: Test-Driven Development by Example by Kent Beck.
  • Online Course: Specific courses on TDD in your chosen language.
  • Documentation: Advanced sections of your unit testing framework's documentation.
  • Blog Posts: Articles on "TDD Best Practices," "Refactoring for Testability."

Practical Exercises:

  • Implement a new feature using the TDD cycle from scratch.
  • Convert existing tests to use parameterized tests where applicable.
  • Write tests for methods that throw exceptions.
  • Practice testing asynchronous code (if applicable to your language, e.g., JavaScript Promises, Python asyncio).
  • Refactor a module to improve its testability using dependency injection.

Week 3: Automated Test Generation Strategies & Code Analysis

Learning Objectives:

  • Explore different paradigms for automated test generation beyond manual writing.
  • Understand the principles of property-based testing and its benefits.
  • Learn about symbolic execution and its role in finding edge cases.
  • Investigate fuzz testing for discovering vulnerabilities and crashes.
  • Understand how static and dynamic code analysis can inform test generation.

Topics:

  • Overview of Automated Test Generation Techniques:

* Property-Based Testing: Principles, generators, shrinking (e.g., Hypothesis, QuickCheck).

* Symbolic Execution: How it works, path exploration, constraint solving.

* Fuzz Testing: Input generation, mutation, crash detection.

* Model-Based Testing: Generating tests from system models.

  • Code Analysis for Test Generation:

* Static Analysis (AST, Control Flow Graphs, Data Flow Graphs)

* Dynamic Analysis (Runtime instrumentation, coverage measurement)

  • Identifying Test Gaps and Weak Spots in Existing Codebases
  • Challenges and Limitations of Automated Test Generation

Recommended Resources:

  • Book Chapters: Relevant chapters in books on software testing, formal methods, or program analysis.
  • Papers/Articles: Research papers on property-based testing, symbolic execution, and fuzzing (e.g., "QuickCheck: A Lightweight Tool for Random Testing of Erlang Programs").
  • Tools Documentation: Documentation for property-based testing libraries (e.g., Hypothesis for Python, QuickCheck for Haskell).
  • Videos: Conference talks or lectures on automated test generation.

Practical Exercises:

  • Implement property-based tests for a simple function (e.g., reverse a list, sort an array).
  • Research and experiment with a basic fuzzing tool on a small target.
  • Analyze the Abstract Syntax Tree (AST) of a simple code snippet using a library in your chosen language (e.g., Python ast module, Java javaparser).
  • Brainstorm how code analysis data could inform the generation of test inputs.

Week 4: AI/ML for Test Generation & Architectural Planning

Learning Objectives:

  • Understand the role of AI/ML, especially Large Language Models (LLMs) like Gemini, in automated test generation.
  • Explore different AI/ML approaches: code completion, natural language to code, bug prediction.
  • Identify the components and workflow of an AI-powered Unit Test Generator.
  • Develop a high-level architectural plan for a "Unit Test Generator."
  • Discuss ethical considerations and limitations of AI-generated tests.

Topics:

  • Introduction to AI/ML in Software Engineering and Testing
  • Leveraging Large Language Models (LLMs) for Test Generation:

* Code completion for tests

* Generating test cases from natural language descriptions or function signatures

* Test case summarization and explanation

  • Machine Learning for Bug Prediction and Prioritization
  • Reinforcement Learning for Test Sequence Generation
  • Architectural Considerations for a Unit Test Generator:

* Input/Output formats (source code, AST, test files)

* Core generation engine (AI model, rule-based, hybrid)

* Code analysis component

* Test execution and validation component

* Feedback loop mechanisms

  • Ethical Implications, Bias, and Limitations of AI-Generated Tests

Recommended Resources:

  • Papers/Articles: Recent research papers on "AI for Software Testing," "LLMs for Code Generation," "Automated Test Generation using Machine Learning."
  • Blog Posts: Articles from Google AI, OpenAI, Microsoft Research on code generation and LLMs.
  • Videos: Talks from AI conferences or academic lectures on code intelligence.
  • Case Studies: Examples of tools that use AI for code or test generation (e.g., GitHub Copilot, deep learning-based code analysis tools).

Practical Exercises:

  • Experiment with an LLM (e.g., Gemini API, OpenAI GPT, or a local model if available) to generate simple unit tests for a given function signature or description.
  • Draft a high-level architectural diagram for a "Unit Test Generator," outlining its major components and their interactions.
  • Write a brief proposal detailing the input, output, and core logic of a specific feature within the generator (e.g., a component that generates assertions based on function return types).
  • Reflect on the challenges of validating AI-generated tests and propose strategies.

3. Key Milestones

  • End of Week 1: Successfully set up a unit testing environment, write basic unit tests following FIRST principles, and utilize test doubles effectively.
  • End of Week 2: Consistently apply the TDD cycle, implement parameterized tests, and demonstrate proficiency with advanced framework features.
  • End of Week 3: Understand and articulate the differences between property-based testing, symbolic execution, and fuzzing; demonstrate basic application of one technique.
  • End of Week 4: Articulate the role of AI/ML in test generation, and produce a high-level architectural design for a "Unit Test Generator" system.

4. Assessment Strategies

  • Weekly Code Reviews/Self-Reflection: Review your own code and tests against the FIRST principles and TDD practices. Seek peer review if possible.
  • Mini-Projects/Coding Challenges: Apply learned concepts to solve small, well-defined coding problems, demonstrating test-driven development.
  • Conceptual Quizzes/Discussions: Regularly test your understanding of key terms, concepts, and design patterns related to unit testing and test generation.
  • Architectural Design Document: At the end of Week 4, present your high-level architectural plan for the "Unit Test Generator," explaining your design choices and the rationale behind them.
  • Practical Demonstrations: Showcase your ability to use testing frameworks, apply TDD, and experiment with automated generation techniques.

5. Recommended Tools & Environment

  • Integrated Development Environment (IDE): IntelliJ IDEA (Java), VS Code (Python, JavaScript, C#), Visual Studio (C#)
  • Programming Language: Choose one primary language (Java, Python, JavaScript, C#) for practical exercises.
  • Unit Testing Frameworks:

* Java: JUnit 5, Mockito

* Python: Pytest, unittest, Hypothesis (for property-based testing)

* JavaScript: Jest, Mocha, Chai, Sinon.js (for test doubles)

* C#: NUnit, xUnit.net, Moq

  • Code Coverage Tools: JaCoCo (Java), Coverage.py (Python), Istanbul/NYC (JavaScript), dotCover (C#)
  • Version Control: Git and GitHub/GitLab for managing code and tracking progress.
  • AI Access: Access to an LLM API (e.g., Gemini API, OpenAI GPT API) for Week 4 exercises.

This detailed study plan provides a structured path to mastering the intricacies of unit testing and understanding the landscape of automated test generation, culminating in the ability to conceptualize an AI-powered "Unit Test Generator." Good luck with your studies!

4. Explanation of Generated Tests

  • pytest.fixture (calculator_instance):

* This fixture ensures that each test function receives a new, isolated instance of the Calculator class.

gemini Output

Unit Test Generation Report

Project: Unit Test Generator Workflow

Step: gemini → review_and_document

Date: October 26, 2023


1. Introduction

This document presents the detailed output from the "Unit Test Generator" workflow, specifically focusing on the review_and_document phase. The primary objective of this step is to provide a comprehensive and professionally structured set of unit tests, along with clear instructions for their integration and use.

The generated unit tests are designed to be robust, readable, and maintainable, adhering to industry best practices. They aim to cover critical functionalities, edge cases, and error conditions, thereby enhancing code quality, reliability, and facilitating future development and refactoring efforts.

2. Generated Unit Tests

Below is an example of unit tests generated for a hypothetical Calculator class in Python, using the pytest framework. This example demonstrates the structure, coverage, and style of the tests produced by our generator.

2.1. Example: test_calculator.py


import pytest
from your_project.calculator import Calculator # Assuming Calculator is in 'your_project/calculator.py'

# --- Fixtures (for setup/teardown if needed, or common objects) ---
@pytest.fixture
def calculator_instance():
    """Provides a fresh Calculator instance for each test."""
    return Calculator()

# --- Test Cases for Calculator.add() ---
def test_add_positive_numbers(calculator_instance):
    """Tests addition of two positive integers."""
    assert calculator_instance.add(2, 3) == 5

def test_add_negative_numbers(calculator_instance):
    """Tests addition of two negative integers."""
    assert calculator_instance.add(-5, -10) == -15

def test_add_positive_and_negative_numbers(calculator_instance):
    """Tests addition of a positive and a negative integer."""
    assert calculator_instance.add(7, -3) == 4
    assert calculator_instance.add(-10, 5) == -5

def test_add_with_zero(calculator_instance):
    """Tests addition involving zero."""
    assert calculator_instance.add(0, 8) == 8
    assert calculator_instance.add(-12, 0) == -12
    assert calculator_instance.add(0, 0) == 0

def test_add_float_numbers(calculator_instance):
    """Tests addition of floating-point numbers."""
    assert calculator_instance.add(2.5, 3.5) == 6.0
    assert calculator_instance.add(0.1, 0.2) == pytest.approx(0.3) # Use approx for float comparisons

# --- Test Cases for Calculator.subtract() ---
def test_subtract_positive_numbers(calculator_instance):
    """Tests subtraction of two positive integers."""
    assert calculator_instance.subtract(10, 4) == 6

def test_subtract_negative_numbers(calculator_instance):
    """Tests subtraction of two negative integers."""
    assert calculator_instance.subtract(-5, -2) == -3

def test_subtract_positive_from_negative(calculator_instance):
    """Tests subtracting a positive number from a negative number."""
    assert calculator_instance.subtract(-8, 3) == -11

def test_subtract_with_zero(calculator_instance):
    """Tests subtraction involving zero."""
    assert calculator_instance.subtract(15, 0) == 15
    assert calculator_instance.subtract(0, 7) == -7
    assert calculator_instance.subtract(0, 0) == 0

# --- Test Cases for Calculator.multiply() ---
def test_multiply_positive_numbers(calculator_instance):
    """Tests multiplication of two positive integers."""
    assert calculator_instance.multiply(2, 5) == 10

def test_multiply_with_zero(calculator_instance):
    """Tests multiplication involving zero."""
    assert calculator_instance.multiply(7, 0) == 0
    assert calculator_instance.multiply(0, 100) == 0

def test_multiply_negative_numbers(calculator_instance):
    """Tests multiplication of negative numbers (even and odd count)."""
    assert calculator_instance.multiply(-3, 4) == -12
    assert calculator_instance.multiply(-2, -5) == 10

# --- Test Cases for Calculator.divide() ---
def test_divide_positive_numbers(calculator_instance):
    """Tests division of two positive integers, exact division."""
    assert calculator_instance.divide(10, 2) == 5.0

def test_divide_positive_numbers_float_result(calculator_instance):
    """Tests division resulting in a float."""
    assert calculator_instance.divide(7, 2) == 3.5

def test_divide_by_negative_number(calculator_instance):
    """Tests division by a negative number."""
    assert calculator_instance.divide(10, -2) == -5.0
    assert calculator_instance.divide(-12, -3) == 4.0

def test_divide_zero_by_number(calculator_instance):
    """Tests division of zero by a non-zero number."""
    assert calculator_instance.divide(0, 5) == 0.0

def test_divide_by_zero_raises_error(calculator_instance):
    """Tests that division by zero raises a ValueError."""
    with pytest.raises(ValueError, match="Cannot divide by zero"):
        calculator_instance.divide(10, 0)

# --- Example of a simple Calculator class (for context) ---
# This class would reside in 'your_project/calculator.py'
# class Calculator:
#     def add(self, a, b):
#         return a + b
#
#     def subtract(self, a, b):
#         return a - b
#
#     def multiply(self, a, b):
#         return a * b
#
#     def divide(self, a, b):
#         if b == 0:
#             raise ValueError("Cannot divide by zero")
#         return a / b

2.2. Explanation of Generated Tests

The provided unit tests for the Calculator class demonstrate the following characteristics:

  • Framework: Utilizes pytest, a popular and powerful testing framework for Python, known for its simplicity and expressiveness.
  • Fixture Usage: Employs a pytest.fixture (calculator_instance) to ensure each test receives a fresh instance of the Calculator, promoting test isolation and preventing side effects.
  • Comprehensive Coverage:

* Happy Path: Tests basic, expected operations with valid inputs.

* Edge Cases: Includes tests for zero inputs, negative numbers, and floating-point arithmetic.

* Error Handling: Specifically tests the divide method to ensure it correctly raises a ValueError when attempting division by zero, using pytest.raises.

* Floating Point Precision: Uses pytest.approx for comparing floating-point results, which is crucial for avoiding precision errors in comparisons.

  • Readability: Test function names are descriptive (test_add_positive_numbers, test_divide_by_zero_raises_error), clearly indicating the scenario being tested. Docstrings further elaborate on the test's purpose.
  • Maintainability: The tests are modular and focused on single responsibilities, making them easier to understand, debug, and update as the application evolves.

3. Analysis and Coverage

The generated unit tests provide a solid foundation for ensuring the reliability of the Calculator class.

3.1. Test Scenarios Covered

  • Basic Arithmetic Operations: add, subtract, multiply, divide with various combinations of positive, negative, and zero integers.
  • Floating-Point Arithmetic: Specific tests for add and divide to handle decimal numbers correctly.
  • Identity Elements: Testing operations with zero (e.g., add(x, 0), multiply(x, 0)).
  • Error Conditions: Explicitly testing the divide_by_zero scenario to validate error handling.

3.2. Best Practices Applied

  • Test Isolation: Each test runs independently, facilitated by fixtures, preventing tests from affecting one another.
  • Descriptive Naming: Test names clearly convey their purpose.
  • Assertion Clarity: Assertions are straightforward and directly check expected outcomes.
  • Use of Appropriate Tools: Leveraging pytest.approx for float comparisons and pytest.raises for exception handling.
  • Readability & Maintainability: The structure and comments enhance the overall understanding and ease of modification.

4. Integration Guide

To integrate these unit tests into your project, follow these steps:

4.1. Prerequisites

  1. Python Environment: Ensure Python 3.x is installed.
  2. pytest Installation: If not already installed, install pytest using pip:

    pip install pytest
  1. Target Code: The code under test (e.g., Calculator class) must be accessible from the test file. Adjust the import statement (from your_project.calculator import Calculator) to match your project's structure.

4.2. Steps to Integrate

  1. Create a Test Directory: It's common practice to create a dedicated directory for tests, typically named tests/ at the root of your project.
  2. Save the Test File: Save the generated unit test code (e.g., test_calculator.py) within this tests/ directory.

    your_project/
    ├── your_project/
    │   └── calculator.py
    └── tests/
        └── test_calculator.py
  1. Adjust Imports: Verify and adjust the import statement in test_calculator.py to correctly point to your Calculator class. For example, if your Calculator class is in src/my_app/calculator.py, the import might be from src.my_app.calculator import Calculator.

4.3. How to Run Tests

  1. Navigate to Project Root: Open your terminal or command prompt and navigate to the root directory of your project (where your_project/ and tests/ reside).
  2. Execute pytest: Run pytest from the command line. pytest will automatically discover test files (those starting with test_ or ending with _test.py) and execute the tests.

    pytest
  1. View Results: pytest will output a summary of the test run, indicating how many tests passed, failed, or were skipped.

* Example Success Output:


        ============================= test session starts ==============================
        platform linux -- Python 3.x.x, pytest-x.x.x, pluggy-x.x.x
        rootdir: /path/to/your_project
        collected 20 items

        tests/test_calculator.py ....................                               [100%]

        ============================== 20 passed in X.XXs ==============================

* Example Failure Output: (if a test fails)


        ============================= test session starts ==============================
        ...
        tests/test_calculator.py ......F.............                               [100%]

        =================================== FAILURES ===================================
        ________________________ test_add_positive_numbers _________________________

        calculator_instance = <your_project.calculator.Calculator object at 0x...>

            def test_add_positive_numbers(calculator_instance):
        >       assert calculator_instance.add(2, 3) == 6 # Intentionally wrong for example
        E       assert 5 == 6
        E        +  where 5 = <your_project.calculator.Calculator object at 0x...>.add(2, 3)

        tests/test_calculator.py:16: AssertionError
        =========================== short test summary info ============================
        FAILED tests/test_calculator.py::test_add_positive_numbers - assert 5 == 6
        ========================= 1 failed, 19 passed in X.XXs =========================

5. Recommendations for Further Enhancement

While the generated tests provide a strong starting point, consider these recommendations for continuous improvement:

  • Expand Test Coverage:

* Boundary Conditions: Systematically test the limits of input values (e.g., max_int, min_int).

* Invalid Inputs: Add tests for non-numeric inputs or inputs of incorrect types if your function is expected to handle or validate them.

* Complex Scenarios: If your Calculator had more complex state or dependencies, consider mocking or more intricate fixture setups.

  • Integration with CI/CD: Incorporate these unit tests into your Continuous Integration/Continuous Deployment (CI/CD) pipeline. This ensures that tests are automatically run on every code commit, catching regressions early.
  • Code Coverage Tools: Use tools like pytest-cov to measure the percentage of your code exercised by tests. This helps identify areas lacking test coverage.
  • Refactoring Opportunities: The act of writing tests can often highlight areas in your production code that are difficult to test, suggesting opportunities for refactoring to improve modularity and testability.
  • Performance Testing: For critical operations, consider adding simple performance tests to ensure operations remain within acceptable time limits.
  • Documentation Maintenance: Keep test documentation (e.g., docstrings) up-to-date as the codebase evolves.

6. Conclusion and Support

This deliverable provides a robust, professionally generated set of unit tests designed to immediately enhance the quality and reliability of your codebase. By integrating and regularly running these tests, you establish a strong safety net for future development.

Should you require any further assistance with integration, customization, or expanding test coverage, please do not hesitate to contact our support team. We are committed to ensuring your success.

7. Important Notes and Disclaimers

  • Review and Validate: While AI-generated tests are highly effective, it is crucial for a human developer to review and validate them against the actual business logic and requirements of the application.
  • Context Dependency: The effectiveness of generated tests depends on the accuracy and completeness of the input provided to the generator.
  • Dynamic Environments: Unit tests are typically designed for isolated code units. Integration with complex systems or external dependencies might require additional testing strategies (e.g., integration tests, end-to-end tests).
unit_test_generator.txt
Download source file
Copy all content
Full output as text
Download ZIP
IDE-ready project ZIP
Copy share link
Permanent URL for this run
Get Embed Code
Embed this result on any website
Print / Save PDF
Use browser print dialog
"); var hasSrcMain=Object.keys(extracted).some(function(k){return k.indexOf("src/main")>=0;}); if(!hasSrcMain) zip.file(folder+"src/main."+ext,"import React from 'react' import ReactDOM from 'react-dom/client' import App from './App' import './index.css' ReactDOM.createRoot(document.getElementById('root')!).render( ) "); var hasSrcApp=Object.keys(extracted).some(function(k){return k==="src/App."+ext||k==="App."+ext;}); if(!hasSrcApp) zip.file(folder+"src/App."+ext,"import React from 'react' import './App.css' function App(){ return(

"+slugTitle(pn)+"

Built with PantheraHive BOS

) } export default App "); zip.file(folder+"src/index.css","*{margin:0;padding:0;box-sizing:border-box} body{font-family:system-ui,-apple-system,sans-serif;background:#f0f2f5;color:#1a1a2e} .app{min-height:100vh;display:flex;flex-direction:column} .app-header{flex:1;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:12px;padding:40px} h1{font-size:2.5rem;font-weight:700} "); zip.file(folder+"src/App.css",""); zip.file(folder+"src/components/.gitkeep",""); zip.file(folder+"src/pages/.gitkeep",""); zip.file(folder+"src/hooks/.gitkeep",""); Object.keys(extracted).forEach(function(p){ var fp=p.startsWith("src/")?p:"src/"+p; zip.file(folder+fp,extracted[p]); }); zip.file(folder+"README.md","# "+slugTitle(pn)+" Generated by PantheraHive BOS. ## Setup ```bash npm install npm run dev ``` ## Build ```bash npm run build ``` ## Open in IDE Open the project folder in VS Code or WebStorm. "); zip.file(folder+".gitignore","node_modules/ dist/ .env .DS_Store *.local "); } /* --- Vue (Vite + Composition API + TypeScript) --- */ function buildVue(zip,folder,app,code,panelTxt){ var pn=pkgName(app); var C=cc(pn); var extracted=extractCode(panelTxt); zip.file(folder+"package.json",'{ "name": "'+pn+'", "version": "0.0.0", "type": "module", "scripts": { "dev": "vite", "build": "vue-tsc -b && vite build", "preview": "vite preview" }, "dependencies": { "vue": "^3.5.13", "vue-router": "^4.4.5", "pinia": "^2.3.0", "axios": "^1.7.9" }, "devDependencies": { "@vitejs/plugin-vue": "^5.2.1", "typescript": "~5.7.3", "vite": "^6.0.5", "vue-tsc": "^2.2.0" } } '); zip.file(folder+"vite.config.ts","import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import { resolve } from 'path' export default defineConfig({ plugins: [vue()], resolve: { alias: { '@': resolve(__dirname,'src') } } }) "); zip.file(folder+"tsconfig.json",'{"files":[],"references":[{"path":"./tsconfig.app.json"},{"path":"./tsconfig.node.json"}]} '); zip.file(folder+"tsconfig.app.json",'{ "compilerOptions":{ "target":"ES2020","useDefineForClassFields":true,"module":"ESNext","lib":["ES2020","DOM","DOM.Iterable"], "skipLibCheck":true,"moduleResolution":"bundler","allowImportingTsExtensions":true, "isolatedModules":true,"moduleDetection":"force","noEmit":true,"jsxImportSource":"vue", "strict":true,"paths":{"@/*":["./src/*"]} }, "include":["src/**/*.ts","src/**/*.d.ts","src/**/*.tsx","src/**/*.vue"] } '); zip.file(folder+"env.d.ts","/// "); zip.file(folder+"index.html"," "+slugTitle(pn)+"
"); var hasMain=Object.keys(extracted).some(function(k){return k==="src/main.ts"||k==="main.ts";}); if(!hasMain) zip.file(folder+"src/main.ts","import { createApp } from 'vue' import { createPinia } from 'pinia' import App from './App.vue' import './assets/main.css' const app = createApp(App) app.use(createPinia()) app.mount('#app') "); var hasApp=Object.keys(extracted).some(function(k){return k.indexOf("App.vue")>=0;}); if(!hasApp) zip.file(folder+"src/App.vue"," "); zip.file(folder+"src/assets/main.css","*{margin:0;padding:0;box-sizing:border-box}body{font-family:system-ui,sans-serif;background:#fff;color:#213547} "); zip.file(folder+"src/components/.gitkeep",""); zip.file(folder+"src/views/.gitkeep",""); zip.file(folder+"src/stores/.gitkeep",""); Object.keys(extracted).forEach(function(p){ var fp=p.startsWith("src/")?p:"src/"+p; zip.file(folder+fp,extracted[p]); }); zip.file(folder+"README.md","# "+slugTitle(pn)+" Generated by PantheraHive BOS. ## Setup ```bash npm install npm run dev ``` ## Build ```bash npm run build ``` Open in VS Code or WebStorm. "); zip.file(folder+".gitignore","node_modules/ dist/ .env .DS_Store *.local "); } /* --- Angular (v19 standalone) --- */ function buildAngular(zip,folder,app,code,panelTxt){ var pn=pkgName(app); var C=cc(pn); var sel=pn.replace(/_/g,"-"); var extracted=extractCode(panelTxt); zip.file(folder+"package.json",'{ "name": "'+pn+'", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test" }, "dependencies": { "@angular/animations": "^19.0.0", "@angular/common": "^19.0.0", "@angular/compiler": "^19.0.0", "@angular/core": "^19.0.0", "@angular/forms": "^19.0.0", "@angular/platform-browser": "^19.0.0", "@angular/platform-browser-dynamic": "^19.0.0", "@angular/router": "^19.0.0", "rxjs": "~7.8.0", "tslib": "^2.3.0", "zone.js": "~0.15.0" }, "devDependencies": { "@angular-devkit/build-angular": "^19.0.0", "@angular/cli": "^19.0.0", "@angular/compiler-cli": "^19.0.0", "typescript": "~5.6.0" } } '); zip.file(folder+"angular.json",'{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "'+pn+'": { "projectType": "application", "root": "", "sourceRoot": "src", "prefix": "app", "architect": { "build": { "builder": "@angular-devkit/build-angular:application", "options": { "outputPath": "dist/'+pn+'", "index": "src/index.html", "browser": "src/main.ts", "tsConfig": "tsconfig.app.json", "styles": ["src/styles.css"], "scripts": [] } }, "serve": {"builder":"@angular-devkit/build-angular:dev-server","configurations":{"production":{"buildTarget":"'+pn+':build:production"},"development":{"buildTarget":"'+pn+':build:development"}},"defaultConfiguration":"development"} } } } } '); zip.file(folder+"tsconfig.json",'{ "compileOnSave": false, "compilerOptions": {"baseUrl":"./","outDir":"./dist/out-tsc","forceConsistentCasingInFileNames":true,"strict":true,"noImplicitOverride":true,"noPropertyAccessFromIndexSignature":true,"noImplicitReturns":true,"noFallthroughCasesInSwitch":true,"paths":{"@/*":["src/*"]},"skipLibCheck":true,"esModuleInterop":true,"sourceMap":true,"declaration":false,"experimentalDecorators":true,"moduleResolution":"bundler","importHelpers":true,"target":"ES2022","module":"ES2022","useDefineForClassFields":false,"lib":["ES2022","dom"]}, "references":[{"path":"./tsconfig.app.json"}] } '); zip.file(folder+"tsconfig.app.json",'{ "extends":"./tsconfig.json", "compilerOptions":{"outDir":"./dist/out-tsc","types":[]}, "files":["src/main.ts"], "include":["src/**/*.d.ts"] } '); zip.file(folder+"src/index.html"," "+slugTitle(pn)+" "); zip.file(folder+"src/main.ts","import { bootstrapApplication } from '@angular/platform-browser'; import { appConfig } from './app/app.config'; import { AppComponent } from './app/app.component'; bootstrapApplication(AppComponent, appConfig) .catch(err => console.error(err)); "); zip.file(folder+"src/styles.css","* { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: system-ui, -apple-system, sans-serif; background: #f9fafb; color: #111827; } "); var hasComp=Object.keys(extracted).some(function(k){return k.indexOf("app.component")>=0;}); if(!hasComp){ zip.file(folder+"src/app/app.component.ts","import { Component } from '@angular/core'; import { RouterOutlet } from '@angular/router'; @Component({ selector: 'app-root', standalone: true, imports: [RouterOutlet], templateUrl: './app.component.html', styleUrl: './app.component.css' }) export class AppComponent { title = '"+pn+"'; } "); zip.file(folder+"src/app/app.component.html","

"+slugTitle(pn)+"

Built with PantheraHive BOS

"); zip.file(folder+"src/app/app.component.css",".app-header{display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:60vh;gap:16px}h1{font-size:2.5rem;font-weight:700;color:#6366f1} "); } zip.file(folder+"src/app/app.config.ts","import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; export const appConfig: ApplicationConfig = { providers: [ provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes) ] }; "); zip.file(folder+"src/app/app.routes.ts","import { Routes } from '@angular/router'; export const routes: Routes = []; "); Object.keys(extracted).forEach(function(p){ var fp=p.startsWith("src/")?p:"src/"+p; zip.file(folder+fp,extracted[p]); }); zip.file(folder+"README.md","# "+slugTitle(pn)+" Generated by PantheraHive BOS. ## Setup ```bash npm install ng serve # or: npm start ``` ## Build ```bash ng build ``` Open in VS Code with Angular Language Service extension. "); zip.file(folder+".gitignore","node_modules/ dist/ .env .DS_Store *.local .angular/ "); } /* --- Python --- */ function buildPython(zip,folder,app,code){ var title=slugTitle(app); var pn=pkgName(app); var src=code.replace(/^```[w]* ?/m,"").replace(/ ?```$/m,"").trim(); var reqMap={"numpy":"numpy","pandas":"pandas","sklearn":"scikit-learn","tensorflow":"tensorflow","torch":"torch","flask":"flask","fastapi":"fastapi","uvicorn":"uvicorn","requests":"requests","sqlalchemy":"sqlalchemy","pydantic":"pydantic","dotenv":"python-dotenv","PIL":"Pillow","cv2":"opencv-python","matplotlib":"matplotlib","seaborn":"seaborn","scipy":"scipy"}; var reqs=[]; Object.keys(reqMap).forEach(function(k){if(src.indexOf("import "+k)>=0||src.indexOf("from "+k)>=0)reqs.push(reqMap[k]);}); var reqsTxt=reqs.length?reqs.join(" "):"# add dependencies here "; zip.file(folder+"main.py",src||"# "+title+" # Generated by PantheraHive BOS print(title+" loaded") "); zip.file(folder+"requirements.txt",reqsTxt); zip.file(folder+".env.example","# Environment variables "); zip.file(folder+"README.md","# "+title+" Generated by PantheraHive BOS. ## Setup ```bash python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt ``` ## Run ```bash python main.py ``` "); zip.file(folder+".gitignore",".venv/ __pycache__/ *.pyc .env .DS_Store "); } /* --- Node.js --- */ function buildNode(zip,folder,app,code){ var title=slugTitle(app); var pn=pkgName(app); var src=code.replace(/^```[w]* ?/m,"").replace(/ ?```$/m,"").trim(); var depMap={"mongoose":"^8.0.0","dotenv":"^16.4.5","axios":"^1.7.9","cors":"^2.8.5","bcryptjs":"^2.4.3","jsonwebtoken":"^9.0.2","socket.io":"^4.7.4","uuid":"^9.0.1","zod":"^3.22.4","express":"^4.18.2"}; var deps={}; Object.keys(depMap).forEach(function(k){if(src.indexOf(k)>=0)deps[k]=depMap[k];}); if(!deps["express"])deps["express"]="^4.18.2"; var pkgJson=JSON.stringify({"name":pn,"version":"1.0.0","main":"src/index.js","scripts":{"start":"node src/index.js","dev":"nodemon src/index.js"},"dependencies":deps,"devDependencies":{"nodemon":"^3.0.3"}},null,2)+" "; zip.file(folder+"package.json",pkgJson); var fallback="const express=require("express"); const app=express(); app.use(express.json()); app.get("/",(req,res)=>{ res.json({message:""+title+" API"}); }); const PORT=process.env.PORT||3000; app.listen(PORT,()=>console.log("Server on port "+PORT)); "; zip.file(folder+"src/index.js",src||fallback); zip.file(folder+".env.example","PORT=3000 "); zip.file(folder+".gitignore","node_modules/ .env .DS_Store "); zip.file(folder+"README.md","# "+title+" Generated by PantheraHive BOS. ## Setup ```bash npm install ``` ## Run ```bash npm run dev ``` "); } /* --- Vanilla HTML --- */ function buildVanillaHtml(zip,folder,app,code){ var title=slugTitle(app); var isFullDoc=code.trim().toLowerCase().indexOf("=0||code.trim().toLowerCase().indexOf("=0; var indexHtml=isFullDoc?code:" "+title+" "+code+" "; zip.file(folder+"index.html",indexHtml); zip.file(folder+"style.css","/* "+title+" — styles */ *{margin:0;padding:0;box-sizing:border-box} body{font-family:system-ui,-apple-system,sans-serif;background:#fff;color:#1a1a2e} "); zip.file(folder+"script.js","/* "+title+" — scripts */ "); zip.file(folder+"assets/.gitkeep",""); zip.file(folder+"README.md","# "+title+" Generated by PantheraHive BOS. ## Open Double-click `index.html` in your browser. Or serve locally: ```bash npx serve . # or python3 -m http.server 3000 ``` "); zip.file(folder+".gitignore",".DS_Store node_modules/ .env "); } /* ===== MAIN ===== */ var sc=document.createElement("script"); sc.src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"; sc.onerror=function(){ if(lbl)lbl.textContent="Download ZIP"; alert("JSZip load failed — check connection."); }; sc.onload=function(){ var zip=new JSZip(); var base=(_phFname||"output").replace(/.[^.]+$/,""); var app=base.toLowerCase().replace(/[^a-z0-9]+/g,"_").replace(/^_+|_+$/g,"")||"my_app"; var folder=app+"/"; var vc=document.getElementById("panel-content"); var panelTxt=vc?(vc.innerText||vc.textContent||""):""; var lang=detectLang(_phCode,panelTxt); if(_phIsHtml){ buildVanillaHtml(zip,folder,app,_phCode); } else if(lang==="flutter"){ buildFlutter(zip,folder,app,_phCode,panelTxt); } else if(lang==="react-native"){ buildReactNative(zip,folder,app,_phCode,panelTxt); } else if(lang==="swift"){ buildSwift(zip,folder,app,_phCode,panelTxt); } else if(lang==="kotlin"){ buildKotlin(zip,folder,app,_phCode,panelTxt); } else if(lang==="react"){ buildReact(zip,folder,app,_phCode,panelTxt); } else if(lang==="vue"){ buildVue(zip,folder,app,_phCode,panelTxt); } else if(lang==="angular"){ buildAngular(zip,folder,app,_phCode,panelTxt); } else if(lang==="python"){ buildPython(zip,folder,app,_phCode); } else if(lang==="node"){ buildNode(zip,folder,app,_phCode); } else { /* Document/content workflow */ var title=app.replace(/_/g," "); var md=_phAll||_phCode||panelTxt||"No content"; zip.file(folder+app+".md",md); var h=""+title+""; h+="

"+title+"

"; var hc=md.replace(/&/g,"&").replace(//g,">"); hc=hc.replace(/^### (.+)$/gm,"

$1

"); hc=hc.replace(/^## (.+)$/gm,"

$1

"); hc=hc.replace(/^# (.+)$/gm,"

$1

"); hc=hc.replace(/**(.+?)**/g,"$1"); hc=hc.replace(/ {2,}/g,"

"); h+="

"+hc+"

Generated by PantheraHive BOS
"; zip.file(folder+app+".html",h); zip.file(folder+"README.md","# "+title+" Generated by PantheraHive BOS. Files: - "+app+".md (Markdown) - "+app+".html (styled HTML) "); } zip.generateAsync({type:"blob"}).then(function(blob){ var a=document.createElement("a"); a.href=URL.createObjectURL(blob); a.download=app+".zip"; a.click(); URL.revokeObjectURL(a.href); if(lbl)lbl.textContent="Download ZIP"; }); }; document.head.appendChild(sc); }function phShare(){navigator.clipboard.writeText(window.location.href).then(function(){var el=document.getElementById("ph-share-lbl");if(el){el.textContent="Link copied!";setTimeout(function(){el.textContent="Copy share link";},2500);}});}function phEmbed(){var runId=window.location.pathname.split("/").pop().replace(".html","");var embedUrl="https://pantherahive.com/embed/"+runId;var code='';navigator.clipboard.writeText(code).then(function(){var el=document.getElementById("ph-embed-lbl");if(el){el.textContent="Embed code copied!";setTimeout(function(){el.textContent="Get Embed Code";},2500);}});}