1package ollama
  2
  3import (
  4	"context"
  5	"testing"
  6	"time"
  7)
  8
  9func TestStartService(t *testing.T) {
 10	if !IsInstalled() {
 11		t.Skip("Ollama is not installed, skipping StartService test")
 12	}
 13
 14	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
 15	defer cancel()
 16
 17	// Check if already running
 18	if IsRunning(ctx) {
 19		t.Log("✓ Ollama is already running, skipping start test")
 20		return
 21	}
 22
 23	t.Log("Starting Ollama service...")
 24	err := StartService(ctx)
 25	if err != nil {
 26		t.Fatalf("Failed to start Ollama service: %v", err)
 27	}
 28
 29	// Verify it's running
 30	if !IsRunning(ctx) {
 31		t.Fatal("Ollama service was started but IsRunning returns false")
 32	}
 33
 34	t.Log("✓ Ollama service started successfully")
 35
 36	// Cleanup
 37	defer func() {
 38		if processManager.crushStartedOllama {
 39			cleanup()
 40		}
 41	}()
 42}
 43
 44func TestEnsureRunning(t *testing.T) {
 45	if !IsInstalled() {
 46		t.Skip("Ollama is not installed, skipping EnsureRunning test")
 47	}
 48
 49	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
 50	defer cancel()
 51
 52	err := EnsureRunning(ctx)
 53	if err != nil {
 54		t.Fatalf("EnsureRunning failed: %v", err)
 55	}
 56
 57	if !IsRunning(ctx) {
 58		t.Fatal("EnsureRunning succeeded but Ollama is not running")
 59	}
 60
 61	t.Log("✓ EnsureRunning succeeded")
 62
 63	// Test calling it again when already running
 64	err = EnsureRunning(ctx)
 65	if err != nil {
 66		t.Fatalf("EnsureRunning failed on second call: %v", err)
 67	}
 68
 69	t.Log("✓ EnsureRunning is idempotent")
 70
 71	// Cleanup
 72	defer func() {
 73		if processManager.crushStartedOllama {
 74			cleanup()
 75		}
 76	}()
 77}
 78
 79func TestEnsureModelLoaded(t *testing.T) {
 80	if !IsInstalled() {
 81		t.Skip("Ollama is not installed, skipping EnsureModelLoaded test")
 82	}
 83
 84	ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
 85	defer cancel()
 86
 87	// Get available models
 88	if err := EnsureRunning(ctx); err != nil {
 89		t.Fatalf("Failed to ensure Ollama is running: %v", err)
 90	}
 91
 92	models, err := GetModels(ctx)
 93	if err != nil {
 94		t.Fatalf("Failed to get models: %v", err)
 95	}
 96
 97	if len(models) == 0 {
 98		t.Skip("No models available, skipping EnsureModelLoaded test")
 99	}
100
101	// Pick a smaller model if available
102	testModel := models[0].Name
103	for _, model := range models {
104		if model.Name == "phi3:3.8b" || model.Name == "llama3.2:3b" {
105			testModel = model.Name
106			break
107		}
108	}
109
110	t.Logf("Testing with model: %s", testModel)
111
112	err = EnsureModelLoaded(ctx, testModel)
113	if err != nil {
114		t.Fatalf("Failed to ensure model is loaded: %v", err)
115	}
116
117	// Verify model is loaded
118	running, err := IsModelRunning(ctx, testModel)
119	if err != nil {
120		t.Fatalf("Failed to check if model is running: %v", err)
121	}
122
123	if !running {
124		t.Fatal("EnsureModelLoaded succeeded but model is not running")
125	}
126
127	t.Log("✓ EnsureModelLoaded succeeded")
128
129	// Test calling it again when already loaded
130	err = EnsureModelLoaded(ctx, testModel)
131	if err != nil {
132		t.Fatalf("EnsureModelLoaded failed on second call: %v", err)
133	}
134
135	t.Log("✓ EnsureModelLoaded is idempotent")
136
137	// Cleanup
138	defer func() {
139		if processManager.crushStartedOllama {
140			cleanup()
141		}
142	}()
143}