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

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

  delete require.cache[require.resolve('./list-banners')]
  const listBannersCommand = require('./list-banners')

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

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

test('listBannersCommand exits when app id is missing', async () => {
  const config = require('../lib/config')
  const originalGetConfig = config.getConfig
  config.getConfig = () => ({ cmsUrl: 'https://cms.example.com', defaultAppId: null })

  delete require.cache[require.resolve('./list-banners')]
  const listBannersCommand = require('./list-banners')

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

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