The Fernandine Wars (from Spanish and Portuguese Guerras Fernandinas) were a series of three conflicts (1369–70, 1372–73, 1381–82) between the Kingdom of Portugal under King Ferdinand I and the Crown of Castile under King Henry II. They were fought over Ferdinand's claim to the Castilian succession after the death of King Peter of Castile in 1369.
Fernandine Wars Wikipedia
The Treaty of Salvaterra de Magos was an agreement signed in 1383 between Portugal and Castile in order to end a period known in Portugal as the Fernandine Wars (after the name of Fernando I).
To celebrate the peace between the two kingdoms, king Fernando I of Portugal agreed on the marriage of his daughter and heir, Beatrice of Portugal, with king John I of Castile. However, to avoid the union of the two countries, this pre-nuptial agreement established the rules to the succession for the two thrones.
It was never Fernando's intention to form a union of the kingdoms of Portugal and Castile. Therefore, it was agreed that following his death his wife, queen Leonor Telles de Menezes would assume the regency in Portugal until the son of Beatrice and John would reach 14 years old, when he would become the future king of Portugal.
The main clauses of the Treaty of Salvaterra stipulated:The separation between the kingdoms of Portugal and Castile only could be eliminated with the consent of the Cortes;
The recognition of Beatrice and her husband as kings of Portugal (him, as king consort), if Fernando I would die without any other male heir;
The succession in the throne of Portugal to the offspring of Beatrice and her husband;
The succession in the throne of Portugal to John of Castille and his offspring of the first marriage, if Beatrice would die childless before him, but the Portuguese naturals (property owners) must to receive him as their king;
That queen Leonor Telles de Menezes should remain regent of the kingdom if Fernando I would die without any other heirs and while Beatrice didn't have a 14-year-old son;
And other less important clauses (included in Chronicle of king Fernando I, by Fernão Lopes, Chapter CLVII).
In the famous Cortes of Coimbra (1385), João das Regras, a lawyer supporting John Master of Aviz claims to the throne of Portugal, defended that due to the disrespect to the Treaty of Salvaterra clauses, Beatrice and John I of Castile should be excluded from the throne.