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

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

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

let testSmartai: smartai.SmartAi;

tap.test('ElevenLabs Audio: should create a smartai instance with ElevenLabs provider', async () => {
  testSmartai = new smartai.SmartAi({
    elevenlabsToken: await testQenv.getEnvVarOnDemand('ELEVENLABS_TOKEN'),
    elevenlabs: {
      defaultVoiceId: '19STyYD15bswVz51nqLf',
    },
  });
  await testSmartai.start();
});

tap.test('ElevenLabs Audio: should create audio response', async () => {
  const audioStream = await testSmartai.elevenlabsProvider.audio({
    message: 'Welcome to SmartAI, the unified interface for the world\'s leading artificial intelligence providers. SmartAI brings together OpenAI, Anthropic, Perplexity, and ElevenLabs under a single elegant TypeScript API. Whether you need text generation, vision analysis, document processing, or premium text-to-speech capabilities, SmartAI provides a consistent and powerful interface for all your AI needs. Build intelligent applications at lightning speed without vendor lock-in.',
  });
  const chunks: Uint8Array[] = [];
  for await (const chunk of audioStream) {
    chunks.push(chunk as Uint8Array);
  }
  const audioBuffer = Buffer.concat(chunks);
  await smartfs.file('./.nogit/testoutput_elevenlabs.mp3').write(audioBuffer);
  console.log(`Audio Buffer length: ${audioBuffer.length}`);
  expect(audioBuffer.length).toBeGreaterThan(0);
});

tap.test('ElevenLabs Audio: should create audio with custom voice', async () => {
  const audioStream = await testSmartai.elevenlabsProvider.audio({
    message: 'Testing with a different voice.',
    voiceId: 'JBFqnCBsd6RMkjVDRZzb',
  });
  const chunks: Uint8Array[] = [];
  for await (const chunk of audioStream) {
    chunks.push(chunk as Uint8Array);
  }
  const audioBuffer = Buffer.concat(chunks);
  await smartfs.file('./.nogit/testoutput_elevenlabs_custom.mp3').write(audioBuffer);
  console.log(`Audio Buffer length (custom voice): ${audioBuffer.length}`);
  expect(audioBuffer.length).toBeGreaterThan(0);
});

tap.test('ElevenLabs Audio: should stop the smartai instance', async () => {
  await testSmartai.stop();
});

export default tap.start();
