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

const { evaluateBotInternalKey, MIN_LENGTH } = require('../src/utils/botInternalKey')

const STRONG = 'x'.repeat(MIN_LENGTH + 8)

test('a strong key is ok in every environment', () => {
  for (const nodeEnv of ['production', 'development', undefined]) {
    const r = evaluateBotInternalKey({ key: STRONG, nodeEnv })
    assert.equal(r.ok, true)
    assert.equal(r.fatal, false)
    assert.equal(r.message, null)
  }
})

test('empty key is fatal in production, warn otherwise', () => {
  const prod = evaluateBotInternalKey({ key: '', nodeEnv: 'production' })
  assert.equal(prod.ok, false)
  assert.equal(prod.fatal, true)

  const dev = evaluateBotInternalKey({ key: '', nodeEnv: 'development' })
  assert.equal(dev.ok, false)
  assert.equal(dev.fatal, false)
})

test('undefined key behaves like empty', () => {
  const r = evaluateBotInternalKey({ key: undefined, nodeEnv: 'production' })
  assert.equal(r.ok, false)
  assert.equal(r.fatal, true)
})

test('documented placeholders are rejected (fatal in production)', () => {
  for (const key of ['change-me-to-a-long-random-string', 'dev-only-change-me-bot-key']) {
    const r = evaluateBotInternalKey({ key, nodeEnv: 'production' })
    assert.equal(r.ok, false, `placeholder should be rejected: ${key}`)
    assert.equal(r.fatal, true)
  }
})

test('a too-short key is rejected', () => {
  const short = 'a'.repeat(MIN_LENGTH - 1)
  const r = evaluateBotInternalKey({ key: short, nodeEnv: 'production' })
  assert.equal(r.ok, false)
  assert.equal(r.fatal, true)

  const dev = evaluateBotInternalKey({ key: short, nodeEnv: 'development' })
  assert.equal(dev.ok, false)
  assert.equal(dev.fatal, false)
})

test('a key exactly MIN_LENGTH long is accepted', () => {
  const r = evaluateBotInternalKey({ key: 'a'.repeat(MIN_LENGTH), nodeEnv: 'production' })
  assert.equal(r.ok, true)
})
