All files / backend/test/mocks mock-provider.ts

11.11% Statements 6/54
8.69% Branches 2/23
13.04% Functions 3/23
11.11% Lines 6/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302                                                                                                                        6x 6x                                                                                                                                                                                                                                                                 6x 6x                             6x             5x                                                                                                                                                                                
/**
 * Mock SSG Provider
 *
 * A configurable mock implementation of the SSGProvider interface.
 * Used to:
 * 1. Validate that the SSGProvider abstraction works for new providers
 * 2. Test the provider registry with a controllable provider
 * 3. Serve as a template for implementing new providers
 */
 
import type {
  SSGProvider,
  ProviderMetadata,
  SSGProviderDependencies,
  SSGBinaryManager,
  SSGDevServer,
  SSGBuilder,
  SSGConfigQuerier,
  SSGSiteCreationOptions,
  SSGDetectionResult,
  SSGServerConfig,
  SSGBuildConfig,
  DownloadProgress,
  SSGSiteConfig,
} from '../../src/ssg-providers/types.js';
 
/**
 * Configuration for MockProvider behavior
 */
export interface MockProviderConfig {
  /** Provider metadata */
  metadata?: Partial<ProviderMetadata>;
 
  /** Detection configuration */
  detection?: {
    /** Files that indicate this provider */
    configFiles?: string[];
    /** Directories that indicate this provider */
    markerDirs?: string[];
    /** Detection confidence */
    confidence?: 'high' | 'medium' | 'low';
    /** Should detectSite return true? */
    shouldDetect?: boolean;
  };
 
  /** Should return a binary manager? */
  hasBinaryManager?: boolean;
 
  /** Should throw errors? */
  throwErrors?: boolean;
}
 
/**
 * Mock Binary Manager
 */
class MockBinaryManager implements SSGBinaryManager {
  private dependencies: SSGProviderDependencies;
  private config: MockProviderConfig;
 
  constructor(dependencies: SSGProviderDependencies, config: MockProviderConfig) {
    this.dependencies = dependencies;
    this.config = config;
  }
 
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  isVersionInstalled(version: string): boolean {
    // Mock: always return false (not installed)
    return false;
  }
 
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  async *download(version: string, skipExistCheck?: boolean): AsyncGenerator<DownloadProgress> {
    // Simulate download progress
    yield { percent: 0, message: 'Starting download...', complete: false };
    yield { percent: 50, message: 'Downloading...', complete: false };
    yield { percent: 100, message: 'Download complete', complete: true };
  }
 
  async cancel(): Promise<void> {
    this.dependencies.outputConsole.appendLine('Download cancelled');
  }
 
  async ensureAvailable(version: string): Promise<void> {
    if (!this.isVersionInstalled(version)) {
      for await (const progress of this.download(version)) {
        this.dependencies.outputConsole.appendLine(progress.message);
      }
    }
  }
}
 
/**
 * Mock Dev Server
 */
class MockDevServer implements SSGDevServer {
  private config: SSGServerConfig;
  private dependencies: SSGProviderDependencies;
  private process: { pid: number } | null = null;
 
  constructor(config: SSGServerConfig, dependencies: SSGProviderDependencies) {
    this.config = config;
    this.dependencies = dependencies;
  }
 
  async serve(): Promise<void> {
    this.dependencies.outputConsole.appendLine(
      `Mock server started on port ${this.config.port || 13131}`
    );
    // Mock: store a fake process
    this.process = { pid: 12345 };
  }
 
  stopIfRunning(): void {
    if (this.process) {
      this.dependencies.outputConsole.appendLine('Mock server stopped');
      this.process = null;
    }
  }
 
  getCurrentProcess(): unknown {
    return this.process;
  }
}
 
/**
 * Mock Builder
 */
class MockBuilder implements SSGBuilder {
  private config: SSGBuildConfig;
  private dependencies: SSGProviderDependencies;
 
  constructor(config: SSGBuildConfig, dependencies: SSGProviderDependencies) {
    this.config = config;
    this.dependencies = dependencies;
  }
 
  async build(): Promise<void> {
    this.dependencies.outputConsole.appendLine(
      `Mock build started for ${this.config.workspacePath}`
    );
    this.dependencies.outputConsole.appendLine(`Output directory: ${this.config.destination}`);
    this.dependencies.outputConsole.appendLine('Mock build completed successfully');
  }
}
 
