import test from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')

const PRODUCTION_FILES = [
  'src/services/api.js',
  'src/contexts/FilesContext.jsx',
  'src/contexts/SkillsContext.jsx',
  'src/pages/Architecture.jsx',
  'src/pages/CreateSkillChat.jsx',
  'src/services/skillUploadPlan.js',
  'src/components/EvaluationPanel.jsx',
]

test('production app has no local mock data or mock data switch', () => {
  assert.equal(fs.existsSync(path.join(ROOT, 'src/mock')), false, 'src/mock must not exist in the real app')

  for (const rel of PRODUCTION_FILES) {
    const file = path.join(ROOT, rel)
    if (!fs.existsSync(file)) continue
    const text = fs.readFileSync(file, 'utf8')
    assert.doesNotMatch(text, /VITE_USE_MOCK|USE_MOCK|from ['"].*mock|import .*mock|buildSeedFiles|mock\/\*\.js|mock 数据|（mock）|mockAgentReply|buildImmediateSkillCreationPlan|handleImmediateCreateRequest|immediateSkillMarkdown|immediateExamplesMarkdown|immediateRubricMarkdown/i, rel)
  }
})

test('evaluation panel does not cache evaluation reports instead of calling AgentRun', () => {
  const file = path.join(ROOT, 'src/components/EvaluationPanel.jsx')
  const text = fs.readFileSync(file, 'utf8')

  assert.doesNotMatch(text, /eval_skill_|已加载上次评测缓存|localStorage\.setItem\(cacheKey|savedAt/i)
})

test('production prompts do not anchor evaluation to a specific repeated score', () => {
  const files = [
    'src/components/EvaluationPanel.jsx',
    'src/services/chatWorkflow.js',
    'cf-worker/src/routes/skill-run.js',
  ]

  for (const rel of files) {
    const text = fs.readFileSync(path.join(ROOT, rel), 'utf8')
    assert.doesNotMatch(text, /78\/100|总分：78|默认给\s*78/i, rel)
  }
})

test('visible product copy does not mention retired skill runtimes or old workbench names', () => {
  const files = [
    'src/services/api.js',
    'src/components/EvaluationPanel.jsx',
    'src/pages/AiFlowDrawing.jsx',
    'src/components/ProviderIcon.jsx',
    'src/pages/CreateSkill.jsx',
    'cf-worker/src/routes/eval.js',
  ]

  for (const rel of files) {
    const text = fs.readFileSync(path.join(ROOT, rel), 'utf8')
    assert.doesNotMatch(text, /Qoder|BYOK|qoder-byok|Skill Workspace/i, rel)
  }
})
