Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/api/runs/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export async function GET() {
},
],
},
validation: { results: "must contain at least one result" },
returns: { ok: true, runId: "uuid", regressed: 0, flagged: 0, shouldFail: false },
});
}
Expand Down Expand Up @@ -52,6 +53,13 @@ export async function POST(req: NextRequest) {
);
}

if (body.results.length === 0) {
return NextResponse.json(
{ error: "results[] must not be empty" },
{ status: 400 },
);
}

const supabase = db();

// Upsert suite by name.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"test": "node --test test/*.test.mjs",
"lint": "next lint",
"cf:build": "NODE_OPTIONS=--no-deprecation opennextjs-cloudflare build",
"cf:preview": "NODE_OPTIONS=--no-deprecation opennextjs-cloudflare build && NODE_OPTIONS=--no-deprecation opennextjs-cloudflare preview",
Expand Down
94 changes: 94 additions & 0 deletions test/runs-route.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import { createServer } from "node:net";
import test from "node:test";

async function availablePort() {
const server = createServer();
await new Promise((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
const address = server.address();
const port = typeof address === "object" && address ? address.port : 0;
await new Promise((resolve) => server.close(resolve));
return port;
}

async function startServer(t) {
const port = await availablePort();
const child = spawn(
process.execPath,
[
"node_modules/next/dist/bin/next",
"dev",
"--hostname",
"127.0.0.1",
"--port",
String(port),
],
{
env: {
...process.env,
TRACECASE_INGEST_TOKEN: "test-token",
SUPABASE_URL: "",
SUPABASE_SERVICE_ROLE_KEY: "",
},
stdio: ["ignore", "pipe", "pipe"],
},
);
t.after(() => child.kill("SIGTERM"));

let output = "";
await new Promise((resolve, reject) => {
const timeout = setTimeout(
() => reject(new Error(`Next.js did not start:\n${output}`)),
30_000,
);
const onData = (chunk) => {
output += chunk.toString();
if (output.includes("Ready in")) {
clearTimeout(timeout);
resolve();
}
};
child.stdout.on("data", onData);
child.stderr.on("data", onData);
child.once("exit", (code) => {
clearTimeout(timeout);
reject(new Error(`Next.js exited with code ${code}:\n${output}`));
});
});

return `http://127.0.0.1:${port}`;
}

test(
"POST /api/runs rejects an empty results array before database access",
async (t) => {
const origin = await startServer(t);
const infoResponse = await fetch(`${origin}/api/runs`);
assert.equal(infoResponse.status, 200);
assert.deepEqual((await infoResponse.json()).validation, {
results: "must contain at least one result",
});

const response = await fetch(`${origin}/api/runs`, {
method: "POST",
headers: {
"content-type": "application/json",
"x-tracecase-token": "test-token",
},
body: JSON.stringify({
suite: "refund-agent",
label: "empty run",
results: [],
}),
});

assert.equal(response.status, 400);
assert.deepEqual(await response.json(), {
error: "results[] must not be empty",
});
},
);