Recently, Spotify, while doing 70M profit for Q3, laid off 1500
people, which could approximately double those profits (those people
would have cost around 280M a year). This had their stock close up 7%.
That assumes that those 1500 people let go had no impact on income, and that it will cost $0 dollars to replace their services.
If they get rid of IT and hire an outside firm, that costs money. If they decide to get rid of sales that will impact income. If they decide programmers are an expense they don't want to pay, they can lose customers if the the software doesn't keep up with required changes need to work on OS updates.
Stock prices react to news depending on what the market thought was going to happen. They may even react differently depending on the timing related to other events.
I wonder how rare it is that a company makes a good profitability move
that has the market respond positively like that but goes bankrupt a
short time after (like 6-18 months).
It happens. When the whole economy is crumbling, or even just that segment of the market is crumbling, moves that will save money can look good, but still result in the company disappearing.
Some companies cut workers during the dot.com bubble, the mid 2000's housing crisis, and the early days of COVID-19; but some still collapsed. Other companies cut employees on a regular basis as they close stores, but they eventually close as their market changes with the times.