Allah will make the Mahdī righteous overnight, hence his title, the Mahdī (the One Rightly Guided)
"Mahdi is one of us, the people of the Household. Allah will rectify him in a single night."
قَالَ رَسُولُ اللَّهِ ﷺ الْمَهْدِيُّ مِنَّا أَهْلَ الْبَيْتِ يُصْلِحُهُ اللَّهُ فِي لَيْلَةٍ