import { describe, expect, it } from 'vitest';
import type { IntegrationPlatform, ModelRateLimitStatus } from '@easyai-ai-gateway/contracts';
import { filterAndSortRealtimeLoad } from './RealtimeLoadPanel';

function status(input: Partial<ModelRateLimitStatus> & Pick<ModelRateLimitStatus, 'platformModelId' | 'platformId' | 'displayName'>): ModelRateLimitStatus {
  const { displayName, ...overrides } = input;
  return {
    platformName: input.platformId,
    provider: 'provider',
    platformStatus: 'enabled',
    platformPriority: 10,
    platformEffectivePriority: 10,
    modelName: displayName,
    displayName,
    modelType: ['image_generate'],
    enabled: true,
    concurrent: { currentValue: 0, usedValue: 0, reservedValue: 0, limitValue: 10, limited: true, ratio: 0 },
    queuedTasks: 0,
    rpm: { currentValue: 0, usedValue: 0, reservedValue: 0, limitValue: 100, limited: true, ratio: 0 },
    tpm: { currentValue: 0, usedValue: 0, reservedValue: 0, limitValue: 1000, limited: true, ratio: 0 },
    loadRatio: 0,
    ...overrides,
  };
}

describe('filterAndSortRealtimeLoad', () => {
  const platforms = new Map<string, IntegrationPlatform>([
    ['platform-a', { id: 'platform-a', name: 'Alpha', internalName: '主平台', platformKey: 'alpha', provider: 'provider', status: 'enabled' } as IntegrationPlatform],
    ['platform-b', { id: 'platform-b', name: 'Beta', platformKey: 'beta', provider: 'provider', status: 'disabled' } as IntegrationPlatform],
  ]);
  const statuses = [
    status({ platformModelId: 'model-a', platformId: 'platform-a', displayName: 'Image A', loadRatio: 0.4, queuedTasks: 2 }),
    status({ platformModelId: 'model-b', platformId: 'platform-b', displayName: 'Image B', loadRatio: 0.9, enabled: false, platformStatus: 'disabled' }),
  ];

  it('searches platform aliases and sorts by load descending', () => {
    const result = filterAndSortRealtimeLoad(statuses, platforms, {
      query: '平台',
      platformId: '',
      availability: 'enabled',
      modelType: '',
      runtimeState: '',
      sortField: 'load',
      sortDirection: 'desc',
      now: Date.now(),
    });
    expect(result.map((item) => item.platformModelId)).toEqual(['model-a']);
  });

  it('filters queued and attention states without changing source order', () => {
    const queued = filterAndSortRealtimeLoad(statuses, platforms, {
      query: '',
      platformId: '',
      availability: 'enabled',
      modelType: 'image_generate',
      runtimeState: 'queued',
      sortField: 'queued',
      sortDirection: 'desc',
      now: Date.now(),
    });
    const attention = filterAndSortRealtimeLoad(statuses, platforms, {
      query: '',
      platformId: '',
      availability: 'all',
      modelType: '',
      runtimeState: 'attention',
      sortField: 'load',
      sortDirection: 'desc',
      now: Date.now(),
    });
    expect(queued.map((item) => item.platformModelId)).toEqual(['model-a']);
    expect(attention.map((item) => item.platformModelId)).toEqual(['model-b']);
    expect(statuses.map((item) => item.platformModelId)).toEqual(['model-a', 'model-b']);
  });

  it('hides disabled entries by default and supports all or disabled-only filters', () => {
    const base = {
      query: '',
      platformId: '',
      modelType: '',
      runtimeState: '',
      sortField: 'load' as const,
      sortDirection: 'desc' as const,
      now: Date.now(),
    };
    expect(filterAndSortRealtimeLoad(statuses, platforms, { ...base, availability: 'enabled' }).map((item) => item.platformModelId)).toEqual(['model-a']);
    expect(filterAndSortRealtimeLoad(statuses, platforms, { ...base, availability: 'all' }).map((item) => item.platformModelId)).toEqual(['model-b', 'model-a']);
    expect(filterAndSortRealtimeLoad(statuses, platforms, { ...base, availability: 'disabled' }).map((item) => item.platformModelId)).toEqual(['model-b']);
  });
});
