import { describe, it, expect, beforeEach, vi } from 'vitest'

import {
  initCustomerModeConfig,
  getCustomerModeConfig,
} from './customerModeConfig.js'

describe('customerModeConfig', () => {
  beforeEach(() => {
    // Reset to a known empty state between tests
    initCustomerModeConfig({})
  })

  it('defaults canvasHomepage to empty string', () => {
    const cfg = getCustomerModeConfig()
    expect(cfg.canvasHomepage).toBe('')
    expect(cfg.protoHomepage).toBe('')
  })

  it('accepts canvasHomepage from config', () => {
    initCustomerModeConfig({ enabled: true, canvasHomepage: 'landing' })
    expect(getCustomerModeConfig().canvasHomepage).toBe('landing')
  })

  it('warns when both protoHomepage and canvasHomepage are set', () => {
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
    initCustomerModeConfig({
      enabled: true,
      protoHomepage: '/MyProto',
      canvasHomepage: 'landing',
    })
    expect(warnSpy).toHaveBeenCalledTimes(1)
    expect(warnSpy.mock.calls[0][0]).toMatch(/protoHomepage.*canvasHomepage/)
    expect(warnSpy.mock.calls[0][0]).toMatch(/canvasHomepage.*takes precedence/)
    warnSpy.mockRestore()
  })

  it('does not warn when only one is set', () => {
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
    initCustomerModeConfig({ enabled: true, canvasHomepage: 'landing' })
    expect(warnSpy).not.toHaveBeenCalled()
    initCustomerModeConfig({ enabled: true, protoHomepage: '/Proto' })
    expect(warnSpy).not.toHaveBeenCalled()
    warnSpy.mockRestore()
  })
})
