import * as LogAbstract from "@terrencecrowley/logabstract";
import * as Context from "@terrencecrowley/context";

interface TestEnv { context: Context.IContext, log: LogAbstract.ILog };

import * as OT from "../lib/ottypes";
import * as OTC from "../lib/otcomposite";
import * as OTA from "../lib/otarray";
import * as fs from 'fs';
import { testutil as testutil } from './testutil'

export namespace test {

	// function: test_compose
	//
	// Description:
	//	Takes a JSON file containing an array of OTCompositeResource.
	//	For each unit of three, composes the first two and compares with the final to see if they match
	//

	export function test_compose(env: TestEnv): void
		{
			env.log.log("===compose Tests===");
			let s: string = fs.readFileSync(testutil.find_data_path('compose.json'), 'utf8');
			let o: any = JSON.parse(testutil.strip_comments(s));
			let a: OTC.OTCompositeResource[] = [];
			for (let i = 0; i < o['tests'].length; i++)
				a[i] = OTC.OTCompositeResource.constructFromObject((o['tests'])[i]);

			let nFail: number = 0;
			let nTests: number = a.length / 3;
			for (let i = 0; i < a.length; i += 3)
			{
				let a1: OTC.OTCompositeResource = a[i].copy();
				let a2: OTC.OTCompositeResource = a[i+1].copy();
				let r1: OTC.OTCompositeResource = a[i+2].copy();
				let n: number = i / 3;

				a1.compose(a2);
				let bOK: boolean = a1.effectivelyEqual(r1);
				if (! bOK)
					env.log.log("composeTest: " + n + (bOK ? " success" : " failure"));
				if (!bOK)
				{
					nFail++;
					env.log.log("\tActual: " + JSON.stringify(a1), 1);
					env.log.log("\t  Goal: " + JSON.stringify(r1), 1);
				}
			}
			if (nFail > 0)
				env.log.log("compose Tests: " + (nTests - nFail) + " of " + nTests + " successful.");
			else
				env.log.log("compose Tests: " + String(nTests) + " successful.");
		}

	export function test_compose_random(env: TestEnv): void
		{
			const NTests: number = 1000;
			const InitStringLength: number = 12;
			let ops = new OTA.OTStringOperations();

			env.log.log("===Random compose Tests===");
			let nFail: number = 0;
			for (let i: number = 0; i < NTests; i++)
			{
				let a1: OTC.OTCompositeResource = new OTC.OTCompositeResource('1', '0');
				let sEdit1 = new OTA.OTStringResource("1");
				a1.edits.push(sEdit1);
				let a2: OTC.OTCompositeResource = new OTC.OTCompositeResource('1', '1');
				let sEdit2 = new OTA.OTStringResource("1");
				a2.edits.push(sEdit2);
				let c1: OTC.OTCompositeResource;

				sEdit1.generateRandom(InitStringLength, a1.clientID);
				sEdit2.generateRandom(sEdit1.finalLength(), a2.clientID);
				c1 = a1.copy();
				c1.compose(a2);
				let sEditC: OTA.OTStringResource = c1.edits[0] as OTA.OTStringResource;
				let bFail: boolean = sEditC.originalLength() != sEdit1.originalLength() || sEditC.finalLength() != sEdit2.finalLength();
				if (bFail)
				{
					env.log.log(i + ": structural failure");
					nFail++;
				}
				else
				{
					let arrRes1 = [ ops.constructN(InitStringLength) ];
					let arrRes2 = arrRes1.slice();
					arrRes1 = a1.apply(arrRes1);
					arrRes1 = a2.apply(arrRes1);
					arrRes2 = c1.apply(arrRes2);
					bFail = arrRes1[0] !== arrRes2[0];
					if (bFail)
					{
						env.log.log(i + ": logical failure");
						nFail++;
					}
				}
				if (bFail && (nFail == 1 || sEdit1.length+sEdit2.length < 8))
				{
					env.log.log(JSON.stringify(a1) + ",", 1);
					env.log.log(JSON.stringify(a2) + ",", 1);
					env.log.log(JSON.stringify(c1) + ",", 1);
					if (nFail > 1)
						break; // temporary - break out on second failure rather than being inundated with dups
				}
			}
			if (nFail > 0)
				env.log.log("Random compose Tests: " + (NTests - nFail) + " of " + NTests + " successful.");
			else
				env.log.log("Random compose Tests: all successful.");
		}

	export function tests(): Array<(env: TestEnv) => void> {
		let a = [ test.test_compose, test.test_compose_random ];
		return a;
	}
};
