tests — extensions

Module: tests-extensions Cohesion: 0.80 Members: 0

tests — extensions

This document describes the ExtensionLoader module, which is comprehensively tested by tests/extensions/extension-loader.test.ts. While the provided source code is the test file itself, the documentation focuses on the ExtensionLoader class and its associated types and behaviors, as defined and validated by these tests.

The ExtensionLoader is a core component responsible for discovering, loading, managing, and orchestrating the lifecycle of extensions within the system.


ExtensionLoader Module Documentation

Purpose

The ExtensionLoader module (src/extensions/extension-loader.ts) provides the foundational capabilities for managing extensions. It enables the application to:

The extension-loader.test.ts file serves as the primary specification for the ExtensionLoader's expected behavior, covering its API, error handling, and lifecycle management.

Key Concepts

graph TD
    subgraph Extension Lifecycle
        A[Discovered] --> B(Loaded);
        B -- activate(config) --> C(Active);
        C -- deactivate() --> D(Disabled);
        B -- dispose() --> E(Disposed);
        C -- dispose() --> E;
        D -- dispose() --> E;
        B -- activate(invalid config/deps) --> F(Error);
        D -- activate() --> C;
    end

ExtensionLoader Class

The ExtensionLoader class is the central API for extension management.

Constructor

new ExtensionLoader(searchPaths: string[])

Initializes the loader with an array of directory paths to search for extensions. These paths are scanned during the discover phase.

Static Methods

##### ExtensionLoader.parseManifest(dir: string): ExtensionManifest | null

Parses the extension.json file within the given directory.

Instance Methods

##### discover(): ExtensionManifest[]

Scans the searchPaths provided during construction to find all valid extension manifests.

##### load(name: string): ExtensionInstance | { error: string }

Loads a specific extension by its name. This makes the extension's metadata available but does not activate its functionality.

##### loadAll(): ExtensionInstance[]

Discovers all extensions in the configured searchPaths and attempts to load them.

##### list(type?: ExtensionManifest['type']): ExtensionInstance[]

Retrieves a list of all currently loaded extensions, optionally filtered by type.

##### get(name: string): ExtensionInstance | undefined

Retrieves a specific loaded extension by its name.

##### validateConfig(name: string, config: Record): { valid: boolean; errors: string[] }

Validates a given configuration object against an extension's configSchema defined in its manifest.

##### checkDependencies(name: string): { satisfied: boolean; missing: string[] }

Checks if all dependencies declared by an extension are currently loaded.

##### activate(name: string, config?: Record): Promise

Activates a loaded extension, making it operational. This involves validating its configuration and checking its dependencies.

##### deactivate(name: string): Promise

Deactivates an active extension, transitioning its status to 'disabled'.

##### dispose(): Promise

Clears all loaded extensions and performs any necessary cleanup.

Event Emission

The ExtensionLoader extends EventEmitter and emits the following events:

Test Utilities (extension-loader.test.ts)

The test file includes several helper functions to facilitate testing the ExtensionLoader in isolation:

These utilities are crucial for setting up controlled test environments and simulating various extension configurations without relying on actual file system structures.