Pretrain-Test Task Alignment Governs Generalization in In-Context Learning