#include "Misc/AutomationTest.h"

#include "HyperTwistRecognition/HyperTwistSpeechClient.h"
#include "HyperTwistRecognition/HyperTwistVisionClient.h"
#include "HyperTwistRecognition/HyperTwistVoiceClient.h"

#if WITH_DEV_AUTOMATION_TESTS

IMPLEMENT_SIMPLE_AUTOMATION_TEST(
	FHyperTwistHttpClientConfigurationTest,
	"HyperTwist.Recognition.HttpClients.DefaultConfiguration",
	EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter
)

bool FHyperTwistHttpClientConfigurationTest::RunTest(const FString& Parameters)
{
	static_cast<void>(Parameters);

	const UHyperTwistHttpVisionClient* VisionClient = GetDefault<UHyperTwistHttpVisionClient>();
	const UHyperTwistHttpSpeechClient* SpeechClient = GetDefault<UHyperTwistHttpSpeechClient>();
	const UHyperTwistHttpVoiceClient* VoiceClient = GetDefault<UHyperTwistHttpVoiceClient>();

	TestNotNull(TEXT("The default vision client must be available."), VisionClient);
	TestNotNull(TEXT("The default speech client must be available."), SpeechClient);
	TestNotNull(TEXT("The default voice client must be available."), VoiceClient);
	if (VisionClient == nullptr || SpeechClient == nullptr || VoiceClient == nullptr)
	{
		return false;
	}

	const auto TestServiceUrl = [this](const TCHAR* Label, const FString& ServiceBaseUrl)
	{
		TestTrue(
			FString::Printf(TEXT("%s must retain a complete HTTP service URL after config parsing."), Label),
			ServiceBaseUrl.StartsWith(TEXT("http://"), ESearchCase::IgnoreCase)
				|| ServiceBaseUrl.StartsWith(TEXT("https://"), ESearchCase::IgnoreCase)
		);
		TestFalse(
			FString::Printf(TEXT("%s must not contain a collapsed single-slash scheme."), Label),
			ServiceBaseUrl.StartsWith(TEXT("http:/"), ESearchCase::IgnoreCase)
				&& !ServiceBaseUrl.StartsWith(TEXT("http://"), ESearchCase::IgnoreCase)
		);
	};

	TestServiceUrl(TEXT("VisionClient.ServiceBaseUrl"), VisionClient->ServiceBaseUrl);
	TestServiceUrl(TEXT("SpeechClient.ServiceBaseUrl"), SpeechClient->ServiceBaseUrl);
	TestServiceUrl(TEXT("VoiceClient.ServiceBaseUrl"), VoiceClient->ServiceBaseUrl);
	return true;
}

#endif
