Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

3.2 KB
Raw Blame
// The Creative Direction harness scripts, run the way Harness runs them.
//   node --test test/*.test.mjs
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
import { accessSync, constants, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { after, test } from 'node:test'

const here = fileURLToPath(new URL('..', import.meta.url))
const manifest = JSON.parse(readFileSync(join(here, 'harness.json'), 'utf8'))
const scratch = []
after(() => { for (const dir of scratch) rmSync(dir, { recursive: true, force: true }) })

function tempDir() { const d = mkdtempSync(join(tmpdir(), 'direct-')); scratch.push(d); return d }
function run(script, { cwd = here, env = {} } = {}) {
  const merged = { ...process.env }
  for (const [k, v] of Object.entries(env)) v === null ? delete merged[k] : (merged[k] = v)
  const r = spawnSync(join(here, script), { cwd, env: merged, encoding: 'utf8' })
  return { code: r.status, stdout: r.stdout, stderr: r.stderr }
}
const seedScript = join(here, 'skills/direct/scripts/seed-verdict.sh')

test('every script the manifest names is in the folder and executable', () => {
  for (const s of [manifest.workspace.init, manifest.toolchain.setup, manifest.toolchain.doctor]) {
    assert.doesNotThrow(() => accessSync(join(here, s), constants.X_OK), s)
  }
})

test('setup installs package-local exports and locates the browser', () => {
  const result = run(manifest.toolchain.setup)
  assert.equal(result.code, 0, result.stderr)
  assert.match(result.stdout, /ok   export browser:/)
})

test('doctor verifies Node and an export browser', () => {
  assert.equal(run(manifest.toolchain.doctor).code, 0)
})

test('init lays out a workspace and seeds a not-ready verdict', () => {
  const ws = tempDir()
  assert.equal(run(manifest.workspace.init, { cwd: ws, env: { HARNESS_DSH: manifest.id } }).code, 0)
  assert.equal(JSON.parse(readFileSync(join(ws, '.harness/verdict.json'), 'utf8')).ready, false)
  assert.equal(readFileSync(join(ws, '.harness-initialized'), 'utf8'), `initialized by ${manifest.id}\n`)
})

test('the manifest names its workspace bridge and an HTML artifact', () => {
  assert.equal(manifest.viewer.command, 'toolchain/viewer.sh')
  assert.deepEqual(manifest.viewer.artifactExtensions, ['.html'])
})

test('seed-verdict reflects whether a board exists', () => {
  const ws = tempDir()
  run(manifest.workspace.init, { cwd: ws })
  assert.equal(spawnSync(seedScript, [ws], { cwd: ws, encoding: 'utf8' }).status, 0)
  assert.equal(JSON.parse(readFileSync(join(ws, '.harness/verdict.json'), 'utf8')).ready, false)
  mkdirSync(join(ws, 'board'), { recursive: true })
  writeFileSync(join(ws, 'board/index.html'), '<!doctype html>')
  assert.equal(spawnSync(seedScript, [ws], { cwd: ws, encoding: 'utf8' }).status, 0)
  assert.equal(JSON.parse(readFileSync(join(ws, '.harness/verdict.json'), 'utf8')).ready, false)
})


test('the manifest routes the actual project through its local workspace bridge', () => {
  assert.equal(manifest.viewer.url, 'http://127.0.0.1:${port}/')
  assert.ok(manifest.workspace.marker.endsWith('/index.html'))
})