import http from "k6/http";
import { check } from "k6";
import { Counter } from "k6/metrics";

const status200 = new Counter("status_200");
const status401 = new Counter("status_401");
const status500 = new Counter("status_500");
const status502 = new Counter("status_502");

export const options = {
  scenarios: {
    default: {
      executor: "constant-arrival-rate",
      rate: 1000,
      timeUnit: "1s",
      duration: "30s",
      preAllocatedVUs: 50,
      maxVUs: 200,
    },
  },
  thresholds: {
    http_req_failed: ["rate<0.01"],
    http_req_duration: ["p(95)<500"],
  },
};

export default function () {
  const res = http.get("http://localhost/api/expenses?page=0&size=20", {
    headers: {
      Authorization: `Bearer ${__ENV.ACCESS_TOKEN}`,
    },
  });

    if (res.status === 200) status200.add(1);
    if (res.status === 401) status401.add(1);
    if (res.status === 500) status500.add(1);
    if (res.status === 502) status502.add(1);

  check(res, {
    "status 200": (r) => r.status === 200,
  });
}
