import assert from 'node:assert/strict';
import test from 'node:test';

import { moderateImage } from '../src/moderation.js';

test('queues uploads when AI moderation is not configured', async () => {
  const originalApiKey = process.env.OPENAI_API_KEY;
  delete process.env.OPENAI_API_KEY;

  try {
    const moderation = await moderateImage({
      buffer: Buffer.from('image'),
      mime: 'image/webp'
    });

    assert.equal(moderation.status, 'pending');
    assert.equal(moderation.score, 50);
    assert.match(moderation.reason, /not configured/);
  } finally {
    if (originalApiKey === undefined) {
      delete process.env.OPENAI_API_KEY;
    } else {
      process.env.OPENAI_API_KEY = originalApiKey;
    }
  }
});

test('queues model approvals below the auto-publish quality threshold', async () => {
  const originalApiKey = process.env.OPENAI_API_KEY;
  const originalThreshold = process.env.AUTO_APPROVE_MIN_SCORE;
  const originalFetch = globalThis.fetch;
  process.env.OPENAI_API_KEY = 'test-key';
  process.env.AUTO_APPROVE_MIN_SCORE = '90';

  globalThis.fetch = async (_url, options) => {
    const body = JSON.parse(options.body);
    const prompt = body.input[0].content.find((item) => item.type === 'input_text').text;

    assert.match(prompt, /could the exact same caption work on 50 unrelated images/);
    assert.match(prompt, /Use approved only when it appears legal and score is at least 90/);

    return {
      ok: true,
      json: async () => ({
        output_text: JSON.stringify({
          decision: 'approved',
          score: 89,
          reason: 'Specific joke, but not strong enough.'
        })
      })
    };
  };

  try {
    const moderation = await moderateImage({
      buffer: Buffer.from('image'),
      mime: 'image/webp'
    });

    assert.equal(moderation.status, 'pending');
    assert.equal(moderation.score, 89);
    assert.match(moderation.reason, /Low MEME_CONSENSUS_SCORE queued for review/);
  } finally {
    globalThis.fetch = originalFetch;
    if (originalApiKey === undefined) {
      delete process.env.OPENAI_API_KEY;
    } else {
      process.env.OPENAI_API_KEY = originalApiKey;
    }
    if (originalThreshold === undefined) {
      delete process.env.AUTO_APPROVE_MIN_SCORE;
    } else {
      process.env.AUTO_APPROVE_MIN_SCORE = originalThreshold;
    }
  }
});
