test: add unit tests and sample notes
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package index
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInMemoryVectorIndex_AddSearch(t *testing.T) {
|
||||
idx := NewInMemoryVectorIndex()
|
||||
|
||||
idx.Add("a", []float64{1, 0, 0})
|
||||
idx.Add("b", []float64{0, 1, 0})
|
||||
idx.Add("c", []float64{0, 0, 1})
|
||||
|
||||
if idx.Len() != 3 {
|
||||
t.Fatalf("Len = %d, want 3", idx.Len())
|
||||
}
|
||||
|
||||
hits := idx.Search([]float64{1, 0, 0}, 3)
|
||||
if len(hits) != 3 {
|
||||
t.Fatalf("Search: got %d hits, want 3", len(hits))
|
||||
}
|
||||
if hits[0].ID != "a" {
|
||||
t.Errorf("top hit = %q, want %q", hits[0].ID, "a")
|
||||
}
|
||||
if math.Abs(hits[0].Score-1.0) > 1e-9 {
|
||||
t.Errorf("top score = %f, want 1.0", hits[0].Score)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInMemoryVectorIndex_Remove(t *testing.T) {
|
||||
idx := NewInMemoryVectorIndex()
|
||||
idx.Add("a", []float64{1, 0})
|
||||
idx.Add("b", []float64{0, 1})
|
||||
|
||||
idx.Remove("a")
|
||||
if idx.Len() != 1 {
|
||||
t.Fatalf("Len after remove = %d, want 1", idx.Len())
|
||||
}
|
||||
|
||||
hits := idx.Search([]float64{0, 1}, 5)
|
||||
if len(hits) != 1 || hits[0].ID != "b" {
|
||||
t.Errorf("expected only 'b' in results")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInMemoryVectorIndex_RemoveByPrefix(t *testing.T) {
|
||||
idx := NewInMemoryVectorIndex()
|
||||
idx.Add("note1_0", []float64{1, 0})
|
||||
idx.Add("note1_1", []float64{0.9, 0.1})
|
||||
idx.Add("note2_0", []float64{0, 1})
|
||||
|
||||
idx.RemoveByPrefix("note1_")
|
||||
if idx.Len() != 1 {
|
||||
t.Fatalf("Len after RemoveByPrefix = %d, want 1", idx.Len())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCosine(t *testing.T) {
|
||||
tests := []struct {
|
||||
a, b []float64
|
||||
want float64
|
||||
}{
|
||||
{[]float64{1, 0}, []float64{1, 0}, 1.0},
|
||||
{[]float64{1, 0}, []float64{0, 1}, 0.0},
|
||||
{[]float64{1, 0}, []float64{-1, 0}, -1.0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got := cosine(tt.a, tt.b)
|
||||
if math.Abs(got-tt.want) > 1e-9 {
|
||||
t.Errorf("cosine(%v, %v) = %f, want %f", tt.a, tt.b, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package rag
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestChunkMarkdown_ByHeaders(t *testing.T) {
|
||||
md := `# Title
|
||||
|
||||
## Section 1
|
||||
|
||||
Content of section 1.
|
||||
|
||||
## Section 2
|
||||
|
||||
Content of section 2.`
|
||||
|
||||
chunks := ChunkMarkdown(md, 1000)
|
||||
if len(chunks) < 2 {
|
||||
t.Errorf("expected at least 2 chunks, got %d", len(chunks))
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkMarkdown_SmallContent(t *testing.T) {
|
||||
md := "Just a short note with no headers."
|
||||
chunks := ChunkMarkdown(md, 500)
|
||||
if len(chunks) != 1 {
|
||||
t.Errorf("expected 1 chunk, got %d", len(chunks))
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkMarkdown_SplitLargeSection(t *testing.T) {
|
||||
md := `## Big Section
|
||||
|
||||
Paragraph one with enough content to make this section reasonably long for testing purposes.
|
||||
|
||||
Paragraph two with more content that should be in a separate chunk when the max size is small.`
|
||||
|
||||
chunks := ChunkMarkdown(md, 80)
|
||||
if len(chunks) < 2 {
|
||||
t.Errorf("expected at least 2 chunks for large section, got %d", len(chunks))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMemoryNoteStore_CRUD(t *testing.T) {
|
||||
s := NewMemoryNoteStore()
|
||||
ctx := context.Background()
|
||||
|
||||
note := &Note{ID: "test-1", Title: "Test Note", Content: "Hello", Tags: []string{"test"}}
|
||||
if err := s.Create(ctx, note); err != nil {
|
||||
t.Fatalf("Create: %v", err)
|
||||
}
|
||||
|
||||
got, err := s.GetByID(ctx, "test-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetByID: %v", err)
|
||||
}
|
||||
if got.Title != "Test Note" {
|
||||
t.Errorf("Title = %q, want %q", got.Title, "Test Note")
|
||||
}
|
||||
|
||||
list, err := s.List(ctx)
|
||||
if err != nil || len(list) != 1 {
|
||||
t.Fatalf("List: len=%d, err=%v", len(list), err)
|
||||
}
|
||||
|
||||
if err := s.Delete(ctx, "test-1"); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
_, err = s.GetByID(ctx, "test-1")
|
||||
if err == nil {
|
||||
t.Fatal("expected error after delete")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemoryNoteStore_SearchByKeyword(t *testing.T) {
|
||||
s := NewMemoryNoteStore()
|
||||
ctx := context.Background()
|
||||
|
||||
s.Create(ctx, &Note{ID: "1", Title: "Go Concurrency", Content: "goroutine patterns"})
|
||||
s.Create(ctx, &Note{ID: "2", Title: "Python Basics", Content: "variables and types"})
|
||||
|
||||
results, err := s.SearchByKeyword(ctx, "goroutine")
|
||||
if err != nil {
|
||||
t.Fatalf("SearchByKeyword: %v", err)
|
||||
}
|
||||
if len(results) != 1 || results[0].ID != "1" {
|
||||
t.Errorf("expected 1 result with ID=1, got %d", len(results))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user