import { vi } from 'vitest'

vi.mock('../../lib/prisma', () => ({
  prisma: {},
}))

vi.mock('../../lib/redis', () => ({
  redis: {
    on: vi.fn(),
    get: vi.fn(),
    set: vi.fn(),
    del: vi.fn(),
    quit: vi.fn(),
  },
}))

import request from 'supertest'
import { describe, expect, it } from 'vitest'
import { createApp } from '../../app'

const app = createApp()

describe('API smoke e2e', () => {
  it('serves health, API index, machine docs, OpenAPI, and Swagger UI in one client flow', async () => {
    const health = await request(app).get('/health')
    expect(health.status).toBe(200)
    expect(health.body.status).toBe('ok')

    const index = await request(app).get('/api/v1')
    expect(index.status).toBe(200)
    expect(index.body.docs).toBe('/api/v1/docs')
    expect(index.body.openapi).toBeUndefined()
    expect(index.body.openApi).toBe('/api/v1/openapi.json')

    const docs = await request(app).get(index.body.docs)
    expect(docs.status).toBe(200)
    expect(docs.body.routes.length).toBeGreaterThan(5)

    const openApi = await request(app).get(docs.body.openApi)
    expect(openApi.status).toBe(200)
    expect(openApi.body.info).toEqual(expect.objectContaining({ title: expect.any(String) }))

    const swaggerRedirect = await request(app).get(docs.body.swaggerUi)
    expect(swaggerRedirect.status).toBe(301)
    expect(swaggerRedirect.headers.location).toBe('/docs/')

    const swagger = await request(app).get('/docs/')
    expect(swagger.status).toBe(200)
    expect(swagger.headers['content-type']).toContain('text/html')
  })
})
