# leancodepl/patrol architecture

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

- Interactive diagram: https://gitdiagram.com/leancodepl/patrol
- Repository: https://github.com/leancodepl/patrol

## Overview

##### Project Type & Purpose
- Patrol is a comprehensive UI testing framework built for Flutter apps. It overcomes limitations in flutter_test and integration_test by providing native automation capabilities (e.g., handling OS interactions like permissions, notifications, and toggling system features) and a set of custom finders that simplify test authoring.
- The project is structured as a monorepo with multiple interrelated packages (for example, patrol, patrol_cli, patrol_finders, patrol_gen, etc.) and includes integrations with native mobile platforms (Android, iOS, macOS).

##### File Structure
- Key directories include:
– Top-level configuration files (CI workflows in the .github folder, build scripts, and configuration files for CI/CD).
– The dev folder that contains sample integration tests and demonstration apps (e2e tests).
– The packages folder, which is segmented into components such as:
- patrol: the core testing framework with custom finder APIs and native automation extensions.
- patrol_cli: the command-line interface that facilitates test execution, contract generation, and native automation communication.
- patrol_finders: a companion package providing enhanced finders for Flutter UI.
- patrol_gen: a code-generation tool for bridging Dart and native contracts.
- Others like adb, patrol_devtools_extension, and patrol_log that extend functionality (e.g., logging, integration with devtools, and ADB communication).
- Additionally, documentation folders (docs and dev_docs) supply the user and developer guides.

##### README for Additional Insights
- The README explains the framework’s purpose: to simplify writing UI tests with improved APIs (using “patrolTest,” custom finders, and native automation calls).
- It showcases examples where test functions use a DSL to interact with both Flutter widgets and native features.
- It also provides instructions for generating native contracts, highlighting how the CLI and code generator (patrol_gen) integrate with the core library.

a. Main Components:
– Flutter App/Test Runner: Represents the environment where integration tests are executed.
– Patrol Core Library (packages/patrol): Contains the testing DSL (e.g., patrolTest), custom finder implementations, and the binding between Flutter and native automation.
– Patrol CLI (packages/patrol_cli): Acts as the orchestration engine to run tests, manage device communication, and enable native automation features.
– Native Automation Layer:
- For Android: Modules written in Kotlin that interface with native device features.
- For iOS/macOS: Swift/Objective-C and related native code (also seen in packages like patrol_devtools_extension and patrol_finders).
– Contract/Code Generator (packages/patrol_gen): Generates bridge code (“contracts”) ensuring that communication between the Dart layer and native automation is synchronized.
– Supplementary Components: adb package (providing tooling for Android Debug Bridge interactions), logging (patrol_log), and integrations with developer tools.
– Continuous Integration & Deployment (CI/CD): GitHub workflows and scripts for testing on various platforms, device emulators, and publishing artifacts.

b. Relationships & Interactions:
– The CLI invokes the core testing library and passes commands and test instructions to the native automation layer.
– The core library in the Flutter app communicates with native modules (via platform channels or defined contracts) to perform OS-level tasks.
– The patrol_gen tool updates or generates native contracts that both the Dart layer and native code expect, ensuring consistent communication.
– Auxiliary packages (like patrol_finders and patrol_devtools_extension) extend core functionality and might plug into the testing workflow.

c. Architectural Patterns & Design Principles:
– Modularized, multi-package monorepo: Each package represents a clear concern (test DSL, CLI, native automation, logging, etc.).
– Decoupling between test writing (DSL) and native interactions: The contract generator enforces this boundary.
– Client-server style communication between the Flutter testing interface and native services (with the CLI acting as an orchestrator and mediator).
– Extensibility: Custom finders and plugins (e.g., devtools extension) allow for easier integration with additional debugging or testing tools.
– Use of CI/CD pipelines to drive multi-platform testing, reflecting the real-world device and emulator interactions.

d. Relevant Technologies & Frameworks:
– Flutter and Dart for the main testing API.
– Kotlin (Android) and Swift/Objective-C (iOS/macOS) for native automation.
– GitHub Actions for CI/CD workflows.
– Standard Dart tooling (pubspec, analysis_options) and code generation libraries.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "CI/CD and Automation"
        CICD["CI/CD Pipelines"]:::external
    end

    subgraph "Test Environment"
        TEST_RUNNER["Flutter Test Runner"]:::runner
        CORE_LIB["Patrol Core Library"]:::dart
    end

    subgraph "Test Orchestration"
        CLI["Patrol CLI"]:::dart
        CODE_GEN["Contract/Code Generator"]:::dart
    end

    subgraph "Native Automation"
        NATIVE_ANDROID["Native Automation – Android"]:::native
        NATIVE_IOS["Native Automation – iOS/macOS"]:::native
    end

    subgraph "Supplementary Tools"
        FINDERS["Patrol Finders"]:::supplement
        ADB["ADB Tooling"]:::supplement
        LOGGING["Logging Module"]:::supplement
        DEVTOOLS["Devtools Extension"]:::supplement
    end

    %% Connections from CI/CD
    CICD -->|"triggers"| TEST_RUNNER
    CICD -->|"triggers"| CLI

    %% Test Environment interactions
    TEST_RUNNER -->|"executes_DSL"| CORE_LIB

    %% Orchestration interactions
    CLI -->|"orchestrates"| CORE_LIB
    CLI -->|"generates_contract"| CODE_GEN
    CODE_GEN -->|"syncs_contracts"| CORE_LIB
    CODE_GEN -->|"updates_contracts"| NATIVE_ANDROID
    CODE_GEN -->|"updates_contracts"| NATIVE_IOS

    %% Native Automation interactions
    CORE_LIB -->|"calls"| NATIVE_ANDROID
    CORE_LIB -->|"calls"| NATIVE_IOS

    %% Supplementary Tools integration
    CORE_LIB -->|"enhanced_by"| FINDERS
    CORE_LIB -->|"logs_to"| LOGGING
    CLI -->|"uses"| ADB
    CLI -->|"integrates"| DEVTOOLS

    %% Click Events
    click TEST_RUNNER "https://github.com/leancodepl/patrol/tree/master/dev/e2e_app"
    click CORE_LIB "https://github.com/leancodepl/patrol/tree/master/packages/patrol"
    click CLI "https://github.com/leancodepl/patrol/tree/master/packages/patrol_cli"
    click CODE_GEN "https://github.com/leancodepl/patrol/tree/master/packages/patrol_gen"
    click NATIVE_ANDROID "https://github.com/leancodepl/patrol/tree/master/packages/patrol/android"
    click NATIVE_IOS "https://github.com/leancodepl/patrol/tree/master/packages/patrol/darwin"
    click FINDERS "https://github.com/leancodepl/patrol/tree/master/packages/patrol_finders"
    click ADB "https://github.com/leancodepl/patrol/tree/master/packages/adb"
    click LOGGING "https://github.com/leancodepl/patrol/tree/master/packages/patrol_log"
    click DEVTOOLS "https://github.com/leancodepl/patrol/tree/master/packages/patrol_devtools_extension"
    click CICD "https://github.com/leancodepl/patrol/tree/master/.github/workflows"

    %% Styles
    classDef runner fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef dart fill:#B2F2BB,stroke:#333,stroke-width:2px;
    classDef native fill:#FFD6A5,stroke:#333,stroke-width:2px;
    classDef supplement fill:#E0BBE4,stroke:#333,stroke-width:2px;
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:2px;
```
