The Soviet Union may have sent two major arms shipments to Nicaragua in as many weeks recently to test how the United States would react to steps to improve Nicaragua's defenses during President Reagan's second term, according to diplomats and other foreign observers here.

Washington sent the message loud and clear that it strongly opposed any further arms buildup by Nicaragua, even in weapons viewed here as predominantly defensive, the sources said. The U.S. response most likely reinforced Moscow's misgivings about providing Nicaragua with Mig warplanes, which would be widely viewed as offensive weapons. But it was uncertain whether the Soviets would balk at supplying additional defensive arms, they said.

Senior government officials said that the shipments -- which included Nicaragua's first sophisticated helicopter gunships -- were needed to step up the battle against antigovernment guerrillas right now as the coffee harvest is beginning. Coffee accounts for the bulk of Nicaragua's foreign exchange earnings, and the harvest is a major target of the rebels.

Details remain secret about the discussions between Moscow and Managua over Soviet arms supplies, but the two recent shipments appear to have reflected a compromise between Nicaragua's desire to obtain more weapons and Moscow's desire to avoid provoking a confrontation with the United States, several diplomats said. The net result was an raising of the stakes in deliveries of defensive arms without openly spitting in Washington's eye by importing warplanes.

A source close to Defense Minister Humberto Ortega said that this pattern was likely to continue in coming months. Nicaragua intends to obtain more helicopters, but it will not be getting any combat planes "for the moment," the official said.

"We don't need conventional air power. We need antiguerrilla air power. We need helicopters," he said.

The United States repeatedly has protested the Nicaraguan arms buildup and has suggested that it would attack Migs or possibly even less potent warplanes delivered here.

Nicaragua's left-wing government constantly has been pressing the Soviets to provide more aid to help it defend against antigovernment rebels organized and previously financed by the CIA, and against a possible direct U.S. attack, the diplomats said. The Nicaraguans recently sought more and better defensive weapons, and the Soviets were willing to supply them, in part because the rebels appear to be fighting better since the spring, these sources said.

But three characteristics of the recent shipments also led observers here to suggest that the Soviets were testing U.S. reactions:

* First, there were two sizable arms shipments in a row after nearly three months without one. A Bulgarian ship arrived in the last week of October, and a Soviet ship docked on Nov. 7.

* In addition, the shipments included Soviet-made Mi24 helicopter gunships and antiaircraft weapons with Fire Can radar systems that are more sophisticated than what the Nicaraguans had received previously.

* Finally, the shipments arrived at a particularly delicate moment: the first just before and the second just after elections in both Nicaragua and the United States.

"I'm convinced the Soviet boat was a tryout," a senior, non-U.S. diplomat said. "It was the first arms boat to come here directly from the Soviet Union, and it arrived the day after Reagan's reelection."

This view was not shared by all diplomats interviewed here. One senior envoy said: "There are solid military reasons for all this stuff. The Nicaraguans thought they could get away with Mi24 helicopters. I think that's a perfectly good explanation without the dramatic theory of the Russians testing or provoking Reagan."

Agrarian Reform Minister Jaime Wheelock, who also is one of the nine top commanders making up the Sandinista National Directorate, said the Mi24s were useful both for hunting the rebels in isolated mountainous areas unreachable by roads and for going after the DC3 airplanes based in Honduras that drop supplies to the rebels inside Nicaragua.

The Sandinistas say they would prefer to obtain Migs or smaller, Czechoslovak-made L39 combat planes to chase the rebels' supply planes. But military experts here agreed that the speedy, heavily armed Mi24 helicopters would be useful against the DC3s.

The arrival of the second Soviet ship drew world attention after Reagan administration sources leaked word that it might contain Migs, leading to fears that the United States might attack. The ship proved not to contain Migs, however, and the Sandinistas and most diplomats here said they believed Washington had exaggerated the reports to stir up anti-Nicaraguan feeling in the United States.

Several senior diplomats reiterated their previous analyses that the Soviets were not interested in provoking the United States by giving Migs to Nicaragua when the Kremlin has a much greater interest in cooperating with Reagan on issues such as arms control in the president's second term.

"I find it inconceivable that Chernenko and the Politburo want to begin relations with a second Reagan term in this way. Presumably they told the Nicaraguans: 'We're not going to give you everything you want, but we'll do something for you,' " a senior diplomat said.

The two recent arms shipments included between four and six Mi24 helicopters, according to U.S. and Sandinista officials. While diplomats here agreed that they were of great practical defensive use in fighting the rebels, the Mi24s also represent a significant technological advance over the dozen Soviet-made Mi8 helicopters that Nicaragua already has. The Mi8 is used primarily for ferrying passengers or cargo, while the Mi24 is an assault helicopter designed to attack enemy soldiers on the ground.

The two other principal items included in the arms shipments, according to foreign military sources, were clearly of a defensive nature: four patrol boats equipped as mine sweepers, and several 57-mm antiaircraft guns with Fire Can radar systems.

Nevertheless, the antiaircraft guns irritated U.S. officials because they, like the Mi24s, represented a technological advance. Previously Nicaragua only had 37-mm guns that were not equipped with radar.