domainCatalog.test.mjs 3.13 KB
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)), '../..')
const catalogPath = path.join(projectRoot, 'docs/knowledge/domain-catalog.json')
const gapsPath = path.join(projectRoot, 'docs/knowledge/known-gaps.json')
const graphPath = path.join(projectRoot, 'docs/generated/knowledge-graph.json')

test('catalog defines the six current-project domains and classifies every route', () => {
  assert.equal(fs.existsSync(catalogPath), true, 'domain catalog must exist')
  const catalog = JSON.parse(fs.readFileSync(catalogPath, 'utf8'))
  const app = JSON.parse(fs.readFileSync(path.join(projectRoot, 'app.json'), 'utf8'))

  assert.deepEqual(
    catalog.domains.map((domain) => domain.name),
    ['application', 'workspace', 'study', 'matters', 'workOrder', 'shared'],
  )

  const roots = catalog.domains.flatMap((domain) =>
    domain.roots.map((root) => ({
      domain: domain.name,
      root: root.replace(/\/$/, ''),
    })),
  )
  const routes = [
    ...(app.pages || []),
    ...(app.subpackages || app.subPackages || []).flatMap((subpackage) =>
      (subpackage.pages || []).map((page) => `${subpackage.root}/${page}`),
    ),
  ]

  for (const route of routes) {
    assert.ok(
      roots.some(({ root }) => route === root || route.startsWith(`${root}/`)),
      `unclassified route: ${route}`,
    )
  }
  for (const root of ['components', 'api', 'store', 'utils', 'behaviors', 'wxs']) {
    assert.ok(
      roots.some((entry) => entry.domain === 'shared' && entry.root === root),
      `shared root missing: ${root}`,
    )
  }
  for (const domain of catalog.domains) {
    for (const document of domain.documents) {
      assert.equal(
        fs.existsSync(path.join(projectRoot, document)),
        true,
        `catalog document missing: ${document}`,
      )
    }
  }
})

test('known gaps explicitly describe only the two static-analysis boundaries', () => {
  assert.equal(fs.existsSync(gapsPath), true, 'known gaps file must exist')
  const catalog = JSON.parse(fs.readFileSync(catalogPath, 'utf8'))
  const domains = new Set(catalog.domains.map((domain) => domain.name))
  const gaps = JSON.parse(fs.readFileSync(gapsPath, 'utf8'))

  assert.deepEqual(
    gaps.gaps.map((gap) => [gap.domain, gap.kind]),
    [
      ['application', 'route'],
      ['shared', 'endpoint'],
    ],
  )
  for (const gap of gaps.gaps) {
    assert.ok(domains.has(gap.domain), `unknown gap domain: ${gap.domain}`)
    assert.ok(gap.reason)
    assert.ok(gap.nextStep)
  }
})

test('generated graph indexes all 72 pages without scanner fixtures', () => {
  assert.equal(fs.existsSync(graphPath), true, 'generated graph must exist')
  const graph = JSON.parse(fs.readFileSync(graphPath, 'utf8'))
  const fixturePrefix = 'tests/knowledge-graph/fixtures/'

  assert.equal(graph.stats.nodeTypes.page, 72)
  assert.equal(
    graph.nodes.some((node) => node.path?.startsWith(fixturePrefix)),
    false,
  )
  assert.equal(
    graph.unresolved.some((item) => item.path.startsWith(fixturePrefix)),
    false,
  )
})