All files / generators/_collections cli-runner.util.ts

100% Statements 278/278
96.87% Branches 31/32
100% Functions 7/7
100% Lines 278/278

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 2791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 3x 3x 4x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 6x 6x 3x 3x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 7x 7x 7x 7x 7x 7x 5x 5x 7x 5x 5x 7x 5x 5x 7x 5x 5x 7x 5x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 5x 1x 1x 5x 1x 1x 1x 1x 1x  
import { DyE2E_FormSuite_Generator } from '../form-suite.generator';
import { DyE2E_ViewportSuite_Generator } from '../viewport-suite.generator';
import { DyE2E_CWVSuite_Generator } from '../../cwv/cwv-suite.generator';
import { DyE2E_FormSuiteGenerate_Options } from '../_models/form-suite-generate-options.interface';
import { DyE2E_ViewportSuiteGenerate_Options } from '../_models/viewport-suite-generate-options.interface';
import { DyE2E_CWVSuiteGenerate_Options } from '../../cwv/_models/cwv-suite-generate-options.interface';
 
/**
 * Stateless CLI-runner — a `bin/dye2e.js` köztes-réteg. Bemenet: args + olvasott
 * config-JSON; kimenet: emit-elt spec.ts string + return-code.
 *
 * Külön util, hogy jasmine-spec-elhető legyen (a bin/* I/O-mentes wrap).
 */
export class DyE2E_CliRunner_Util {
 
  static parseArgs(argv: string[]): { command: string; flags: Record<string, string> } {
    if (argv.length === 0) {
      return { command: 'help', flags: {} };
    }
    const command: string = argv[0];
    const flags: Record<string, string> = {};
    for (let i: number = 1; i < argv.length; i++) {
      const arg: string = argv[i];
      if (arg.startsWith('--')) {
        const key: string = arg.slice(2);
        const next: string | undefined = argv[i + 1];
        if (next && !next.startsWith('--')) {
          flags[key] = next;
          i++;
        } else {
          flags[key] = 'true';
        }
      }
    }
    return { command, flags };
  }
 
  static helpText(): string {
    return `dye2e — @futdevpro/dynamo-e2e standalone CLI
 
Usage:
  dye2e <command> [flags]
 
Commands:
  generate-form-suite      Settings POJO → Playwright form-coverage spec.ts
  generate-viewport-audit  Route-list → 7 viewport × theme spec.ts
  generate-cwv-diag        Route-list → CWV synthetic measurement spec.ts
  doctor                   Diagnose project config (config-files, deps, conventions)
  bootstrap                Scaffold playwright.config.ts + e2e/ + generate.ts
  summarize                Read CWV JSON-report → human-readable summary
  diff                     Compare two CWV reports → regression analysis
  info                     Show registered field-types + CLI capabilities
  schema                   Print JSON Schema for a config type
  validate-config          Validate a config file against its JSON Schema
  version                  Print package version
  help                     This message
 
Flags (generate-form-suite):
  --config <path>          Path to JSON config (formSettings, formRoute, ...)
  --out <path>             Output spec.ts path
  --stdout                 Print to stdout instead of writing to disk
 
Flags (generate-viewport-audit):
  --config <path>          Path to JSON config (routes, viewports, ...)
  --out <path>             Output spec.ts path
  --stdout                 Print to stdout
 
Flags (generate-cwv-diag):
  --config <path>          Path to JSON config (routes, thresholds, ...)
  --out <path>             Output spec.ts path
  --stdout                 Print to stdout
 
Flags (doctor):
  --pwd <path>             Project root (default: cwd)
 
Flags (bootstrap):
  --pwd <path>             Project root (default: cwd)
  --force                  Overwrite existing files
 
Flags (summarize):
  --report <path>          Path to CWV JSON-report
  --format <fmt>           Output format: 'markdown' (default), 'json'
 
Flags (diff):
  --old <path>             Path to baseline CWV JSON-report
  --new <path>             Path to current CWV JSON-report
  --tolerance <pct>        Stable-tolerance % (default 5)
 
Flags (schema):
  --type <type>            Schema type: 'form-suite' | 'viewport-audit' | 'cwv-diag'
 
Flags (validate-config):
  --config <path>          Path to config file
  --type <type>            Schema type: 'form-suite' | 'viewport-audit' | 'cwv-diag'
`;
  }
 
