fix: prevent closed-store panics and consolidate test suites (bd-1rh)
- Add ErrStoreClosed guard to DoltStore query methods (execContext, queryContext, queryRowContext) to prevent nil-pointer panics when accessing a closed store - Add IsClosed() method to DoltStore for callers to check store state - Guard maybeAutoBackup, maybeAutoPush, and maybeAutoCommit against closed stores in PersistentPostRun - Consolidate metadata_filter_test.go from 9 separate stores to 1 shared suite (TestMetadataFilterSuite) - Consolidate metadata_ready_test.go from 4 separate stores to 1 shared suite (TestGetReadyWork_MetadataSuite) - Consolidate thread_test.go from 5 separate stores to 1 shared suite (TestThreadTraversalSuite) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Executed-By: beads/polecats/onyx Rig: beads Role: polecats
This commit is contained in:
parent
72b78474e6
commit
97462205e9
7 changed files with 393 additions and 682 deletions
|
|
@ -36,7 +36,7 @@ func maybeAutoBackup(ctx context.Context) {
|
|||
if !isBackupAutoEnabled() {
|
||||
return
|
||||
}
|
||||
if store == nil {
|
||||
if store == nil || store.IsClosed() {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ func maybeAutoCommit(ctx context.Context, p doltAutoCommitParams) error {
|
|||
}
|
||||
|
||||
st := getStore()
|
||||
if st == nil {
|
||||
if st == nil || st.IsClosed() {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ func isDoltAutoPushEnabled(ctx context.Context) bool {
|
|||
}
|
||||
// Auto-enable when a Dolt remote exists
|
||||
st := getStore()
|
||||
if st == nil {
|
||||
if st == nil || st.IsClosed() {
|
||||
return false
|
||||
}
|
||||
has, err := st.HasRemote(ctx, "origin")
|
||||
|
|
@ -38,7 +38,7 @@ func maybeAutoPush(ctx context.Context) {
|
|||
}
|
||||
|
||||
st := getStore()
|
||||
if st == nil {
|
||||
if st == nil || st.IsClosed() {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,290 +9,208 @@ import (
|
|||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func TestSearchIssues_MetadataFieldMatch(t *testing.T) {
|
||||
func TestMetadataFilterSuite(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
issue1 := &types.Issue{
|
||||
Title: "Platform issue",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"platform","sprint":"Q1"}`),
|
||||
// Create all test data up front — one DB for all subtests.
|
||||
// Use unique metadata keys per subtest group to avoid interference.
|
||||
|
||||
// --- MetadataFieldMatch data ---
|
||||
mfm1 := &types.Issue{
|
||||
ID: "mfm-1", Title: "Platform issue (mfm)", Priority: 2,
|
||||
IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"mfm_team":"platform","mfm_sprint":"Q1"}`),
|
||||
}
|
||||
issue2 := &types.Issue{
|
||||
Title: "Frontend issue",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"frontend","sprint":"Q1"}`),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue1, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue2, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
mfm2 := &types.Issue{
|
||||
ID: "mfm-2", Title: "Frontend issue (mfm)", Priority: 2,
|
||||
IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"mfm_team":"frontend","mfm_sprint":"Q1"}`),
|
||||
}
|
||||
|
||||
// Search for team=platform → should find only issue1
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{"team": "platform"},
|
||||
// --- HasMetadataKey data ---
|
||||
hmk1 := &types.Issue{
|
||||
ID: "hmk-1", Title: "Has team key (hmk)", Priority: 2,
|
||||
IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"hmk_team":"platform"}`),
|
||||
}
|
||||
hmk2 := &types.Issue{
|
||||
ID: "hmk-2", Title: "No metadata (hmk)", Priority: 2,
|
||||
IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
}
|
||||
|
||||
// --- MultipleMetadataFieldsANDed data ---
|
||||
and1 := &types.Issue{
|
||||
ID: "and-1", Title: "Both match (and)", Priority: 2,
|
||||
IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"and_team":"platform","and_sprint":"Q1"}`),
|
||||
}
|
||||
and2 := &types.Issue{
|
||||
ID: "and-2", Title: "Partial match (and)", Priority: 2,
|
||||
IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"and_team":"platform","and_sprint":"Q2"}`),
|
||||
}
|
||||
|
||||
// --- NoMetadataDoesNotMatch data ---
|
||||
nometa := &types.Issue{
|
||||
ID: "nometa-1", Title: "No metadata (nometa)", Priority: 2,
|
||||
IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
}
|
||||
|
||||
// --- CreateIssue_WithMetadata data ---
|
||||
withmeta := &types.Issue{
|
||||
ID: "withmeta-1", Title: "Issue with metadata (withmeta)", Priority: 2,
|
||||
IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"wm_team":"platform","wm_sprint":"Q1","wm_points":5}`),
|
||||
}
|
||||
|
||||
// --- CreateIssue_WithMetadata_Queryable data ---
|
||||
queryable := &types.Issue{
|
||||
ID: "queryable-1", Title: "Queryable metadata (queryable)", Priority: 2,
|
||||
IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"qm_team":"backend"}`),
|
||||
}
|
||||
|
||||
// Bulk create all issues
|
||||
allIssues := []*types.Issue{mfm1, mfm2, hmk1, hmk2, and1, and2, nometa, withmeta, queryable}
|
||||
for _, issue := range allIssues {
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue(%s): %v", issue.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("MetadataFieldMatch", func(t *testing.T) {
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{"mfm_team": "platform"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != mfm1.ID {
|
||||
t.Errorf("expected issue %s, got %s", mfm1.ID, results[0].ID)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != issue1.ID {
|
||||
t.Errorf("expected issue %s, got %s", issue1.ID, results[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchIssues_MetadataFieldNoMatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
Title: "Platform issue",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"platform"}`),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{"team": "backend"},
|
||||
t.Run("MetadataFieldNoMatch", func(t *testing.T) {
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{"mfm_team": "backend"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 0 {
|
||||
t.Errorf("expected 0 results, got %d", len(results))
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 0 {
|
||||
t.Errorf("expected 0 results, got %d", len(results))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchIssues_HasMetadataKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
issue1 := &types.Issue{
|
||||
Title: "Has team key",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"platform"}`),
|
||||
}
|
||||
issue2 := &types.Issue{
|
||||
Title: "No metadata",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue1, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue2, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
HasMetadataKey: "team",
|
||||
t.Run("HasMetadataKey", func(t *testing.T) {
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
HasMetadataKey: "hmk_team",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != hmk1.ID {
|
||||
t.Errorf("expected issue %s, got %s", hmk1.ID, results[0].ID)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != issue1.ID {
|
||||
t.Errorf("expected issue %s, got %s", issue1.ID, results[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchIssues_MultipleMetadataFieldsANDed(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
issue1 := &types.Issue{
|
||||
Title: "Both match",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"platform","sprint":"Q1"}`),
|
||||
}
|
||||
issue2 := &types.Issue{
|
||||
Title: "Partial match",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"platform","sprint":"Q2"}`),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue1, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue2, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{
|
||||
"team": "platform",
|
||||
"sprint": "Q1",
|
||||
},
|
||||
t.Run("MultipleMetadataFieldsANDed", func(t *testing.T) {
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{
|
||||
"and_team": "platform",
|
||||
"and_sprint": "Q1",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != and1.ID {
|
||||
t.Errorf("expected issue %s, got %s", and1.ID, results[0].ID)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != issue1.ID {
|
||||
t.Errorf("expected issue %s, got %s", issue1.ID, results[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchIssues_MetadataFieldInvalidKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{"'; DROP TABLE issues; --": "val"},
|
||||
t.Run("MetadataFieldInvalidKey", func(t *testing.T) {
|
||||
_, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{"'; DROP TABLE issues; --": "val"},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid metadata key, got nil")
|
||||
}
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid metadata key, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchIssues_HasMetadataKeyInvalidKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
HasMetadataKey: "bad key!",
|
||||
t.Run("HasMetadataKeyInvalidKey", func(t *testing.T) {
|
||||
_, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
HasMetadataKey: "bad key!",
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid metadata key, got nil")
|
||||
}
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid metadata key, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchIssues_NoMetadataDoesNotMatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
Title: "No metadata",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{"team": "platform"},
|
||||
t.Run("NoMetadataDoesNotMatch", func(t *testing.T) {
|
||||
// Search for a key that no issue has (unique to this test)
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{"nometa_team": "platform"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 0 {
|
||||
t.Errorf("expected 0 results for nonexistent metadata key, got %d", len(results))
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 0 {
|
||||
t.Errorf("expected 0 results for issue without metadata, got %d", len(results))
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateIssue_WithMetadata verifies metadata is persisted when set at creation time.
|
||||
func TestCreateIssue_WithMetadata(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
t.Run("CreateIssue_WithMetadata_Roundtrip", func(t *testing.T) {
|
||||
got, err := store.GetIssue(ctx, withmeta.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetIssue: %v", err)
|
||||
}
|
||||
if got.Metadata == nil {
|
||||
t.Fatal("expected metadata to be set, got nil")
|
||||
}
|
||||
|
||||
meta := json.RawMessage(`{"team":"platform","sprint":"Q1","points":5}`)
|
||||
issue := &types.Issue{
|
||||
Title: "Issue with metadata",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: meta,
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
var parsed map[string]interface{}
|
||||
if err := json.Unmarshal(got.Metadata, &parsed); err != nil {
|
||||
t.Fatalf("failed to parse metadata: %v", err)
|
||||
}
|
||||
if parsed["wm_team"] != "platform" {
|
||||
t.Errorf("expected wm_team=platform, got %v", parsed["wm_team"])
|
||||
}
|
||||
if parsed["wm_sprint"] != "Q1" {
|
||||
t.Errorf("expected wm_sprint=Q1, got %v", parsed["wm_sprint"])
|
||||
}
|
||||
// JSON numbers unmarshal as float64
|
||||
if parsed["wm_points"] != float64(5) {
|
||||
t.Errorf("expected wm_points=5, got %v", parsed["wm_points"])
|
||||
}
|
||||
})
|
||||
|
||||
// Fetch and verify metadata round-trips
|
||||
got, err := store.GetIssue(ctx, issue.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetIssue: %v", err)
|
||||
}
|
||||
if got.Metadata == nil {
|
||||
t.Fatal("expected metadata to be set, got nil")
|
||||
}
|
||||
|
||||
var parsed map[string]interface{}
|
||||
if err := json.Unmarshal(got.Metadata, &parsed); err != nil {
|
||||
t.Fatalf("failed to parse metadata: %v", err)
|
||||
}
|
||||
if parsed["team"] != "platform" {
|
||||
t.Errorf("expected team=platform, got %v", parsed["team"])
|
||||
}
|
||||
if parsed["sprint"] != "Q1" {
|
||||
t.Errorf("expected sprint=Q1, got %v", parsed["sprint"])
|
||||
}
|
||||
// JSON numbers unmarshal as float64
|
||||
if parsed["points"] != float64(5) {
|
||||
t.Errorf("expected points=5, got %v", parsed["points"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateIssue_WithMetadata_Queryable verifies metadata set at creation time is queryable.
|
||||
func TestCreateIssue_WithMetadata_Queryable(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
Title: "Queryable metadata",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"backend"}`),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
|
||||
// Search by metadata field
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{"team": "backend"},
|
||||
t.Run("CreateIssue_WithMetadata_Queryable", func(t *testing.T) {
|
||||
results, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MetadataFields: map[string]string{"qm_team": "backend"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != queryable.ID {
|
||||
t.Errorf("expected issue %s, got %s", queryable.ID, results[0].ID)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SearchIssues: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != issue.ID {
|
||||
t.Errorf("expected issue %s, got %s", issue.ID, results[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
// Key validation unit tests (don't need a store)
|
||||
|
|
|
|||
|
|
@ -10,129 +10,85 @@ import (
|
|||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func TestGetReadyWork_MetadataFieldMatch(t *testing.T) {
|
||||
func TestGetReadyWork_MetadataSuite(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
issue1 := &types.Issue{
|
||||
Title: "Platform task",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"platform"}`),
|
||||
// Create all test data up front with unique metadata keys per subtest.
|
||||
allIssues := []*types.Issue{
|
||||
// --- FieldMatch data ---
|
||||
{ID: "mr-fm-1", Title: "Platform task (fm)", Priority: 2, IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"mr_fm_team":"platform"}`)},
|
||||
{ID: "mr-fm-2", Title: "Frontend task (fm)", Priority: 2, IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"mr_fm_team":"frontend"}`)},
|
||||
// --- HasMetadataKey data ---
|
||||
{ID: "mr-hmk-1", Title: "Has team (hmk)", Priority: 2, IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"mr_hmk_team":"platform"}`)},
|
||||
{ID: "mr-hmk-2", Title: "No metadata (hmk)", Priority: 2, IssueType: types.TypeTask, Status: types.StatusOpen},
|
||||
// --- NoMatch data ---
|
||||
{ID: "mr-nm-1", Title: "Platform task (nm)", Priority: 2, IssueType: types.TypeTask, Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"mr_nm_team":"platform"}`)},
|
||||
}
|
||||
issue2 := &types.Issue{
|
||||
Title: "Frontend task",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"frontend"}`),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue1, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue2, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
for _, issue := range allIssues {
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue(%s): %v", issue.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
results, err := store.GetReadyWork(ctx, types.WorkFilter{
|
||||
Status: "open",
|
||||
MetadataFields: map[string]string{"team": "platform"},
|
||||
t.Run("FieldMatch", func(t *testing.T) {
|
||||
results, err := store.GetReadyWork(ctx, types.WorkFilter{
|
||||
Status: "open",
|
||||
MetadataFields: map[string]string{"mr_fm_team": "platform"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReadyWork: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != "mr-fm-1" {
|
||||
t.Errorf("expected issue mr-fm-1, got %s", results[0].ID)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReadyWork: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != issue1.ID {
|
||||
t.Errorf("expected issue %s, got %s", issue1.ID, results[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetReadyWork_HasMetadataKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
issue1 := &types.Issue{
|
||||
Title: "Has team",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"platform"}`),
|
||||
}
|
||||
issue2 := &types.Issue{
|
||||
Title: "No metadata",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue1, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue2, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
|
||||
results, err := store.GetReadyWork(ctx, types.WorkFilter{
|
||||
Status: "open",
|
||||
HasMetadataKey: "team",
|
||||
t.Run("HasMetadataKey", func(t *testing.T) {
|
||||
results, err := store.GetReadyWork(ctx, types.WorkFilter{
|
||||
Status: "open",
|
||||
HasMetadataKey: "mr_hmk_team",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReadyWork: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != "mr-hmk-1" {
|
||||
t.Errorf("expected issue mr-hmk-1, got %s", results[0].ID)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReadyWork: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
if results[0].ID != issue1.ID {
|
||||
t.Errorf("expected issue %s, got %s", issue1.ID, results[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetReadyWork_MetadataFieldNoMatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
Title: "Platform task",
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Status: types.StatusOpen,
|
||||
Metadata: json.RawMessage(`{"team":"platform"}`),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue: %v", err)
|
||||
}
|
||||
|
||||
results, err := store.GetReadyWork(ctx, types.WorkFilter{
|
||||
Status: "open",
|
||||
MetadataFields: map[string]string{"team": "backend"},
|
||||
t.Run("FieldNoMatch", func(t *testing.T) {
|
||||
results, err := store.GetReadyWork(ctx, types.WorkFilter{
|
||||
Status: "open",
|
||||
MetadataFields: map[string]string{"mr_nm_team": "backend"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReadyWork: %v", err)
|
||||
}
|
||||
if len(results) != 0 {
|
||||
t.Errorf("expected 0 results, got %d", len(results))
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReadyWork: %v", err)
|
||||
}
|
||||
if len(results) != 0 {
|
||||
t.Errorf("expected 0 results, got %d", len(results))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetReadyWork_MetadataFieldInvalidKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
store := newTestStore(t, tmpDir)
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := store.GetReadyWork(ctx, types.WorkFilter{
|
||||
Status: "open",
|
||||
MetadataFields: map[string]string{"'; DROP TABLE issues; --": "val"},
|
||||
t.Run("FieldInvalidKey", func(t *testing.T) {
|
||||
_, err := store.GetReadyWork(ctx, types.WorkFilter{
|
||||
Status: "open",
|
||||
MetadataFields: map[string]string{"'; DROP TABLE issues; --": "val"},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid metadata key, got nil")
|
||||
}
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid metadata key, got nil")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,139 +11,150 @@ import (
|
|||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// TestThreadTraversal tests the findRepliesTo() and findReplies() functions
|
||||
// that were added in Decision 004 Phase 4 to support message thread navigation.
|
||||
func TestThreadTraversal(t *testing.T) {
|
||||
func TestThreadTraversalSuite(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpDir := t.TempDir()
|
||||
testStore := newTestStore(t, filepath.Join(tmpDir, ".beads", "beads.db"))
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a 3-message thread chain: original → reply1 → reply2
|
||||
now := time.Now()
|
||||
|
||||
// === Linear chain data: original → reply1 → reply2 ===
|
||||
original := &types.Issue{
|
||||
Title: "Original Message",
|
||||
Description: "This is the original message",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: "message",
|
||||
Assignee: "worker",
|
||||
Sender: "manager",
|
||||
Ephemeral: true,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
ID: "tt-orig", Title: "Original Message", Description: "This is the original message",
|
||||
Status: types.StatusOpen, Priority: 2, IssueType: "message",
|
||||
Assignee: "worker", Sender: "manager", Ephemeral: true,
|
||||
CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := testStore.CreateIssue(ctx, original, "test"); err != nil {
|
||||
t.Fatalf("Failed to create original message: %v", err)
|
||||
}
|
||||
|
||||
reply1 := &types.Issue{
|
||||
Title: "Re: Original Message",
|
||||
Description: "This is reply 1",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: "message",
|
||||
Assignee: "manager",
|
||||
Sender: "worker",
|
||||
Ephemeral: true,
|
||||
CreatedAt: now.Add(time.Minute),
|
||||
UpdatedAt: now.Add(time.Minute),
|
||||
ID: "tt-reply1", Title: "Re: Original Message", Description: "This is reply 1",
|
||||
Status: types.StatusOpen, Priority: 2, IssueType: "message",
|
||||
Assignee: "manager", Sender: "worker", Ephemeral: true,
|
||||
CreatedAt: now.Add(time.Minute), UpdatedAt: now.Add(time.Minute),
|
||||
}
|
||||
if err := testStore.CreateIssue(ctx, reply1, "test"); err != nil {
|
||||
t.Fatalf("Failed to create reply1: %v", err)
|
||||
}
|
||||
|
||||
reply2 := &types.Issue{
|
||||
Title: "Re: Re: Original Message",
|
||||
Description: "This is reply 2",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: "message",
|
||||
Assignee: "worker",
|
||||
Sender: "manager",
|
||||
Ephemeral: true,
|
||||
CreatedAt: now.Add(2 * time.Minute),
|
||||
UpdatedAt: now.Add(2 * time.Minute),
|
||||
ID: "tt-reply2", Title: "Re: Re: Original Message", Description: "This is reply 2",
|
||||
Status: types.StatusOpen, Priority: 2, IssueType: "message",
|
||||
Assignee: "worker", Sender: "manager", Ephemeral: true,
|
||||
CreatedAt: now.Add(2 * time.Minute), UpdatedAt: now.Add(2 * time.Minute),
|
||||
}
|
||||
if err := testStore.CreateIssue(ctx, reply2, "test"); err != nil {
|
||||
t.Fatalf("Failed to create reply2: %v", err)
|
||||
for _, msg := range []*types.Issue{original, reply1, reply2} {
|
||||
if err := testStore.CreateIssue(ctx, msg, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue(%s): %v", msg.ID, err)
|
||||
}
|
||||
}
|
||||
for _, dep := range []*types.Dependency{
|
||||
{IssueID: reply1.ID, DependsOnID: original.ID, Type: types.DepRepliesTo, CreatedAt: now.Add(time.Minute)},
|
||||
{IssueID: reply2.ID, DependsOnID: reply1.ID, Type: types.DepRepliesTo, CreatedAt: now.Add(2 * time.Minute)},
|
||||
} {
|
||||
if err := testStore.AddDependency(ctx, dep, "test"); err != nil {
|
||||
t.Fatalf("AddDependency: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Add replies-to dependencies to form the thread chain
|
||||
// reply1 replies to original
|
||||
dep1 := &types.Dependency{
|
||||
IssueID: reply1.ID,
|
||||
DependsOnID: original.ID,
|
||||
Type: types.DepRepliesTo,
|
||||
CreatedAt: now.Add(time.Minute),
|
||||
// === Standalone message data ===
|
||||
standalone := &types.Issue{
|
||||
ID: "tt-standalone", Title: "Standalone Message", Description: "No thread",
|
||||
Status: types.StatusOpen, Priority: 2, IssueType: "message",
|
||||
Assignee: "user", Sender: "sender", Ephemeral: true,
|
||||
CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := testStore.AddDependency(ctx, dep1, "test"); err != nil {
|
||||
t.Fatalf("Failed to add reply1 -> original dependency: %v", err)
|
||||
if err := testStore.CreateIssue(ctx, standalone, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue(standalone): %v", err)
|
||||
}
|
||||
|
||||
// reply2 replies to reply1
|
||||
dep2 := &types.Dependency{
|
||||
IssueID: reply2.ID,
|
||||
DependsOnID: reply1.ID,
|
||||
Type: types.DepRepliesTo,
|
||||
CreatedAt: now.Add(2 * time.Minute),
|
||||
// === Branching data: branchOrig → replyA, replyB ===
|
||||
branchOrig := &types.Issue{
|
||||
ID: "tt-branch-orig", Title: "Branch Original", Description: "Multiple replies",
|
||||
Status: types.StatusOpen, Priority: 2, IssueType: "message",
|
||||
Assignee: "user", Sender: "sender", Ephemeral: true,
|
||||
CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := testStore.AddDependency(ctx, dep2, "test"); err != nil {
|
||||
t.Fatalf("Failed to add reply2 -> reply1 dependency: %v", err)
|
||||
replyA := &types.Issue{
|
||||
ID: "tt-reply-a", Title: "Reply A", Description: "First branch",
|
||||
Status: types.StatusOpen, Priority: 2, IssueType: "message",
|
||||
Assignee: "sender", Sender: "user", Ephemeral: true,
|
||||
CreatedAt: now.Add(time.Minute), UpdatedAt: now.Add(time.Minute),
|
||||
}
|
||||
replyB := &types.Issue{
|
||||
ID: "tt-reply-b", Title: "Reply B", Description: "Second branch",
|
||||
Status: types.StatusOpen, Priority: 2, IssueType: "message",
|
||||
Assignee: "sender", Sender: "another-user", Ephemeral: true,
|
||||
CreatedAt: now.Add(2 * time.Minute), UpdatedAt: now.Add(2 * time.Minute),
|
||||
}
|
||||
for _, msg := range []*types.Issue{branchOrig, replyA, replyB} {
|
||||
if err := testStore.CreateIssue(ctx, msg, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue(%s): %v", msg.ID, err)
|
||||
}
|
||||
}
|
||||
for _, dep := range []*types.Dependency{
|
||||
{IssueID: replyA.ID, DependsOnID: branchOrig.ID, Type: types.DepRepliesTo, CreatedAt: now.Add(time.Minute)},
|
||||
{IssueID: replyB.ID, DependsOnID: branchOrig.ID, Type: types.DepRepliesTo, CreatedAt: now.Add(2 * time.Minute)},
|
||||
} {
|
||||
if err := testStore.AddDependency(ctx, dep, "test"); err != nil {
|
||||
t.Fatalf("AddDependency: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("findRepliesTo walks UP the thread", func(t *testing.T) {
|
||||
// From reply2, should find reply1
|
||||
// === OnlyRepliesTo data: blocks dep should be ignored ===
|
||||
msg1 := &types.Issue{
|
||||
ID: "tt-blocks-1", Title: "Message 1", Description: "Target of blocks dep",
|
||||
Status: types.StatusOpen, Priority: 2, IssueType: "message",
|
||||
Assignee: "user", Sender: "sender", Ephemeral: true,
|
||||
CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
msg2 := &types.Issue{
|
||||
ID: "tt-blocks-2", Title: "Message 2", Description: "Has blocks dep to msg1",
|
||||
Status: types.StatusOpen, Priority: 2, IssueType: "message",
|
||||
Assignee: "user", Sender: "sender", Ephemeral: true,
|
||||
CreatedAt: now.Add(time.Minute), UpdatedAt: now.Add(time.Minute),
|
||||
}
|
||||
for _, msg := range []*types.Issue{msg1, msg2} {
|
||||
if err := testStore.CreateIssue(ctx, msg, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue(%s): %v", msg.ID, err)
|
||||
}
|
||||
}
|
||||
if err := testStore.AddDependency(ctx, &types.Dependency{
|
||||
IssueID: msg2.ID, DependsOnID: msg1.ID, Type: types.DepBlocks, CreatedAt: now.Add(time.Minute),
|
||||
}, "test"); err != nil {
|
||||
t.Fatalf("AddDependency(blocks): %v", err)
|
||||
}
|
||||
|
||||
// === Tests ===
|
||||
|
||||
t.Run("LinearChain_findRepliesTo_walksUp", func(t *testing.T) {
|
||||
parent := findRepliesTo(ctx, reply2.ID, testStore)
|
||||
if parent != reply1.ID {
|
||||
t.Errorf("findRepliesTo(reply2) = %q, want %q", parent, reply1.ID)
|
||||
}
|
||||
|
||||
// From reply1, should find original
|
||||
parent = findRepliesTo(ctx, reply1.ID, testStore)
|
||||
if parent != original.ID {
|
||||
t.Errorf("findRepliesTo(reply1) = %q, want %q", parent, original.ID)
|
||||
}
|
||||
|
||||
// From original, should return empty (no parent)
|
||||
parent = findRepliesTo(ctx, original.ID, testStore)
|
||||
if parent != "" {
|
||||
t.Errorf("findRepliesTo(original) = %q, want empty string", parent)
|
||||
t.Errorf("findRepliesTo(original) = %q, want empty", parent)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("findReplies walks DOWN the thread", func(t *testing.T) {
|
||||
// From original, should find reply1
|
||||
t.Run("LinearChain_findReplies_walksDown", func(t *testing.T) {
|
||||
replies := findReplies(ctx, original.ID, testStore)
|
||||
if len(replies) != 1 {
|
||||
t.Fatalf("findReplies(original) returned %d replies, want 1", len(replies))
|
||||
if len(replies) != 1 || replies[0].ID != reply1.ID {
|
||||
t.Errorf("findReplies(original) = %v, want [%s]", replies, reply1.ID)
|
||||
}
|
||||
if replies[0].ID != reply1.ID {
|
||||
t.Errorf("findReplies(original)[0].ID = %q, want %q", replies[0].ID, reply1.ID)
|
||||
}
|
||||
|
||||
// From reply1, should find reply2
|
||||
replies = findReplies(ctx, reply1.ID, testStore)
|
||||
if len(replies) != 1 {
|
||||
t.Fatalf("findReplies(reply1) returned %d replies, want 1", len(replies))
|
||||
if len(replies) != 1 || replies[0].ID != reply2.ID {
|
||||
t.Errorf("findReplies(reply1) = %v, want [%s]", replies, reply2.ID)
|
||||
}
|
||||
if replies[0].ID != reply2.ID {
|
||||
t.Errorf("findReplies(reply1)[0].ID = %q, want %q", replies[0].ID, reply2.ID)
|
||||
}
|
||||
|
||||
// From reply2, should return empty (no children)
|
||||
replies = findReplies(ctx, reply2.ID, testStore)
|
||||
if len(replies) != 0 {
|
||||
t.Errorf("findReplies(reply2) returned %d replies, want 0", len(replies))
|
||||
t.Errorf("findReplies(reply2) = %d replies, want 0", len(replies))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("thread root finding via repeated findRepliesTo", func(t *testing.T) {
|
||||
// Starting from reply2, walk up via findRepliesTo() until reaching original
|
||||
t.Run("LinearChain_threadRootFinding", func(t *testing.T) {
|
||||
current := reply2.ID
|
||||
var visited []string
|
||||
visited = append(visited, current)
|
||||
|
||||
for {
|
||||
parent := findRepliesTo(ctx, current, testStore)
|
||||
if parent == "" {
|
||||
|
|
@ -152,167 +163,42 @@ func TestThreadTraversal(t *testing.T) {
|
|||
current = parent
|
||||
visited = append(visited, current)
|
||||
}
|
||||
|
||||
// Should have visited: reply2, reply1, original
|
||||
if len(visited) != 3 {
|
||||
t.Fatalf("Thread walk visited %d nodes, want 3: %v", len(visited), visited)
|
||||
}
|
||||
|
||||
// Final current should be the original (root)
|
||||
if current != original.ID {
|
||||
t.Errorf("Thread root = %q, want %q", current, original.ID)
|
||||
}
|
||||
})
|
||||
|
||||
// Verify visited order: reply2 -> reply1 -> original
|
||||
if visited[0] != reply2.ID {
|
||||
t.Errorf("visited[0] = %q, want %q", visited[0], reply2.ID)
|
||||
t.Run("EmptyThread_standalone", func(t *testing.T) {
|
||||
parent := findRepliesTo(ctx, standalone.ID, testStore)
|
||||
if parent != "" {
|
||||
t.Errorf("findRepliesTo(standalone) = %q, want empty", parent)
|
||||
}
|
||||
if visited[1] != reply1.ID {
|
||||
t.Errorf("visited[1] = %q, want %q", visited[1], reply1.ID)
|
||||
}
|
||||
if visited[2] != original.ID {
|
||||
t.Errorf("visited[2] = %q, want %q", visited[2], original.ID)
|
||||
replies := findReplies(ctx, standalone.ID, testStore)
|
||||
if len(replies) != 0 {
|
||||
t.Errorf("findReplies(standalone) = %d, want 0", len(replies))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestThreadTraversalEmptyThread tests thread traversal with an isolated message
|
||||
func TestThreadTraversalEmptyThread(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
testStore := newTestStore(t, filepath.Join(tmpDir, ".beads", "beads.db"))
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a single message with no thread
|
||||
now := time.Now()
|
||||
standalone := &types.Issue{
|
||||
Title: "Standalone Message",
|
||||
Description: "This message has no thread",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: "message",
|
||||
Assignee: "user",
|
||||
Sender: "sender",
|
||||
Ephemeral: true,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := testStore.CreateIssue(ctx, standalone, "test"); err != nil {
|
||||
t.Fatalf("Failed to create standalone message: %v", err)
|
||||
}
|
||||
|
||||
// findRepliesTo should return empty
|
||||
parent := findRepliesTo(ctx, standalone.ID, testStore)
|
||||
if parent != "" {
|
||||
t.Errorf("findRepliesTo(standalone) = %q, want empty string", parent)
|
||||
}
|
||||
|
||||
// findReplies should return empty slice
|
||||
replies := findReplies(ctx, standalone.ID, testStore)
|
||||
if len(replies) != 0 {
|
||||
t.Errorf("findReplies(standalone) returned %d replies, want 0", len(replies))
|
||||
}
|
||||
}
|
||||
|
||||
// TestThreadTraversalBranching tests a branching thread (one message with multiple replies)
|
||||
func TestThreadTraversalBranching(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
testStore := newTestStore(t, filepath.Join(tmpDir, ".beads", "beads.db"))
|
||||
ctx := context.Background()
|
||||
|
||||
now := time.Now()
|
||||
|
||||
// Create original message
|
||||
original := &types.Issue{
|
||||
Title: "Original Message",
|
||||
Description: "This message will have multiple replies",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: "message",
|
||||
Assignee: "user",
|
||||
Sender: "sender",
|
||||
Ephemeral: true,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := testStore.CreateIssue(ctx, original, "test"); err != nil {
|
||||
t.Fatalf("Failed to create original message: %v", err)
|
||||
}
|
||||
|
||||
// Create two replies to the original (branching)
|
||||
replyA := &types.Issue{
|
||||
Title: "Reply A",
|
||||
Description: "First branch reply",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: "message",
|
||||
Assignee: "sender",
|
||||
Sender: "user",
|
||||
Ephemeral: true,
|
||||
CreatedAt: now.Add(time.Minute),
|
||||
UpdatedAt: now.Add(time.Minute),
|
||||
}
|
||||
if err := testStore.CreateIssue(ctx, replyA, "test"); err != nil {
|
||||
t.Fatalf("Failed to create replyA: %v", err)
|
||||
}
|
||||
|
||||
replyB := &types.Issue{
|
||||
Title: "Reply B",
|
||||
Description: "Second branch reply",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: "message",
|
||||
Assignee: "sender",
|
||||
Sender: "another-user",
|
||||
Ephemeral: true,
|
||||
CreatedAt: now.Add(2 * time.Minute),
|
||||
UpdatedAt: now.Add(2 * time.Minute),
|
||||
}
|
||||
if err := testStore.CreateIssue(ctx, replyB, "test"); err != nil {
|
||||
t.Fatalf("Failed to create replyB: %v", err)
|
||||
}
|
||||
|
||||
// Both replies point to original
|
||||
depA := &types.Dependency{
|
||||
IssueID: replyA.ID,
|
||||
DependsOnID: original.ID,
|
||||
Type: types.DepRepliesTo,
|
||||
CreatedAt: now.Add(time.Minute),
|
||||
}
|
||||
if err := testStore.AddDependency(ctx, depA, "test"); err != nil {
|
||||
t.Fatalf("Failed to add replyA -> original dependency: %v", err)
|
||||
}
|
||||
|
||||
depB := &types.Dependency{
|
||||
IssueID: replyB.ID,
|
||||
DependsOnID: original.ID,
|
||||
Type: types.DepRepliesTo,
|
||||
CreatedAt: now.Add(2 * time.Minute),
|
||||
}
|
||||
if err := testStore.AddDependency(ctx, depB, "test"); err != nil {
|
||||
t.Fatalf("Failed to add replyB -> original dependency: %v", err)
|
||||
}
|
||||
|
||||
t.Run("findRepliesTo from branches find original", func(t *testing.T) {
|
||||
t.Run("Branching_findRepliesTo", func(t *testing.T) {
|
||||
parentA := findRepliesTo(ctx, replyA.ID, testStore)
|
||||
if parentA != original.ID {
|
||||
t.Errorf("findRepliesTo(replyA) = %q, want %q", parentA, original.ID)
|
||||
if parentA != branchOrig.ID {
|
||||
t.Errorf("findRepliesTo(replyA) = %q, want %q", parentA, branchOrig.ID)
|
||||
}
|
||||
|
||||
parentB := findRepliesTo(ctx, replyB.ID, testStore)
|
||||
if parentB != original.ID {
|
||||
t.Errorf("findRepliesTo(replyB) = %q, want %q", parentB, original.ID)
|
||||
if parentB != branchOrig.ID {
|
||||
t.Errorf("findRepliesTo(replyB) = %q, want %q", parentB, branchOrig.ID)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("findReplies from original returns both branches", func(t *testing.T) {
|
||||
replies := findReplies(ctx, original.ID, testStore)
|
||||
t.Run("Branching_findReplies_returnsBoth", func(t *testing.T) {
|
||||
replies := findReplies(ctx, branchOrig.ID, testStore)
|
||||
if len(replies) != 2 {
|
||||
t.Fatalf("findReplies(original) returned %d replies, want 2", len(replies))
|
||||
t.Fatalf("findReplies(branchOrig) = %d, want 2", len(replies))
|
||||
}
|
||||
|
||||
// Verify both replies are present (order may vary)
|
||||
foundA := false
|
||||
foundB := false
|
||||
foundA, foundB := false, false
|
||||
for _, r := range replies {
|
||||
if r.ID == replyA.ID {
|
||||
foundA = true
|
||||
|
|
@ -321,96 +207,30 @@ func TestThreadTraversalBranching(t *testing.T) {
|
|||
foundB = true
|
||||
}
|
||||
}
|
||||
if !foundA {
|
||||
t.Errorf("findReplies(original) missing replyA")
|
||||
if !foundA || !foundB {
|
||||
t.Errorf("missing replies: A=%v B=%v", foundA, foundB)
|
||||
}
|
||||
if !foundB {
|
||||
t.Errorf("findReplies(original) missing replyB")
|
||||
})
|
||||
|
||||
t.Run("NonexistentIssue", func(t *testing.T) {
|
||||
parent := findRepliesTo(ctx, "nonexistent-id", testStore)
|
||||
if parent != "" {
|
||||
t.Errorf("findRepliesTo(nonexistent) = %q, want empty", parent)
|
||||
}
|
||||
replies := findReplies(ctx, "nonexistent-id", testStore)
|
||||
if len(replies) != 0 {
|
||||
t.Errorf("findReplies(nonexistent) = %d, want 0", len(replies))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("OnlyRepliesTo_ignoresBlocksDeps", func(t *testing.T) {
|
||||
parent := findRepliesTo(ctx, msg2.ID, testStore)
|
||||
if parent != "" {
|
||||
t.Errorf("findRepliesTo(msg2) = %q, want empty (blocks should be ignored)", parent)
|
||||
}
|
||||
replies := findReplies(ctx, msg1.ID, testStore)
|
||||
if len(replies) != 0 {
|
||||
t.Errorf("findReplies(msg1) = %d, want 0 (blocks should be ignored)", len(replies))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestThreadTraversalNonexistentIssue tests behavior with nonexistent issue IDs
|
||||
func TestThreadTraversalNonexistentIssue(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
testStore := newTestStore(t, filepath.Join(tmpDir, ".beads", "beads.db"))
|
||||
ctx := context.Background()
|
||||
|
||||
// findRepliesTo with nonexistent ID should return empty
|
||||
parent := findRepliesTo(ctx, "nonexistent-id", testStore)
|
||||
if parent != "" {
|
||||
t.Errorf("findRepliesTo(nonexistent) = %q, want empty string", parent)
|
||||
}
|
||||
|
||||
// findReplies with nonexistent ID should return nil/empty
|
||||
replies := findReplies(ctx, "nonexistent-id", testStore)
|
||||
if len(replies) != 0 {
|
||||
t.Errorf("findReplies(nonexistent) returned %d replies, want 0", len(replies))
|
||||
}
|
||||
}
|
||||
|
||||
// TestThreadTraversalOnlyRepliesTo verifies that only replies-to dependencies are followed
|
||||
func TestThreadTraversalOnlyRepliesTo(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
testStore := newTestStore(t, filepath.Join(tmpDir, ".beads", "beads.db"))
|
||||
ctx := context.Background()
|
||||
|
||||
now := time.Now()
|
||||
|
||||
// Create three messages to test different dependency types
|
||||
msg1 := &types.Issue{
|
||||
Title: "Message 1",
|
||||
Description: "First message (target of blocks dep)",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: "message",
|
||||
Assignee: "user",
|
||||
Sender: "sender",
|
||||
Ephemeral: true,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := testStore.CreateIssue(ctx, msg1, "test"); err != nil {
|
||||
t.Fatalf("Failed to create msg1: %v", err)
|
||||
}
|
||||
|
||||
msg2 := &types.Issue{
|
||||
Title: "Message 2",
|
||||
Description: "Second message with blocks dependency to msg1",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: "message",
|
||||
Assignee: "user",
|
||||
Sender: "sender",
|
||||
Ephemeral: true,
|
||||
CreatedAt: now.Add(time.Minute),
|
||||
UpdatedAt: now.Add(time.Minute),
|
||||
}
|
||||
if err := testStore.CreateIssue(ctx, msg2, "test"); err != nil {
|
||||
t.Fatalf("Failed to create msg2: %v", err)
|
||||
}
|
||||
|
||||
// Add a "blocks" dependency (NOT replies-to)
|
||||
// msg2 depends on (blocks) msg1
|
||||
blocksDep := &types.Dependency{
|
||||
IssueID: msg2.ID,
|
||||
DependsOnID: msg1.ID,
|
||||
Type: types.DepBlocks,
|
||||
CreatedAt: now.Add(time.Minute),
|
||||
}
|
||||
if err := testStore.AddDependency(ctx, blocksDep, "test"); err != nil {
|
||||
t.Fatalf("Failed to add blocks dependency: %v", err)
|
||||
}
|
||||
|
||||
// findRepliesTo should NOT find msg1 (blocks dependency, not replies-to)
|
||||
parent := findRepliesTo(ctx, msg2.ID, testStore)
|
||||
if parent != "" {
|
||||
t.Errorf("findRepliesTo(msg2) = %q, want empty (blocks dep should be ignored)", parent)
|
||||
}
|
||||
|
||||
// findReplies from msg1 should NOT find msg2 (blocks dependency, not replies-to)
|
||||
replies := findReplies(ctx, msg1.ID, testStore)
|
||||
if len(replies) != 0 {
|
||||
t.Errorf("findReplies(msg1) returned %d replies, want 0 (blocks dep should be ignored)", len(replies))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -307,9 +307,15 @@ func endSpan(span trace.Span, err error) {
|
|||
// execContext wraps a write statement in an explicit BEGIN/COMMIT to ensure
|
||||
// durability when the Dolt server runs with autocommit disabled (the default
|
||||
// when started with --no-auto-commit). Without this, writes remain in an
|
||||
// ErrStoreClosed is returned when an operation is attempted on a closed store.
|
||||
var ErrStoreClosed = errors.New("store is closed")
|
||||
|
||||
// uncommitted implicit transaction that Dolt rolls back on connection close,
|
||||
// causing silent data loss for callers that do not use db.BeginTx themselves.
|
||||
func (s *DoltStore) execContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
|
||||
if s.closed.Load() {
|
||||
return nil, ErrStoreClosed
|
||||
}
|
||||
ctx, span := doltTracer.Start(ctx, "dolt.exec",
|
||||
trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(append(s.doltSpanAttrs(),
|
||||
|
|
@ -344,6 +350,9 @@ func (s *DoltStore) DB() *sql.DB {
|
|||
|
||||
// queryContext wraps s.db.QueryContext with retry for transient errors.
|
||||
func (s *DoltStore) queryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
|
||||
if s.closed.Load() {
|
||||
return nil, ErrStoreClosed
|
||||
}
|
||||
ctx, span := doltTracer.Start(ctx, "dolt.query",
|
||||
trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(append(s.doltSpanAttrs(),
|
||||
|
|
@ -370,6 +379,9 @@ func (s *DoltStore) queryContext(ctx context.Context, query string, args ...any)
|
|||
// queryRowContext wraps s.db.QueryRowContext with retry for transient errors.
|
||||
// The scan function receives the *sql.Row and should call .Scan() on it.
|
||||
func (s *DoltStore) queryRowContext(ctx context.Context, scan func(*sql.Row) error, query string, args ...any) error {
|
||||
if s.closed.Load() {
|
||||
return ErrStoreClosed
|
||||
}
|
||||
ctx, span := doltTracer.Start(ctx, "dolt.query_row",
|
||||
trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(append(s.doltSpanAttrs(),
|
||||
|
|
@ -886,6 +898,11 @@ func isOnlyComments(stmt string) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// IsClosed returns true if the store has been closed.
|
||||
func (s *DoltStore) IsClosed() bool {
|
||||
return s.closed.Load()
|
||||
}
|
||||
|
||||
// Close closes the database connection and removes any 0-byte noms LOCK files
|
||||
// left behind by the embedded Dolt engine.
|
||||
func (s *DoltStore) Close() error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue