beads/examples/library-usage/main_test.go
Test User b2e512ba79 test: reconcile the sqlite-era tests the reapply left asserting live support
The #4847 reapply brought back tests written when sqlite was still a
supported backend; update them to the tombstone contract:

- internal/beads: FindDatabasePath returns "" for sqlite metadata (fail
  closed at store selection) and never creates the database file.
- doctor: server health reports the generic non-Dolt warning; the perf
  refusal message names the backend without a support claim.
- examples/library-usage: exercise the API against embedded Dolt instead
  of the removed sqlite backend.

Also sweep stragglers flagged in review: bd sql help text, conformance
audit provenance comments, the test-strategy example path, and a comment
tying the flake devShell sqlite3 CLI to the legacy migration scripts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-18 02:43:57 +00:00

175 lines
4.4 KiB
Go

package main
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads"
)
// TestExampleCompiles ensures the example code compiles and basic API works
func TestExampleCompiles(t *testing.T) {
// Create temporary database for testing
tmpDir, err := os.MkdirTemp("", "beads-example-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
t.Fatalf("Failed to create .beads dir: %v", err)
}
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"backend":"dolt"}`), 0o600); err != nil {
t.Fatalf("Failed to create metadata.json: %v", err)
}
// Open storage
ctx := context.Background()
store, err := beads.OpenBestAvailable(ctx, beadsDir)
if err != nil {
t.Fatalf("Failed to open storage: %v", err)
}
defer store.Close()
// A fresh workspace needs an issue prefix before issues can be created
// (bd init normally seeds this).
if err := store.SetConfig(ctx, "issue_prefix", "ex"); err != nil {
t.Fatalf("Failed to seed issue prefix: %v", err)
}
// Create an issue (from example code)
newIssue := &beads.Issue{
ID: "",
Title: "Test library-created issue",
Description: "This verifies the library example works",
Status: beads.StatusOpen,
Priority: 2,
IssueType: beads.TypeTask,
}
if err := store.CreateIssue(ctx, newIssue, "test"); err != nil {
t.Fatalf("Failed to create issue: %v", err)
}
if newIssue.ID == "" {
t.Error("Issue ID should be auto-generated")
}
// Get ready work (from example code)
ready, err := store.GetReadyWork(ctx, beads.WorkFilter{
Status: beads.StatusOpen,
Limit: 5,
})
if err != nil {
t.Fatalf("Failed to get ready work: %v", err)
}
if len(ready) == 0 {
t.Error("Expected at least one ready issue")
}
// Add label (from example code)
if err := store.AddLabel(ctx, newIssue.ID, "test-label", "test"); err != nil {
t.Fatalf("Failed to add label: %v", err)
}
labels, err := store.GetLabels(ctx, newIssue.ID)
if err != nil {
t.Fatalf("Failed to get labels: %v", err)
}
if len(labels) != 1 || labels[0] != "test-label" {
t.Errorf("Expected label 'test-label', got %v", labels)
}
// Update status (from example code)
updates := map[string]interface{}{
"status": beads.StatusInProgress,
}
if err := store.UpdateIssue(ctx, newIssue.ID, updates, "test"); err != nil {
t.Fatalf("Failed to update issue: %v", err)
}
// Get statistics (from example code)
stats, err := store.GetStatistics(ctx)
if err != nil {
t.Fatalf("Failed to get statistics: %v", err)
}
if stats.TotalIssues == 0 {
t.Error("Expected at least one total issue")
}
// Close issue (from example code)
if err := store.CloseIssue(ctx, newIssue.ID, "Test complete", "test", ""); err != nil {
t.Fatalf("Failed to close issue: %v", err)
}
// Verify closed
closed, err := store.GetIssue(ctx, newIssue.ID)
if err != nil {
t.Fatalf("Failed to get issue: %v", err)
}
if closed.Status != beads.StatusClosed {
t.Errorf("Expected status closed, got %v", closed.Status)
}
t.Log("✅ All example operations work correctly")
}
// TestDependencyConstants ensures all constants are accessible
func TestDependencyConstants(t *testing.T) {
// Test that all constants from the example are accessible
_ = beads.StatusOpen
_ = beads.StatusInProgress
_ = beads.StatusClosed
_ = beads.StatusBlocked
_ = beads.TypeBug
_ = beads.TypeFeature
_ = beads.TypeTask
_ = beads.TypeEpic
_ = beads.TypeChore
_ = beads.DepBlocks
_ = beads.DepRelated
_ = beads.DepParentChild
_ = beads.DepDiscoveredFrom
}
// TestFindDatabasePath tests database discovery
func TestFindDatabasePath(t *testing.T) {
// Create temp directory with .beads
tmpDir, err := os.MkdirTemp("", "beads-finddb-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatalf("Failed to create .beads dir: %v", err)
}
dbPath := filepath.Join(beadsDir, "test.db")
f, err := os.Create(dbPath)
if err != nil {
t.Fatalf("Failed to create db file: %v", err)
}
f.Close()
// Change to temp directory
t.Chdir(tmpDir)
// Test FindDatabasePath
found := beads.FindDatabasePath()
if found == "" {
t.Error("Expected to find database, got empty string")
}
t.Logf("Found database: %s", found)
}