Indeed,
Why would they do it,
Cost you gold
Cost you a free tech you paid your gold to get
Pretend to be your friend, make you unawares to the attack to come, (a very human thing to do)
Basically what my brother says, it doesn't matter if it costs them to do it, the idea behind it is to make you pay, hurting themselfs to spite you. Its very human and thats what we want the AI to be like.