meta {
  name: POST cast lock pulse defeat spine (NEO-82)
  type: http
  seq: 30
}

docs {
  NEO-82 spine: slot 1 (slot 0 may be cooling from happy seq 22). Pre-request locks alpha, casts pulse four times (3.2s between), fifth request expects target_defeated deny.
}

script:pre-request {
  const axios = require("axios");
  const baseUrl = bru.getEnvVar("baseUrl") || bru.getVar("baseUrl");
  const playerId = bru.getEnvVar("playerId") || bru.getVar("playerId");
  const jsonHeaders = { headers: { "Content-Type": "application/json" } };

  function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }

  await axios.post(
    `${baseUrl}/game/players/${playerId}/move`,
    { schemaVersion: 1, target: { x: -5, y: 0.9, z: -5 } },
    jsonHeaders,
  );

  await axios.post(
    `${baseUrl}/game/players/${playerId}/hotbar-loadout`,
    {
      schemaVersion: 1,
      slots: [{ slotIndex: 1, abilityId: "prototype_pulse" }],
    },
    jsonHeaders,
  );

  await axios.post(
    `${baseUrl}/game/players/${playerId}/target/select`,
    { schemaVersion: 1, targetId: "prototype_target_alpha" },
    jsonHeaders,
  );

  const castBody = {
    schemaVersion: 1,
    slotIndex: 1,
    abilityId: "prototype_pulse",
    targetId: "prototype_target_alpha",
  };

  const castResults = [];
  for (let i = 0; i < 4; i++) {
    const response = await axios.post(
      `${baseUrl}/game/players/${playerId}/ability-cast`,
      castBody,
      jsonHeaders,
    );
    const body = response.data;
    if (!body?.accepted || !body?.combatResolution) {
      throw new Error(
        `neo82 cast ${i + 1} failed: ${JSON.stringify(body)}`,
      );
    }
    castResults.push(body);
    if (i < 3) {
      await sleep(3200);
    }
  }

  bru.setVar("neo82CastResults", JSON.stringify(castResults));
  await sleep(3200);
}

post {
  url: {{baseUrl}}/game/players/dev-local-1/ability-cast
  body: json
  auth: none
}

body:json {
  {
    "schemaVersion": 1,
    "slotIndex": 1,
    "abilityId": "prototype_pulse",
    "targetId": "prototype_target_alpha"
  }
}

tests {
  test("pulse HP stepping on casts 1-4", function () {
    const casts = JSON.parse(bru.getVar("neo82CastResults"));
    expect(casts[0].combatResolution.targetRemainingHp).to.equal(75);
    expect(casts[1].combatResolution.targetRemainingHp).to.equal(50);
    expect(casts[2].combatResolution.targetRemainingHp).to.equal(25);
    expect(casts[3].combatResolution.targetRemainingHp).to.equal(0);
    expect(casts[3].combatResolution.targetDefeated).to.equal(true);
  });

  test("fifth cast denies target_defeated", function () {
    const body = res.getBody();
    expect(res.getStatus()).to.equal(200);
    expect(body.accepted).to.equal(false);
    expect(body.reasonCode).to.equal("target_defeated");
    expect(body.combatResolution).to.equal(undefined);
  });
}
