import { describe, expect, it } from 'vitest';
import type { FileStorageChannel } from '@easyai-ai-gateway/contracts';
import { channelToForm, formToPayload } from './SystemSettingsPanel';

describe('SystemSettingsPanel object storage form', () => {
  it('promotes legacy CDN and expiration config while preserving unknown extensions', () => {
    const channel: FileStorageChannel = {
      id: 'channel-1',
      channelKey: 'oss-primary',
      name: 'OSS primary',
      provider: 'aliyun_oss',
      credentialsPreview: {},
      scenes: ['upload', 'image_result', 'request_asset'],
      config: {
        endpoint: 'https://oss-cn-shanghai.aliyuncs.com',
        region: 'cn-shanghai',
        bucket: 'media',
        cdnDomain: 'https://cdn.example.com/',
        apiReturnExpirePolicy: '3m',
        signedUrlExpiresSeconds: 3600,
        customHeader: 'retained',
      },
      retryPolicy: {},
      priority: 100,
      status: 'enabled',
      createdAt: '2026-08-04T00:00:00Z',
      updatedAt: '2026-08-04T00:00:00Z',
    };

    const form = channelToForm(channel);
    expect(form.publicBaseUrl).toBe('https://cdn.example.com/');
    expect(form.temporaryFileExpirePolicy).toBe('3m');
    expect(form.signedUrlExpiresMinutes).toBe('60');
    expect(JSON.parse(form.configJson)).toEqual({ customHeader: 'retained' });

    const payload = formToPayload({
      ...form,
      objectPrefix: '/gateway/',
      temporaryFileExpirePolicy: '1m',
    });
    expect(payload.config).toEqual({
      endpoint: 'https://oss-cn-shanghai.aliyuncs.com',
      region: 'cn-shanghai',
      bucket: 'media',
      publicBaseUrl: 'https://cdn.example.com',
      objectPrefix: 'gateway',
      temporaryFileExpirePolicy: '1m',
      signedUrlExpiresSeconds: 3600,
      customHeader: 'retained',
    });
  });
});
