Files
Ange-Marie MAURIN - A2M Systèmes 832bd52fc9 feat(studio/host): brique supervisor, cycle de vie du Core
En-têtes repris d'un subagent interrompu, implémentation et tests écrits ensuite : CoreProcess (CreateProcessW avec héritage limité au journal, citation compatible CommandLineToArgvW, journal en ajout ; posix_spawn sous POSIX, non testé), is_process_alive, fichier d'exécution atomique, copies fantômes et nettoyage, read_last_lines, FileWatcher. 15 cas (Debug, ASan) avec un processus enfant de test.
2026-11-06 23:06:34 +01:00

103 lines
3.0 KiB
C++

// SPDX-License-Identifier: MPL-2.0
#include "sequa/studio/host/files.hpp"
#include "sequa/studio/host/random.hpp"
#include "sequa/studio/host/time.hpp"
#include "temp_dir.hpp"
#include <doctest/doctest.h>
#include <chrono>
#include <fstream>
#include <string>
#include <thread>
using namespace sequa::studio::host;
using namespace std::chrono_literals;
namespace {
void write(const std::filesystem::path& path, const std::string& content)
{
std::ofstream file(path, std::ios::binary | std::ios::trunc);
file << content;
}
} // namespace
TEST_CASE("random_hex : longueur, alphabet, unicite")
{
const auto a = random_hex(16);
const auto b = random_hex(16);
REQUIRE(a.has_value());
REQUIRE(b.has_value());
CHECK(a->size() == 32);
CHECK(a->find_first_not_of("0123456789abcdef") == std::string::npos);
CHECK(*a != *b);
CHECK(random_hex(0) == std::string());
}
TEST_CASE("Horodatages UTC")
{
const std::string iso = utc_now_iso8601();
CHECK(iso.size() == 20);
CHECK(iso[4] == '-');
CHECK(iso[10] == 'T');
CHECK(iso.back() == 'Z');
const std::string compact = utc_now_compact();
CHECK(compact.size() == 16);
CHECK(compact[8] == 'T');
CHECK(compact.back() == 'Z');
}
TEST_CASE("read_last_lines : fin de fichier, CRLF, fichier absent")
{
testing::TempDir dir;
const auto path = dir.path() / "journal.log";
write(path, "un\r\ndeux\ntrois\r\nquatre\n");
const auto last = read_last_lines(path, 2);
REQUIRE(last.size() == 2);
CHECK(last[0] == "trois");
CHECK(last[1] == "quatre");
CHECK(read_last_lines(path, 10).size() == 4);
CHECK(read_last_lines(dir.path() / "absent.log", 5).empty());
}
TEST_CASE("read_last_lines : gros fichier lu seulement par la fin")
{
testing::TempDir dir;
const auto path = dir.path() / "gros.log";
{
std::ofstream file(path, std::ios::binary);
for (int i = 0; i < 200000; ++i) {
file << "ligne numero " << i << "\n";
}
}
const auto last = read_last_lines(path, 3);
REQUIRE(last.size() == 3);
CHECK(last[2] == "ligne numero 199999");
}
TEST_CASE("FileWatcher : changement signale seulement apres stabilite, une fois")
{
testing::TempDir dir;
const auto path = dir.path() / "sequa-core.exe";
write(path, "v1");
FileWatcher watcher(path, 150ms);
CHECK_FALSE(watcher.poll()); // état initial : rien de nouveau
write(path, "version 2 plus longue");
CHECK_FALSE(watcher.poll()); // changé mais pas encore stable
std::this_thread::sleep_for(60ms);
write(path, "version 3, encore en cours d'ecriture");
CHECK_FALSE(watcher.poll()); // nouvelle modification : le compteur repart
std::this_thread::sleep_for(200ms);
CHECK(watcher.poll());
CHECK_FALSE(watcher.poll()); // une seule fois par changement
std::filesystem::remove(path);
CHECK_FALSE(watcher.poll());
std::this_thread::sleep_for(200ms);
CHECK_FALSE(watcher.poll()); // une suppression n'est pas un nouvel exécutable
}