(t *testing.T)
| 20 | ) |
| 21 | |
| 22 | func TestKeywordSuggestionCache(t *testing.T) { |
| 23 | // Clear cache and stats before testing |
| 24 | ClearSuggestionCache() |
| 25 | ResetSuggestionCacheStats() |
| 26 | |
| 27 | t.Run("cache miss then hit", func(t *testing.T) { |
| 28 | ClearSuggestionCache() |
| 29 | ResetSuggestionCacheStats() |
| 30 | |
| 31 | // First call should compute the result (cache miss) |
| 32 | result1 := SuggestKeyword("SELCT") |
| 33 | if result1 != "SELECT" { |
| 34 | t.Errorf("SuggestKeyword(SELCT) = %q, want SELECT", result1) |
| 35 | } |
| 36 | |
| 37 | // Check cache size increased |
| 38 | if SuggestionCacheSize() != 1 { |
| 39 | t.Errorf("cache size = %d, want 1", SuggestionCacheSize()) |
| 40 | } |
| 41 | |
| 42 | // Second call should return cached result (cache hit) |
| 43 | result2 := SuggestKeyword("SELCT") |
| 44 | if result2 != "SELECT" { |
| 45 | t.Errorf("SuggestKeyword(SELCT) cached = %q, want SELECT", result2) |
| 46 | } |
| 47 | |
| 48 | // Verify hit/miss stats |
| 49 | stats := GetSuggestionCacheStats() |
| 50 | if stats.Hits != 1 { |
| 51 | t.Errorf("stats.Hits = %d, want 1", stats.Hits) |
| 52 | } |
| 53 | if stats.Misses != 1 { |
| 54 | t.Errorf("stats.Misses = %d, want 1", stats.Misses) |
| 55 | } |
| 56 | }) |
| 57 | |
| 58 | t.Run("cache stores empty results", func(t *testing.T) { |
| 59 | ClearSuggestionCache() |
| 60 | |
| 61 | // This should return empty (too different from any keyword) |
| 62 | result1 := SuggestKeyword("XYZABC123") |
| 63 | if result1 != "" { |
| 64 | t.Errorf("SuggestKeyword(XYZABC123) = %q, want empty", result1) |
| 65 | } |
| 66 | |
| 67 | // Verify it was cached |
| 68 | if SuggestionCacheSize() != 1 { |
| 69 | t.Errorf("cache size = %d, want 1", SuggestionCacheSize()) |
| 70 | } |
| 71 | |
| 72 | // Second call should return cached empty result |
| 73 | result2 := SuggestKeyword("XYZABC123") |
| 74 | if result2 != "" { |
| 75 | t.Errorf("SuggestKeyword(XYZABC123) cached = %q, want empty", result2) |
| 76 | } |
| 77 | }) |
| 78 | |
| 79 | t.Run("case insensitive caching", func(t *testing.T) { |
nothing calls this directly
no test coverage detected