agentDocsLayout.test.mjs 5.15 KB
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
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)), '../..');
const resolveProjectPath = (relativePath) => path.join(projectRoot, relativePath);

test('Codex and Cursor share canonical agent instructions and skills', () => {
  for (const relativePath of [
    'AGENTS.md',
    '.agents/skills/requirement-impact-analysis/SKILL.md',
    '.agents/skills/requirement-impact-analysis/references/reference.md',
    '.agents/skills/requirement-impact-analysis/references/reference-full.md',
    'tabWorkOrder/pages/createOrder/AGENTS.md',
  ]) {
    assert.ok(existsSync(resolveProjectPath(relativePath)), `${relativePath} must exist`);
  }

  for (const relativePath of ['.cursor/rules', '.cursor/skills']) {
    assert.equal(existsSync(resolveProjectPath(relativePath)), false, `${relativePath} must be retired`);
  }
});

test('canonical agent files expose required project and workflow contracts', () => {
  const rootAgents = readFileSync(resolveProjectPath('AGENTS.md'), 'utf8');
  assert.match(rootAgents, /# 项目 Agent 约定/);
  assert.match(rootAgents, /## 迁移门禁/);
  assert.match(rootAgents, /## 影响面检查/);
  assert.match(rootAgents, /## 实现规范/);
  assert.match(rootAgents, /## 测试与完成标准/);

  const skill = readFileSync(
    resolveProjectPath('.agents/skills/requirement-impact-analysis/SKILL.md'),
    'utf8',
  );
  assert.match(skill, /^---\r?\nname: requirement-impact-analysis\r?\n/m);
  assert.match(skill, /description: >-\r?\n\s+Use when/);
  assert.match(skill, /做需求/);
  assert.match(skill, /快改/);
  assert.match(skill, /只分析/);

  const createOrderAgents = readFileSync(
    resolveProjectPath('tabWorkOrder/pages/createOrder/AGENTS.md'),
    'utf8',
  );
  assert.match(createOrderAgents, /员工选择/);
  assert.match(createOrderAgents, /礼券/);
  assert.match(createOrderAgents, /orderCouponEffects\.js/);
});

test('durable documents use canonical tool-neutral locations', () => {
  for (const relativePath of [
    'docs/designs/2026-08-17-agent-and-docs-directory-governance-design.md',
    'docs/plans/2026-08-17-agent-and-docs-directory-governance.md',
    'docs/reports/2026-08-10-store-daily-report-final-fix.md',
    'docs/reference/api/README.md',
    'docs/reference/api/rop-swagger-2026-07-22.json',
    'packageEquity/docs/SUBPACKAGE-ANALYSIS.md',
    'tabWorkOrder/docs/createOrder.md',
  ]) {
    assert.ok(existsSync(resolveProjectPath(relativePath)), `${relativePath} must exist`);
  }

  for (const relativePath of [
    'docs/superpowers',
    'docs/packageEquity',
    'docs/tabWorkOrder',
  ]) {
    assert.equal(existsSync(resolveProjectPath(relativePath)), false, `${relativePath} must be retired`);
  }
});

test('retired tool directories are absent', () => {
  for (const relativePath of ['.cursor', '.codex', '.idea', '.cloudbase', '.superpowers']) {
    assert.equal(existsSync(resolveProjectPath(relativePath)), false, `${relativePath} must be absent`);
  }
});

test('portable agent references resolve locally and avoid vendor-specific workflows', () => {
  const referencePaths = [
    '.agents/skills/requirement-impact-analysis/SKILL.md',
    '.agents/skills/requirement-impact-analysis/references/reference.md',
    '.agents/skills/requirement-impact-analysis/references/reference-full.md',
    'tabIndex/docs/ROADMAP.md',
  ];

  for (const relativePath of referencePaths) {
    const absolutePath = resolveProjectPath(relativePath);
    const markdown = readFileSync(absolutePath, 'utf8').replace(/```[\s\S]*?```/g, '');
    for (const match of markdown.matchAll(/\[[^\]]*\]\(([^)]+)\)/g)) {
      const target = match[1].split('#')[0].split('?')[0].trim();
      if (!target || /^(?:https?:|mailto:|\/)/.test(target)) continue;
      const resolvedTarget = path.resolve(path.dirname(absolutePath), decodeURIComponent(target));
      assert.ok(existsSync(resolvedTarget), `${relativePath} has a broken link: ${match[1]}`);
    }
  }

  const skillText = referencePaths
    .slice(0, 3)
    .map(relativePath => readFileSync(resolveProjectPath(relativePath), 'utf8'))
    .join('\n');
  assert.doesNotMatch(skillText, /\bCursor\b|CreatePlan|Agent \/ Plan \/ Ask|wechat-change-standards/);
});

test('store mock extraction requires an explicit transcript path', () => {
  const emptyHome = mkdtempSync(path.join(tmpdir(), 'store-mock-transcript-'));
  const env = { ...process.env };
  delete env.STORE_MOCK_TRANSCRIPT;
  env.USERPROFILE = emptyHome;
  env.HOME = emptyHome;

  let result;
  try {
    result = spawnSync(
      process.execPath,
      [resolveProjectPath('packageDynamicForm/scripts/extract-store-mock-jsonContent.cjs')],
      { cwd: projectRoot, env, encoding: 'utf8' },
    );
  } finally {
    rmSync(emptyHome, { recursive: true, force: true });
  }

  assert.equal(result.status, 1);
  assert.match(result.stderr, /STORE_MOCK_TRANSCRIPT is required/);
  assert.doesNotMatch(result.stderr, /\.cursor[\\/]/);
});