// Standalone tests for reasampler::readFileBytes — no REAPER, no framework.
// The ONE whole-file loader (Q-W1, T2-03) shared by both artifacts. Exercises
// the three-way contract: exact bytes back, empty on a missing file, empty on
// an empty file. Uses a scratch file in the test's working directory.

#include "../src/core/util/file_bytes.h"

#include <cstdio>
#include <fstream>
#include <string>
#include <vector>

using namespace reasampler;
using namespace reasampler::util;

static int g_fail = 0;
#define CHECK(cond) do { if(!(cond)) { \
    std::printf("FAIL line %d: %s\n", __LINE__, #cond); ++g_fail; } } while(0)

static const char* kScratch = "file_bytes_scratch.bin";

static void testReadsExactBytesBack() {
    // Binary content incl. NUL and 0xFF — the loader must be byte-transparent.
    const std::vector<std::uint8_t> payload = {0x00, 0x01, 0xFF, 0x7E, 0x00, 0x0A};
    {
        std::ofstream f(kScratch, std::ios::binary | std::ios::trunc);
        f.write(reinterpret_cast<const char*>(payload.data()),
                static_cast<std::streamsize>(payload.size()));
    }
    CHECK(readFileBytes(kScratch) == payload);
    std::remove(kScratch);
}

static void testMissingFileIsEmpty() {
    CHECK(readFileBytes("no_such_file_anywhere.bin").empty());
}

static void testEmptyFileIsEmpty() {
    { std::ofstream f(kScratch, std::ios::binary | std::ios::trunc); }
    CHECK(readFileBytes(kScratch).empty());
    std::remove(kScratch);
}

int main() {
    testReadsExactBytesBack();
    testMissingFileIsEmpty();
    testEmptyFileIsEmpty();

    if (g_fail == 0) std::printf("file_bytes: all tests passed\n");
    else             std::printf("file_bytes: %d CHECK(s) FAILED\n", g_fail);
    return g_fail == 0 ? 0 : 1;
}
