/*
Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Backend
Copyright (C) 2024 Christian Fraß

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see 
<https://www.gnu.org/licenses/>. 
 */

const nm_fs = require("fs");
const nm_assert = require("assert");

type type_test_section = {
	cases : Array<any>;
};

const _data : Record<string, type_test_section> = lib_plankton.json.decode(nm_fs.readFileSync("test-data.json"));


describe(
	"service.member.validate_password",
	() => {
		const data : {
			cases : Array<
				{
					name : string;
					input : {
						conf : any;
						password : string;
					};
					output : Array<
						{
							incident : string;
							details : Record<string, any>;
						}
					>;
				}
			>;
		} = _data["service.member.validate_password"];
		data.cases.forEach(
			case_ => {
				it(
					case_.name,
					() => {
						_espe.conf.inject(case_.input.conf);
						const result : Array<
							{
								incident : string;
								details : Record<string, any>;
							}
						> = _espe.service.member.validate_password(case_.input.password);
						nm_assert.deepEqual(result, case_.output);
						_espe.conf.inject({});
					}
				);
			}
		);
	}
);
describe(
	"service.member.name_login",
	() => {
		const data : {
			cases : Array<
				{
					name : string;
					input : {
						name_real_value : string;
						name_real_index : int;
					};
					output : string;
				}
			>;
		} = _data["service.member.name_login"];
		data.cases.forEach(
			case_ => {
				it(
					case_.name,
					() => {
						const member_object : _espe.type.member_object = {
							"membership_number": "",
							"name_real_value": case_.input.name_real_value,
							"name_real_index": case_.input.name_real_index,
							"email_address_private": "",
							"registered": false,
							"enabled": false,
							"email_use_veiled_address": false,
							"email_use_nominal_address": false,
							"email_redirect_to_private_address": false,
							"email_allow_sending": false,
							"password_image": null,
						};
						const result : string = _espe.service.member.name_login(member_object);
						nm_assert.equal(result, case_.output);
					}
				);
			}
		);
	}
);
describe(
	"service.member.export_authelia_user_file",
	() => {
		const data : {
			cases : Array<
				{
					name : string;
					input : {
						conf : any;
						members : Array<
							{
								membership_number : string;
								name_real_value : string;
								name_real_index : int;
								email_address_private : (null | string);
								registered : boolean;
								enabled : boolean;
								email_use_veiled_address : boolean;
								email_use_nominal_address : boolean;
								email_redirect_to_private_address : boolean;
								email_allow_sending : boolean;
								password_image : (null | string);
							}
						>;
					};
					output : string;
				}
			>;
		} = _data["service.member.export_authelia_user_file"];
		data.cases.forEach(
			case_ => {
				it(
					case_.name,
					async () => {
						// setup
						_espe.conf.inject(case_.input.conf);
						
						// execution
						const result : string = await _espe.service.member.export_authelia_user_file(
							{
								"custom_data": case_.input.members.map(
									member_raw => (
										{
											"membership_number": member_raw.membership_number,
											"name_real_value": member_raw.name_real_value,
											"name_real_index": member_raw.name_real_index,
											"email_address_private": member_raw.email_address_private,
											"registered": member_raw.registered,
											"enabled": member_raw.enabled,
											"email_use_veiled_address": member_raw.email_use_veiled_address,
											"email_use_nominal_address": member_raw.email_use_nominal_address,
											"email_redirect_to_private_address": member_raw.email_redirect_to_private_address,
											"email_allow_sending": member_raw.email_allow_sending,
											"password_image": member_raw.password_image,
										}
									)
								),
							}
						);
						
						// assertions
						nm_assert.equal(result, case_.output);
						
						// cleanup
						_espe.conf.inject({});
					}
				);
			}
		);
	}
);
