knowledgeWorkflow.test.mjs 3.98 KB
/**
 * @file 门禁面契约:本 lane 的离线测试入口、知识图谱命令与 live 写操作闸门必须留在原位。
 *
 * 源工程用 GitHub Actions 的 yml 承载这些约束;并入 monorepo 后 CI 真源是仓库根的
 * .gitlab-ci.yml,lane 里再放一份 .github/workflows 只会是永不执行的假配置。因此这里改成
 * 直接断言「门禁本身」——脚本清单、运行器的 live 排除、以及 live 配置的写入闸门。
 * 迁移这次丢的就是第一类(package.json 的 scripts 面),所以它排在最前。
 */
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

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

function read(relativePath) {
  return fs.readFileSync(path.join(projectRoot, relativePath), 'utf8')
}

test('lane gate exposes offline tests, knowledge commands and migration check', () => {
  const scripts = JSON.parse(read('package.json')).scripts

  for (const required of [
    'test',
    'test:live',
    'test:affected',
    'knowledge:build',
    'knowledge:check',
    'impact',
    'gen:component-index',
    'check:migration',
  ]) {
    assert.ok(scripts?.[required], `package.json scripts.${required} must exist`)
  }

  assert.match(scripts.test, /run-tests\.mjs/)
  assert.doesNotMatch(scripts.test, /--live/, '默认 test 不得带上需要开发者工具的 live 用例')
  assert.match(scripts['test:live'], /--live/)
})

test('test runner excludes live cases unless asked, and reports failures', () => {
  const runner = read('scripts/run-tests.mjs')

  assert.match(runner, /\.live\.test\.mjs/)
  assert.match(runner, /--live/)
  // fixtures 目录里是知识图谱的样例工程,不是本仓测试。
  assert.match(runner, /fixtures/)
  assert.match(runner, /process\.exit\(counts\.fail/)
})

test('live order runs require manual opt-in, approved profiles and explicit write approval', () => {
  const config = read('tests/order-lifecycle/e2e/testConfig.mjs')
  const profiles = read('tests/order-lifecycle/liveProfiles.mjs')

  assert.match(config, /RUN_ORDER_E2E/)
  assert.match(config, /ORDER_E2E_PROFILE/)
  // 写操作闸门在 liveProfiles 里收口:没有显式 LIVE_ALLOW_WRITES=1 一律拒绝。
  assert.match(profiles, /LIVE_ALLOW_WRITES/)
  assert.match(profiles, /mingming-dev/)
  assert.match(profiles, /bobo-test/)

  // 手机号只能来自 CI secret / 本机环境变量,不得写进 lane 任何 live 文件。
  const liveDir = path.join(projectRoot, 'tests/order-lifecycle')
  const offenders = []
  const scan = (dir) => {
    for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
      const full = path.join(dir, entry.name)
      if (entry.isDirectory()) scan(full)
      else if (/\.(mjs|js)$/.test(entry.name) && /\b1\d{10}\b/.test(read(path.relative(projectRoot, full)))) {
        offenders.push(path.relative(projectRoot, full))
      }
    }
  }
  scan(liveDir)
  assert.deepEqual(offenders, [], `live 目录中出现疑似硬编码手机号: ${offenders.join(', ')}`)
})

test('documentation entry describes setup, querying, maintenance and Live boundaries', () => {
  const documentation = read('docs/README.md')

  for (const required of [
    'npm run knowledge:build',
    'npm run knowledge:check',
    'npm run impact',
    'npm run test:affected',
    'npm test',
    '### 日常维护',
    '### Live 自动化',
  ]) {
    assert.ok(documentation.includes(required), `missing docs entry: ${required}`)
  }
})

test('root README exposes the knowledge workflow to first-time contributors', () => {
  const readme = read('README.md')

  for (const required of [
    '领域知识图谱与增量测试',
    'npm run knowledge:build',
    'npm run knowledge:check',
    'npm run impact',
    'npm run test:affected',
    './docs/README.md#领域知识图谱搭建与使用',
  ]) {
    assert.ok(readme.includes(required), `missing root README entry: ${required}`)
  }
})