meta {
  name: POST inventory remove
  type: http
  seq: 4
}

docs {
  NEO-55: remove stack via mutationKind remove. Pre-request seeds scrap_metal_bulk stock when needed so this request is order-independent.
}

script:pre-request {
  const { ensureItemQuantity } = require("./scripts/inventory-api-helper.js");
  const removeQuantity = 2;
  const before = await ensureItemQuantity(bru, "scrap_metal_bulk", removeQuantity);
  bru.setVar("scrapBeforeRemove", before);
  bru.setVar("removeQuantity", removeQuantity);
}

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

body:json {
  {
    "schemaVersion": 1,
    "mutationKind": "remove",
    "itemId": "scrap_metal_bulk",
    "quantity": 2
  }
}

tests {
  test("remove returns 200 with applied true and decreases 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");
    const before = bru.getVar("scrapBeforeRemove");
    const removeQuantity = bru.getVar("removeQuantity");
    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 - removeQuantity);
  });
}
