meta {
  name: POST inventory add
  type: http
  seq: 2
}

docs {
  NEO-55: add stack via mutationKind add. Pre-request records scrap_metal_bulk baseline so assertions are order-independent.
}

script:pre-request {
  const { getInventory, sumItemQuantity } = require("./scripts/inventory-api-helper.js");
  const inventory = await getInventory(bru);
  bru.setVar("scrapBeforeAdd", sumItemQuantity(inventory, "scrap_metal_bulk"));
  bru.setVar("addQuantity", 5);
}

post {
  url: {{baseUrl}}/game/players/{{playerId}}/inventory
  body: json
  auth: none
}

body:json {
  {
    "schemaVersion": 1,
    "mutationKind": "add",
    "itemId": "scrap_metal_bulk",
    "quantity": 5
  }
}

tests {
  test("add returns 200 with applied true and increases scrap by requested quantity", function () {
    expect(res.getStatus()).to.equal(200);
    const body = res.getBody();
    expect(body.schemaVersion).to.equal(1);
    expect(body.applied).to.equal(true);
    expect(body.reasonCode).to.equal(null);
    expect(body.inventory).to.be.an("object");
    expect(body.inventory.playerId).to.equal(bru.getEnvVar("playerId") || bru.getVar("playerId"));
    const before = bru.getVar("scrapBeforeAdd");
    const addQuantity = bru.getVar("addQuantity");
    const after = [...body.inventory.bagSlots, ...body.inventory.equipmentSlots]
      .filter((slot) => slot.itemId === "scrap_metal_bulk")
      .reduce((total, slot) => total + slot.quantity, 0);
    expect(after).to.equal(before + addQuantity);
  });
}
