<?php

namespace Tests\Feature;

use Tests\TestCase;

use App\Classes\File;
use App\Classes\FTN\Packet;
use App\Models\{Address,Domain,System,Zone};

class PacketTest extends TestCase
{
	private System $so;
	private Domain $do;

	private function init()
	{
		System::unguard();
		Domain::unguard();
		$this->so = System::firstOrCreate(['name'=>'test','sysop'=>'sysop','location'=>'location','active'=>TRUE]);
		$this->do = Domain::firstOrCreate(['name'=>'packets','active'=>TRUE]);
	}

	public function test_null()
	{
		return $this->assertTrue(TRUE);
	}

	/*
	 * @todo This packet doesnt have an origin line, need a new one with nomsg and an origin line
	public function test_nomsgid_origin()
	{
		$this->init();

		Zone::unguard();
		Address::unguard();
		$zo = Zone::firstOrCreate(['zone_id'=>21,'default'=>TRUE,'active'=>TRUE,'domain_id'=>$this->do->id,'system_id'=>$this->so->id]);
		$src = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>3,'node_id'=>151,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);
		$hub = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>1,'node_id'=>1,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);
		$ao = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>1,'node_id'=>4,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);

		// This packet has an incorrect zone in the Origin
		$f = new File(__DIR__.'/data/test_nomsgid_origin.pkt');
		foreach ($f as $packet) {
			$pkt = Packet::process($packet,$f->itemName(),$f->itemSize());

			$this->assertEquals(1,$pkt->count());

			$messages = FALSE;
			foreach ($pkt as $msg) {
				$messages = TRUE;
				$this->assertNotTrue($msg->isNetmail());
				$this->assertNotFalse($msg->pathaddress->search($hub->id));
				$this->assertCount(0,$msg->rogue_seenby);
			}

			$this->assertTrue($messages);
		}
	}
	*/

	/*
	 * @todo We are not correctly setup to parse messages without an origin line
	public function test_nomsgid_noorigin()
	{
		$this->init();

		Zone::unguard();
		Address::unguard();
		$zo = Zone::firstOrCreate(['zone_id'=>10,'default'=>TRUE,'active'=>TRUE,'domain_id'=>$this->do->id,'system_id'=>$this->so->id]);
		$src = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>999,'node_id'=>1,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);

		// This packet has no Origin Line
		$f = new File(__DIR__.'/data/test_nomsgid_noorigin.pkt');
		foreach ($f as $packet) {
			$pkt = Packet::process($packet,$f->itemName(),$f->itemSize(),$this->do);

			$this->assertEquals(1,$pkt->count());

			$messages = FALSE;
			foreach ($pkt as $msg) {
				$messages = TRUE;
				$this->assertNotTrue($msg->isNetmail());
				$this->assertNotFalse($msg->path->search('1/1 999/1'));
				$this->assertNotFalse($msg->seenby->search('1/1 4 3/0 999/1 999'));
			}

			$this->assertTrue($messages);
		}
	}
	*/

