import { describe, it, expect } from 'vitest';
import { snapshotToForm } from './snapshotToForm';
import { defaultForm } from '../hooks/useDeploymentPageState';

describe('snapshotToForm', () => {
  it('overrides monitoring fields from agentConfig', () => {
    const snapshot = {
      jarVersionId: 'v1',
      agentConfig: { engineLevel: 'COMPLETE', samplingRate: 0.5, compressSuccess: true },
      containerConfig: {},
      sensitiveKeys: null,
    };
    const result = snapshotToForm(snapshot, defaultForm);
    expect(result.monitoring.engineLevel).toBe('COMPLETE');
    expect(result.monitoring.samplingRate).toBe('0.5');
    expect(result.monitoring.compressSuccess).toBe(true);
    // fields not in snapshot fall back to defaults
    expect(result.monitoring.payloadSize).toBe(defaultForm.monitoring.payloadSize);
    expect(result.monitoring.replayEnabled).toBe(defaultForm.monitoring.replayEnabled);
  });

  it('overrides resources fields from containerConfig', () => {
    const snapshot = {
      jarVersionId: 'v1',
      agentConfig: null,
      containerConfig: {
        memoryLimitMb: 1024,
        replicas: 3,
        deploymentStrategy: 'rolling',
        customEnvVars: { FOO: 'bar', BAZ: 'qux' },
      },
      sensitiveKeys: ['SECRET_KEY'],
    };
    const result = snapshotToForm(snapshot, defaultForm);
    expect(result.resources.memoryLimit).toBe('1024');
    expect(result.resources.replicas).toBe('3');
    expect(result.resources.deployStrategy).toBe('rolling');
    expect(result.variables.envVars).toEqual([
      { key: 'FOO', value: 'bar' },
      { key: 'BAZ', value: 'qux' },
    ]);
    expect(result.sensitiveKeys.sensitiveKeys).toEqual(['SECRET_KEY']);
  });

  it('falls back to defaults for missing fields', () => {
    const snapshot = {
      jarVersionId: 'v1',
      agentConfig: null,
      containerConfig: {},
      sensitiveKeys: null,
    };
    const result = snapshotToForm(snapshot, defaultForm);
    expect(result.resources.memoryLimit).toBe(defaultForm.resources.memoryLimit);
    expect(result.resources.cpuRequest).toBe(defaultForm.resources.cpuRequest);
    expect(result.variables.envVars).toEqual(defaultForm.variables.envVars);
    expect(result.sensitiveKeys.sensitiveKeys).toEqual(defaultForm.sensitiveKeys.sensitiveKeys);
  });
});
