openpencil/tools/ci/policy/tests/classify.test.ts
Danila Poyarkov 1aa10a4fed
build(tools): group tools by role and gate them like the rest of the repo (#791)
Tools live under tools/<role>/<domain> (checks, generate, release, ci, dev), every tool is a workspace named @open-pencil/<domain>-tools, a shared tools/tsconfig.json backs the new check:tools gate that fixed 55 latent type errors, test:tools runs through bun --filter, the placement check is its own checks/test-homes package, and every tool resolves the repository through resolveWorkspaceRoot. Bun, Node, and mdast types live in a tools-root workspace so they never reach the app program.
2026-09-30 05:00:31 +04:00

48 lines
1.8 KiB
TypeScript

import { afterEach, expect, test } from 'bun:test'
import { mkdtemp, readFile, rename, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
const roots: string[] = []
afterEach(async () => {
await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })))
})
function git(cwd: string, ...args: string[]): string {
const result = Bun.spawnSync(
['git', '-c', 'user.name=CI Test', '-c', 'user.email=ci@example.invalid', ...args],
{ cwd }
)
if (result.exitCode !== 0) throw new Error(result.stderr.toString())
return result.stdout.toString().trim()
}
test('classifier reads real Git deletions and renames without losing the original path', async () => {
const root = await mkdtemp(join(tmpdir(), 'open-pencil-ci-'))
roots.push(root)
git(root, 'init')
await writeFile(join(root, 'runtime.ts'), 'export const version = 1\n')
git(root, 'add', '.')
git(root, 'commit', '-m', 'base')
const base = git(root, 'rev-parse', 'HEAD')
await rename(join(root, 'runtime.ts'), join(root, 'README.md'))
git(root, 'add', '.')
git(root, 'commit', '-m', 'move')
const output = join(root, 'output')
const command = fileURLToPath(import.meta.resolve('#ci/classify'))
const result = Bun.spawnSync([process.execPath, command], {
cwd: root,
env: { ...process.env, CI_BASE_SHA: base, GITHUB_OUTPUT: output }
})
expect(result.exitCode).toBe(0)
expect(await readFile(output, 'utf8')).toBe('scope=code\n')
const failed = Bun.spawnSync([process.execPath, command], {
cwd: root,
env: { ...process.env, CI_BASE_SHA: 'invalid', GITHUB_OUTPUT: output }
})
expect(failed.exitCode).not.toBe(0)
expect(await readFile(output, 'utf8')).toBe('scope=code\n')
})