import { getImageProvider } from '../../../../src/modules/netaclaw/image_providers/types.js';
import '../../../../src/modules/netaclaw/image_providers/ark.js';
import nock = require('nock');

describe('ArkImageProvider', () => {
  afterEach(() => {
    nock.cleanAll();
    jest.restoreAllMocks();
  });

  it('sends image-to-image reference image as top-level Ark image field', async () => {
    let requestBody: any;

    nock('https://ark.cn-beijing.volces.com')
      .post('/api/v3/images/generations', body => {
        requestBody = body;
        return true;
      })
      .reply(200, {
        created: 1,
        data: [{ url: 'https://example.com/out.jpeg' }],
      });

    const provider = getImageProvider('volcengine', 'https://ark.cn-beijing.volces.com/api/v3');
    await provider!.imageToImage({
      prompt: '只修改姓名为李鑫，其他保持不变',
      referenceImage: 'https://example.com/input.jpg',
      aspectRatio: '3:2',
      n: 1,
      watermark: false,
      responseFormat: 'url',
    }, {
      baseUrl: 'https://ark.cn-beijing.volces.com/api/v3',
      apiKey: 'test-key',
      supplier: 'volcengine',
      modelId: 'doubao-seedream-5-0-lite',
      promptHint: null,
    });

    expect(requestBody.image).toBe('https://example.com/input.jpg');
    expect(requestBody.extra_body?.image).toBeUndefined();
    expect(requestBody.watermark).toBe(false);
    expect(requestBody.size).toBe('2496x1664');
  });

  it('converts localhost reference image URLs to data URLs before calling Ark', async () => {
    let requestBody: any;
    const imageBytes = Buffer.from('fake-jpeg-bytes');

    nock('http://127.0.0.1:8003')
      .get('/upload/20260515/input.jpg')
      .reply(200, imageBytes, { 'content-type': 'image/jpeg' });

    nock('https://ark.cn-beijing.volces.com')
      .post('/api/v3/images/generations', body => {
        requestBody = body;
        return true;
      })
      .reply(200, {
        created: 1,
        data: [{ url: 'https://example.com/out.jpeg' }],
      });

    const provider = getImageProvider('volcengine', 'https://ark.cn-beijing.volces.com/api/v3');
    await provider!.imageToImage({
      prompt: '只修改姓名为李鑫，其他保持不变',
      referenceImage: 'http://127.0.0.1:8003/upload/20260515/input.jpg',
      n: 1,
      watermark: false,
      responseFormat: 'url',
    }, {
      baseUrl: 'https://ark.cn-beijing.volces.com/api/v3',
      apiKey: 'test-key',
      supplier: 'volcengine',
      modelId: 'doubao-seedream-5-0-lite',
      promptHint: null,
    });

    expect(requestBody.image).toBe(`data:image/jpeg;base64,${imageBytes.toString('base64')}`);
  });
});
