1#!/usr/bin/env node --redirect-warnings=/dev/null
 2
 3const fs = require("fs");
 4const { randomBytes } = require("crypto");
 5const { execFileSync } = require("child_process");
 6const {
 7  minimizeTestPlan,
 8  buildTests,
 9  runTests,
10} = require("./randomized-test-minimize");
11
12const { ZED_SERVER_URL } = process.env;
13if (!ZED_SERVER_URL) throw new Error("Missing env var `ZED_SERVER_URL`");
14
15main();
16
17async function main() {
18  buildTests();
19
20  const seed = randomU64();
21  const commit = execFileSync("git", ["rev-parse", "HEAD"], {
22    encoding: "utf8",
23  }).trim();
24
25  console.log("commit:", commit);
26  console.log("starting seed:", seed);
27
28  const planPath = "target/test-plan.json";
29  const minPlanPath = "target/test-plan.min.json";
30  const failingSeed = runTests({
31    SEED: seed,
32    SAVE_PLAN: planPath,
33    ITERATIONS: 50000,
34    OPERATIONS: 200,
35  });
36
37  if (!failingSeed) {
38    console.log("tests passed");
39    return;
40  }
41
42  console.log("found failure at seed", failingSeed);
43  const minimizedSeed = minimizeTestPlan(planPath, minPlanPath);
44  const minimizedPlan = fs.readFileSync(minPlanPath, "utf8");
45
46  console.log("minimized plan:\n", minimizedPlan);
47
48  const url = `${ZED_SERVER_URL}/api/randomized_test_failure`;
49  const body = {
50    seed: minimizedSeed,
51    plan: JSON.parse(minimizedPlan),
52    commit: commit,
53  };
54  await fetch(url, {
55    method: "POST",
56    headers: { "Content-Type": "application/json" },
57    body: JSON.stringify(body),
58  });
59
60  process.exit(1);
61}
62
63function randomU64() {
64  const bytes = randomBytes(8);
65  const hexString = bytes.reduce(
66    (string, byte) => string + byte.toString(16),
67    "",
68  );
69  return BigInt("0x" + hexString).toString(10);
70}