import { describe, expect, it } from 'vitest'
import {
  DEFAULT_ORIGIN_ZONE,
  ensureAllowedOrigin,
  mergeOrigins,
  subdomainOrigin,
  type OriginFsDeps,
} from '../src/service/originConfig.js'

function memFs(initial: string | null): { fs: OriginFsDeps; get(): string } {
  const store = { content: initial }
  const fs: OriginFsDeps = {
    exists: () => store.content !== null,
    read: () => store.content ?? '',
    write: (_p, c) => {
      store.content = c
    },
  }
  return { fs, get: () => store.content ?? '' }
}

const PATH = '/etc/web-terminal.env'

describe('ensureAllowedOrigin (T17, EXPLORE §3)', () => {
  it('composes https://<subdomain>.term.<domain>', () => {
    expect(subdomainOrigin('host-42', 'example.com')).toBe('https://host-42.term.example.com')
  })

  it('appends the origin when the file has none', () => {
    const { fs, get } = memFs('PORT=3000\n')
    ensureAllowedOrigin(PATH, 'host-42', 'example.com', fs)
    expect(get()).toContain('ALLOWED_ORIGINS=https://host-42.term.example.com')
    expect(get()).toContain('PORT=3000')
  })

  it('is idempotent (no duplicate on a second run)', () => {
    const { fs, get } = memFs('ALLOWED_ORIGINS=https://host-42.term.example.com\n')
    ensureAllowedOrigin(PATH, 'host-42', 'example.com', fs)
    const matches = get().match(/host-42\.term\.example\.com/g) ?? []
    expect(matches).toHaveLength(1)
  })

  it('preserves existing origins (never weakens the Origin check)', () => {
    const { fs, get } = memFs('ALLOWED_ORIGINS=https://existing.example.com\n')
    ensureAllowedOrigin(PATH, 'host-42', 'example.com', fs)
    expect(get()).toContain('https://existing.example.com')
    expect(get()).toContain('https://host-42.term.example.com')
  })
})

describe('zone parameterization (PLAN_NATIVE_TUNNEL S2)', () => {
  it('defaults to the historical `term` zone', () => {
    expect(DEFAULT_ORIGIN_ZONE).toBe('term')
    expect(subdomainOrigin('t1', 'yaojia.wang')).toBe('https://t1.term.yaojia.wang')
  })

  it('composes the `terminal` zone for native-tunnel hosts', () => {
    expect(subdomainOrigin('t1', 'yaojia.wang', 'terminal')).toBe('https://t1.terminal.yaojia.wang')
  })

  it('ensureAllowedOrigin writes the caller-selected zone', () => {
    const { fs, get } = memFs('PORT=3000\n')
    ensureAllowedOrigin(PATH, 't1', 'yaojia.wang', fs, 'terminal')
    expect(get()).toContain('ALLOWED_ORIGINS=https://t1.terminal.yaojia.wang')
    expect(get()).not.toContain('.term.yaojia.wang')
  })
})

describe('mergeOrigins (PLAN_NATIVE_TUNNEL S2)', () => {
  it('appends to an empty/undefined value', () => {
    expect(mergeOrigins(undefined, 'https://a.example.com')).toBe('https://a.example.com')
    expect(mergeOrigins('', 'https://a.example.com')).toBe('https://a.example.com')
  })

  it('de-duplicates an origin already present', () => {
    expect(mergeOrigins('https://a.example.com', 'https://a.example.com')).toBe('https://a.example.com')
  })

  it('appends a new origin, trimming whitespace, preserving existing ones', () => {
    expect(mergeOrigins(' https://a.example.com , https://b.example.com ', 'https://c.example.com')).toBe(
      'https://a.example.com,https://b.example.com,https://c.example.com',
    )
  })
})
