#include "acutest.h"
#include "vieter_package_internal.h"

void test_info_parse() {
    FILE *f = fopen("./test/package/.PKGINFO", "r");
	TEST_ASSERT_(f != NULL, "could not find test .PKGINFO file in ./test/package ");
	fseek(f, 0L, SEEK_END);
	size_t size = ftell(f);
	fflush(stdout);
	rewind(f);
	char *pkg_info_str = malloc(size + 1);
	fread(pkg_info_str, 1, size, f);
	pkg_info_str[size] = '\0';
	fclose(f);

	vieter_package_info *pkg_info = vieter_package_info_init();
	vieter_package_info_parse(pkg_info, pkg_info_str);

	TEST_CHECK(!strcmp(pkg_info->name, "xcursor-dmz"));
	TEST_CHECK(!strcmp(pkg_info->base, "xcursor-dmz"));
	TEST_CHECK(!strcmp(pkg_info->version, "0.4.5-2"));
	TEST_CHECK(!strcmp(pkg_info->description, "Style neutral, scalable cursor theme"));
	TEST_CHECK(!strcmp(pkg_info->url, "https://packages.debian.org/sid/dmz-cursor-theme"));
	TEST_CHECK(pkg_info->build_date == 1673751613);
	TEST_CHECK(!strcmp(pkg_info->packager, "Unknown Packager"));
	TEST_CHECK(pkg_info->size == 3469584);
	TEST_CHECK(!strcmp(pkg_info->arch, "any"));

	TEST_CHECK(!strcmp(pkg_info->licenses->array[0], "MIT"));
	TEST_CHECK(!strcmp(pkg_info->replaces->array[0], "test1"));
	TEST_CHECK(!strcmp(pkg_info->groups->array[0], "x11"));
	TEST_CHECK(!strcmp(pkg_info->conflicts->array[0], "test2"));
	TEST_CHECK(!strcmp(pkg_info->conflicts->array[1], "test3"));
	TEST_CHECK(!strcmp(pkg_info->provides->array[0], "test4"));
	TEST_CHECK(!strcmp(pkg_info->depends->array[0], "test5"));
	TEST_CHECK(!strcmp(pkg_info->depends->array[1], "test6"));
	TEST_CHECK(!strcmp(pkg_info->optdepends->array[0], "test7"));
	TEST_CHECK(!strcmp(pkg_info->makedepends->array[0], "xorg-xcursorgen"));
	TEST_CHECK(!strcmp(pkg_info->checkdepends->array[0], "test8"));

	free(pkg_info_str);
	vieter_package_info_free(pkg_info);
}

void test_pkg_read_archive_files() {
	vieter_package *pkg = vieter_package_init();
	vieter_package_error e = vieter_package_read_archive(pkg, "./test/package/xcursor-dmz-0.4.5-2-any.pkg.tar.zst");
	TEST_ASSERT_(e == vieter_package_ok, "failure parsing pkg archive");

	FILE *f = fopen("./test/package/files", "r");
	TEST_ASSERT_(f != NULL, "could not find test files file in ./test/package");
	char buff[128];
	size_t i = 0;

	while ((fgets(buff, 128, f)) != NULL || i < pkg->files->size) {
		if (buff[strlen(buff) - 1] == '\n') {
			buff[strlen(buff) - 1] = '\0';
		}

		TEST_CHECK_(!strcmp(pkg->files->array[i], buff), "%s != %s", pkg->files->array[i], buff);
		i++;
	}
	TEST_CHECK(pkg->compression = 14);

	vieter_package_free(&pkg);
}

void test_pkg_read_archive_desc() {
	vieter_package *pkg = vieter_package_init();
	vieter_package_error e = vieter_package_read_archive(pkg, "./test/package/xcursor-dmz-0.4.5-2-any.pkg.tar.zst");
	TEST_ASSERT_(e == vieter_package_ok, "failure parsing pkg archive");

	char *description = vieter_package_to_description(pkg);

	FILE *f = fopen("./test/package/desc", "r");
	TEST_ASSERT_(f != NULL, "could not find test desc file in ./test/package");
	fseek(f, 0, SEEK_END);
	size_t size = ftell(f);
	rewind(f);
	char *desc = malloc(size + 1);
	fread(desc, 1, size, f);
	desc[size] = '\0';
	fclose(f);

	TEST_CHECK(!strcmp(description, desc));

	vieter_package_free(&pkg);
	free(description);
	free(desc);
}

TEST_LIST = {
	{".PKGINFO parse", test_info_parse},
	{"files array creation", test_pkg_read_archive_files},
	{"desc file creation", test_pkg_read_archive_desc},
	{NULL, NULL}
};