forked from gastownhall/beads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeads_test.go
More file actions
334 lines (287 loc) · 9 KB
/
beads_test.go
File metadata and controls
334 lines (287 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package beads_test
import (
"context"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/steveyegge/beads"
"github.com/steveyegge/beads/internal/testutil"
)
// testServerPort is the port of the shared test Dolt server (0 = not running).
var testServerPort int
func TestMain(m *testing.M) {
os.Setenv("BEADS_TEST_MODE", "1")
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
} else {
defer testutil.TerminateDoltContainer()
testServerPort = testutil.DoltContainerPortInt()
}
code := m.Run()
os.Unsetenv("BEADS_DOLT_PORT")
os.Unsetenv("BEADS_TEST_MODE")
os.Exit(code)
}
func skipIfNoDolt(t *testing.T) {
t.Helper()
if _, err := exec.LookPath("dolt"); err != nil {
t.Skip("Dolt not installed, skipping test")
}
}
func skipIfNoDoltServer(t *testing.T) {
t.Helper()
if testServerPort == 0 {
t.Skip("Test Dolt server not available, skipping test")
}
addr := fmt.Sprintf("127.0.0.1:%d", testServerPort)
conn, err := net.DialTimeout("tcp", addr, 200*time.Millisecond)
if err != nil {
t.Skipf("Dolt server not running on %s, skipping test", addr)
}
_ = conn.Close()
}
func TestOpen(t *testing.T) {
skipIfNoDoltServer(t)
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test-dolt")
ctx := context.Background()
store, err := beads.Open(ctx, dbPath)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer store.Close()
if store == nil {
t.Error("expected non-nil storage")
}
}
func TestFindDatabasePath(t *testing.T) {
// This will return empty string in test environment without a database
path := beads.FindDatabasePath()
// Just verify it doesn't panic
_ = path
}
func TestFindBeadsDir(t *testing.T) {
// This will return empty string or a valid path
dir := beads.FindBeadsDir()
// Just verify it doesn't panic
_ = dir
}
func TestOpenFromConfig_Embedded(t *testing.T) {
// This test requires a running Dolt server (embedded mode is not yet implemented;
// New() always connects via MySQL protocol to dolt sql-server).
skipIfNoDoltServer(t)
// Create a .beads dir with metadata.json configured for embedded mode
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0755); err != nil {
t.Fatalf("failed to create .beads dir: %v", err)
}
metadata := `{"backend":"dolt","database":"dolt","dolt_database":"testdb","dolt_mode":"embedded"}`
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(metadata), 0644); err != nil {
t.Fatalf("failed to write metadata.json: %v", err)
}
ctx := context.Background()
store, err := beads.OpenFromConfig(ctx, beadsDir)
if err != nil {
t.Fatalf("OpenFromConfig (embedded) failed: %v", err)
}
defer store.Close()
if store == nil {
t.Error("expected non-nil storage")
}
}
func TestOpenFromConfig_DefaultsToEmbedded(t *testing.T) {
// This test requires a running Dolt server (embedded mode is not yet implemented;
// New() always connects via MySQL protocol to dolt sql-server).
skipIfNoDoltServer(t)
// metadata.json without dolt_mode should default to embedded
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0755); err != nil {
t.Fatalf("failed to create .beads dir: %v", err)
}
metadata := `{"backend":"dolt","database":"dolt"}`
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(metadata), 0644); err != nil {
t.Fatalf("failed to write metadata.json: %v", err)
}
ctx := context.Background()
store, err := beads.OpenFromConfig(ctx, beadsDir)
if err != nil {
t.Fatalf("OpenFromConfig (default) failed: %v", err)
}
defer store.Close()
if store == nil {
t.Error("expected non-nil storage")
}
}
func TestOpenFromConfig_ServerModeFailsWithoutServer(t *testing.T) {
// Server mode should fail-fast when no server is listening.
// Temporarily unset BEADS_DOLT_PORT/BEADS_TEST_MODE so the config port
// isn't overridden by applyConfigDefaults to the test server.
if prev := os.Getenv("BEADS_DOLT_PORT"); prev != "" {
os.Unsetenv("BEADS_DOLT_PORT")
t.Cleanup(func() { os.Setenv("BEADS_DOLT_PORT", prev) })
}
if prev := os.Getenv("BEADS_TEST_MODE"); prev != "" {
os.Unsetenv("BEADS_TEST_MODE")
t.Cleanup(func() { os.Setenv("BEADS_TEST_MODE", prev) })
}
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0755); err != nil {
t.Fatalf("failed to create .beads dir: %v", err)
}
// Dynamically find an unused port by binding to :0 then closing
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("failed to find free port: %v", err)
}
freePort := ln.Addr().(*net.TCPAddr).Port
ln.Close()
metadata := fmt.Sprintf(`{"backend":"dolt","database":"dolt","dolt_mode":"server","dolt_server_host":"127.0.0.1","dolt_server_port":%d}`, freePort)
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(metadata), 0644); err != nil {
t.Fatalf("failed to write metadata.json: %v", err)
}
ctx := context.Background()
_, openErr := beads.OpenFromConfig(ctx, beadsDir)
if openErr == nil {
t.Fatal("OpenFromConfig (server mode) should fail when no server is running")
}
// Should contain "unreachable" from the fail-fast TCP check
if !strings.Contains(openErr.Error(), "unreachable") {
t.Errorf("expected 'unreachable' in error, got: %v", openErr)
}
}
func TestOpenFromConfig_NoMetadata(t *testing.T) {
skipIfNoDoltServer(t)
// Missing metadata.json should use defaults (server mode)
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0755); err != nil {
t.Fatalf("failed to create .beads dir: %v", err)
}
ctx := context.Background()
store, err := beads.OpenFromConfig(ctx, beadsDir)
if err != nil {
t.Fatalf("OpenFromConfig (no metadata) failed: %v", err)
}
defer store.Close()
if store == nil {
t.Error("expected non-nil storage")
}
}
func TestFindAllDatabases(t *testing.T) {
// This scans the file system, just verify it doesn't panic
dbs := beads.FindAllDatabases()
// Should return a slice (possibly empty)
if dbs == nil {
t.Error("expected non-nil slice")
}
}
// Test that exported constants have correct values
func TestConstants(t *testing.T) {
// Status constants
if beads.StatusOpen != "open" {
t.Errorf("StatusOpen = %q, want %q", beads.StatusOpen, "open")
}
if beads.StatusInProgress != "in_progress" {
t.Errorf("StatusInProgress = %q, want %q", beads.StatusInProgress, "in_progress")
}
if beads.StatusBlocked != "blocked" {
t.Errorf("StatusBlocked = %q, want %q", beads.StatusBlocked, "blocked")
}
if beads.StatusClosed != "closed" {
t.Errorf("StatusClosed = %q, want %q", beads.StatusClosed, "closed")
}
// IssueType constants
if beads.TypeBug != "bug" {
t.Errorf("TypeBug = %q, want %q", beads.TypeBug, "bug")
}
if beads.TypeFeature != "feature" {
t.Errorf("TypeFeature = %q, want %q", beads.TypeFeature, "feature")
}
if beads.TypeTask != "task" {
t.Errorf("TypeTask = %q, want %q", beads.TypeTask, "task")
}
if beads.TypeEpic != "epic" {
t.Errorf("TypeEpic = %q, want %q", beads.TypeEpic, "epic")
}
// DependencyType constants
if beads.DepBlocks != "blocks" {
t.Errorf("DepBlocks = %q, want %q", beads.DepBlocks, "blocks")
}
if beads.DepRelated != "related" {
t.Errorf("DepRelated = %q, want %q", beads.DepRelated, "related")
}
}
func TestPublicAPITypeAssertions(t *testing.T) {
skipIfNoDoltServer(t)
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test-dolt")
ctx := context.Background()
store, err := beads.Open(ctx, dbPath)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer store.Close()
t.Run("RemoteStore", func(t *testing.T) {
rs, ok := store.(beads.RemoteStore)
if !ok {
t.Fatal("store does not satisfy beads.RemoteStore")
}
// Verify a method is callable (no remotes configured, so empty list)
remotes, err := rs.ListRemotes(ctx)
if err != nil {
t.Fatalf("ListRemotes failed: %v", err)
}
_ = remotes
})
t.Run("SyncStore", func(t *testing.T) {
_, ok := store.(beads.SyncStore)
if !ok {
t.Fatal("store does not satisfy beads.SyncStore")
}
})
t.Run("VersionControlReader", func(t *testing.T) {
vcr, ok := store.(beads.VersionControlReader)
if !ok {
t.Fatal("store does not satisfy beads.VersionControlReader")
}
branch, err := vcr.CurrentBranch(ctx)
if err != nil {
t.Fatalf("CurrentBranch failed: %v", err)
}
if branch == "" {
t.Error("expected non-empty branch name")
}
commit, err := vcr.GetCurrentCommit(ctx)
if err != nil {
t.Fatalf("GetCurrentCommit failed: %v", err)
}
if commit == "" {
t.Error("expected non-empty commit hash")
}
exists, err := vcr.CommitExists(ctx, commit)
if err != nil {
t.Fatalf("CommitExists failed: %v", err)
}
if !exists {
t.Errorf("CommitExists(%s) = false, want true", commit)
}
status, err := vcr.Status(ctx)
if err != nil {
t.Fatalf("Status failed: %v", err)
}
_ = status
logs, err := vcr.Log(ctx, 5)
if err != nil {
t.Fatalf("Log failed: %v", err)
}
_ = logs
})
}