package main import ( "path/filepath" "testing" ) func TestOpenDB(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") db, err := OpenDB(dbPath) if err != nil { t.Fatalf("OpenDB failed: %v", err) } defer db.Close() count, err := db.Count() if err != nil { t.Fatalf("Count failed: %v", err) } if count != 0 { t.Errorf("expected 0 journals, got %d", count) } } func TestInsertAndSearch(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") db, err := OpenDB(dbPath) if err != nil { t.Fatalf("OpenDB failed: %v", err) } defer db.Close() journals := []Journal{ {FullName: "Journal of Applied Physics", Abbreviation: "J APPL PHYS"}, {FullName: "Nature Medicine", Abbreviation: "NAT MED"}, {FullName: "Physical Review Letters", Abbreviation: "PHYS REV LETT"}, } if err := db.InsertJournals(journals); err != nil { t.Fatalf("InsertJournals failed: %v", err) } count, err := db.Count() if err != nil { t.Fatalf("Count failed: %v", err) } if count != 3 { t.Errorf("expected 3 journals, got %d", count) } results, err := db.SearchJournals("physics", 10) if err != nil { t.Fatalf("SearchJournals failed: %v", err) } if len(results) == 0 { t.Error("expected at least one result for 'physics'") } found := false for _, r := range results { if r.FullName == "Journal of Applied Physics" { found = true if r.Abbreviation != "J APPL PHYS" { t.Errorf("expected abbreviation 'J APPL PHYS', got %q", r.Abbreviation) } } } if !found { t.Error("expected to find 'Journal of Applied Physics' in results") } } func TestSearchPrefix(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") db, err := OpenDB(dbPath) if err != nil { t.Fatalf("OpenDB failed: %v", err) } defer db.Close() journals := []Journal{ {FullName: "Journal of Applied Physics", Abbreviation: "J APPL PHYS"}, {FullName: "Nature Medicine", Abbreviation: "NAT MED"}, } if err := db.InsertJournals(journals); err != nil { t.Fatalf("InsertJournals failed: %v", err) } results, err := db.SearchJournals("nat med", 10) if err != nil { t.Fatalf("SearchJournals failed: %v", err) } if len(results) == 0 { t.Error("expected results for prefix query 'nat med', got none") } } func TestSearchLimit(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") db, err := OpenDB(dbPath) if err != nil { t.Fatalf("OpenDB failed: %v", err) } defer db.Close() journals := []Journal{ {FullName: "Journal One", Abbreviation: "J ONE"}, {FullName: "Journal Two", Abbreviation: "J TWO"}, {FullName: "Journal Three", Abbreviation: "J THREE"}, } if err := db.InsertJournals(journals); err != nil { t.Fatalf("InsertJournals failed: %v", err) } results, err := db.SearchJournals("journal", 2) if err != nil { t.Fatalf("SearchJournals failed: %v", err) } if len(results) > 2 { t.Errorf("expected at most 2 results, got %d", len(results)) } } func TestSanitizeFTS5(t *testing.T) { cases := []struct { input string expected string }{ {"hello world", "hello* world*"}, {"hello world", "hello* world*"}, {"hello-world", "hello* world*"}, {"hello&world", "hello* world*"}, {"hello(world)", "hello* world*"}, {"C++", "c*"}, {"Nature & Science", "nature* science*"}, {"phys*", "phys*"}, {"a", "a*"}, {"", ""}, {"!!!", ""}, } for _, c := range cases { got := sanitizeFTS5(c.input) if got != c.expected { t.Errorf("sanitizeFTS5(%q) = %q, want %q", c.input, got, c.expected) } } } func TestSearchWithSpecialChars(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") db, err := OpenDB(dbPath) if err != nil { t.Fatalf("OpenDB failed: %v", err) } defer db.Close() journals := []Journal{ {FullName: "Nature & Science", Abbreviation: "NAT SCI"}, {FullName: "C++ Weekly", Abbreviation: "C WEEKLY"}, {FullName: "Physical Review (Letters)", Abbreviation: "PHYS REV LETT"}, } if err := db.InsertJournals(journals); err != nil { t.Fatalf("InsertJournals failed: %v", err) } for _, q := range []string{"Nature & Science", "C++", "Physical Review (Letters)"} { results, err := db.SearchJournals(q, 10) if err != nil { t.Fatalf("SearchJournals(%q) failed: %v", q, err) } if len(results) == 0 { t.Errorf("expected results for %q, got none", q) } } }