/**
 * cli:run-ui-test — types.ts
 *
 * Pure test-execution CLI. Reads manifest + users, runs each test via
 * dev-browser-driver, emits a single envelope with all results on stdout.
 *
 * Persistence (UiTestRun + UiTestResult rows) is the Studio runner's job —
 * it parses this envelope and calls uiTestService.recordResult per entry.
 */
import { z } from 'zod';

export const RunUiTestInputSchema = z.object({
  projectPath: z.string().min(1),
  frontendUrl: z.string().url(),
  apiUrl: z.string().url(),
  /** Filter to one module if provided. Manifest must contain the module. */
  module: z.string().optional(),
  /** Path to manifest relative to projectPath. Default: tests/ui-test/manifest.json. */
  manifestPath: z.string().default('tests/ui-test/manifest.json'),
  /** Path to test-users.json relative to projectPath. Default: tests/ui-test/test-users.json. */
  usersPath: z.string().default('tests/ui-test/test-users.json'),
  /** Capture dir relative to projectPath for screenshots. Default: tests/ui-test/captures. */
  captureDir: z.string().default('tests/ui-test/captures'),
  /** Per-test wall-clock cap in ms. Default 60s. */
  timeoutMs: z.number().int().positive().default(60_000),
  /** When true, only run tests for this single test id. */
  testId: z.string().optional(),
});

export type RunUiTestInput = z.infer<typeof RunUiTestInputSchema>;

export interface RunUiTestResult {
  testId: string;
  scenario: string;
  page: string;
  role: string;
  status: 'pass' | 'fail';
  reason: string;
  durationMs: number;
  capture: {
    httpRequests?: Array<{ url: string; method: string; status: number }>;
    consoleErrors?: string[];
    screenshotRelPath?: string;
    stderrTail?: string;
    exitCode?: number | null;
  };
}

export interface RunUiTestReport {
  module: string;
  appCode: string;
  totalTests: number;
  passed: number;
  failed: number;
  durationMs: number;
  results: RunUiTestResult[];
}
