They weren’t really fighting “for” the Khmer Rouge back then. China saw itself encircled by USSR aligned countries, the incursion (can’t call it invasion, it was only a couple local division, no airt support, no artillery, no naval support) served to demonstrate to Vietnam that the USSR would not go all in for it and it should thus cut the crap it did at the time (such as offloading reactionaries to China instead of dealing with them themselves, diverting weapons intended for Cambodias liberation struggle for themselves). China achieved it goal, hence it considers the incursion a military victory, because strategically it was. Vietnam stopped being a dick.
The alignment with Cambodia was because China needed a local counter balance to Vietnam. once the strategical goal was achieved, the alignment ended.