	/*
	public function test_msgid_origin()
	{
		$this->init();

		Zone::unguard();
		Address::unguard();
		$zo = Zone::firstOrCreate(['zone_id'=>10,'default'=>TRUE,'active'=>TRUE,'domain_id'=>$this->do->id,'system_id'=>$this->so->id]);
		$src = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>999,'node_id'=>1,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);

		// This packet has an incorrect zone in the Origin
		$f = new File(__DIR__.'/data/test_msgid_origin.pkt');
		foreach ($f as $packet) {
			$pkt = Packet::process($packet,$f->itemName(),$f->itemSize(),$this->do);

			$this->assertEquals(1,$pkt->count());

			$messages = FALSE;
			foreach ($pkt as $msg) {
				$messages = TRUE;
				$this->assertNotTrue($msg->isNetmail());
				$this->assertNotFalse($msg->seenby->search('1/1 999/1 999'));
			}

			$this->assertTrue($messages);
		}
	}

	public function test_packet_parse()
	{
		$this->init();

		$zo = Zone::firstOrCreate(['zone_id'=>21,'default'=>TRUE,'active'=>TRUE,'domain_id'=>$this->do->id,'system_id'=>$this->so->id]);

		// This packet has a SOH<char>SOH sequence
		$f = new File(__DIR__.'/data/test_binary_content-2.pkt');
		foreach ($f as $packet) {
			$pkt = Packet::process($packet,$f->itemName(),$f->itemSize(),$zo->domain);

			$this->assertEquals(1,$pkt->count());

			$messages = FALSE;
			foreach ($pkt as $msg) {
				$messages = TRUE;

				$this->assertNotTrue($msg->isNetmail());
				$this->assertSame('21:1/151 6189F64C',$msg->msgid);
				$this->assertSame('db727bd3778ddd457784ada4bf016010',md5($msg->message));
				$this->assertSame('5b627ab5936b0550a97b738f4deff419',md5($msg->message_src));
				$this->assertContains('3/2744 4/100 106 5/100',$msg->seenby);
				$this->assertContains('1/151 100 3/100',$msg->path);
				$this->assertCount(11,$msg->seenby);
				$this->assertCount(0,$msg->unknown);
			}

			$this->assertTrue($messages);
		}

		// This packet has SOH in the message content
		$f = new File(__DIR__.'/data/test_binary_content.pkt');
		foreach ($f as $packet) {
			$pkt = Packet::process($packet,$f->itemName(),$f->itemSize());

			$this->assertEquals(1,$pkt->count());

			$messages = FALSE;
			foreach ($pkt as $msg) {
				$messages = TRUE;

				$this->assertNotTrue($msg->isNetmail());
				$this->assertSame('21:1/126 eec6e958',$msg->msgid);
				$this->assertSame('5a525cc1c393292dc65160a852d4d615',md5($msg->message));
				$this->assertSame('a3193edcc68521d4ed07da6db2aeb0b6',md5($msg->message_src));
				$this->assertContains('1/995 2/100 116 1202 3/100 105 107 108 109 110 111 112 113 117 119',$msg->seenby);
				$this->assertContains('1/126 100 3/100',$msg->path);
				$this->assertCount(10,$msg->seenby);
				$this->assertCount(0,$msg->unknown);
			}

			$this->assertTrue($messages);
		}

		// This packet has an incorrect zone in the Origin
		$f = new File(__DIR__.'/data/test_msgid_origin.pkt');
		foreach ($f as $packet) {
			$pkt = Packet::process($packet,$f->itemName(),$f->itemSize());

			$this->assertEquals(1,$pkt->count());

			$messages = FALSE;
			foreach ($pkt as $msg) {
				$messages = TRUE;

				$this->assertNotTrue($msg->isNetmail());
				$this->assertSame('10:999/1 612aecda',$msg->msgid);
				$this->assertSame('61078e680cda04c8b5eba0f712582e70',md5($msg->message));
				$this->assertSame('b9d65d4f7319ded282f3f1986276ae79',md5($msg->message_src));
				$this->assertContains('1/1 999/1 999',$msg->seenby);
				$this->assertContains('999/1',$msg->path);
				$this->assertCount(1,$msg->seenby);
				$this->assertCount(0,$msg->unknown);
			}

			$this->assertTrue($messages);
		}
	}

	public function test_soh_in_origin()
	{
		$this->init();

		Zone::unguard();
		Address::unguard();

		$zo = Zone::firstOrCreate(['zone_id'=>3,'default'=>TRUE,'active'=>TRUE,'domain_id'=>$this->do->id,'system_id'=>$this->so->id]);
		$src = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>712,'node_id'=>886,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);

		// This packet has a SOH<char>SOH sequence
		$f = new File(__DIR__.'/data/test_msg_with_soh_in_origin.pkt');

		foreach ($f as $packet) {
			$pkt = Packet::process($packet,$f->itemName(),$f->itemSize());

			$this->assertEquals(9,$pkt->count());

			$messages = FALSE;
			$c = 0;
			foreach ($pkt as $msg) {
				$c++;
				$messages = TRUE;

				$this->assertNotTrue($msg->isNetmail());

				switch ($c) {
					case 1:
						$this->assertSame('3:712/886 220da89f',$msg->msgid);
						$this->assertSame('9f5544bea46ef57a45f561b9e07dd71e',md5($msg->message));
						$this->assertSame('9bf4b8c348ac235cc218577abf7140af',md5($msg->message_src));
						$this->assertContains('633/0 267 280 281 408 410 412 509 509 640/1384 712/114 550 620 848',$msg->seenby);
						$this->assertContains('712/886 848 633/280',$msg->path);
						$this->assertCount(2,$msg->seenby);
						$this->assertCount(0,$msg->unknown);
						break;

					case 4:
						$this->assertSame('',$msg->msgid);
						$this->assertSame('b975057002def556c5a9497aacd000fb',md5($msg->message));
						$this->assertSame('c90dd234d2aa029af22c453a25b79a4e',md5($msg->message_src));
						$this->assertContains('633/267 280 281 384 408 410 412 418 420 509 509 712/848 770/1 100 330',$msg->seenby);
						$this->assertContains('772/210 770/1 633/280',$msg->path);
						$this->assertCount(2,$msg->seenby);
						$this->assertCount(0,$msg->unknown);

						default:
							continue 2;
				}
			}

			$this->assertTrue($messages);
		}
	}
	*/
}