< Summary - Envilder CLI

Information
Class: src/envilder/apps/cli/validate/ValidationJsonReport.ts
Assembly: Default
File(s): src/envilder/apps/cli/validate/ValidationJsonReport.ts
Tag: 605_44106794472
Line coverage
100%
Covered lines: 15
Uncovered lines: 0
Coverable lines: 15
Total lines: 155
Line coverage: 100%
Branch coverage
89%
Covered branches: 26
Total branches: 29
Branch coverage: 89.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/apps/cli/validate/ValidationJsonReport.ts

#LineLine coverage
 1import {
 2  type EnvFileKeyComparison,
 3  type EnvValueCheck,
 4  type EnvValueStatus,
 5  summarizeEnvFileFailures,
 6  summarizeValueFailures,
 7} from '../../../core/domain/EnvFileComparison.js';
 8import type { MapFileViolation } from '../../../core/domain/MapFileValidator.js';
 9import {
 10  type RemoteKeyCheck,
 11  type SecretExistenceStatus,
 12  summarizeFailures,
 13} from '../../../core/domain/SecretExistence.js';
 14import { describeError } from '../../../core/infrastructure/describeError.js';
 15
 16/**
 17 * What `envilder validate` found, step by step. A step that did not run, for
 18 * example the remote check with `--offline` or after an invalid map file, is
 19 * left out.
 20 */
 21export type ValidationResults = {
 22  offline: boolean;
 23  mapFile?: {
 24    readonly path: string;
 25    readonly violations: readonly MapFileViolation[];
 26  };
 27  keys?: {
 28    readonly provider: string;
 29    readonly checks: readonly RemoteKeyCheck[];
 30  };
 31  envFile?: {
 32    readonly path: string;
 33    readonly comparison: EnvFileKeyComparison;
 34  };
 35  values?: readonly EnvValueCheck[];
 36};
 37
 38export type ValidationJsonKey = {
 39  name: string;
 40  provider: string;
 41  path: string;
 42  status: SecretExistenceStatus;
 43  reason?: string;
 44};
 45
 46export type ValidationJsonEnvFileKey = {
 47  name: string;
 48  status: 'present' | 'missing';
 49  value?: EnvValueStatus;
 50  reason?: string;
 51};
 52
 53/**
 54 * The document `envilder validate --format json` prints. A section is left
 55 * out when its step did not run, and `error` when nothing stopped the run. It never carries a secret value: value
 56 * comparison only reports `matches`, `differs` or `unreadable`.
 57 */
 58export type ValidationJson = {
 59  valid: boolean;
 60  offline: boolean;
 61  mapFile?: {
 62    path: string;
 63    valid: boolean;
 64    errors: { key: string; message: string }[];
 65  };
 66  keys?: ValidationJsonKey[];
 67  envFile?: {
 68    path: string;
 69    valid: boolean;
 70    keys: ValidationJsonEnvFileKey[];
 71    duplicates: { name: string; count: number }[];
 72    undeclared: string[];
 73  };
 74  error?: { message: string };
 75};
 76
 77/**
 78 * Shapes the validation results as JSON. `error` is what stopped the run
 79 * before every step finished, such as expired credentials; a failed check is
 80 * not an error.
 81 */
 82export function toValidationJson(
 83  results: ValidationResults,
 84  error?: unknown,
 85): ValidationJson {
 1586  const mapFile = results.mapFile && {
 87    path: results.mapFile.path,
 88    valid: results.mapFile.violations.length === 0,
 289    errors: results.mapFile.violations.map(({ key, message }) => ({
 90      key,
 91      message,
 92    })),
 93  };
 1594  const keys = results.keys?.checks.map((check) =>
 1595    toKey(check, results.keys?.provider ?? ''),
 96  );
 1597  const envFile = results.envFile && toEnvFile(results.envFile, results.values);
 98
 1599  return {
 100    valid:
 101      error === undefined &&
 102      mapFile?.valid === true &&
 103      summarizeFailures(results.keys?.checks ?? []) === '' &&
 104      (envFile?.valid ?? true),
 105    offline: results.offline,
 106    ...(mapFile !== undefined && { mapFile }),
 107    ...(keys !== undefined && { keys }),
 108    ...(envFile !== undefined && { envFile }),
 109    ...(error !== undefined && { error: { message: describeError(error) } }),
 110  };
 111}
 112
 113function toKey(check: RemoteKeyCheck, provider: string): ValidationJsonKey {
 15114  return {
 115    name: check.variable,
 116    provider,
 117    path: check.path,
 118    status: check.status,
 119    ...(check.reason !== undefined && { reason: check.reason }),
 120  };
 121}
 122
 123function toEnvFile(
 124  envFile: NonNullable<ValidationResults['envFile']>,
 125  values: readonly EnvValueCheck[] | undefined,
 126): NonNullable<ValidationJson['envFile']> {
 3127  const { comparison } = envFile;
 4128  const valueChecks = new Map(values?.map((check) => [check.variable, check]));
 3129  const present = comparison.present.map((name) => {
 5130    const value = valueChecks.get(name);
 5131    return {
 132      name,
 133      status: 'present' as const,
 134      ...(value !== undefined && { value: value.status }),
 135      ...(value?.reason !== undefined && { reason: value.reason }),
 136    };
 137  });
 3138  const missing = comparison.missing.map((name) => ({
 139    name,
 140    status: 'missing' as const,
 141  }));
 142
 3143  return {
 144    path: envFile.path,
 145    valid:
 146      summarizeEnvFileFailures(comparison, envFile.path) === '' &&
 147      summarizeValueFailures(values ?? []) === '',
 148    keys: [...present, ...missing],
 1149    duplicates: comparison.duplicates.map(({ key, count }) => ({
 150      name: key,
 151      count,
 152    })),
 153    undeclared: [...comparison.undeclared],
 154  };
 155}