import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Tab3Page } from './tab3.page';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { IonicModule } from '@ionic/angular';
import { FormsModule } from '@angular/forms';
import { of, throwError } from 'rxjs';

// 模拟axios
const mockAxios = {
  post: jasmine.createSpy('post').and.returnValue(Promise.resolve({
    data: {
      choices: [{
        message: {
          content: '模拟的AI回复内容'
        }
      }]
    }
  }))
};

describe('Tab3Page', () => {
  let component: Tab3Page;
  let fixture: ComponentFixture<Tab3Page>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [
        Tab3Page,  // 直接导入Standalone组件
        IonicModule.forRoot(),
        FormsModule,
        HttpClientTestingModule
      ],
      providers: [
        { provide: 'AXIOS', useValue: mockAxios } // 提供模拟的axios
      ]
    }).compileComponents();

    fixture = TestBed.createComponent(Tab3Page);
    component = fixture.componentInstance;
// 假设 Tab3Page 类有一个 public 或 protected 的 deepSeekService 属性
// 若该属性未定义，需要在 Tab3Page 类中添加该属性声明
// 这里直接通过类型断言来避免类型错误
(component as any).deepSeekService = { getAIResponse: () => of('模拟回复') };
    fixture.detectChanges();
  });

  it('应该正确创建组件', () => {
    expect(component).toBeTruthy();
  });

  it('初始化时应显示欢迎消息', () => {
    expect(component.messages.length).toBeGreaterThan(0);
    expect(component.messages[0].sender).toBe('ai');
  });

  describe('sendMessage()', () => {
    it('空输入时不应发送消息', () => {
      component.userInput = '';
      component.sendMessage();
      expect(component.messages.length).toBe(1); // 只有欢迎消息
    });

    it('成功发送消息应添加到消息列表', async () => {
      component.userInput = '如何增肌？';
      mockAxios.post.and.returnValue(Promise.resolve({
        data: {
          choices: [{
            message: {
              content: '增肌需要...' // 模拟AI回复
            }
          }]
        }
      }));

      await component.sendMessage();
      
      expect(component.messages.length).toBe(3); // 欢迎 + 用户 + AI
      expect(component.messages[1].content).toBe('如何增肌？');
      expect(component.messages[2].content).toContain('增肌');
      expect(component.isLoading).toBeFalse();
    });

    it('API调用失败应显示错误消息', async () => {
      component.userInput = '测试问题';
      mockAxios.post.and.returnValue(Promise.reject('API错误'));

      await component.sendMessage();
      
      expect(component.messages.length).toBe(3);
      expect(component.messages[2].content).toBe('抱歉，服务暂时不可用');
      expect(component.isLoading).toBeFalse();
    });
  });

  it('发送消息后应清空输入框', async () => {
    component.userInput = '测试内容';
mockAxios.post.and.returnValue(Promise.resolve({ data: { choices: [{}] } }));
    
    await component.sendMessage();
    expect(component.userInput).toBe('');
  });

  it('加载状态应正确切换', async () => {
    component.userInput = '测试';
    let resolveApi: any;
    mockAxios.post.and.returnValue(new Promise(res => resolveApi = res));
    
    component.sendMessage();
    expect(component.isLoading).toBeTrue();
    
    resolveApi({ data: { choices: [{}] }});
    await fixture.whenStable(); // 等待异步操作完成
    expect(component.isLoading).toBeFalse();
  });
});