import {
  TEST_TYPES,
  WU_TYPES
} from "./chunk-V6OJGLBA.js";

// ../core/dist/manual-test-validator.js
import path from "path";
var CODE_EXTENSIONS = Object.freeze([".js", ".ts", ".tsx", ".ts"]);
var NON_CODE_EXTENSIONS = Object.freeze([".md", ".yaml", ".yml", ".json"]);
var CONFIG_PATTERNS = Object.freeze([
  /config\./i,
  // vitest.config.ts, eslint.config.js
  /\.config\./i,
  // *.config.ts, *.config.js
  /rc\.[jt]s$/i,
  // .eslintrc.js, .prettierrc.ts
  /^\.[a-z]+rc\./i
  // .eslintrc.*, .prettierrc.*
]);
var HEX_CORE_CODE_PATTERNS = Object.freeze([
  "packages/@lumenflow/core/",
  "packages/@lumenflow/cli/",
  "packages/@lumenflow/agent/"
]);
var EXEMPT_LANES = Object.freeze([]);
var EXEMPT_TYPES = Object.freeze([WU_TYPES.DOCUMENTATION]);
var TDD_EXCEPTION_MARKER = "tdd-exception:";
function isCodeFile(filePath) {
  if (!filePath || typeof filePath !== "string") {
    return false;
  }
  const fileName = path.basename(filePath);
  const ext = path.extname(filePath).toLowerCase();
  if (NON_CODE_EXTENSIONS.includes(ext)) {
    return false;
  }
  if (!CODE_EXTENSIONS.includes(ext)) {
    return false;
  }
  const isConfig = CONFIG_PATTERNS.some((pattern) => pattern.test(fileName));
  if (isConfig) {
    return false;
  }
  return true;
}
function hasDocumentedTddException(notes) {
  if (typeof notes === "string") {
    return notes.toLowerCase().includes(TDD_EXCEPTION_MARKER);
  }
  if (Array.isArray(notes)) {
    return notes.some((entry) => typeof entry === "string" && hasDocumentedTddException(entry));
  }
  return false;
}
function isExemptFromAutomatedTests(doc) {
  if (!doc)
    return false;
  const type = doc.type || "";
  if (typeof type === "string" && EXEMPT_TYPES.includes(type)) {
    return true;
  }
  if (hasDocumentedTddException(doc.notes)) {
    return true;
  }
  return false;
}
function hasAutomatedTest(tests) {
  if (!tests || typeof tests !== "object")
    return false;
  const hasItems = (arr) => Array.isArray(arr) && arr.length > 0;
  const t = tests;
  return hasItems(t[TEST_TYPES.UNIT]) || hasItems(t[TEST_TYPES.E2E]) || hasItems(t[TEST_TYPES.INTEGRATION]);
}
function analyzeCodePaths(codePaths) {
  if (!codePaths || !Array.isArray(codePaths) || codePaths.length === 0) {
    return { hasCodeFiles: false, codeFiles: [] };
  }
  const codeFiles = codePaths.filter((p) => isCodeFile(p));
  return {
    hasCodeFiles: codeFiles.length > 0,
    codeFiles
  };
}
function validateAutomatedTestRequirement(doc) {
  const errors = [];
  if (!doc) {
    return { valid: true, errors: [] };
  }
  if (isExemptFromAutomatedTests(doc)) {
    return { valid: true, errors: [] };
  }
  const codePaths = doc.code_paths || [];
  const { hasCodeFiles, codeFiles } = analyzeCodePaths(codePaths);
  if (!hasCodeFiles) {
    return { valid: true, errors: [] };
  }
  const tests = doc.tests || doc.test_paths || {};
  if (!hasAutomatedTest(tests)) {
    errors.push(`WU modifies code files but has no automated tests.
  Code files: ${codeFiles.join(", ")}
  Required: At least one automated test (unit, e2e, or integration)
  Manual-only tests are not allowed for code changes.

  Fix: Add tests to tests.unit, tests.e2e, or tests.integration in WU YAML.`);
    return { valid: false, errors };
  }
  return { valid: true, errors: [] };
}

export {
  isCodeFile,
  validateAutomatedTestRequirement
};
