Ex-CIA agent Edwin P. Wilson was "literally roaring with laughter" after he and several associates successfully shipped an automatic rifle to Libya in 1979 as a sample of weapons Wilson could supply the regime, a former Wilson aide testified yesterday.

"He [Wilson] was very, very happy," said the aide, Peter R. Goulding. Goulding said Wilson handed the weapon, an M16 rifle shipped secretly from Washington, to a Libyan official who gave it "a full function test out the window of his office . . . a full clip of 30 rounds."

Federal prosecutors charged that Wilson, whose trial on illegal firearms charges opened yesterday in U.S. District Court in Alexandria, subsequently won a $22 million contract to supply arms to the Libyan army after furnishing the rifle and four handguns as samples.

The 54-year-old Wilson, who was lured out of Libya and arrested last summer in an elaborate plan by U.S. authorities, faces separate trials later this month in Texas and Washington on charges growing out of a six-year investigation of illegal arms dealings and conspiracy to murder.

Wilson is in custody under $60 million bond. An alleged Wilson confederate, former CIA employe Francis E. Terpil, is still a fugitive.

Assistant U.S. Attorney Theodore S. Greenberg said yesterday in an opening statement that Wilson was "motivated by greed" and was "desperately" trying to get an M-16 to Libya. "It was his overriding preoccupation," in 1978 and '79, Greenberg said.

According to Goulding, Wilson boasted the weapon was "the most expensive M16 in the world" and that it had cost him nearly $40,000 to ship it clandestinely to the North African country.

Defense lawyer Herald Price Fahringer told the jury that Wilson's business activities served instead as cover for intelligence gathering beneficial to the CIA.

Wilson, an agent for the CIA and then for a secret naval intelligence group until the mid-1970s, was furnishing vital data on Soviet Union military equipment and Libyan intelligence to American officials at the time of the alleged weapons offenses, Fahringer said.

He said successful arms dealing was crucial to Wilson to "sustain his position" in Libya and that by 1978 the Libyans had agreed to release Soviet equipment "the CIA was very interested in getting" if Wilson could supply arms in return.

The lawyer said Wilson was in contact with two officials then at the CIA, Theodore Shackley and Thomas Clines, and a senior Pentagon officer, Maj. Gen. Richard V. Secord. "Requests were made to him Wilson whether he could get certain Russian equipment" for the Americans, according to Fahringer.

Under cross-examination by Fahringer, a former Wilson employe, Roberta Barnes, testified Wilson told her that during a visit to Libya in early 1981 he had learned Libya had acquired "the equipment and know-how to build an atomic bomb."

Barnes was cut off by District Judge Richard L. Williams, who agreed with the government that such testimony may be raised later in the trial by the defense.

Another former Wilson employe, Wallace L. Klink, testified yesterday that he traveled to Fayetteville, N.C., to buy four handguns also sought by Wilson. The handguns were ferried via Dulles International Airport to Bonn, where they were turned over a Libyan intelligence officer, according to prosecution witnesses.

Paul Cyr, a former Energy Department employe and longtime friend of Wilson, testified he furnished the M-16 at Wilson's request. The defense contended, however, that the rifle was inoperative.