packageBoundary.test.mjs 2.18 KB
import assert from 'node:assert/strict'
import path from 'node:path'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

import {
  checkPackageBoundaries,
  findIllegalPackageEdges,
} from '../../scripts/check-package-boundaries.mjs'

const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
const subpackageRoots = ['packageStudy', 'packageWorkspace', 'tabWorkOrder']

function graphWithEdges(edges) {
  return {
    nodes: [
      { id: 'main', type: 'module', path: 'utils/main.js' },
      { id: 'application', type: 'domain' },
      { id: 'study', type: 'module', path: 'packageStudy/utils/study.js' },
      { id: 'workspace', type: 'component', path: 'packageWorkspace/components/card/card' },
      { id: 'work-order', type: 'page', path: 'tabWorkOrder/pages/orderDetail/orderDetail' },
      { id: 'test', type: 'test', path: 'tests/package-size/example.test.mjs' },
    ],
    edges,
  }
}

test('package boundary audit rejects main-to-subpackage and cross-subpackage edges', () => {
  const graph = graphWithEdges([
    { from: 'main', to: 'study', type: 'imports', evidence: { path: 'utils/main.js' } },
    {
      from: 'application',
      to: 'study',
      type: 'usesComponent',
      evidence: { path: 'app.json' },
    },
    {
      from: 'study',
      to: 'workspace',
      type: 'usesComponent',
      evidence: { path: 'packageStudy/pages/example/example.json' },
    },
    { from: 'work-order', to: 'main', type: 'imports', evidence: {} },
    { from: 'test', to: 'study', type: 'imports', evidence: {} },
  ])

  assert.deepEqual(
    findIllegalPackageEdges({ graph, subpackageRoots }).map(edge => [
      edge.fromOwner,
      edge.toOwner,
      edge.type,
    ]),
    [
      ['main', 'packageStudy', 'usesComponent'],
      ['packageStudy', 'packageWorkspace', 'usesComponent'],
      ['main', 'packageStudy', 'imports'],
    ],
  )
})

test('current runtime graph contains no illegal package dependency edges', async () => {
  const report = await checkPackageBoundaries({ rootDir: projectRoot })

  assert.deepEqual(report.illegalEdges, [])
  assert.ok(report.checkedEdges > 0)
  assert.deepEqual(report.subpackageRoots, subpackageRoots)
})