From 358d75157609a3251ab7cc8e3306cb2272fb77c2 Mon Sep 17 00:00:00 2001
From: James <namnh0122@gmail.com>
Date: Wed, 11 Sep 2024 01:35:29 +0700
Subject: [PATCH] feat: add pytest for e2e testing

---
 engine/e2e-test/main.py                 |  7 +++++++
 engine/e2e-test/test_cli_engine_get.py  | 27 +++++++++++++++++++++++++
 engine/e2e-test/test_cli_engine_list.py | 24 ++++++++++++++++++++++
 engine/e2e-test/test_runner.py          | 13 ++++++++++++
 4 files changed, 71 insertions(+)
 create mode 100644 engine/e2e-test/main.py
 create mode 100644 engine/e2e-test/test_cli_engine_get.py
 create mode 100644 engine/e2e-test/test_cli_engine_list.py
 create mode 100644 engine/e2e-test/test_runner.py

diff --git a/engine/e2e-test/main.py b/engine/e2e-test/main.py
new file mode 100644
index 000000000..ef1af5748
--- /dev/null
+++ b/engine/e2e-test/main.py
@@ -0,0 +1,7 @@
+import pytest
+
+from test_cli_engine_list import TestCliEngineList
+from test_cli_engine_get import TestCliEngineGet
+
+if __name__ == "__main__":
+    pytest.main([__file__, "-v"])
diff --git a/engine/e2e-test/test_cli_engine_get.py b/engine/e2e-test/test_cli_engine_get.py
new file mode 100644
index 000000000..92c62f351
--- /dev/null
+++ b/engine/e2e-test/test_cli_engine_get.py
@@ -0,0 +1,27 @@
+import platform
+
+import pytest
+from test_runner import run
+
+
+class TestCliEngineGet:
+    @pytest.mark.skipif(platform.system() != "Windows", reason="Windows-specific test")
+    def test_engines_list_run_successfully_on_windows(self):
+        # TODO: implement
+        assert 0 == 0
+
+    @pytest.mark.skipif(platform.system() != "Darwin", reason="macOS-specific test")
+    def test_engines_get_run_successfully_on_macos(self):
+        exit_code, output, error = run("Get engine", ["engines", "get", "cortex.llamacpp"])
+        assert exit_code == 0, f"Get engine failed with error: {error}"
+
+    @pytest.mark.skipif(platform.system() != "Darwin", reason="macOS-specific test")
+    def test_engines_get_llamacpp_on_macos(self):
+        exit_code, output, error = run("Get engine", ["engines", "get", "cortex.llamacpp"])
+        assert exit_code == 0, f"Get engine failed with error: {error}"
+        assert "Incompatible" not in output, f"cortex.llamacpp can only be Ready or Not Installed"
+
+    @pytest.mark.skipif(platform.system() != "Linux", reason="Linux-specific test")
+    def test_engines_list_run_successfully_on_linux(self):
+        # TODO: implement
+        assert 0 == 0
diff --git a/engine/e2e-test/test_cli_engine_list.py b/engine/e2e-test/test_cli_engine_list.py
new file mode 100644
index 000000000..38faa75d0
--- /dev/null
+++ b/engine/e2e-test/test_cli_engine_list.py
@@ -0,0 +1,24 @@
+import platform
+
+import pytest
+from test_runner import run
+
+
+class TestCliEngineList:
+    @pytest.mark.skipif(platform.system() != "Windows", reason="Windows-specific test")
+    def test_engines_list_run_successfully_on_windows(self):
+        exit_code, output, error = run("List engines", ["engines", "list"])
+        assert exit_code == 0, f"List engines failed with error: {error}"
+        assert "llama.cpp" in output
+
+    @pytest.mark.skipif(platform.system() != "Darwin", reason="macOS-specific test")
+    def test_engines_list_run_successfully_on_macos(self):
+        exit_code, output, error = run("List engines", ["engines", "list"])
+        assert exit_code == 0, f"List engines failed with error: {error}"
+        assert "llama.cpp" in output
+
+    @pytest.mark.skipif(platform.system() != "Linux", reason="Linux-specific test")
+    def test_engines_list_run_successfully_on_linux(self):
+        exit_code, output, error = run("List engines", ["engines", "list"])
+        assert exit_code == 0, f"List engines failed with error: {error}"
+        assert "llama.cpp" in output
\ No newline at end of file
diff --git a/engine/e2e-test/test_runner.py b/engine/e2e-test/test_runner.py
new file mode 100644
index 000000000..6df74f20f
--- /dev/null
+++ b/engine/e2e-test/test_runner.py
@@ -0,0 +1,13 @@
+import platform
+import subprocess
+import os
+from typing import List, Tuple
+
+
+def run(name: str, arguments: List[str]):
+    if platform.system() == "Windows":
+        executable = "build\\cortex-cpp.exe"
+    else:
+        executable = "build/cortex-cpp"
+    result = subprocess.run([executable] + arguments, capture_output=True, text=True)
+    return result.returncode, result.stdout, result.stderr