from odoo.tests.common import TransactionCase


def _sel_groups_field_name(group_ids):
    """Generate sel_groups field name from group ids (replaces removed name_selection_groups)."""
    return 'sel_groups_' + '_'.join(str(i) for i in sorted(group_ids))


class TestFieldsGet(TransactionCase):
    def test_base(self):
        demo_user = self.env["res.users"].create({
            "name": "Test Demo User Fields",
            "login": "test_demo_fields_get",
            "groups_id": [(6, 0, [self.env.ref("base.group_user").id])],
        })
        group_erp_manager = self.env.ref("base.group_erp_manager")
        group_system = self.env.ref("base.group_system")

        demo_user.write({"group_ids": [(3, group_system.id)]})
        group_system.write({"user_ids": [(3, demo_user.id)]})
        demo_user.write({"group_ids": [(4, group_erp_manager.id)]})

        view_users_form = self.env.ref("base.view_users_form")
        (
            self.env["res.users"]
            .with_user(demo_user)
            .with_context({"uid": demo_user.id})
            .get_views([[view_users_form.id, "form"]])
        )

        sel_groups = _sel_groups_field_name([group_erp_manager.id])
        res = self.env["res.users"].with_user(demo_user).fields_get()
        self.assertTrue(res.get(sel_groups))

        demo_user.write({"groups_id": [(4, group_system.id)]})

        sel_groups = _sel_groups_field_name([group_erp_manager.id, group_system.id])
        res = self.env["res.users"].with_user(demo_user).fields_get()
        self.assertTrue(res.get(sel_groups))
