From 9252ddcfae8a6ca748cd54df37349bf4fd22f746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Wed, 4 Feb 2026 01:30:49 +0100 Subject: [PATCH] test: add tests for file metadata and range parameters - testFileRange: test GetFileWithRange with various offset/limit values - testDeclarationsWithMetadata: test file metadata in declarations - Verify byte_size and line_count are computed correctly - Test edge cases: offset beyond EOF, non-indexed files Co-Authored-By: Claude Opus 4.5 --- internal/database/database_test.go | 173 +++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) diff --git a/internal/database/database_test.go b/internal/database/database_test.go index 11200cc..bd8c713 100644 --- a/internal/database/database_test.go +++ b/internal/database/database_test.go @@ -19,6 +19,8 @@ func runStoreTests(t *testing.T, newStore func(t *testing.T) Store) { {"OptionChildren", testOptionChildren}, {"Declarations", testDeclarations}, {"Files", testFiles}, + {"FileRange", testFileRange}, + {"DeclarationsWithMetadata", testDeclarationsWithMetadata}, {"SchemaVersion", testSchemaVersion}, } @@ -451,6 +453,14 @@ func testFiles(t *testing.T, store Store) { t.Errorf("Extension = %q, want .nix", got.Extension) } + // Verify file metadata was computed + if got.ByteSize != len(file.Content) { + t.Errorf("ByteSize = %d, want %d", got.ByteSize, len(file.Content)) + } + if got.LineCount != 3 { + t.Errorf("LineCount = %d, want 3", got.LineCount) + } + // Get non-existent file got, err = store.GetFile(ctx, rev.ID, "nonexistent.nix") if err != nil { @@ -476,6 +486,169 @@ func testFiles(t *testing.T, store Store) { } } +func testFileRange(t *testing.T, store Store) { + ctx := context.Background() + if err := store.Initialize(ctx); err != nil { + t.Fatalf("Initialize failed: %v", err) + } + + rev := &Revision{GitHash: "range123", ChannelName: "test"} + if err := store.CreateRevision(ctx, rev); err != nil { + t.Fatalf("CreateRevision failed: %v", err) + } + + // Create a multi-line file + content := "line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\nline 9\nline 10" + file := &File{ + RevisionID: rev.ID, + FilePath: "multiline.nix", + Extension: ".nix", + Content: content, + } + if err := store.CreateFile(ctx, file); err != nil { + t.Fatalf("CreateFile failed: %v", err) + } + + // Test default range (first 250 lines, but we have less) + result, err := store.GetFileWithRange(ctx, rev.ID, "multiline.nix", FileRange{}) + if err != nil { + t.Fatalf("GetFileWithRange default failed: %v", err) + } + if result == nil { + t.Fatal("Expected result, got nil") + } + if result.TotalLines != 10 { + t.Errorf("TotalLines = %d, want 10", result.TotalLines) + } + if result.StartLine != 1 { + t.Errorf("StartLine = %d, want 1", result.StartLine) + } + if result.EndLine != 10 { + t.Errorf("EndLine = %d, want 10", result.EndLine) + } + + // Test with offset + result, err = store.GetFileWithRange(ctx, rev.ID, "multiline.nix", FileRange{Offset: 2, Limit: 3}) + if err != nil { + t.Fatalf("GetFileWithRange with offset failed: %v", err) + } + if result.StartLine != 3 { + t.Errorf("StartLine = %d, want 3", result.StartLine) + } + if result.EndLine != 5 { + t.Errorf("EndLine = %d, want 5", result.EndLine) + } + if result.Content != "line 3\nline 4\nline 5" { + t.Errorf("Content = %q, want lines 3-5", result.Content) + } + + // Test offset beyond file + result, err = store.GetFileWithRange(ctx, rev.ID, "multiline.nix", FileRange{Offset: 100}) + if err != nil { + t.Fatalf("GetFileWithRange beyond end failed: %v", err) + } + if result.StartLine != 0 { + t.Errorf("StartLine = %d, want 0 for beyond end", result.StartLine) + } + if result.Content != "" { + t.Errorf("Content = %q, want empty for beyond end", result.Content) + } + + // Test non-existent file + result, err = store.GetFileWithRange(ctx, rev.ID, "nonexistent.nix", FileRange{}) + if err != nil { + t.Fatalf("GetFileWithRange for nonexistent failed: %v", err) + } + if result != nil { + t.Error("Expected nil for nonexistent file") + } +} + +func testDeclarationsWithMetadata(t *testing.T, store Store) { + ctx := context.Background() + if err := store.Initialize(ctx); err != nil { + t.Fatalf("Initialize failed: %v", err) + } + + rev := &Revision{GitHash: "metadata123", ChannelName: "test"} + if err := store.CreateRevision(ctx, rev); err != nil { + t.Fatalf("CreateRevision failed: %v", err) + } + + // Create a file + file := &File{ + RevisionID: rev.ID, + FilePath: "modules/nginx.nix", + Extension: ".nix", + Content: "line 1\nline 2\nline 3", + } + if err := store.CreateFile(ctx, file); err != nil { + t.Fatalf("CreateFile failed: %v", err) + } + + // Create an option with declarations + opt := &Option{ + RevisionID: rev.ID, + Name: "services.nginx.enable", + ParentPath: "services.nginx", + Type: "boolean", + } + if err := store.CreateOption(ctx, opt); err != nil { + t.Fatalf("CreateOption failed: %v", err) + } + + // Create declarations - one pointing to indexed file, one to non-indexed + decls := []*Declaration{ + {OptionID: opt.ID, FilePath: "modules/nginx.nix", Line: 10}, + {OptionID: opt.ID, FilePath: "modules/other.nix", Line: 20}, + } + if err := store.CreateDeclarationsBatch(ctx, decls); err != nil { + t.Fatalf("CreateDeclarationsBatch failed: %v", err) + } + + // Get declarations with metadata + declMetas, err := store.GetDeclarationsWithMetadata(ctx, rev.ID, opt.ID) + if err != nil { + t.Fatalf("GetDeclarationsWithMetadata failed: %v", err) + } + if len(declMetas) != 2 { + t.Fatalf("Expected 2 declarations, got %d", len(declMetas)) + } + + // Find the declaration for the indexed file + var indexed, notIndexed *DeclarationWithMetadata + for _, d := range declMetas { + if d.FilePath == "modules/nginx.nix" { + indexed = d + } else { + notIndexed = d + } + } + + if indexed == nil { + t.Fatal("Expected indexed declaration") + } + if !indexed.HasFile { + t.Error("Expected HasFile=true for indexed file") + } + if indexed.ByteSize != len(file.Content) { + t.Errorf("ByteSize = %d, want %d", indexed.ByteSize, len(file.Content)) + } + if indexed.LineCount != 3 { + t.Errorf("LineCount = %d, want 3", indexed.LineCount) + } + + if notIndexed == nil { + t.Fatal("Expected not-indexed declaration") + } + if notIndexed.HasFile { + t.Error("Expected HasFile=false for non-indexed file") + } + if notIndexed.ByteSize != 0 { + t.Errorf("ByteSize = %d, want 0 for non-indexed", notIndexed.ByteSize) + } +} + func testSchemaVersion(t *testing.T, store Store) { ctx := context.Background()