meta {
  name: GET gig progression after defeat spine (NEO-44)
  type: http
  seq: 31
}

docs {
  NEO-44 AC spine: pre-request resets prototype target HP (NEO-83 fixture), then four pulse casts on alpha (slot 2 — avoids slot 0/1 cooldown from ability-cast and combat-targets folders).
  Asserts breach xp increased by 25 from pre-spine baseline; salvage/refine skill xp unchanged.
}

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));
  }

  const skillBefore = await axios.get(
    `${baseUrl}/game/players/${playerId}/skill-progression`,
    jsonHeaders,
  );
  bru.setVar("neo44SalvageXpBefore", String(skillBefore.data.skills.find((x) => x.id === "salvage").xp));
  bru.setVar("neo44RefineXpBefore", String(skillBefore.data.skills.find((x) => x.id === "refine").xp));

  const gigBefore = await axios.get(
    `${baseUrl}/game/players/${playerId}/gig-progression`,
    jsonHeaders,
  );
  const breachBefore = gigBefore.data.gigs.find((x) => x.id === "breach");
  bru.setVar(
    "neo44BreachXpBefore",
    String(breachBefore?.xp ?? 0),
  );

  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: 2, 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: 2,
    abilityId: "prototype_pulse",
    targetId: "prototype_target_alpha",
  };

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

get {
  url: {{baseUrl}}/game/players/{{playerId}}/gig-progression
  body: none
  auth: none
}

script:post-response {
  const axios = require("axios");
  const baseUrl = bru.getEnvVar("baseUrl") || bru.getVar("baseUrl");
  const playerId = bru.getEnvVar("playerId") || bru.getVar("playerId");
  const skillAfter = await axios.get(
    `${baseUrl}/game/players/${playerId}/skill-progression`,
  );
  bru.setVar("neo44SkillAfterJson", JSON.stringify(skillAfter.data));
}

tests {
  test("breach gig xp increases by 25 after alpha defeat", function () {
    const body = res.getBody();
    expect(res.getStatus()).to.equal(200);
    expect(body.mainGigId).to.equal("breach");
    const breach = body.gigs.find((x) => x.id === "breach");
    expect(breach).to.be.an("object");
    const breachXpBefore = Number(bru.getVar("neo44BreachXpBefore"));
    expect(breach.xp).to.equal(breachXpBefore + 25);
    expect(breach.level).to.equal(1);
  });

  test("salvage and refine skill xp unchanged by combat defeat", function () {
    const skillAfter = JSON.parse(bru.getVar("neo44SkillAfterJson"));
    const salvageBefore = Number(bru.getVar("neo44SalvageXpBefore"));
    const refineBefore = Number(bru.getVar("neo44RefineXpBefore"));
    const salvageAfter = skillAfter.skills.find((x) => x.id === "salvage").xp;
    const refineAfter = skillAfter.skills.find((x) => x.id === "refine").xp;
    expect(salvageAfter).to.equal(salvageBefore);
    expect(refineAfter).to.equal(refineBefore);
  });
}
