package dev.peerat.backend.userstories;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

import dev.peerat.backend.Main;
import dev.peerat.backend.WebClient;
import dev.peerat.framework.utils.json.JsonMap;

@TestInstance(Lifecycle.PER_METHOD)
public class GroupTests extends BaseUserStoriesTest{
	
	private Thread server;
	private WebClient client;
	
	@BeforeEach
	public void init() throws Exception{
		Class.forName("com.mysql.cj.jdbc.Driver");
		
		super.init();
		getRepository().init();
		getRepository().reset();
		
		server = new Thread(new Runnable(){
			@Override
			public void run(){
				try {
					Main.main(null);
				} catch (Exception e){
					e.printStackTrace();
				};
			}
		});
		server.start();
		client = new WebClient("localhost", 80);
		
//		System.out.println(Password.hash("password").withArgon2().getResult());
		
		try {
			client.auth("userTest", "password");
			client.assertResponseCode(200);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@AfterEach
	public void stop(){
		server.interrupt();
	}

	@Test
	void createNormalGroup() throws Exception{
		JsonMap json = new JsonMap();
		json.set("name", "Group_test");
		json.set("chapter", 1);
		client.route("/groupCreate", "POST", json.toString());
		client.assertResponseCode(200);
	}
	
	@Test
	void leaveNormalGroup() throws Exception{
		createNormalGroup();
		JsonMap json = new JsonMap();
		json.set("name", "Group_test");
		json.set("chapter", 1);
		client.route("/groupQuit", "POST", json.toString());
		client.assertResponseCode(200);
	}
	
	@Test
	void joinNormalGroup() throws Exception{
		leaveNormalGroup();
		JsonMap json = new JsonMap();
		json.set("name", "Group_test");
		json.set("chapter", 1);
		client.route("/groupJoin", "POST", json.toString());
		client.assertResponseCode(200);
	}
}
