meta {
  name: POST branch select deny branch already chosen
  type: http
  seq: 4
}

docs {
  NEO-48: pre-request sets salvage level 2 + tier-1 scrap_efficiency pick via mastery-fixture + perk-state; main request conflicts with bulk_haul.
}

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" } };
  const reset = await axios.post(
    `${baseUrl}/game/players/${playerId}/__dev/mastery-fixture`,
    {
      schemaVersion: 1,
      resetPerkState: true,
      skillXp: { salvage: 100 },
    },
    jsonHeaders,
  );
  if (reset.status !== 200 || reset.data?.applied !== true) {
    throw new Error(`mastery fixture reset failed: ${reset.status} ${JSON.stringify(reset.data)}`);
  }
  const firstPick = await axios.post(
    `${baseUrl}/game/players/${playerId}/perk-state`,
    {
      schemaVersion: 1,
      skillId: "salvage",
      tierIndex: 1,
      branchId: "scrap_efficiency",
    },
    jsonHeaders,
  );
  if (firstPick.status !== 200 || firstPick.data?.selected !== true) {
    throw new Error(`tier-1 pick failed: ${firstPick.status} ${JSON.stringify(firstPick.data)}`);
  }
}

post {
  url: {{baseUrl}}/game/players/{{playerId}}/perk-state
  body: json
  auth: none
}

body:json {
  {
    "schemaVersion": 1,
    "skillId": "salvage",
    "tierIndex": 1,
    "branchId": "bulk_haul"
  }
}

tests {
  test("denies second pick with branch_already_chosen", function () {
    expect(res.getStatus()).to.equal(200);
    const body = res.getBody();
    expect(body.schemaVersion).to.equal(1);
    expect(body.selected).to.equal(false);
    expect(body.reasonCode).to.equal("branch_already_chosen");
    expect(body.unlockedEvents).to.be.an("array").that.is.empty;
    const salvage = body.perkState.branchPicks.find((t) => t.skillId === "salvage");
    expect(salvage).to.be.an("object");
    const pick = salvage.picks.find((p) => p.tierIndex === 1);
    expect(pick.branchId).to.equal("scrap_efficiency");
  });
}
