1package server
2
3import (
4 "encoding/json"
5 "net/http"
6 "net/http/httptest"
7 "testing"
8)
9
10// TestContextWindowSizePreservedOnNavigation tests that context window size
11// is correctly returned when loading different conversations
12func TestContextWindowSizePreservedOnNavigation(t *testing.T) {
13 h := NewTestHarness(t)
14 defer h.Close()
15
16 // Create first conversation and get a response
17 h.NewConversation("echo: first message", "/tmp")
18 resp1 := h.WaitResponse()
19 t.Logf("First conversation first response: %q", resp1)
20
21 // Get context window size for first conversation
22 firstConvID := h.convID
23 firstConvSize := h.GetContextWindowSize()
24 t.Logf("First conversation context window size: %d", firstConvSize)
25 if firstConvSize == 0 {
26 t.Fatal("expected non-zero context window size for first conversation")
27 }
28
29 // Create second conversation and get a response
30 h.NewConversation("echo: second message with much more text to ensure different context size", "/tmp")
31 resp2 := h.WaitResponse()
32 t.Logf("Second conversation first response: %q", resp2)
33
34 secondConvID := h.convID
35 secondConvSize := h.GetContextWindowSize()
36 t.Logf("Second conversation context window size: %d", secondConvSize)
37 if secondConvSize == 0 {
38 t.Fatal("expected non-zero context window size for second conversation")
39 }
40
41 // Now simulate "navigating" back to the first conversation by fetching it via GET
42 // This is what the UI does when switching conversations
43 req := httptest.NewRequest("GET", "/api/conversation/"+firstConvID, nil)
44 w := httptest.NewRecorder()
45 h.server.handleGetConversation(w, req, firstConvID)
46
47 if w.Code != http.StatusOK {
48 t.Fatalf("GET first conversation returned %d: %s", w.Code, w.Body.String())
49 }
50
51 var resp StreamResponse
52 if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
53 t.Fatalf("Failed to decode response: %v", err)
54 }
55
56 t.Logf("First conversation on navigation: context_window_size=%d, messages=%d",
57 resp.ContextWindowSize, len(resp.Messages))
58
59 if resp.ContextWindowSize != firstConvSize {
60 t.Errorf("context_window_size mismatch on navigation: got %d, want %d",
61 resp.ContextWindowSize, firstConvSize)
62 }
63
64 // Now navigate to second conversation
65 req = httptest.NewRequest("GET", "/api/conversation/"+secondConvID, nil)
66 w = httptest.NewRecorder()
67 h.server.handleGetConversation(w, req, secondConvID)
68
69 if w.Code != http.StatusOK {
70 t.Fatalf("GET second conversation returned %d: %s", w.Code, w.Body.String())
71 }
72
73 if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
74 t.Fatalf("Failed to decode response: %v", err)
75 }
76
77 t.Logf("Second conversation on navigation: context_window_size=%d, messages=%d",
78 resp.ContextWindowSize, len(resp.Messages))
79
80 if resp.ContextWindowSize != secondConvSize {
81 t.Errorf("context_window_size mismatch on navigation: got %d, want %d",
82 resp.ContextWindowSize, secondConvSize)
83 }
84}