import { describe, expect, it } from 'vitest'
import { problemDetailsSchema, resolveProblemInteraction } from '../problem'

describe('ProblemDetails runtime contract', () => {
  it('accepts a structured API problem', () => {
    expect(problemDetailsSchema.safeParse({
      type: 'https://example.test/problems/validation',
      title: 'Validation failed',
      status: 422,
      detail: 'One or more fields are invalid.',
      traceId: 'trace-1',
      errors: { name: ['Required'] },
    }).success).toBe(true)
  })

  it('rejects malformed or non-HTTP error payloads', () => {
    expect(problemDetailsSchema.safeParse({ title: 'Bad', status: 200 }).success).toBe(false)
    expect(problemDetailsSchema.safeParse({ title: 'Bad', status: 500, unexpected: true }).success).toBe(false)
  })

  it('accepts KBX-derived discriminators without changing the HTTP boundary', () => {
    expect(problemDetailsSchema.safeParse({
      type: 'conflict', title: 'Version conflict', status: 409,
      correlationId: 'corr-1', currentVersion: 4,
      actions: [{ id: 'reload', label: '새로 고침' }], retryable: false,
    }).success).toBe(true)
  })

  it.each([
    [401, undefined, 'UNAUTHORIZED'], [403, 'permission', 'FORBIDDEN'],
    [409, 'conflict', 'CONFLICT'], [422, 'validation', 'VALIDATION'],
    [503, 'integration', 'RETRYABLE'], [500, 'system', 'ERROR'],
  ] as const)('maps %s to the standard interaction boundary', (status, type, expected) => {
    expect(resolveProblemInteraction({ status, type, retryable: false })).toBe(expected)
  })
})
