import {afterAll, expect, test} from 'bun:test'

import app from '../app'
import {cleanupCreatedUsers, createUser} from '../test-fixtures'

afterAll(cleanupCreatedUsers)

// invalid params 400 before any upstream call, but still count
// against the quota — keeps these tests network free
test('places requests are capped per user', async () => {
  const user = await createUser()
  for (let i = 0; i < 60; i++) {
    const res = await app.request('/places/nearby?lat=999', {
      headers: {Authorization: `Bearer ${user.token}`},
    })
    expect(res.status).toBe(400)
  }
  const limited = await app.request('/places/nearby?lat=999', {
    headers: {Authorization: `Bearer ${user.token}`},
  })
  expect(limited.status).toBe(429)

  const other = await createUser()
  const fresh = await app.request('/places/nearby?lat=999', {
    headers: {Authorization: `Bearer ${other.token}`},
  })
  expect(fresh.status).toBe(400)
})

test('extract requests are capped per user', async () => {
  const user = await createUser()
  for (let i = 0; i < 10; i++) {
    const res = await app.request('/extract', {
      method: 'POST',
      headers: {Authorization: `Bearer ${user.token}`},
    })
    expect(res.status).toBe(400)
  }
  const limited = await app.request('/extract', {
    method: 'POST',
    headers: {Authorization: `Bearer ${user.token}`},
  })
  expect(limited.status).toBe(429)
})
