codeburn/scripts/smoke-plugin-socket.mjs
iamtoruk 52bde7c4dd feat(plugins): CB-3 plugin socket — wire guard, CLI inspector, payload seam
Teams issue #3 introduces the codeburn plugin socket: an opt-in escape
hatch for third parties to add sync attributes, payload sections, and CLI
commands without touching the codeburn CLI source tree. This commit ships
the first half (CB-3) — the manifest layer, the wire guard, and the user-
facing inspector. Release-key signing (the 9b seam) lands separately.

Why split now:
  - The wire guard (filterPluginAttributes in sync/otlp.ts) is the load-
    bearing piece. With it, no plugin code can run until the user opts
    in by installing one — the default empty socket keeps every byte of
    the existing telemetry wire byte-identical. That is the contract the
    existing four sync test files were already pinning; this commit
    re-pins it explicitly with a new test (tests/plugin-socket.test.ts).
  - The inspector () makes rejections
    visible. Without it, a misconfigured plugin would just vanish
    silently — exactly the foot-gun the socket exists to prevent.

What ships:
  - src/plugins/loader.ts: enumerates ~/.config/codeburn/plugins/<name>/,
    reads + parses each codeburn-plugin.json, applies name/dir match and
    cliCompat check, hands off to verifyPlugin (the 9b seam). Rejected
    plugins contribute nothing to the wire.
  - src/plugins/manifest.ts: parses + validates the manifest shape
    (name, version, cliCompat, capabilities.{commands, syncAttributes,
    payloadSections, spanKinds}). Returns {ok, reason} on any failure.
  - src/plugins/cli.ts: . Read-only;
    never mutates the plugins directory.
  - src/sync/otlp.ts: filterPluginAttributes strips any key not in the
    declared set, called from buildOtlpPayload when pluginAttributes are
    supplied. With no plugin installed, the call path is a no-op.
  - src/sync/push.ts, src/sync/cli.ts: thread pluginAttributeKeys
    through sendBatches -> buildOtlpPayload. The wire is unchanged when
    no plugin declares anything.
  - src/usage-aggregator.ts, src/menubar-json.ts: add the
    MenubarPayload.plugins field and populate it from pluginPayloadSections
    (empty when no plugin installed).
  - src/main.ts: register the plugin subcommand.
  - tests/plugin-socket.test.ts: 12 tests covering the wire guard, the
    byte-identical default, the loader rejection paths, and the CLI.
  - scripts/smoke-plugin-socket.mjs: end-to-end smoke test against the
    built CLI (13 assertions).

Byte-identical guarantee:
  - With no plugin installed: loadPlugins() returns [], pluginPayloadSections
    returns {}, filterPluginAttributes(empty) returns []. The OTLP payload
    and the menubar payload are bit-for-bit the same as before this commit.
  - The dry-run output gains one new line listing the active plugin names
    (empty string when no plugin). User-facing only; not on the wire.

What's intentionally NOT here:
  - Plugin command invocation:  lands when at
    least one community plugin publishes a command (deferred).
  - : shipping this with no signed-plugin
    ecosystem in place would invite supply-chain risk. Deferred.
  - Release-key signing (the verifyPlugin return path). Deferred to 9b.

Refs: teams issue #3, CB-3 sync boundary spec section 9a.
2026-08-27 09:29:15 -07:00

114 lines
No EOL
4.6 KiB
JavaScript

