import { describe, expect, test } from 'bun:test'
import { buildAppUrl, toSubdomain } from './urls'

describe('toSubdomain', () => {
  test('passes through clean names', () => {
    expect(toSubdomain('clock')).toBe('clock')
    expect(toSubdomain('my-app')).toBe('my-app')
  })

  test('replaces dots with hyphens', () => {
    expect(toSubdomain('mcp.txt')).toBe('mcp-txt')
    expect(toSubdomain('my.cool.app')).toBe('my-cool-app')
  })

  test('replaces underscores with hyphens', () => {
    expect(toSubdomain('my_app')).toBe('my-app')
  })

  test('collapses multiple hyphens', () => {
    expect(toSubdomain('my--app')).toBe('my-app')
    expect(toSubdomain('a..b')).toBe('a-b')
  })

  test('strips leading and trailing hyphens', () => {
    expect(toSubdomain('.hidden')).toBe('hidden')
    expect(toSubdomain('trailing.')).toBe('trailing')
    expect(toSubdomain('-dashed-')).toBe('dashed')
  })

  test('lowercases', () => {
    expect(toSubdomain('MyApp')).toBe('myapp')
  })
})

describe('buildAppUrl', () => {
  test('prepends sanitized name as subdomain', () => {
    expect(buildAppUrl('clock', 'http://toes.local')).toBe('http://clock.toes.local')
    expect(buildAppUrl('mcp.txt', 'http://toes.local')).toBe('http://mcp-txt.toes.local')
  })

  test('works with localhost and port', () => {
    expect(buildAppUrl('my-app', 'http://localhost:3000')).toBe('http://my-app.localhost:3000')
  })
})
