From 2fad569ab64c07a202c45df546fefd879dc8f78c Mon Sep 17 00:00:00 2001
From: Svjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Date: Thu, 30 May 2024 22:58:49 +0300
Subject: [PATCH] Improve selftest

---
 .../eu/svjatoslav/alyverkko_cli/AiTask.java   |  3 +-
 .../commands/SelftestCommand.java             | 62 +++++++++++++++++--
 2 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/src/main/java/eu/svjatoslav/alyverkko_cli/AiTask.java b/src/main/java/eu/svjatoslav/alyverkko_cli/AiTask.java
index 832c1b8..2beb037 100644
--- a/src/main/java/eu/svjatoslav/alyverkko_cli/AiTask.java
+++ b/src/main/java/eu/svjatoslav/alyverkko_cli/AiTask.java
@@ -11,6 +11,7 @@ import static java.lang.String.join;
 public class AiTask {
     public static final String AI_RESPONSE_MARKER = "ASSISTANT:";
     private static final String LLAMA_CPP_META_INFO_MARKER = "llm_load_print_meta: ";
+    public static final String MAIN_EXECUTABLE_NAME = "main";
 
     MailQuery mailQuery;
     private final Float temperature;
@@ -142,7 +143,7 @@ public class AiTask {
         String executablePath = configuration.getLlamaCppDirPath().getAbsolutePath();
         if (!executablePath.endsWith("/")) executablePath += "/";
 
-        executablePath += "main";
+        executablePath += MAIN_EXECUTABLE_NAME;
 
         return join(" ",
                 "nice", "-n", Integer.toString(niceValue),
diff --git a/src/main/java/eu/svjatoslav/alyverkko_cli/commands/SelftestCommand.java b/src/main/java/eu/svjatoslav/alyverkko_cli/commands/SelftestCommand.java
index 836ef1e..2afef6c 100644
--- a/src/main/java/eu/svjatoslav/alyverkko_cli/commands/SelftestCommand.java
+++ b/src/main/java/eu/svjatoslav/alyverkko_cli/commands/SelftestCommand.java
@@ -1,8 +1,10 @@
 package eu.svjatoslav.alyverkko_cli.commands;
 
+import eu.svjatoslav.alyverkko_cli.AiTask;
 import eu.svjatoslav.alyverkko_cli.Command;
 import eu.svjatoslav.alyverkko_cli.Main;
 
+import java.io.File;
 import java.io.IOException;
 
 import static eu.svjatoslav.alyverkko_cli.Main.configuration;
@@ -21,27 +23,77 @@ public class SelftestCommand implements Command {
         System.out.println("Starting selftest...");
 
         configuration = loadConfiguration();
+        boolean allChecksPassed = true;
+
 
         // Check if the configuration is loaded
         if (Main.configuration == null) {
             System.err.println("Configuration not found or invalid.");
-            return;
+            allChecksPassed = false;
         }
 
         // Validate models directory
         if (!Main.configuration.getModelsDirectory().exists() || !Main.configuration.getModelsDirectory().isDirectory()) {
             System.err.println("Models directory does not exist or is not a directory: " + Main.configuration.getModelsDirectory());
-            return;
+            allChecksPassed = false;
         }
 
         // Validate llama.cpp executable path
         if (!Main.configuration.getLlamaCppDirPath().exists() || !Main.configuration.getLlamaCppDirPath().isDirectory()) {
             System.err.println("llama.cpp project directory not found at: " + Main.configuration.getLlamaCppDirPath());
-            return;
+            allChecksPassed = false;
+        }
+
+        // Validate mail directory
+        if (!Main.configuration.getMailDirectory().exists() || !Main.configuration.getMailDirectory().isDirectory()) {
+            System.err.println("Mail directory does not exist or is not a directory: " + Main.configuration.getMailDirectory());
+            allChecksPassed = false;
+        }
+
+        // Validate prompts
+        if (Main.configuration.getPrompts().isEmpty()) {
+            System.err.println("No prompts are defined in the configuration.");
+            allChecksPassed = false;
+        }
+
+        // Validate models
+        if (Main.configuration.getModels().isEmpty()) {
+            System.err.println("No models are defined in the configuration.");
+            allChecksPassed = false;
+        }
+
+        // Validate default temperature
+        if (Main.configuration.getDefaultTemperature() < 0 || Main.configuration.getDefaultTemperature() > 3) {
+            System.err.println("Default temperature must be between 0 and 3.");
+            allChecksPassed = false;
+        }
+
+        // Validate thread count
+        if (Main.configuration.getThreadCount() < 1) {
+            System.err.println("Thread count must be at least 1.");
+            allChecksPassed = false;
+        }
+
+        // Validate batch thread count
+        if (Main.configuration.getBatchThreadCount() < 1) {
+            System.err.println("Batch thread count must be at least 1.");
+            allChecksPassed = false;
+        }
+
+        // Validate models
+        if (Main.configuration.getModels().isEmpty()) {
+            System.err.println("No models are defined in the configuration.");
+            allChecksPassed = false;
         }
 
-        // Additional checks like model file existence, etc., can be added here
+        // Validate the main executable
+        File llamaMainExecutable = new File(configuration.getLlamaCppDirPath(), AiTask.MAIN_EXECUTABLE_NAME);
+        if (!llamaMainExecutable.exists() || !llamaMainExecutable.isFile()) {
+            System.err.println("The 'main' executable was not found in the llama.cpp directory.");
+            allChecksPassed = false;
+        }
 
-        System.out.println("Selftest completed successfully.");
+        if (allChecksPassed)
+            System.out.println("Selftest completed successfully.");
     }
 }
-- 
2.20.1