|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestSessionCache_HasSession(t *testing.T) { |
| 11 | + // Create temp cache dir |
| 12 | + tmpDir := t.TempDir() |
| 13 | + t.Setenv("SKILLS_CACHE_DIR", tmpDir) |
| 14 | + |
| 15 | + cache, err := NewSessionCache("test-client") |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("Failed to create session cache: %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + // Test empty cache |
| 21 | + if cache.HasSession("session-1") { |
| 22 | + t.Error("Expected HasSession to return false for empty cache") |
| 23 | + } |
| 24 | + |
| 25 | + // Record a session |
| 26 | + if err := cache.RecordSession("session-1"); err != nil { |
| 27 | + t.Fatalf("Failed to record session: %v", err) |
| 28 | + } |
| 29 | + |
| 30 | + // Test that session is now found |
| 31 | + if !cache.HasSession("session-1") { |
| 32 | + t.Error("Expected HasSession to return true after recording") |
| 33 | + } |
| 34 | + |
| 35 | + // Test that other sessions are not found |
| 36 | + if cache.HasSession("session-2") { |
| 37 | + t.Error("Expected HasSession to return false for unrecorded session") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestSessionCache_RecordSession(t *testing.T) { |
| 42 | + tmpDir := t.TempDir() |
| 43 | + t.Setenv("SKILLS_CACHE_DIR", tmpDir) |
| 44 | + |
| 45 | + cache, err := NewSessionCache("test-client") |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("Failed to create session cache: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + // Record multiple sessions |
| 51 | + sessions := []string{"session-1", "session-2", "session-3"} |
| 52 | + for _, s := range sessions { |
| 53 | + if err := cache.RecordSession(s); err != nil { |
| 54 | + t.Fatalf("Failed to record session %s: %v", s, err) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Verify all are recorded |
| 59 | + for _, s := range sessions { |
| 60 | + if !cache.HasSession(s) { |
| 61 | + t.Errorf("Expected session %s to be recorded", s) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Verify file format |
| 66 | + data, err := os.ReadFile(cache.FilePath()) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("Failed to read session file: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + // File should have 3 lines |
| 72 | + lines := 0 |
| 73 | + for _, b := range data { |
| 74 | + if b == '\n' { |
| 75 | + lines++ |
| 76 | + } |
| 77 | + } |
| 78 | + if lines != 3 { |
| 79 | + t.Errorf("Expected 3 lines in session file, got %d", lines) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestSessionCache_EmptySessionID(t *testing.T) { |
| 84 | + tmpDir := t.TempDir() |
| 85 | + t.Setenv("SKILLS_CACHE_DIR", tmpDir) |
| 86 | + |
| 87 | + cache, err := NewSessionCache("test-client") |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("Failed to create session cache: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + // Empty session ID should not be recorded |
| 93 | + if err := cache.RecordSession(""); err != nil { |
| 94 | + t.Fatalf("RecordSession with empty ID should not error: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + // File should not exist |
| 98 | + if _, err := os.Stat(cache.FilePath()); !os.IsNotExist(err) { |
| 99 | + t.Error("Expected no file to be created for empty session ID") |
| 100 | + } |
| 101 | + |
| 102 | + // HasSession should return false for empty |
| 103 | + if cache.HasSession("") { |
| 104 | + t.Error("Expected HasSession to return false for empty session ID") |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestSessionCache_CullOldEntries(t *testing.T) { |
| 109 | + tmpDir := t.TempDir() |
| 110 | + t.Setenv("SKILLS_CACHE_DIR", tmpDir) |
| 111 | + |
| 112 | + cache, err := NewSessionCache("test-client") |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("Failed to create session cache: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + // Write some entries with old timestamps manually |
| 118 | + oldTime := time.Now().Add(-10 * 24 * time.Hour).UTC().Format(time.RFC3339) |
| 119 | + newTime := time.Now().UTC().Format(time.RFC3339) |
| 120 | + |
| 121 | + // Ensure directory exists |
| 122 | + if err := os.MkdirAll(filepath.Dir(cache.FilePath()), 0755); err != nil { |
| 123 | + t.Fatalf("Failed to create directory: %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + content := "old-session " + oldTime + "\n" + |
| 127 | + "new-session " + newTime + "\n" |
| 128 | + |
| 129 | + if err := os.WriteFile(cache.FilePath(), []byte(content), 0644); err != nil { |
| 130 | + t.Fatalf("Failed to write test data: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + // Cull entries older than 5 days |
| 134 | + if err := cache.CullOldEntries(5 * 24 * time.Hour); err != nil { |
| 135 | + t.Fatalf("Failed to cull old entries: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + // Old session should be gone |
| 139 | + if cache.HasSession("old-session") { |
| 140 | + t.Error("Expected old session to be culled") |
| 141 | + } |
| 142 | + |
| 143 | + // New session should still exist |
| 144 | + if !cache.HasSession("new-session") { |
| 145 | + t.Error("Expected new session to still exist after culling") |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestSessionCache_Clear(t *testing.T) { |
| 150 | + tmpDir := t.TempDir() |
| 151 | + t.Setenv("SKILLS_CACHE_DIR", tmpDir) |
| 152 | + |
| 153 | + cache, err := NewSessionCache("test-client") |
| 154 | + if err != nil { |
| 155 | + t.Fatalf("Failed to create session cache: %v", err) |
| 156 | + } |
| 157 | + |
| 158 | + // Record a session |
| 159 | + if err := cache.RecordSession("session-1"); err != nil { |
| 160 | + t.Fatalf("Failed to record session: %v", err) |
| 161 | + } |
| 162 | + |
| 163 | + // Clear cache |
| 164 | + if err := cache.Clear(); err != nil { |
| 165 | + t.Fatalf("Failed to clear cache: %v", err) |
| 166 | + } |
| 167 | + |
| 168 | + // Session should no longer exist |
| 169 | + if cache.HasSession("session-1") { |
| 170 | + t.Error("Expected session to be cleared") |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func TestSessionCache_MultipleClients(t *testing.T) { |
| 175 | + tmpDir := t.TempDir() |
| 176 | + t.Setenv("SKILLS_CACHE_DIR", tmpDir) |
| 177 | + |
| 178 | + cache1, err := NewSessionCache("client-1") |
| 179 | + if err != nil { |
| 180 | + t.Fatalf("Failed to create cache 1: %v", err) |
| 181 | + } |
| 182 | + |
| 183 | + cache2, err := NewSessionCache("client-2") |
| 184 | + if err != nil { |
| 185 | + t.Fatalf("Failed to create cache 2: %v", err) |
| 186 | + } |
| 187 | + |
| 188 | + // Record to client 1 |
| 189 | + if err := cache1.RecordSession("session-a"); err != nil { |
| 190 | + t.Fatalf("Failed to record to cache 1: %v", err) |
| 191 | + } |
| 192 | + |
| 193 | + // Client 1 should have it |
| 194 | + if !cache1.HasSession("session-a") { |
| 195 | + t.Error("Client 1 should have session-a") |
| 196 | + } |
| 197 | + |
| 198 | + // Client 2 should NOT have it |
| 199 | + if cache2.HasSession("session-a") { |
| 200 | + t.Error("Client 2 should not have session-a") |
| 201 | + } |
| 202 | + |
| 203 | + // Verify different files |
| 204 | + if cache1.FilePath() == cache2.FilePath() { |
| 205 | + t.Error("Different clients should have different cache files") |
| 206 | + } |
| 207 | +} |
0 commit comments