import { describe, expect, it } from 'vitest';
import type { GatewayPricingEstimate } from '@easyai-ai-gateway/contracts';
import {
  MEDIA_ESTIMATE_DEBOUNCE_MS,
  billingEstimateSignature,
  mediaEstimateAllowsProduction,
  mediaEstimateFromResponse,
} from './PlaygroundPage';

describe('playground pricing estimate', () => {
  it('uses the specified 350ms debounce interval', () => {
    expect(MEDIA_ESTIMATE_DEBOUNCE_MS).toBe(350);
  });

  it('only changes its signature for billing-relevant parameters', () => {
    const first = billingEstimateSignature({
      kind: 'images.generations', model: 'image-model', prompt: 'first',
      count: 1, quality: 'high', resolution: '2K',
    });
    const promptOnly = billingEstimateSignature({
      kind: 'images.generations', model: 'image-model', prompt: 'second',
      count: 1, quality: 'high', resolution: '2K',
    });
    const changedCount = billingEstimateSignature({
      kind: 'images.generations', model: 'image-model', prompt: 'second',
      count: 2, quality: 'high', resolution: '2K',
    });
    expect(promptOnly).toBe(first);
    expect(changedCount).not.toBe(first);
  });

  it('distinguishes explicit free and exposes the reservation ceiling', () => {
    const response: GatewayPricingEstimate = {
      candidateCount: 2,
      currency: 'resource',
      items: [{ amount: 0, currency: 'resource', isFree: true }],
      pricingVersion: 'effective-pricing-v2',
      requestFingerprint: 'fingerprint',
      reservationAmount: 0,
      resolver: 'effective-pricing-v2',
      totalAmount: 0,
    };
    expect(mediaEstimateFromResponse(response)).toMatchObject({
      amount: 0,
      candidateCount: 2,
      reservationAmount: 0,
      status: 'free',
    });
  });

  it('only allows production submission for the current pricing signature', () => {
    expect(mediaEstimateAllowsProduction({ status: 'ready', signature: 'current' }, 'current')).toBe(true);
    expect(mediaEstimateAllowsProduction({ status: 'free', signature: 'current' }, 'current')).toBe(true);
    expect(mediaEstimateAllowsProduction({ status: 'ready', signature: 'stale' }, 'current')).toBe(false);
    expect(mediaEstimateAllowsProduction({ status: 'loading', signature: 'current' }, 'current')).toBe(false);
  });
});
