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

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

tap.test('generateImage should return an image response', async () => {
  const apiKey = await testQenv.getEnvVarOnDemand('OPENAI_TOKEN');
  if (!apiKey) {
    console.log('OPENAI_TOKEN not set, skipping test');
    return;
  }

  const result = await generateImage({
    apiKey,
    prompt: 'A simple red circle on a white background',
    model: 'gpt-image-1',
    size: '1024x1024',
    quality: 'low',
    n: 1,
  });

  console.log('Image generation result: images count =', result.images.length);
  expect(result.images).toBeArray();
  expect(result.images.length).toBeGreaterThan(0);

  const firstImage = result.images[0];
  // gpt-image-1 returns b64_json by default
  expect(firstImage.b64_json || firstImage.url).toBeTruthy();

  expect(result.metadata).toBeTruthy();
  expect(result.metadata!.model).toEqual('gpt-image-1');
});

export default tap.start();
