import { describe, expect, it } from 'vitest';

import {
  createEmptyAgent,
  useAgentDesignerState,
} from './useAgentDesignerState';

describe('useAgentDesignerState generation stream', () => {
  it('defaults new and legacy agents to streaming', () => {
    expect(createEmptyAgent().generationConfigJson?.stream).toBe(true);

    const designer = useAgentDesignerState();
    designer.reset({ name: '旧智能体' });

    expect(designer.state.agent.generationConfigJson?.stream).toBe(true);
    expect(designer.buildPayloadAgent().generationConfigJson?.stream).toBe(
      true,
    );
  });

  it('preserves an explicitly disabled stream value in the payload', () => {
    const designer = useAgentDesignerState();
    designer.reset({
      name: '非流式智能体',
      generationConfigJson: { stream: false },
    });

    expect(designer.state.agent.generationConfigJson?.stream).toBe(false);
    expect(designer.buildPayloadAgent().generationConfigJson?.stream).toBe(
      false,
    );
  });
});

describe('useAgentDesignerState visibility scope', () => {
  it('defaults new and legacy agents to private visibility', () => {
    expect(createEmptyAgent().visibilityScope).toBe('PRIVATE');

    const designer = useAgentDesignerState();
    designer.reset({ name: '旧智能体' });

    expect(designer.state.agent.visibilityScope).toBe('PRIVATE');
    expect(designer.buildPayloadAgent().visibilityScope).toBe('PRIVATE');
  });

  it('preserves the selected visibility scope in the save payload', () => {
    const designer = useAgentDesignerState();
    designer.reset({ name: '部门智能体', visibilityScope: 'DEPT' });

    expect(designer.buildPayloadAgent().visibilityScope).toBe('DEPT');
  });
});

describe('useAgentDesignerState document context budget', () => {
  it('defaults new and legacy agents to twenty thousand tokens', () => {
    expect(
      createEmptyAgent().executionConfigJson?.documentContextBudgetTokens,
    ).toBe(20_000);

    const designer = useAgentDesignerState();
    designer.reset({ name: '旧智能体' });

    expect(
      designer.state.agent.executionConfigJson?.documentContextBudgetTokens,
    ).toBe(20_000);
    expect(
      designer.buildPayloadAgent().executionConfigJson
        ?.documentContextBudgetTokens,
    ).toBe(20_000);
  });

  it('preserves a positive custom document context budget', () => {
    const designer = useAgentDesignerState();
    designer.reset({
      executionConfigJson: { documentContextBudgetTokens: 32_000 },
      name: '长文档智能体',
    });

    expect(
      designer.buildPayloadAgent().executionConfigJson
        ?.documentContextBudgetTokens,
    ).toBe(32_000);
  });
});

describe('useAgentDesignerState memory compression', () => {
  it('keeps only token-based compression settings in state and payload', () => {
    expect(
      createEmptyAgent().memoryConfigJson?.compressionParameter,
    ).not.toHaveProperty('msgThreshold');

    const designer = useAgentDesignerState();
    designer.reset({
      name: '旧压缩配置智能体',
      memoryConfigJson: {
        maxAttachedMessageCount: 12,
        compressionParameter: {
          enabled: true,
          msgThreshold: 12,
          lastKeep: 6,
          minCompressionTokenThreshold: 128_000,
        },
      },
    });

    const stateCompressionParameter =
      designer.state.agent.memoryConfigJson?.compressionParameter;
    expect(stateCompressionParameter).not.toHaveProperty('msgThreshold');
    expect(stateCompressionParameter?.lastKeep).toBe(6);
    expect(stateCompressionParameter?.minCompressionTokenThreshold).toBe(
      128_000,
    );

    const payloadMemoryConfig = designer.buildPayloadAgent().memoryConfigJson;
    expect(payloadMemoryConfig).not.toHaveProperty('maxAttachedMessageCount');
    expect(payloadMemoryConfig?.compressionParameter).not.toHaveProperty(
      'msgThreshold',
    );
    expect(
      payloadMemoryConfig?.compressionParameter.minCompressionTokenThreshold,
    ).toBe(128_000);
  });
});
