forked from gastownhall/beads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_interfaces_test.go
More file actions
149 lines (136 loc) · 5.3 KB
/
Copy pathquery_interfaces_test.go
File metadata and controls
149 lines (136 loc) · 5.3 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
package beads_test
import (
"context"
"path/filepath"
"testing"
"github.com/steveyegge/beads"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/dolt"
)
// Compile-time proof that the concrete Dolt store satisfies each narrow public
// interface (the embedded Dolt store is asserted in query_interfaces_cgo_test.go).
// These pin the interfaces to a real implementation, complementing the
// engine-interface guards in beads.go.
var (
_ beads.IssueClaimer = (*dolt.DoltStore)(nil)
_ beads.EventQuerier = (*dolt.DoltStore)(nil)
_ beads.DependentQuerier = (*dolt.DoltStore)(nil)
_ beads.BlockedQuerier = (*dolt.DoltStore)(nil)
)
// TestQueryInterfacesAgainstRealDolt exercises AsEventQuerier / AsDependentQuerier
// (and their methods) against a live Dolt Storage through the public surface,
// the runtime complement to the compile-time guards.
func TestQueryInterfacesAgainstRealDolt(t *testing.T) {
skipIfNoDoltServer(t)
ctx := context.Background()
store, err := beads.Open(ctx, filepath.Join(t.TempDir(), "qi-dolt"))
if err != nil {
t.Fatalf("Open: %v", err)
}
defer store.Close()
if err := store.SetConfig(ctx, "issue_prefix", "qi"); err != nil {
t.Fatalf("SetConfig: %v", err)
}
mk := func(id string) {
iss := &beads.Issue{ID: id, Title: id, Status: beads.StatusOpen, Priority: 2, IssueType: beads.TypeTask}
if err := store.CreateIssue(ctx, iss, "tester"); err != nil {
t.Fatalf("CreateIssue %s: %v", id, err)
}
}
mk("qi-target")
mk("qi-src")
if err := store.AddDependency(ctx, &beads.Dependency{IssueID: "qi-src", DependsOnID: "qi-target", Type: beads.DepBlocks}, "tester"); err != nil {
t.Fatalf("AddDependency: %v", err)
}
dq, ok := beads.AsDependentQuerier(store)
if !ok {
t.Fatalf("AsDependentQuerier returned ok=false for a live Dolt Storage")
}
deps, err := dq.GetDependentRecords(ctx, "qi-target", "", 100, "")
if err != nil {
t.Fatalf("GetDependentRecords: %v", err)
}
if len(deps) != 1 || deps[0].IssueID != "qi-src" {
t.Fatalf("GetDependentRecords = %v, want [qi-src]", deps)
}
if n, err := dq.CountDependentRecords(ctx, "qi-target", ""); err != nil {
t.Fatalf("CountDependentRecords: %v", err)
} else if n != 1 {
t.Fatalf("CountDependentRecords = %d, want 1", n)
}
byTarget, err := dq.GetDependentRecordsForIssues(ctx, []string{"qi-target"})
if err != nil {
t.Fatalf("GetDependentRecordsForIssues: %v", err)
}
if got := byTarget["qi-target"]; len(got) != 1 || got[0].IssueID != "qi-src" || got[0].DependsOnID != "qi-target" {
t.Fatalf("GetDependentRecordsForIssues[qi-target] = %v, want one row {src=qi-src, target=qi-target}", got)
}
eq, ok := beads.AsEventQuerier(store)
if !ok {
t.Fatalf("AsEventQuerier returned ok=false for a live Dolt Storage")
}
evs, err := eq.EventsSince(ctx, beads.EventCursor{}, "", 100)
if err != nil {
t.Fatalf("EventsSince: %v", err)
}
if len(evs) == 0 {
t.Fatalf("EventsSince returned no durable events after creates")
}
bq, ok := beads.AsBlockedQuerier(store)
if !ok {
t.Fatalf("AsBlockedQuerier returned ok=false for a live Dolt Storage")
}
batch, err := bq.IsBlockedBatch(ctx, []string{"qi-src", "qi-target"})
if err != nil {
t.Fatalf("IsBlockedBatch: %v", err)
}
// qi-src blocks-depends on the open qi-target, so it is blocked; qi-target
// has no open blocker. The batch value must match per-row IsBlocked.
for _, id := range []string{"qi-src", "qi-target"} {
want, _, err := bq.IsBlocked(ctx, id)
if err != nil {
t.Fatalf("IsBlocked(%s): %v", id, err)
}
if batch[id] != want {
t.Fatalf("IsBlockedBatch[%s] = %v, want %v (per-row IsBlocked)", id, batch[id], want)
}
}
if !batch["qi-src"] {
t.Fatalf("IsBlockedBatch[qi-src] = false, want true (blocked by open qi-target)")
}
}
// TestAsAccessorsResolveThroughHookDecorator proves the decorator contract that
// lets the As* accessors use a single direct assertion (no UnwrapStore): a
// HookFiringStore embeds storage.DoltStorage, so the engine-interface narrow
// surfaces (claim / event feed / dependents) promote through it and resolve
// without unwrapping. This is the test that would go red if a future decorator
// stopped forwarding one of them — i.e. the reason the removed fallback was dead
// rather than load-bearing.
func TestAsAccessorsResolveThroughHookDecorator(t *testing.T) {
skipIfNoDoltServer(t)
ctx := context.Background()
store, err := beads.Open(ctx, filepath.Join(t.TempDir(), "deco-dolt"))
if err != nil {
t.Fatalf("Open: %v", err)
}
defer store.Close()
ds, ok := store.(storage.DoltStorage)
if !ok {
t.Fatalf("live Dolt store is not a storage.DoltStorage")
}
// nil runner => passthrough decorator; the narrow surfaces are promoted from
// the embedded engine interface, not overridden.
decorated := storage.NewHookFiringStore(ds, nil)
if _, ok := beads.AsIssueClaimer(decorated); !ok {
t.Errorf("AsIssueClaimer returned ok=false through a HookFiringStore decorator")
}
if _, ok := beads.AsEventQuerier(decorated); !ok {
t.Errorf("AsEventQuerier returned ok=false through a HookFiringStore decorator")
}
if _, ok := beads.AsDependentQuerier(decorated); !ok {
t.Errorf("AsDependentQuerier returned ok=false through a HookFiringStore decorator")
}
if _, ok := beads.AsBlockedQuerier(decorated); !ok {
t.Errorf("AsBlockedQuerier returned ok=false through a HookFiringStore decorator")
}
}