import http from 'k6/http';
import { check, fail } from 'k6';

import { apiKeys } from './apikey.js';

export const options = {
	vus: 10,
	duration: '3600s',
};

export default function () {
	const url = 'http://18.167.103.232/api/v1/txt2img/base/llm';

	const params = {
		headers: {
			'apikey': apiKeys[Math.floor(Math.random() * apiKeys.length)],
			'Content-Type': 'application/json'
		}
	}
	const body = JSON.stringify({ 'test': 1 })

	const res = http.post(url, body, params)
	// const res = responses[i];
	const statusCheck = check(res, {
		'status is 200': (r) => r.status === 200,
	});
	const taskCheck = check(res, {
		'task is success': (r) => r.json('task.is_success') === true,
	});
	if (!statusCheck) {
		// console.log('task status failed', res.status);
		fail('task ' + res.headers['Task-Id'] + ' failed with status error: ' + res.status);
	} else if (!taskCheck) {
		// check res.boy is empty
		if (res.body == null) {
			fail('task ' + res.headers['Task-Id'] + ' failed with empty response');
		} else {
			if (res.json('task.api_error.message') !== undefined) {
			console.log(res.body)
				fail('task ' + res.headers['Task-Id'] + ' failed with api error: ' + res.json('task.api_error.message'));
			}
			if (res.json('task.error') !== undefined) {
	console.log(res.body)

				fail('task ' + res.headers['Task-Id'] + ' failed with execute error: ' + res.json('task.error'));
			}
		}
	}

	// console.log("res is ", res);
}
