import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fail, pass, repositoryRoot, walk } from './lib.mjs';

const sourceFiles = await walk(path.join(repositoryRoot, 'src'), (file) =>
  /\.(?:ts|tsx|js|jsx|css)$/.test(file),
);
const errors = [];
const prohibited = [
  { label: 'placeholder domain', pattern: /example\.(?:com|org|net)|placeholder\.(?:com|test)/i },
  { label: 'simulated submission', pattern: /simulate(?:d|Submission)|fakeSubmit/i },
  { label: 'research-only state backdoor', pattern: /[?&](?:state|scenario|fixture)=/i },
  { label: 'prototype research logger', pattern: /researchLog|prototypeLog/i },
];
for (const file of sourceFiles) {
  const text = await readFile(file, 'utf8');
  for (const rule of prohibited) {
    if (rule.pattern.test(text))
      errors.push(`${path.relative(repositoryRoot, file)} contains ${rule.label}.`);
  }
}

if (errors.length > 0) fail(errors);
else
  pass(
    'Runtime source contains no placeholder destination, simulated submission, ' +
      'or research-only backdoor.',
  );
