import type { RuntimePolicySet } from '@easyai-ai-gateway/contracts';
import { describe, expect, it } from 'vitest';
import { formToPayload, policyToForm } from './RuntimePoliciesPanel';

describe('runtime queue policy editor', () => {
  it('preserves unknown policy extensions while editing queue values', () => {
    const policy = {
      id: 'runtime-1',
      policyKey: 'runtime-image',
      name: 'Image runtime',
      status: 'active',
      rateLimitPolicy: {
        extensionMode: 'legacy',
        rules: [
          { metric: 'concurrent', limit: 2, leaseTtlSeconds: 90, vendorHint: 'keep' },
          { metric: 'queue_size', limit: 4, maxWaitSeconds: 120, vendorQueueMode: 'shared' },
          { metric: 'rps', limit: 5, windowSeconds: 1, vendorWindow: true },
        ],
      },
      retryPolicy: {},
      autoDisablePolicy: {},
      degradePolicy: {},
      metadata: {},
      createdAt: '2026-07-29T00:00:00Z',
      updatedAt: '2026-07-29T00:00:00Z',
    } as RuntimePolicySet;

    const form = policyToForm(policy);
    const payload = formToPayload({
      ...form,
      queueSize: '6',
      queueMaxWaitSeconds: '300',
    });

    expect(payload.rateLimitPolicy).toEqual({
      extensionMode: 'legacy',
      rules: [
        { metric: 'rps', limit: 5, windowSeconds: 1, vendorWindow: true },
        {
          vendorHint: 'keep',
          metric: 'concurrent',
          limit: 2,
          windowSeconds: 60,
          leaseTtlSeconds: 90,
        },
        {
          vendorQueueMode: 'shared',
          metric: 'queue_size',
          limit: 6,
          maxWaitSeconds: 300,
        },
      ],
    });
  });
});