  /**
   * Bootstrap scaffold — visszaadja a generálandó fájlok mappáját
   * ({ path → content }), a CLI ezeket beírja.
   */
  static bootstrapFiles(): Record<string, string> {
    return {
      'playwright.config.ts': `import { defineConfig, devices } from '@playwright/test';
 
// E2E env convention (memory feedback_e2e_ldp_local_cicd_test_target):
// - LDP (\`dc ldp\`) cél: LOCAL env (set E2E_BASE_URL=http://localhost:<port>)
// - CICD (\`dc cdp\`) cél: TEST server (default — biztonságos fallback)
// - SOHA NE localhost-ra default-old hogy CICD-runner ne PROD-ot célozzon
const DEFAULT_TEST_BASE_URL: string = 'https://test.your-app.example.com';
 
export default defineConfig({
  testDir: './e2e/tests',
  fullyParallel: true,
  retries: 0,
  reporter: [['html'], ['list']],
  use: {
    baseURL: process.env.E2E_BASE_URL || DEFAULT_TEST_BASE_URL,
    ignoreHTTPSErrors: process.env.E2E_IGNORE_HTTPS_ERRORS === '1' || (process.env.E2E_BASE_URL || DEFAULT_TEST_BASE_URL).startsWith('https://'),
    trace: 'retain-on-failure',
  },
  projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
});
`,
      'e2e/generate.ts': `import { DyE2E_FormSuite_Generator } from '@futdevpro/dynamo-e2e/generators';
import * as fs from 'fs';
import * as path from 'path';
 
const OUT_DIR: string = path.join(__dirname, 'tests/generated');
fs.mkdirSync(OUT_DIR, { recursive: true });
 
// Példa: 1 mintaform — bővítsd a saját Settings POJO-iddal
const exampleConfig = {
  formSettings: {
    key: 'login',
    fields: [
      { key: 'email', type: 'text', required: true, maxLength: 254 },
      { key: 'password', type: 'password', required: true, minLength: 8 },
    ],
  },
  formRoute: '/login',
  formSelector: 'dynamo-form-new',
};
 
const spec = DyE2E_FormSuite_Generator.emit(exampleConfig);
fs.writeFileSync(path.join(OUT_DIR, 'login.spec.ts'), spec);
console.log('[generate] wrote', fs.readdirSync(OUT_DIR).length, 'spec files');
`,
      'e2e/tests/.gitkeep': '',
      'e2e/tests/generated/.gitkeep': '',
      'e2e/README.md': `# E2E tests (dynamo-e2e)
 
\`\`\`bash
# Generate
node e2e/generate.ts
 
# Run
npx playwright test
\`\`\`
 
See: https://github.com/futdevpro/dynamo-e2e/blob/master/__documentations/getting-started.md
`,
    };
  }
 
  /**
   * Doctor parancs — projekt-config-validáció.
   *
   * Megnézi:
   *  - van-e `@futdevpro/dynamo-e2e` a package.json deps-jében
   *  - van-e `@playwright/test`
   *  - létezik-e `playwright.config.ts` vagy `playwright.config.js`
   *  - létezik-e `e2e/` vagy `tests/` mappa
   *  - létezik-e `.dynamo/pipeline.cicd.config.json` (FDP-project esetén)
   *
   * Bemenet: cwd → kimenet JSON-szerű object.
   */
  static doctorReport(cwd: string, fs: { existsSync: (p: string) => boolean; readFileSync: (p: string, enc: string) => string }): {
    hasDynamoE2E: boolean;
    hasPlaywrightTest: boolean;
    hasPlaywrightConfig: boolean;
    hasE2EDir: boolean;
    hasCicdConfig: boolean;
    suggestions: string[];
  } {
    const pkgJsonPath: string = `${cwd}/package.json`;
    let hasDynamoE2E: boolean = false;
    let hasPlaywrightTest: boolean = false;
    if (fs.existsSync(pkgJsonPath)) {
      try {
        const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
        const allDeps: Record<string, string> = { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) };
        hasDynamoE2E = '@futdevpro/dynamo-e2e' in allDeps;
        hasPlaywrightTest = '@playwright/test' in allDeps;
      } catch (e: unknown) { /* swallow */ }
    }
    const hasPlaywrightConfig: boolean = fs.existsSync(`${cwd}/playwright.config.ts`) || fs.existsSync(`${cwd}/playwright.config.js`);
    const hasE2EDir: boolean = fs.existsSync(`${cwd}/e2e`) || fs.existsSync(`${cwd}/tests`);
    const hasCicdConfig: boolean = fs.existsSync(`${cwd}/.dynamo/pipeline.cicd.config.json`);
 
