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

const loadNetworksModule = async () => import('../networks')

describe('frontend network config', () => {
  afterEach(() => {
    vi.unstubAllEnvs()
    vi.resetModules()
  })

  it('keeps Chain 2138 disabled by default', async () => {
    const networks = await loadNetworksModule()

    expect(networks.chain2138TestnetEnabled).toBe(false)
    expect(networks.frontendSourceChainIds).toEqual([138])
    expect(networks.defaultFrontendChainId).toBe(138)
  })

  it('enables Chain 2138 and promotes it to the default frontend chain when configured', async () => {
    vi.stubEnv('VITE_ENABLE_CHAIN2138', 'true')
    vi.stubEnv('VITE_DEFAULT_FRONTEND_CHAIN_ID', '2138')
    vi.stubEnv('VITE_RPC_URL_2138', 'https://rpc.public-2138.defi-oracle.io')
    vi.stubEnv('VITE_EXPLORER_URL_2138', 'https://public-2138.defi-oracle.io')

    const networks = await loadNetworksModule()

    expect(networks.chain2138TestnetEnabled).toBe(true)
    expect(networks.frontendSourceChainIds).toEqual([138, 2138])
    expect(networks.defaultFrontendChainId).toBe(2138)
    expect(networks.defaultFrontendChainName).toBe('DeFi Oracle Meta Testnet')
  })
})
