<?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_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(),NULL,FALSE);

			$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(1,$msg->rogue_path);
				$this->assertNotFalse($msg->rogue_path->search('21:999/1'));

				$this->assertCount(3,$msg->rogue_seenby);
				$this->assertNotFalse($msg->rogue_seenby->search('21:999/1'));
				$this->assertNotFalse($msg->rogue_seenby->search('21:999/999'));
			}

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

	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 an incorrect zone in the Origin
		$f = new File(__DIR__.'/data/test_nomsgid_noorigin.pkt');
		foreach ($f as $packet) {
			$pkt = Packet::process($packet,$f->itemName(),$f->itemSize(),$this->so,FALSE);

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

			$messages = FALSE;
			foreach ($pkt as $msg) {
				$messages = TRUE;
				$this->assertNotTrue($msg->isNetmail());

				$this->assertCount(1,$msg->rogue_path);
				$this->assertNotFalse($msg->rogue_path->search('10:1/1'));

				$this->assertCount(0,$msg->rogue_seenby);
			}

			$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->so,FALSE);

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

			$messages = FALSE;
			foreach ($pkt as $msg) {
				$messages = TRUE;
				$this->assertNotTrue($msg->isNetmail());
				$this->assertCount(0,$msg->rogue_path);

				$this->assertCount(1,$msg->rogue_seenby);
				$this->assertNotFalse($msg->rogue_seenby->search('10:1/1'));

				$this->assertNotFalse($msg->seenaddress->search($src->id));
			}

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

	public function test_packet_parse()
	{
		// 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(),NULL,FALSE);

			$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('a8791fd3d261734bb524bc5ed929aa4c',md5($msg->message));
				$this->assertSame('5b627ab5936b0550a97b738f4deff419',md5($msg->message_src));
				$this->assertCount(3,$msg->rogue_path);
				$this->assertCount(170,$msg->rogue_seenby);
				$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(),NULL,FALSE);

			$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('c0b00abfc3eff7e297bf14f5812a7261',md5($msg->message));
				$this->assertSame('a3193edcc68521d4ed07da6db2aeb0b6',md5($msg->message_src));
				$this->assertCount(3,$msg->rogue_path);
				$this->assertCount(161,$msg->rogue_seenby);
				$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(),NULL,FALSE);

			$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->assertCount(1,$msg->pathaddress);
				$this->assertCount(1,$msg->rogue_seenby);
				$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);
		}
	}
}