/*
 * Copyright © 2020-2026 Pierrick Caillon <pierrick.caillon+zipdirfs@megami.fr>
 */
#include "FileTime.h"
#include "ZipDirFs/Utilities/FileTime.h"
#include "test/gtest.h"
#include <chrono>
#include <filesystem>
#include <ctime>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace Test::ZipDirFs::Utilities
{
	using ::ZipDirFs::Utilities::FileTime;
	namespace fs = std::filesystem;

	static std::time_t getRandTime() { return ::Test::rand(631152000L, 3221225471); }

	TEST_F(FileTimeTest, FromFileTimeType)
	{
		const auto expectedTime = getRandTime();
		const FileTime expectedFileTime{expectedTime};
		EXPECT_EQ(FileTime{::Test::to_file_time(expectedTime)}, expectedFileTime);
	}

	TEST_F(FileTimeTest, ToTimeTRoundTripWithSubSecondDelta)
	{
		using namespace std::chrono;
		const auto expectedTime = getRandTime();
		const auto fsStart = ::Test::to_file_time(expectedTime) + milliseconds(789);
		const auto fileTime = FileTime{fsStart};
		const auto fsBack = ::Test::to_file_time(std::time_t{fileTime});

		EXPECT_EQ(std::time_t{fileTime}, expectedTime);
		EXPECT_EQ(duration_cast<milliseconds>(fsStart - fsBack).count(), 789);
	}
} // namespace Test::ZipDirFs::Utilities
