From 13b17cd7779d0e9be2b308f2d07ea72fca50cfa5 Mon Sep 17 00:00:00 2001 From: Levi Neely Date: Tue, 21 Apr 2026 15:39:52 +0200 Subject: [PATCH] backend: extract parseOllamaContextLength, 100% coverage --- pkg/backend/ollama.go | 16 +++++++--- pkg/backend/ollama_stream_test.go | 50 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/pkg/backend/ollama.go b/pkg/backend/ollama.go index 903e86b..e76d9b7 100644 --- a/pkg/backend/ollama.go +++ b/pkg/backend/ollama.go @@ -84,18 +84,26 @@ func (b *OllamaBackend) ContextLength(ctx context.Context) int { return 0 } defer resp.Body.Close() + cl := parseOllamaContextLength(resp.Body) + if cl > 0 { + b.ctxLength = cl + b.ctxModel = b.model + } + return cl +} + +// parseOllamaContextLength extracts the context_length from an /api/show response body. +func parseOllamaContextLength(r io.Reader) int { var result struct { ModelInfo map[string]any `json:"model_info"` } - if json.NewDecoder(resp.Body).Decode(&result) != nil { + if json.NewDecoder(r).Decode(&result) != nil { return 0 } for k, v := range result.ModelInfo { if strings.HasSuffix(k, ".context_length") { if f, ok := v.(float64); ok && f > 0 { - b.ctxLength = int(f) - b.ctxModel = b.model - return b.ctxLength + return int(f) } } } diff --git a/pkg/backend/ollama_stream_test.go b/pkg/backend/ollama_stream_test.go index 235fec0..9165138 100644 --- a/pkg/backend/ollama_stream_test.go +++ b/pkg/backend/ollama_stream_test.go @@ -140,3 +140,53 @@ func TestOllamaNDJSON_ScannerError(t *testing.T) { t.Errorf("final = %+v", fin) } } + +// --- parseOllamaContextLength --- + +func TestParseOllamaContextLength_Found(t *testing.T) { + body := `{"model_info":{"general.context_length":8192}}` + if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 8192 { + t.Errorf("cl = %d; want 8192", cl) + } +} + +func TestParseOllamaContextLength_DifferentPrefix(t *testing.T) { + body := `{"model_info":{"llama.context_length":4096}}` + if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 4096 { + t.Errorf("cl = %d; want 4096", cl) + } +} + +func TestParseOllamaContextLength_NoKey(t *testing.T) { + body := `{"model_info":{"general.embedding_length":768}}` + if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 0 { + t.Errorf("cl = %d; want 0", cl) + } +} + +func TestParseOllamaContextLength_EmptyModelInfo(t *testing.T) { + body := `{"model_info":{}}` + if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 0 { + t.Errorf("cl = %d; want 0", cl) + } +} + +func TestParseOllamaContextLength_MalformedJSON(t *testing.T) { + if cl := parseOllamaContextLength(strings.NewReader("not json")); cl != 0 { + t.Errorf("cl = %d; want 0", cl) + } +} + +func TestParseOllamaContextLength_ZeroValue(t *testing.T) { + body := `{"model_info":{"general.context_length":0}}` + if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 0 { + t.Errorf("cl = %d; want 0", cl) + } +} + +func TestParseOllamaContextLength_NonNumeric(t *testing.T) { + body := `{"model_info":{"general.context_length":"not a number"}}` + if cl := parseOllamaContextLength(strings.NewReader(body)); cl != 0 { + t.Errorf("cl = %d; want 0", cl) + } +}