Stalin wanted communism in Russia. Trotsky wanted to go worldwide. Stalin killed Trotsky.
Stalin’s Red Army was in no way on a par with the Nazi army and he knew it. I think he was signing a side deal, hoping to give the nazis a reason to stop short in Poland and not continue on into the USSR. In the deal he got the Polish back parts of the Tsars’s Russia where they had been for hundreds of years before WWI.
His side deal with the nazis didn’t protect him from the nazi invasion 22 months later.
And don’t forget at that moment, Russia was in a bitter war in the east with Japan. He knew he couldn’t fight them both in 1939. He barely managed to post 1941.
Stalin was actually a ‘National Socialist’, his geopolitical goals were no different than that of the Czars, and he forced all people in the Soviet Union to speak Russian and despite his Georgian heritage, favored Russian culture over all others, as opposed to Lenin, who despised all things Russian.
One of the reasons Japan had no heart to fight the Soviets was that they got their butt kicked in 1939 at the Battle of Khalkhin Go.
The General who led the Red Army at that battle: Georgy Zhukov.
He didn't count on France falling in six weeks. And he also should have known that Hitler had no desire to occupy Britain, but merely to neutralize it, which he had pretty much achieved.