const { test } = require('node:test')
const assert = require('node:assert/strict')

test('listAppsCommand exits when cmsUrl is not configured', async () => {
  const config = require('../lib/config')
  const originalGetConfig = config.getConfig
  config.getConfig = () => ({ cmsUrl: null })

  delete require.cache[require.resolve('./list-apps')]
  const listAppsCommand = require('./list-apps')

  let exitCode
  const originalExit = process.exit
  process.exit = (code) => {
    exitCode = code
    throw new Error('process.exit')
  }

  try {
    await assert.rejects(() => listAppsCommand(), /process\.exit/)
    assert.equal(exitCode, 1)
  } finally {
    process.exit = originalExit
    config.getConfig = originalGetConfig
    delete require.cache[require.resolve('./list-apps')]
  }
})