#!/usr/bin/env node
/**
* Smoke test for the plugin socket step 9.
*
* Exercises:
* - `codeburn plugin list` against an empty dir (default state)
* - `codeburn plugin list` against a dir with one valid + one rejected plugin
* - `codeburn plugin info <name>` for the loaded plugin
* - `codeburn plugin verify <name>` (uses CODEBURN_PLUGIN_DEV=1 since signing lands in 9b)
* - `codeburn plugin verify <name>` for the rejected plugin
* - `codeburn --help` to confirm the subcommand shows up
*
* Exits 0 on full success, 1 on any failure.
*/
import { spawn } from 'child_process'
import { mkdtemp, mkdir, writeFile, rm } from 'fs/promises'
import { join } from 'path'
import { tmpdir } from 'os'
const CLI = join(import.meta.dirname, '..', 'dist', 'main.js')
async function run(args, opts = {}) {
return new Promise((resolve, reject) => {
const child = spawn('node', [CLI, ...args], {
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, ...opts.env },
})
let stdout = ''
let stderr = ''
child.stdout.on('data', d => { stdout += d.toString() })
child.stderr.on('data', d => { stderr += d.toString() })
child.on('close', code => resolve({ code, stdout, stderr }))
child.on('error', reject)
})
}
function assertEq(actual, expected, label) {
if (actual !== expected) {
console.error(`FAIL: ${label}\n expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(actual)}`)
process.exitCode = 1
} else {
console.log(`PASS: ${label}`)
}
}
function assertContains(actual, needle, label) {
if (!actual.includes(needle)) {
console.error(`FAIL: ${label}\n needle: ${JSON.stringify(needle)}\n actual: ${JSON.stringify(actual)}`)
process.exitCode = 1
} else {
console.log(`PASS: ${label}`)
}
}
async function main() {
// 1) help
const help = await run(['--help'])
assertEq(help.code, 0, '`codeburn --help` exits 0')
assertContains(help.stdout, 'plugin', '`codeburn --help` mentions the plugin subcommand')
// 2) plugin list — empty
const emptyDir = await mkdtemp(join(tmpdir(), 'smoke-empty-'))
try {
const r = await run(['plugin', 'list', '--dir', emptyDir])
assertEq(r.code, 0, '`plugin list` against empty dir exits 0')
assertContains(r.stdout, 'No plugins found', '`plugin list` reports empty')
} finally {
await rm(emptyDir, { recursive: true, force: true })
}
// 3) plugin list — mixed
const pluginDir = await mkdtemp(join(tmpdir(), 'smoke-plugins-'))
try {
const goodDir = join(pluginDir, 'good-plugin')
const badDir = join(pluginDir, 'bad-plugin')
await mkdir(goodDir, { recursive: true })
await mkdir(badDir, { recursive: true })
await writeFile(join(goodDir, 'codeburn-plugin.json'), JSON.stringify({
name: 'good-plugin', version: '0.1.0', cliCompat: '>=0.9.22',
capabilities: { commands: ['good'], syncAttributes: [], payloadSections: [], spanKinds: [] },
}))
await writeFile(join(badDir, 'codeburn-plugin.json'), '{not json')
const r = await run(['plugin', 'list', '--dir', pluginDir], { env: { CODEBURN_PLUGIN_DEV: '1' } })
assertEq(r.code, 0, '`plugin list` with one valid + one rejected exits 0')
assertContains(r.stdout, 'loaded good-plugin@0.1.0', '`plugin list` shows the loaded plugin')
assertContains(r.stdout, 'rejected bad-plugin', '`plugin list` shows the rejected plugin')
// 4) info <name>
const info = await run(['plugin', 'info', 'good-plugin', '--dir', pluginDir], { env: { CODEBURN_PLUGIN_DEV: '1' } })
assertEq(info.code, 0, '`plugin info good-plugin` exits 0')
assertContains(info.stdout, '"name": "good-plugin"', '`plugin info` prints the manifest')
// 5) verify <name> — good (with dev flag)
const verify = await run(['plugin', 'verify', 'good-plugin', '--dir', pluginDir], { env: { CODEBURN_PLUGIN_DEV: '1' } })
assertEq(verify.code, 0, '`plugin verify good-plugin` exits 0 under CODEBURN_PLUGIN_DEV=1')
assertContains(verify.stdout, 'verified good-plugin@0.1.0', '`plugin verify` prints the verified line')
// 6) verify <name> — bad (malformed manifest)
const verifyBad = await run(['plugin', 'verify', 'bad-plugin', '--dir', pluginDir])
assertEq(verifyBad.code, 1, '`plugin verify bad-plugin` exits 1 for malformed manifest')
assertContains(verifyBad.stdout + verifyBad.stderr, 'missing or unreadable', '`plugin verify` reports the read failure')
} finally {
await rm(pluginDir, { recursive: true, force: true })
}
if (process.exitCode === 1) {
console.error('\nSMOKE FAILED')
} else {
console.log('\nSMOKE PASSED')
}
}
main().catch(err => { console.error(err); process.exit(1) })