# Copyright 2021 Mikel Arregi Etxaniz - AvanzOSC
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo.tests import tagged

from .common import MrpWorkorderGroupingMaterial


@tagged("post_install", "-at_install")
class TestMrpWorkorderGroupingMaterial(MrpWorkorderGroupingMaterial):
    def test_nest_creation(self):
        self.production_id.onchange_product_id()
        self.production_id._onchange_bom_id()
        self.production_id._onchange_move_raw()
        self.production_id.action_confirm()
        self.production_id.button_plan()
        workorders = self.env["mrp.workorder"].search([])
        wiz_nest = (
            self.env["nested.new.line"]
            .with_context(active_ids=workorders.ids, active_model="mrp.workorder")
            .create(
                {
                    "nest_code": "Test",
                }
            )
        )
        wiz_nest.action_done()
        nest = self.env["mrp.workorder.nest"].search([])
        nest.nest_start()
        self.assertEqual(nest.state, "ready")
        nest.button_start()
        self.assertEqual(nest.state, "progress")
        for line in nest.nested_line_ids.mapped("workorder_id"):
            self.assertEqual(line.state, "progress")
        nest.record_production()
        self.assertEqual(nest.state, "blocked")
        nest.nest_unblocked()
        self.assertEqual(nest.state, "progress")
