#include "test_support.hpp"
#include <functional>
orders::Result<orders::Done>
renameBatch(std::vector<orders::Order> &current,
            const std::function<std::string(const orders::Order &)> &rename) {
  auto candidate = current;
  for (auto &order : candidate)
    order.title = rename(order);
  return orders::replaceValidated(current, candidate);
}
int main() {
  auto current = samples();
  const auto before = current;
  bool threw = false;
  try {
    renameBatch(current, [](const auto &order) -> std::string {
      if (order.id == 2)
        throw std::runtime_error("formatter unavailable");
      return "Changed";
    });
  } catch (const std::runtime_error &) {
    threw = true;
  }
  require(threw && current == before, "exception preserves whole batch");
  require(
      !orders::succeeded(renameBatch(current, [](const auto &) { return ""; })),
      "invalid result rejected");
  require(current == before, "validation preserves whole batch");
  require(
      orders::succeeded(renameBatch(
          current, [](const auto &order) { return "Ready: " + order.title; })),
      "commit valid rename");
  require(current[0].title == "Ready: Deliver motors", "new title");
}
