import assert from 'node:assert/strict'

import { isSetupCompletedState, normalizeState } from '../vite.config.js'

const completedEnv = {
  SETUP_COMPLETED: 'true',
  POSTGRES_HOST: 'database.internal',
  POSTGRES_PORT: '5432',
  POSTGRES_DB: 'x_financial',
  POSTGRES_USER: 'postgres-admin',
  POSTGRES_PASSWORD: 'secret',
  REDIS_URL: 'redis://redis.internal:6379/0'
}

assert.equal(isSetupCompletedState(completedEnv, true), true)
assert.equal(isSetupCompletedState(completedEnv, false), false)
assert.equal(isSetupCompletedState({ SETUP_COMPLETED: 'false' }, true), false)

const publicState = normalizeState(completedEnv, { adminConfigured: true })
assert.equal(publicState.initialized, true)
assert.equal(publicState.database.host, '')
assert.equal(publicState.database.username, '')
assert.equal(publicState.redis.url, '')
assert.equal(publicState.database.password_configured, true)

const setupState = normalizeState(
  { ...completedEnv, SETUP_COMPLETED: 'false' },
  { adminConfigured: false }
)
assert.equal(setupState.initialized, false)
assert.equal(setupState.database.host, 'database.internal')
assert.equal(setupState.database.username, 'postgres-admin')
assert.equal(setupState.redis.url, 'redis://redis.internal:6379/0')

console.log('vite setup lock tests passed')