/**
 * Mock Config Querier
 */
class MockConfigQuerier implements SSGConfigQuerier {
  private workspacePath: string;
  private version: string;
  private configFile?: string;
  private dependencies: SSGProviderDependencies;
 
  constructor(
    workspacePath: string,
    version: string,
    configFile: string | undefined,
    dependencies: SSGProviderDependencies
  ) {
    this.workspacePath = workspacePath;
    this.version = version;
    this.configFile = configFile;
    this.dependencies = dependencies;
  }
 
  async getConfig(): Promise<SSGSiteConfig> {
    return {
      config: {
        title: 'Mock Site',
        baseURL: 'http://example.org',
      },
      contentDirs: ['content'],
    };
  }
 
  async getConfigLines(): Promise<string[]> {
    return ['title: Mock Site', 'baseURL: http://example.org'];
  }
}
 
/**
 * Mock SSG Provider
 */
export class MockProvider implements SSGProvider {
  private dependencies: SSGProviderDependencies;
  private config: MockProviderConfig;
  private binaryManager: MockBinaryManager | null;
 
  constructor(dependencies: SSGProviderDependencies, config: MockProviderConfig = {}) {
    this.dependencies = dependencies;
    this.config = {
      metadata: {},
      detection: {
        configFiles: ['mock.config.json'],
        markerDirs: ['mock-content', 'mock-layouts'],
        confidence: 'high',
        shouldDetect: true,
        ...config.detection,
      },
      hasBinaryManager: true,
      throwErrors: false,
      ...config,
    };
 
    // Create binary manager if configured
    this.binaryManager =
      this.config.hasBinaryManager === true
        ? new MockBinaryManager(dependencies, this.config)
        : null;
  }
 
  getMetadata(): ProviderMetadata {
    return {
      type: 'mock',
      name: 'Mock SSG',
      configFormats: ['json', 'yaml'],
      requiresBinary: this.config.hasBinaryManager === true,
      supportsDevServer: true,
      supportsBuild: true,
      supportsConfigQuery: true,
      version: '1.0.0',
      ...this.config.metadata,
    };
  }
 
  getBinaryManager(): SSGBinaryManager | null {
    return this.binaryManager;
  }
 
  createDevServer(config: SSGServerConfig): SSGDevServer {
    if (this.config.throwErrors) {
      throw new Error('Mock error: Cannot create dev server');
    }
    return new MockDevServer(config, this.dependencies);
  }
 
  createBuilder(config: SSGBuildConfig): SSGBuilder {
    if (this.config.throwErrors) {
      throw new Error('Mock error: Cannot create builder');
    }
    return new MockBuilder(config, this.dependencies);
  }
 
  createConfigQuerier(
    workspacePath: string,
    version: string,
    configFile?: string
  ): SSGConfigQuerier | null {
    if (this.config.throwErrors) {
      return null;
    }
    return new MockConfigQuerier(workspacePath, version, configFile, this.dependencies);
  }
 
  async createSite(options: SSGSiteCreationOptions): Promise<void> {
    if (this.config.throwErrors) {
      throw new Error('Mock error: Cannot create site');
    }
 
    this.dependencies.outputConsole.appendLine(
      `Mock site created at ${options.directory} with title: ${options.title}`
    );
  }
 
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  async detectSite(directory: string): Promise<SSGDetectionResult> {
    if (this.config.throwErrors) {
      throw new Error('Mock error: Cannot detect site');
    }
 
    // Mock detection logic based on configuration
    const { configFiles, markerDirs, confidence, shouldDetect } = this.config.detection!;
 
    if (shouldDetect) {
      return {
        isDetected: true,
        confidence: confidence || 'high',
        configFiles: configFiles,
        metadata: {
          markerDirs,
        },
      };
    }
 
    return {
      isDetected: false,
      confidence: 'low',
    };
  }
}
 
/**
 * Create a mock provider with custom configuration
 */
export function createMockProvider(
  dependencies: SSGProviderDependencies,
  config?: MockProviderConfig
): MockProvider {
  return new MockProvider(dependencies, config);
}