const API_TOKEN = process.env.NUNU_API_TOKEN;
const BASE_URL = "https://nunu.ai/api/v1";
async function startAndWaitForTest(
testId: string,
deploymentConfigId: string
): Promise<any> {
// Start the test
const startResponse = await fetch(`${BASE_URL}/runs`, {
method: "POST",
headers: {
"X-Api-Key": API_TOKEN,
"Content-Type": "application/json",
},
body: JSON.stringify({
multiplayer_test_id: testId,
deployment_config_id: deploymentConfigId,
}),
});
const startResult = await startResponse.json();
if (!startResult.ok) {
throw new Error(`Failed to start test: ${startResult.error}`);
}
const runId = startResult.data.multiplayer_run_id;
console.log(`Test started: ${runId}`);
// Poll for completion
while (true) {
const detailsResponse = await fetch(`${BASE_URL}/runs/${runId}`, {
headers: { "X-Api-Key": API_TOKEN },
});
const details = await detailsResponse.json();
if (details.state === "completed") {
console.log(`Test completed with result: ${details.result}`);
return details;
}
console.log(`Test still running... (state: ${details.state})`);
await new Promise((resolve) => setTimeout(resolve, 30000));
}
}