167 lines
4.0 KiB
C++
167 lines
4.0 KiB
C++
#include "pipeline.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <pidir.h>
|
|
#include <pifile.h>
|
|
|
|
static const PIString testDir = "/tmp/pipeline-runner-test/";
|
|
static const PIString testPipelinesDir = testDir + "pipelines/";
|
|
static const PIString testLogsDir = testDir + "logs/";
|
|
|
|
static void setupDirs() {
|
|
PIDir::make(testDir);
|
|
PIDir::make(testPipelinesDir);
|
|
PIDir::make(testLogsDir);
|
|
}
|
|
|
|
static void cleanupDirs() {
|
|
// Remove all files first using their full path
|
|
PIDir pdir(testPipelinesDir);
|
|
for (const auto & e: pdir.entries()) {
|
|
if (e.isFile()) PIFile::remove(e.path);
|
|
}
|
|
PIDir ldir(testLogsDir);
|
|
for (const auto & e: ldir.entries()) {
|
|
if (e.isFile()) PIFile::remove(e.path);
|
|
}
|
|
PIDir::remove(testPipelinesDir);
|
|
PIDir::remove(testLogsDir);
|
|
PIDir::remove(testDir);
|
|
}
|
|
|
|
static Pipeline makeTestPipeline(const PIString & id = "test-id") {
|
|
Pipeline pl;
|
|
pl.id = id;
|
|
pl.name = "Test Pipeline";
|
|
pl.working_dir = "/tmp/workdir";
|
|
pl.created_at = nowISO();
|
|
pl.updated_at = pl.created_at;
|
|
|
|
Prompt p1;
|
|
p1.id = "prompt-1";
|
|
p1.text = "Do something";
|
|
p1.title = "Step 1";
|
|
p1.order = 0;
|
|
pl.prompts << p1;
|
|
|
|
Prompt p2;
|
|
p2.id = "prompt-2";
|
|
p2.text = "Do something else";
|
|
p2.title = "Step 2";
|
|
p2.order = 1;
|
|
pl.prompts << p2;
|
|
|
|
return pl;
|
|
}
|
|
|
|
TEST(PipelineTest, SaveAndLoad) {
|
|
setupDirs();
|
|
Pipeline pl = makeTestPipeline();
|
|
|
|
EXPECT_TRUE(savePipeline(testPipelinesDir, pl));
|
|
|
|
PIVector<Pipeline> loaded = loadPipelines(testPipelinesDir);
|
|
EXPECT_EQ(loaded.size(), 1);
|
|
EXPECT_EQ(loaded[0].id, pl.id);
|
|
EXPECT_EQ(loaded[0].name, pl.name);
|
|
EXPECT_EQ(loaded[0].working_dir, pl.working_dir);
|
|
EXPECT_EQ(loaded[0].prompts.size(), 2);
|
|
|
|
cleanupDirs();
|
|
}
|
|
|
|
TEST(PipelineTest, FindPipeline) {
|
|
setupDirs();
|
|
Pipeline pl = makeTestPipeline("find-me");
|
|
savePipeline(testPipelinesDir, pl);
|
|
|
|
Pipeline found = findPipeline(testPipelinesDir, "find-me");
|
|
EXPECT_FALSE(found.id.isEmpty());
|
|
EXPECT_EQ(found.id, "find-me");
|
|
EXPECT_EQ(found.name, "Test Pipeline");
|
|
|
|
Pipeline notFound = findPipeline(testPipelinesDir, "nonexistent");
|
|
EXPECT_TRUE(notFound.id.isEmpty());
|
|
|
|
cleanupDirs();
|
|
}
|
|
|
|
TEST(PipelineTest, DeletePipeline) {
|
|
setupDirs();
|
|
Pipeline pl = makeTestPipeline("del-me");
|
|
savePipeline(testPipelinesDir, pl);
|
|
|
|
EXPECT_TRUE(::deletePipeline(testPipelinesDir, "del-me"));
|
|
EXPECT_TRUE(findPipeline(testPipelinesDir, "del-me").id.isEmpty());
|
|
|
|
EXPECT_FALSE(::deletePipeline(testPipelinesDir, "nonexistent"));
|
|
|
|
cleanupDirs();
|
|
}
|
|
|
|
TEST(PipelineTest, MultiplePipelines) {
|
|
setupDirs();
|
|
|
|
Pipeline pl1 = makeTestPipeline("pl-1");
|
|
pl1.name = "Pipeline 1";
|
|
savePipeline(testPipelinesDir, pl1);
|
|
|
|
Pipeline pl2 = makeTestPipeline("pl-2");
|
|
pl2.name = "Pipeline 2";
|
|
savePipeline(testPipelinesDir, pl2);
|
|
|
|
PIVector<Pipeline> loaded = loadPipelines(testPipelinesDir);
|
|
EXPECT_EQ(loaded.size(), 2);
|
|
|
|
cleanupDirs();
|
|
}
|
|
|
|
TEST(PipelineTest, EmptyDir) {
|
|
setupDirs();
|
|
PIVector<Pipeline> loaded = loadPipelines(testPipelinesDir);
|
|
EXPECT_TRUE(loaded.isEmpty());
|
|
cleanupDirs();
|
|
}
|
|
|
|
TEST(PipelineTest, LogAppendAndRead) {
|
|
setupDirs();
|
|
|
|
appendLog(testLogsDir, "run-1", "First log line");
|
|
appendLog(testLogsDir, "run-1", "Second log line");
|
|
appendLog(testLogsDir, "run-2", "Different run");
|
|
|
|
PIString log1 = readLog(testLogsDir, "run-1");
|
|
EXPECT_TRUE(log1.contains("First log line"));
|
|
EXPECT_TRUE(log1.contains("Second log line"));
|
|
EXPECT_FALSE(log1.contains("Different run"));
|
|
|
|
PIString log2 = readLog(testLogsDir, "run-2");
|
|
EXPECT_TRUE(log2.contains("Different run"));
|
|
EXPECT_FALSE(log2.contains("First log line"));
|
|
|
|
PIString emptyLog = readLog(testLogsDir, "nonexistent");
|
|
EXPECT_TRUE(emptyLog.isEmpty());
|
|
|
|
cleanupDirs();
|
|
}
|
|
|
|
TEST(UtilTest, GenerateUUID) {
|
|
PIString uuid1 = generateUUID();
|
|
PIString uuid2 = generateUUID();
|
|
|
|
EXPECT_FALSE(uuid1.isEmpty());
|
|
EXPECT_NE(uuid1, uuid2);
|
|
|
|
// UUID format: 8-4-4-4-12
|
|
EXPECT_EQ(uuid1.find("-"), 8);
|
|
}
|
|
|
|
TEST(UtilTest, NowISO) {
|
|
PIString ts = nowISO();
|
|
EXPECT_FALSE(ts.isEmpty());
|
|
// ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ
|
|
EXPECT_EQ(ts.find("-"), 4);
|
|
EXPECT_TRUE(ts.contains("T"));
|
|
EXPECT_TRUE(ts.endsWith("Z"));
|
|
}
|