import { describe, it, expect } from 'vitest'
import { classifyUploadFile, checkTotalUploadSize, JSON_FILE_MAX_BYTES, SVG_FILE_MAX_BYTES, TOTAL_REQUEST_MAX_BYTES } from '../upload-limits'

describe('classifyUploadFile', () => {
  it('accepts a JSON file under the limit', () => {
    const result = classifyUploadFile({ fileName: 'werk-nord.json', sizeBytes: 1024 })
    expect(result).toEqual({ ok: true, kind: 'json' })
  })

  it('accepts an SVG file under its own (smaller) limit', () => {
    const result = classifyUploadFile({ fileName: 'werk-nord.svg', sizeBytes: 1024 })
    expect(result).toEqual({ ok: true, kind: 'svg' })
  })

  it('rejects an oversized JSON file with reason "oversized", not "unsupported_extension"', () => {
    const result = classifyUploadFile({ fileName: 'gross.json', sizeBytes: JSON_FILE_MAX_BYTES + 1 })
    expect(result.ok).toBe(false)
    if (result.ok) return
    expect(result.reason).toBe('oversized')
    expect(result.limitBytes).toBe(JSON_FILE_MAX_BYTES)
    expect(result.messageDe).toContain('gross.json')
  })

  it('rejects an oversized SVG file against the SVG-specific (2 MB) limit, not the JSON limit', () => {
    const oversizedForSvgOnly = SVG_FILE_MAX_BYTES + 1
    expect(oversizedForSvgOnly).toBeLessThan(JSON_FILE_MAX_BYTES) // sanity: this size would still pass the JSON limit
    const result = classifyUploadFile({ fileName: 'gross.svg', sizeBytes: oversizedForSvgOnly })
    expect(result.ok).toBe(false)
    if (result.ok) return
    expect(result.reason).toBe('oversized')
    expect(result.limitBytes).toBe(SVG_FILE_MAX_BYTES)
  })

  it('rejects an unsupported extension with reason "unsupported_extension", even for a tiny file (never mislabeled as "too big")', () => {
    const result = classifyUploadFile({ fileName: 'notizen.txt', sizeBytes: 20 })
    expect(result.ok).toBe(false)
    if (result.ok) return
    expect(result.reason).toBe('unsupported_extension')
    expect(result.sizeBytes).toBe(20)
    expect(result.messageDe).not.toContain('groß')
  })

  it('extension check runs BEFORE the size check: a huge .txt file is still reported as unsupported extension, not oversized', () => {
    const result = classifyUploadFile({ fileName: 'riesig.txt', sizeBytes: JSON_FILE_MAX_BYTES * 10 })
    expect(result.ok).toBe(false)
    if (result.ok) return
    expect(result.reason).toBe('unsupported_extension')
  })

  it('is case-insensitive on the extension', () => {
    expect(classifyUploadFile({ fileName: 'A.JSON', sizeBytes: 10 })).toEqual({ ok: true, kind: 'json' })
    expect(classifyUploadFile({ fileName: 'B.SVG', sizeBytes: 10 })).toEqual({ ok: true, kind: 'svg' })
  })
})

describe('checkTotalUploadSize', () => {
  it('accepts a total under the limit', () => {
    expect(checkTotalUploadSize(1024)).toEqual({ ok: true })
  })

  it('rejects a total over the limit with an honest, actionable German message', () => {
    const result = checkTotalUploadSize(TOTAL_REQUEST_MAX_BYTES + 1)
    expect(result.ok).toBe(false)
    if (result.ok) return
    expect(result.messageDe).toContain('MB')
  })

  it('the total limit is honestly documented as staying under Vercel\'s ~4.5 MB platform cap', () => {
    expect(TOTAL_REQUEST_MAX_BYTES).toBeLessThan(4.5 * 1024 * 1024)
  })
})
