import assert from 'node:assert/strict' import test from 'node:test' import { collectSelfCheck } from '../bundle/lib/selfcheck.js' import { registerCheckCommand } from '../bundle/command-check.js' const FEATURES = ['guards', 'bootstrap', 'commands', 'workingDirectory'] /** A ctx-like with every service the bundle depends on. */ function fullCtx() { return { tools: { guard() {} }, on() {}, inject() {}, workingDirectory: { get() {} }, } } test('collectSelfCheck ok on full ctx-like', () => { const report = collectSelfCheck(fullCtx()) assert.deepEqual( report.map((entry) => entry.feature), FEATURES, ) for (const entry of report) { assert.equal(entry.ok, true, `${entry.feature} ok`) assert.equal(typeof entry.detail, 'string', `${entry.feature} detail is a string`) assert.ok(entry.detail.length > 0, `${entry.feature} detail is non-empty`) } }) test('collectSelfCheck flags missing tools.guard', () => { const ctx = fullCtx() ctx.tools = {} const report = collectSelfCheck(ctx) const guards = report.find((entry) => entry.feature === 'guards') assert.equal(guards.ok, false) assert.match(guards.detail, /tools\.guard/) for (const entry of report.filter((entry) => entry.feature !== 'guards')) { assert.equal(entry.ok, true, `${entry.feature} stays ok`) } }) test('collectSelfCheck flags missing on()', () => { const ctx = fullCtx() ctx.on = undefined const report = collectSelfCheck(ctx) const bootstrap = report.find((entry) => entry.feature === 'bootstrap') assert.equal(bootstrap.ok, false) for (const entry of report.filter((entry) => entry.feature !== 'bootstrap')) { assert.equal(entry.ok, true, `${entry.feature} stays ok`) } }) test('registerCheckCommand builds report text', async () => { let captured const commands = { register(def) { captured = def }, } registerCheckCommand({ commands, collect: () => collectSelfCheck(fullCtx()) }) assert.equal(captured.name, 'dsh-harness-check') const okResult = await captured.handler({}) assert.match(okResult.text, /guards: ok/) assert.match(okResult.text, /all ok/) const brokenCtx = fullCtx() brokenCtx.tools = {} registerCheckCommand({ commands, collect: () => collectSelfCheck(brokenCtx) }) const badResult = await captured.handler({}) assert.match(badResult.text, /guards: not-ok/) assert.match(badResult.text, /problem/) })