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('OpenAI Audio: should create a smartai instance with OpenAI provider', async () => {
  testSmartai = new smartai.SmartAi({
    openaiToken: await testQenv.getEnvVarOnDemand('OPENAI_TOKEN'),
  });
  await testSmartai.start();
});

tap.test('OpenAI Audio: should create audio response', async () => {
  // Call the audio method with a sample message.
  const audioStream = await testSmartai.openaiProvider.audio({
    message: 'This is a test of audio generation.',
  });
  // Read all chunks from the stream.
  const chunks: Uint8Array[] = [];
  for await (const chunk of audioStream) {
    chunks.push(chunk as Uint8Array);
  }
  const audioBuffer = Buffer.concat(chunks);
  await smartfs.file('./.nogit/testoutput.mp3').write(audioBuffer);
  console.log(`Audio Buffer length: ${audioBuffer.length}`);
  // Assert that the resulting buffer is not empty.
  expect(audioBuffer.length).toBeGreaterThan(0);
});

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

export default tap.start();
