meta {
  name: POST craft deny insufficient materials
  type: http
  seq: 1
}

docs {
  NEO-70: make_field_stim_mk0 without refined_plate_stock denies with insufficient_materials. Pre-request clears refined stock (inventory POST setup only) so the deny is stable after dotnet test / prior Bruno requests.
}

script:pre-request {
  const { getInventory, sumItemQuantity, postMutation } = require("./scripts/inventory-api-helper.js");

  async function clearItem(itemId) {
    let qty = sumItemQuantity(await getInventory(bru), itemId);
    while (qty > 0) {
      const removeQty = Math.min(qty, 99);
      const response = await postMutation(bru, {
        schemaVersion: 1,
        mutationKind: "remove",
        itemId,
        quantity: removeQty,
      });
      if (response.status !== 200 || response.data?.applied !== true) {
        throw new Error(`clear ${itemId} failed: ${response.status} ${JSON.stringify(response.data)}`);
      }
      qty = sumItemQuantity(await getInventory(bru), itemId);
    }
  }

  await clearItem("refined_plate_stock");
}

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

headers {
  content-type: application/json
}

body:json {
  {
    "schemaVersion": 1,
    "recipeId": "make_field_stim_mk0"
  }
}

tests {
  test("denies with insufficient_materials", function () {
    expect(res.getStatus()).to.equal(200);
    const body = res.getBody();
    expect(body.schemaVersion).to.equal(1);
    expect(body.success).to.equal(false);
    expect(body.reasonCode).to.equal("insufficient_materials");
    expect(body.inputsConsumed).to.be.an("array").that.is.empty;
    expect(body.outputsGranted).to.be.an("array").that.is.empty;
  });
}
