a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
package persistence
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestKeywordStore_RoundTrip(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "kw")
|
|
|
|
// A missing file loads as an empty store, not an error.
|
|
loaded, err := LoadKeyword(dir)
|
|
if err != nil {
|
|
t.Fatalf("LoadKeyword on a missing dir errored: %v", err)
|
|
}
|
|
if loaded == nil || len(loaded.Keywords) != 0 {
|
|
t.Fatalf("missing-file load should yield an empty store")
|
|
}
|
|
|
|
store := &KeywordStore{
|
|
Version: "1",
|
|
RepoPath: "/repo",
|
|
Keywords: []KeywordAssoc{
|
|
{Keyword: "auth", Matches: []KeywordMatch{
|
|
{SymbolID: "pkg::LoginService", HitCount: 5, LastUsed: 1700},
|
|
}},
|
|
{Keyword: "token", Matches: []KeywordMatch{
|
|
{SymbolID: "pkg::ParseJWT", HitCount: 3, LastUsed: 1701},
|
|
}},
|
|
},
|
|
}
|
|
if err := SaveKeyword(dir, store); err != nil {
|
|
t.Fatalf("SaveKeyword errored: %v", err)
|
|
}
|
|
back, err := LoadKeyword(dir)
|
|
if err != nil {
|
|
t.Fatalf("LoadKeyword after save errored: %v", err)
|
|
}
|
|
if len(back.Keywords) != 2 {
|
|
t.Fatalf("round-trip lost keywords: got %d, want 2", len(back.Keywords))
|
|
}
|
|
if back.Keywords[0].Keyword != "auth" ||
|
|
back.Keywords[0].Matches[0].SymbolID != "pkg::LoginService" ||
|
|
back.Keywords[0].Matches[0].HitCount != 5 {
|
|
t.Errorf("round-trip corrupted the first keyword: %+v", back.Keywords[0])
|
|
}
|
|
}
|
|
|
|
func TestKeywordStore_TrimsOverCap(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "kw")
|
|
store := &KeywordStore{}
|
|
for i := 0; i < maxKeywords+50; i++ {
|
|
store.Keywords = append(store.Keywords, KeywordAssoc{Keyword: string(rune('a' + i%26))})
|
|
}
|
|
if err := SaveKeyword(dir, store); err != nil {
|
|
t.Fatalf("SaveKeyword errored: %v", err)
|
|
}
|
|
if len(store.Keywords) != maxKeywords {
|
|
t.Errorf("SaveKeyword should trim to %d keywords, got %d", maxKeywords, len(store.Keywords))
|
|
}
|
|
}
|
|
|
|
func TestMaxKeywordEntries_TighterThanCombo(t *testing.T) {
|
|
if MaxKeywordEntries() >= MaxComboEntries() {
|
|
t.Errorf("MaxKeywordEntries (%d) should be tighter than MaxComboEntries (%d)",
|
|
MaxKeywordEntries(), MaxComboEntries())
|
|
}
|
|
}
|