    const suggestions: string[] = [];
    if (!hasDynamoE2E) {
      suggestions.push('Install: pnpm add -D @futdevpro/dynamo-e2e');
    }
    if (!hasPlaywrightTest) {
      suggestions.push('Install: pnpm add -D @playwright/test');
    }
    if (!hasPlaywrightConfig) {
      suggestions.push('Create: playwright.config.ts — lásd getting-started.md');
    }
    if (!hasE2EDir) {
      suggestions.push('Create: e2e/ folder a generated spec-eknek');
    }
    if (!hasCicdConfig) {
      suggestions.push('Optional: .dynamo/pipeline.cicd.config.json — Overseer CICD step (lásd overseer-step-template.md)');
    }
 
    return {
      hasDynamoE2E,
      hasPlaywrightTest,
      hasPlaywrightConfig,
      hasE2EDir,
      hasCicdConfig,
      suggestions,
    };
  }
 
  static versionText(version: string): string {
    return `@futdevpro/dynamo-e2e ${version}`;
  }
 
  /**
   * Info command — registry + capability summary (consumer self-describe).
   */
  static infoText(version: string, fieldTypes: string[]): string {
    const sortedTypes: string[] = [...fieldTypes].sort();
    const wave1: string[] = sortedTypes.filter((t: string): boolean => ['text', 'textarea', 'password', 'number', 'checkbox', 'select', 'multiselect', 'date'].indexOf(t) !== -1);
    const wave2a: string[] = sortedTypes.filter((t: string): boolean => ['slider', 'sliderRange', 'radioSelect', 'dateTime', 'dateRange'].indexOf(t) !== -1);
    const wave2b: string[] = sortedTypes.filter((t: string): boolean => ['chipSelect', 'autocomplete', 'color', 'file', 'richtextarea'].indexOf(t) !== -1);
    return `@futdevpro/dynamo-e2e ${version}
 
Registered field-types (${sortedTypes.length}):
  Wave-1   (${wave1.length}): ${wave1.join(', ')}
  Wave-2a  (${wave2a.length}): ${wave2a.join(', ')}
  Wave-2b  (${wave2b.length}): ${wave2b.join(', ')}
 
CLI capabilities:
  - 3 generators (form-suite, viewport-audit, cwv-diag)
  - 2 report utils (summarize, diff)
  - 2 config helpers (bootstrap, doctor)
  - Wave-3 INP synthetic interaction-driver
  - Per-route CWV threshold overrides
 
Docs: github.com/futdevpro/dynamo-e2e/__documentations/
`;
  }
 
  /**
   * A 3 generate-parancs köztes-runner-je. A konfig egy JSON-string (filesystem
   * read az `bin/`-ben történik), a kimenet a spec.ts string.
   */
  static runGenerate(
    command: 'generate-form-suite' | 'generate-viewport-audit' | 'generate-cwv-diag',
    configJson: string,
  ): string {
    const config: unknown = JSON.parse(configJson);
    if (command === 'generate-form-suite') {
      return DyE2E_FormSuite_Generator.emit(config as DyE2E_FormSuiteGenerate_Options);
    }
    if (command === 'generate-viewport-audit') {
      return DyE2E_ViewportSuite_Generator.emit(config as DyE2E_ViewportSuiteGenerate_Options);
    }
    if (command === 'generate-cwv-diag') {
      return DyE2E_CWVSuite_Generator.emit(config as DyE2E_CWVSuiteGenerate_Options);
    }
    throw new Error(`Unknown command: ${command}`);
  }
}