import { describe, expect, it, vi } from 'vitest'
import { Hono } from 'hono'
import type { AppEnv } from '../src/types'

vi.mock('cloudflare:sockets', () => ({ connect: vi.fn() }), { virtual: true })

import { routes } from '../src/routes'

function assembledApi() {
  const app = new Hono<AppEnv>()
  app.route('/api', routes)
  return app
}

const env = {
  DB: { connectionString: 'postgresql://test:test@localhost/test' },
  ENVIRONMENT: 'development',
} as AppEnv['Bindings']

describe('assembled API route registry', () => {
  it.each([
    ['GET', '/api/time/active'],
    ['GET', '/api/shell/layout?device=desktop'],
    ['PUT', '/api/shell/layout?device=desktop'],
    ['DELETE', '/api/shell/layout?device=desktop'],
  ])('%s %s is registered and authenticated', async (method, path) => {
    const response = await assembledApi().request(path, {
      method,
      headers: method === 'GET' ? undefined : { Origin: 'https://zync.is' },
      body: method === 'PUT' ? JSON.stringify({}) : undefined,
    }, env)

    expect(response.status).toBe(401)
  })
})
