const { spawnSync } = require('child_process')
const path = require('path')
const { test } = require('node:test')
const assert = require('node:assert/strict')
const { version } = require('../package.json')

const cliPath = path.join(__dirname, 'index.js')

test('CLI --version prints package version', () => {
  const result = spawnSync(process.execPath, [cliPath, '--version'], { encoding: 'utf8' })
  assert.equal(result.status, 0)
  assert.match(result.stdout.trim(), new RegExp(`^${version.replace(/\./g, '\\.')}`))
})

test('CLI --help lists core commands', () => {
  const result = spawnSync(process.execPath, [cliPath, '--help'], { encoding: 'utf8' })
  assert.equal(result.status, 0)
  const output = `${result.stdout}\n${result.stderr}`
  assert.match(output, /init/)
  assert.match(output, /login/)
  assert.match(output, /library/)
})
