'use strict';

const tap = require('tap');

const mediasoup = require('../');

tap.test('transport.createAssociatedTransport() must succeed', { timeout: 1000 }, (t) =>
{
	let server = mediasoup.Server();

	t.tearDown(() => server.close());

	let room = server.Room();
	let peer = room.Peer('alice');

	peer.createTransport({ tcp: false })
		.then((transport) =>
		{
			t.pass('peer.createTransport() succeeded');
			t.equal(transport.iceComponent, 'RTP', 'transport must have "RTP" iceComponent');

			transport.iceLocalCandidates.forEach((candidate) =>
			{
				if (candidate.protocol !== 'udp')
					t.fail('there should be just "udp" candidates');
			});

			transport.createAssociatedTransport()
				.then((associatedTransport) =>
				{
					t.pass('transport.createAssociatedTransport() succeeded');
					t.equal(associatedTransport.iceComponent, 'RTCP', 'associated transport must have "RTCP" iceComponent');

					associatedTransport.iceLocalCandidates.forEach((candidate) =>
					{
						t.equal(candidate.protocol, 'udp', 'there should be just "udp" candidates');
					});

					peer.dump()
						.then((data) =>
						{
							t.pass('peer.dump() succeeded');
							t.equal(Object.keys(data.transports).length, 2, 'peer.dump() should retrieve two transports');
							t.end();
						})
						.catch((error) => t.fail(`peer.dump() failed: ${error}`));
				})
				.catch((error) => t.fail(`transport.createAssociatedTransport() failed: ${error}`));
		})
		.catch((error) => t.fail(`peer.createTransport failed: ${error}`));
});

tap.test('transport.close() must succeed', { timeout: 1000 }, (t) =>
{
	let server = mediasoup.Server();

	t.tearDown(() => server.close());

	let room = server.Room();
	let peer = room.Peer('alice');

	peer.createTransport({ tcp: false })
		.then((transport) =>
		{
			transport.on('close', (error) =>
			{
				t.error(error, 'transport should close cleanly');

				peer.dump()
					.then((data) =>
					{
						t.pass('peer.dump() succeeded');
						t.equal(Object.keys(data.transports).length, 0, 'peer.dump() should retrieve zero transports');
						t.end();
					})
					.catch((error) => t.fail(`peer.dump() failed: ${error}`));
			});

			setTimeout(() => transport.close(), 100);
		})
		.catch((error) => t.fail(`peer.createTransport() failed: ${error}`));
});

@@ -608,52 +608,116 @@
 l('t
-here should be just %22udp%22 candidates');%0A%09%09%09%7D
+ransport contains unexpected %22tcp%22 candidates');%0A%09%09%09%7D);%0A%09%09%09t.pass('transport just contains %22udp%22 candidates'
 );%0A%0A
@@ -1053,23 +1053,19 @@
 %7B%0A%09%09%09%09%09%09
-t.equal
+if 
 (candida
@@ -1079,65 +1079,170 @@
 ocol
-,
+ !==
  'udp'
-, 'there should be just %22udp%22 candidates');%0A%09%09%09%09%09%7D
+)%0A%09%09%09%09%09%09%09t.fail('associated transport contains unexpected %22tcp%22 candidates');%0A%09%09%09%09%09%7D);%0A%09%09%09%09%09t.pass('associated transport just contains %22udp%22 candidates'
 );%0A%0A

Improve test_transport.js.