beads/cmd/bd/todo_embedded_test.go
matt wilkie f443df1f2a
fix: resolve CI failures from flock, version bump, and lint (#2981)
* fix: resolve CI failures from flock, version bump, and lint

Three categories of CI failure on main after the v1.0.0 release:

1. rules.go lint/format: Fix gofmt alignment, errcheck on tw.Flush(),
   gosec G304 (add nosec comment), gosec G306 (use 0o600 perms).

2. doctor_test.go version tracking: The 'slightly outdated version'
   test used 0.55.0 which now has a major version diff from 1.0.0,
   correctly triggering a warning. Update expected status to match.

3. Embedded Dolt concurrent tests: The exclusive flock from #2571
   causes concurrent bd processes to fail with 'one writer at a time'.
   Update 58 concurrent test files to tolerate this expected error
   while still failing on unexpected errors and panics.

Amp-Thread-ID: https://ampcode.com/threads/T-019d5386-b41e-7138-bc81-6ea331b80760
Co-authored-by: Amp <amp@ampcode.com>

* fix: concurrent test post-conditions tolerate flock contention

With exclusive flock (commit 543e1943), only some workers can acquire
the lock in concurrent tests. Update post-conditions in 5 tests to:
- Only verify side effects from workers that succeeded (r.err == nil)
- Require at least 1 worker succeeds (fail if all hit flock)
- Log success/failure counts for visibility
- Still verify correct behavior for successful operations

Tests fixed:
- TestEmbeddedCloseConcurrent: remove Fatalf on failures, skip total count check
- TestEmbeddedDeferConcurrent: only check deferred status for successful workers
- TestEmbeddedUndeferConcurrent: only check open status for successful workers
- TestEmbeddedReopenConcurrent: only check open status for successful workers
- TestEmbeddedPromoteCLIConcurrent: only check permanent for successful workers

Amp-Thread-ID: https://ampcode.com/threads/T-019d5386-b41e-7138-bc81-6ea331b80760
Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019d5386-b41e-7138-bc81-6ea331b80760
Co-authored-by: Amp <amp@ampcode.com>

---------

Co-authored-by: Amp <amp@ampcode.com>
2026-04-03 07:27:00 -07:00

301 lines
8.2 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
)
// bdTodo runs "bd todo" with the given args and returns stdout.
func bdTodo(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"todo"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd todo %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
func TestEmbeddedTodo(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "td")
// ===== todo add =====
t.Run("todo_add_basic", func(t *testing.T) {
out := bdTodo(t, bd, dir, "add", "Buy groceries")
if !strings.Contains(out, "Created") {
t.Errorf("expected 'Created' in output: %s", out)
}
if !strings.Contains(out, "Buy groceries") {
t.Errorf("expected title in output: %s", out)
}
})
t.Run("todo_add_json", func(t *testing.T) {
cmd := exec.Command(bd, "todo", "add", "JSON todo", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd todo add --json failed: %v\n%s", err, out)
}
s := strings.TrimSpace(string(out))
start := strings.Index(s, "{")
if start < 0 {
t.Fatalf("no JSON in output: %s", s)
}
var m map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &m); err != nil {
t.Fatalf("parse todo JSON: %v\n%s", err, s)
}
if m["title"] != "JSON todo" {
t.Errorf("expected title 'JSON todo', got %v", m["title"])
}
if m["issue_type"] != "task" {
t.Errorf("expected type task, got %v", m["issue_type"])
}
})
t.Run("todo_add_priority", func(t *testing.T) {
out := bdTodo(t, bd, dir, "add", "Urgent todo", "--priority", "1")
if !strings.Contains(out, "Created") {
t.Errorf("expected 'Created' in output: %s", out)
}
})
t.Run("todo_add_description", func(t *testing.T) {
out := bdTodo(t, bd, dir, "add", "Described todo", "--description", "Details here")
if !strings.Contains(out, "Created") {
t.Errorf("expected 'Created' in output: %s", out)
}
})
// ===== todo list =====
t.Run("todo_list", func(t *testing.T) {
out := bdTodo(t, bd, dir, "list")
// Should show TODOs we created
if !strings.Contains(out, "Buy groceries") && !strings.Contains(out, "td-") {
t.Logf("todo list output: %s", out)
}
})
t.Run("todo_list_json", func(t *testing.T) {
cmd := exec.Command(bd, "todo", "list", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd todo list --json failed: %v\n%s", err, out)
}
s := strings.TrimSpace(string(out))
start := strings.Index(s, "[")
if start < 0 {
t.Fatalf("no JSON array in output: %s", s)
}
var issues []map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &issues); err != nil {
t.Fatalf("parse todo list JSON: %v\n%s", err, s)
}
if len(issues) == 0 {
t.Error("expected at least one todo in list")
}
})
// ===== todo done =====
t.Run("todo_done_single", func(t *testing.T) {
// Create a todo, then mark done
cmd := exec.Command(bd, "todo", "add", "Done test", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd todo add failed: %v\n%s", err, out)
}
s := strings.TrimSpace(string(out))
start := strings.Index(s, "{")
var m map[string]interface{}
json.Unmarshal([]byte(s[start:]), &m)
id := m["id"].(string)
bdTodo(t, bd, dir, "done", id)
got := bdShow(t, bd, dir, id)
if got.Status != "closed" {
t.Errorf("expected closed after done, got %s", got.Status)
}
})
t.Run("todo_done_multiple", func(t *testing.T) {
cmd1 := exec.Command(bd, "todo", "add", "Multi done 1", "--json")
cmd1.Dir = dir
cmd1.Env = bdEnv(dir)
out1, _ := cmd1.CombinedOutput()
var m1 map[string]interface{}
json.Unmarshal([]byte(strings.TrimSpace(string(out1))[strings.Index(strings.TrimSpace(string(out1)), "{"):]), &m1)
id1 := m1["id"].(string)
cmd2 := exec.Command(bd, "todo", "add", "Multi done 2", "--json")
cmd2.Dir = dir
cmd2.Env = bdEnv(dir)
out2, _ := cmd2.CombinedOutput()
var m2 map[string]interface{}
json.Unmarshal([]byte(strings.TrimSpace(string(out2))[strings.Index(strings.TrimSpace(string(out2)), "{"):]), &m2)
id2 := m2["id"].(string)
bdTodo(t, bd, dir, "done", id1, id2)
got1 := bdShow(t, bd, dir, id1)
got2 := bdShow(t, bd, dir, id2)
if got1.Status != "closed" {
t.Errorf("issue1: expected closed, got %s", got1.Status)
}
if got2.Status != "closed" {
t.Errorf("issue2: expected closed, got %s", got2.Status)
}
})
t.Run("todo_done_with_reason", func(t *testing.T) {
cmd := exec.Command(bd, "todo", "add", "Reason done", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, _ := cmd.CombinedOutput()
var m map[string]interface{}
json.Unmarshal([]byte(strings.TrimSpace(string(out))[strings.Index(strings.TrimSpace(string(out)), "{"):]), &m)
id := m["id"].(string)
bdTodo(t, bd, dir, "done", id, "--reason", "No longer needed")
got := bdShow(t, bd, dir, id)
if got.Status != "closed" {
t.Errorf("expected closed, got %s", got.Status)
}
})
t.Run("todo_done_json", func(t *testing.T) {
cmd := exec.Command(bd, "todo", "add", "JSON done", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, _ := cmd.CombinedOutput()
var m map[string]interface{}
json.Unmarshal([]byte(strings.TrimSpace(string(out))[strings.Index(strings.TrimSpace(string(out)), "{"):]), &m)
id := m["id"].(string)
doneCmd := exec.Command(bd, "todo", "done", id, "--json")
doneCmd.Dir = dir
doneCmd.Env = bdEnv(dir)
doneOut, err := doneCmd.CombinedOutput()
if err != nil {
t.Fatalf("bd todo done --json failed: %v\n%s", err, doneOut)
}
})
// ===== todo list --all =====
t.Run("todo_list_all", func(t *testing.T) {
cmd := exec.Command(bd, "todo", "list", "--all", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd todo list --all --json failed: %v\n%s", err, out)
}
s := strings.TrimSpace(string(out))
start := strings.Index(s, "[")
if start < 0 {
t.Fatalf("no JSON array: %s", s)
}
var issues []map[string]interface{}
json.Unmarshal([]byte(s[start:]), &issues)
// Should include both open and closed todos
hasOpen, hasClosed := false, false
for _, iss := range issues {
if iss["status"] == "open" {
hasOpen = true
}
if iss["status"] == "closed" {
hasClosed = true
}
}
if !hasOpen || !hasClosed {
t.Logf("expected both open and closed with --all: open=%v closed=%v (total=%d)", hasOpen, hasClosed, len(issues))
}
})
// ===== Lifecycle =====
t.Run("todo_lifecycle", func(t *testing.T) {
// Add
out := bdTodo(t, bd, dir, "add", "Lifecycle todo")
if !strings.Contains(out, "Created") {
t.Fatal("expected Created")
}
// List
listOut := bdTodo(t, bd, dir, "list")
if !strings.Contains(listOut, "Lifecycle todo") {
t.Logf("lifecycle todo not in list: %s", listOut)
}
})
}
// TestEmbeddedTodoConcurrent exercises todo operations concurrently.
func TestEmbeddedTodoConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "tx")
const numWorkers = 6
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
title := fmt.Sprintf("w%d-todo", worker)
cmd := exec.Command(bd, "todo", "add", title)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("todo add: %v\n%s", err, out)
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}