extends Node
var failures := 0
func check(ok: bool, message: String) -> void:
	if not ok:
		failures += 1
		push_error(message)
func _ready() -> void:
	GameState.save_locked = true
	call_deferred("run")
func run() -> void:
	var restaurant = load("res://scenes/restaurant/restaurant.tscn").instantiate()
	add_child(restaurant)
	await get_tree().process_frame
	restaurant.set_process(false)
	var saved_recipes := DataCatalog.RECIPES.duplicate(true)
	var recipe_id: String = str(DataCatalog.RECIPES.keys()[0])
	var recipe: Dictionary = DataCatalog.RECIPES[recipe_id].duplicate(true)
	var ingredient_id: String = str(DataCatalog.INGREDIENTS.keys()[0])
	recipe.ingredients = {ingredient_id: 1}
	recipe.conditions = []
	recipe.sale_price = 1
	recipe.levels = []
	DataCatalog.RECIPES = {recipe_id: recipe}
	GameState.ingredients = {ingredient_id: 1}
	for old_dish in get_tree().get_nodes_in_group("dish"):
		old_dish.free()
	restaurant.player.held_dish = null
	for station: Dictionary in restaurant.stations:
		station.recipe = ""
		station.remaining = 0.0
	var customer := Customer.new()
	restaurant.add_child(customer)
	customer.setup(str(DataCatalog.CUSTOMER_PROFILES.keys()[0]), restaurant, 0, 0, Vector2.ZERO, Vector2.ZERO)
	customer.set_process(false)
	customer.cash = 1000
	customer.category_preferences = {}
	restaurant.start_cooking(recipe_id, 0)
	check(GameState.get_ingredient_amount(ingredient_id) == 0, "last ingredient consumed by real cooking")
	check(not GameState.can_cook(recipe_id), "pantry cannot produce another plate")
	check(restaurant._choose_available_recipe(customer) == recipe_id, "cooking meal remains orderable")
	restaurant._finish_dish(0)
	restaurant.stations[0].recipe = ""
	restaurant.stations[0].remaining = 0.0
	var dish: Dish = get_tree().get_first_node_in_group("dish")
	check(dish != null, "real cooking produces a plate")
	if dish != null:
		dish.pick_up(restaurant.player)
		check(restaurant.player.held_dish == dish, "last meal is in player's hand")
		check(restaurant._servings_ready(recipe_id) == 1, "held plate counted once")
		restaurant.customer_seated(customer)
		# S'asseoir ouvre la reflexion; c'est en la terminant que le client
		# commande. Le controle veut la commande, pas le delai.
		restaurant.customer_finished_thinking(customer)
		check(customer.state == Customer.State.WAITING and customer.requested_recipe == recipe_id, "seated customer orders the held last meal")
		if customer.state == Customer.State.WAITING:
			restaurant.serve_customer(customer, dish)
			check(customer.state == Customer.State.EATING, "customer eats the last prepared meal")
		else:
			dish.state = Dish.State.SERVING
		check(not restaurant._recipe_is_available(recipe_id), "served last plate is not offered again")
	DataCatalog.RECIPES = saved_recipes
	restaurant.queue_free()
	await get_tree().process_frame
	print("LAST READY MEAL: ", "PASSED" if failures == 0 else "FAILED")
	get_tree().quit(0 if failures == 0 else 1)
