import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as qenv from '@push.rocks/qenv';

const testQenv = new qenv.Qenv('./', './.nogit/');

import * as smartai from '../ts/index.js';

let mistralProvider: smartai.MistralProvider;

tap.test('Mistral Chat: should create and start Mistral provider', async () => {
  mistralProvider = new smartai.MistralProvider({
    mistralToken: await testQenv.getEnvVarOnDemand('MISTRAL_API_KEY'),
  });
  await mistralProvider.start();
  expect(mistralProvider).toBeInstanceOf(smartai.MistralProvider);
});

tap.test('Mistral Chat: should create chat response', async () => {
  const userMessage = 'What is the capital of France? Answer in one word.';
  const response = await mistralProvider.chat({
    systemMessage: 'You are a helpful assistant. Be concise.',
    userMessage: userMessage,
    messageHistory: [],
  });
  console.log(`Mistral Chat - User: ${userMessage}`);
  console.log(`Mistral Chat - Response: ${response.message}`);

  expect(response.role).toEqual('assistant');
  expect(response.message).toBeTruthy();
  expect(response.message.toLowerCase()).toInclude('paris');
});

tap.test('Mistral Chat: should handle message history', async () => {
  const messageHistory: smartai.ChatMessage[] = [
    { role: 'user', content: 'My name is Claude Test' },
    { role: 'assistant', content: 'Nice to meet you, Claude Test!' }
  ];

  const response = await mistralProvider.chat({
    systemMessage: 'You are a helpful assistant with good memory.',
    userMessage: 'What is my name?',
    messageHistory: messageHistory,
  });

  console.log(`Mistral Memory Test - Response: ${response.message}`);
  expect(response.message.toLowerCase()).toInclude('claude test');
});

tap.test('Mistral Chat: should handle longer conversations', async () => {
  const response = await mistralProvider.chat({
    systemMessage: 'You are a helpful coding assistant.',
    userMessage: 'Write a simple hello world function in TypeScript. Keep it brief.',
    messageHistory: [],
  });

  console.log(`Mistral Coding Test - Response: ${response.message}`);
  expect(response.message).toBeTruthy();
  // Should contain some TypeScript/function code
  expect(response.message).toInclude('function');
});

tap.test('Mistral Chat: should stop the provider', async () => {
  await mistralProvider.stop();
});

export default tap.start();
