parent_integration_test.go
1//go:build integration
2
3package main
4
5import (
6 "os/exec"
7 "strings"
8 "testing"
9)
10
11// setupParentTestRepo initializes a git repo with user and agent identities
12func setupParentTestRepo(t *testing.T) string {
13 t.Helper()
14 tmpDir := t.TempDir()
15
16 initCmd := exec.Command("git", "init")
17 initCmd.Dir = tmpDir
18 if err := initCmd.Run(); err != nil {
19 t.Fatalf("failed to init git repo: %v", err)
20 }
21
22 if err := createIdentity(tmpDir, "Test User", "[email protected]", true); err != nil {
23 t.Fatalf("failed to create user identity: %v", err)
24 }
25 if err := createIdentity(tmpDir, "agent", "", false); err != nil {
26 t.Fatalf("failed to create agent identity: %v", err)
27 }
28
29 return tmpDir
30}
31
32// findIssueByTitle returns the BugIssue with the given title
33func findIssueByTitle(t *testing.T, repoPath, title string) BugIssue {
34 t.Helper()
35 issues, err := LoadBugs(repoPath)
36 if err != nil {
37 t.Fatalf("LoadBugs failed: %v", err)
38 }
39 for _, issue := range issues {
40 if issue.Title == title {
41 return issue
42 }
43 }
44 t.Fatalf("bug with title %q not found", title)
45 return BugIssue{}
46}
47
48// TestNewCommand_WithParent creates a child bug linked to a parent via short ID
49func TestNewCommand_WithParent(t *testing.T) {
50 tmpDir := setupParentTestRepo(t)
51
52 parentTitle := "Parent Bug"
53 if err := runNew(tmpDir, parentTitle, "Parent description", ""); err != nil {
54 t.Fatalf("runNew parent failed: %v", err)
55 }
56 parent := findIssueByTitle(t, tmpDir, parentTitle)
57 if parent.ShortID == "" {
58 t.Fatal("parent has empty short ID")
59 }
60
61 childTitle := "Child Bug"
62 if err := runNew(tmpDir, childTitle, "Child description", parent.ShortID); err != nil {
63 t.Fatalf("runNew child failed: %v", err)
64 }
65
66 child := findIssueByTitle(t, tmpDir, childTitle)
67 if child.ParentID != parent.FullID {
68 t.Errorf("child ParentID = %q, want parent full ID %q", child.ParentID, parent.FullID)
69 }
70}
71
72// TestAgentNewCommand_WithParent creates a child bug as agent linked to a parent
73func TestAgentNewCommand_WithParent(t *testing.T) {
74 tmpDir := setupParentTestRepo(t)
75
76 parentTitle := "Agent Parent Bug"
77 if err := runAgentNew(tmpDir, parentTitle, "Parent description", "", "", false); err != nil {
78 t.Fatalf("runAgentNew parent failed: %v", err)
79 }
80 parent := findIssueByTitle(t, tmpDir, parentTitle)
81
82 childTitle := "Agent Child Bug"
83 if err := runAgentNew(tmpDir, childTitle, "Child description", parent.ShortID, "", false); err != nil {
84 t.Fatalf("runAgentNew child failed: %v", err)
85 }
86
87 child := findIssueByTitle(t, tmpDir, childTitle)
88 if child.ParentID != parent.FullID {
89 t.Errorf("child ParentID = %q, want parent full ID %q", child.ParentID, parent.FullID)
90 }
91}
92
93// TestNewCommand_InvalidParentError tests error for unknown parent ID
94func TestNewCommand_InvalidParentError(t *testing.T) {
95 tmpDir := setupParentTestRepo(t)
96
97 err := runNew(tmpDir, "Child Bug", "Child description", "deadbee")
98 if err == nil {
99 t.Error("expected error for unknown parent ID, got nil")
100 } else if !strings.Contains(err.Error(), "deadbee") {
101 t.Errorf("expected error to mention parent ID, got: %v", err)
102 }
103}