meta {
  name: GET combat targets after cast spine (NEO-83)
  type: http
  seq: 3
}

docs {
  NEO-83 AC spine: slot 1 (seq 2 one-pulse uses slot 0 and leaves cooldown). Pre-request locks alpha, casts pulse four times (3.2s between), then GET asserts alpha defeated.
}

script:pre-request {
  const axios = require("axios");
  const { resetPrototypeCombatTargets } = require("./scripts/combat-targets-reset-helper.js");
  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 resetPrototypeCombatTargets(bru);

  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(
        `neo83 cast ${i + 1} failed: ${JSON.stringify(body)}`,
      );
    }
    castResults.push(body);
    if (i < 3) {
      await sleep(3200);
    }
  }

  bru.setVar("neo83CastResults", JSON.stringify(castResults));
}

get {
  url: {{baseUrl}}/game/world/combat-targets
  body: none
  auth: none
}

tests {
  test("returns 200 JSON with schema v1", function () {
    expect(res.getStatus()).to.equal(200);
    const body = res.getBody();
    expect(body.schemaVersion).to.equal(1);
    expect(body.targets.length).to.equal(2);
  });

  test("cast results stepped HP before GET", function () {
    const castResults = JSON.parse(bru.getVar("neo83CastResults"));
    expect(castResults.length).to.equal(4);
    expect(castResults[0].combatResolution.targetRemainingHp).to.equal(75);
    expect(castResults[1].combatResolution.targetRemainingHp).to.equal(50);
    expect(castResults[2].combatResolution.targetRemainingHp).to.equal(25);
    expect(castResults[3].combatResolution.targetRemainingHp).to.equal(0);
    expect(castResults[3].combatResolution.targetDefeated).to.equal(true);
  });

  test("alpha reflects cast damage authoritatively", function () {
    const body = res.getBody();
    const alpha = body.targets.find(
      (x) => x.targetId === "prototype_target_alpha",
    );
    expect(alpha).to.be.an("object");
    expect(alpha.currentHp).to.equal(0);
    expect(alpha.maxHp).to.equal(100);
    expect(alpha.defeated).to.equal(true);
  });

  test("beta unchanged after alpha-only casts", function () {
    const body = res.getBody();
    const beta = body.targets.find(
      (x) => x.targetId === "prototype_target_beta",
    );
    expect(beta).to.be.an("object");
    expect(beta.currentHp).to.equal(100);
    expect(beta.defeated).to.equal(false);
  });
}
