~bigbes/sr-ht-spec

ref: db7d6c0fc92cf56db8913d4af2778d0ddb6dc73e sr-ht-spec/db/proposal_test.go -rw-r--r-- 9.2 KiB
db7d6c0f — bigbes docs: correct the merge model against implementation 27 days ago
                                                                                
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
package db

import (
	"context"
	"errors"
	"testing"

	"sourcecraft.dev/bigbes/sr-ht-spec/core"
)

func TestProposalOpenAndRead(t *testing.T) {
	s, _, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	space := mkSpace(t, s, ctx, "bigbes", "rfcs")
	p := mkProposal(t, s, ctx, space.ID, "Add storage model section")

	if p.ID == 0 {
		t.Fatal("expected a non-zero proposal id")
	}
	if p.Branch != ProposalBranch(p.ID) {
		t.Fatalf("branch = %q, want %q", p.Branch, ProposalBranch(p.ID))
	}
	if p.State != core.StateOpen {
		t.Fatalf("state = %q, want open", p.State)
	}
	if p.Approval != "" || p.MergedRev != "" || p.Resolved != nil {
		t.Fatalf("a fresh proposal must not be resolved: %+v", p)
	}

	got, err := s.GetProposal(ctx, p.ID)
	if err != nil {
		t.Fatalf("get proposal: %v", err)
	}
	if got.Title != p.Title || got.Rationale != "because" || got.BaseRev != p.BaseRev {
		t.Fatalf("round-trip mismatch: %+v", got)
	}
	if got.Agent != "claude-code/spec-writer" || got.AgentSession == "" {
		t.Fatalf("provenance lost: %+v", got)
	}
	if got.Branch != p.Branch || got.State != core.StateOpen {
		t.Fatalf("unexpected proposal: %+v", got)
	}

	if _, err := s.GetProposal(ctx, 99999); !errors.Is(err, ErrNotFound) {
		t.Fatalf("missing proposal = %v, want ErrNotFound", err)
	}

	// An empty rationale round-trips as "" rather than surfacing a NULL.
	bare, err := s.OpenProposal(ctx, &Proposal{
		SpaceID: space.ID, Title: "no rationale", BaseRev: "abc",
		Agent: "a", AgentSession: "s",
	})
	if err != nil {
		t.Fatalf("open bare proposal: %v", err)
	}
	got, err = s.GetProposal(ctx, bare.ID)
	if err != nil {
		t.Fatalf("get bare proposal: %v", err)
	}
	if got.Rationale != "" {
		t.Fatalf("rationale = %q, want empty", got.Rationale)
	}
}

func TestProposalListByState(t *testing.T) {
	s, _, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	space := mkSpace(t, s, ctx, "bigbes", "rfcs")
	first := mkProposal(t, s, ctx, space.ID, "first")
	second := mkProposal(t, s, ctx, space.ID, "second")
	third := mkProposal(t, s, ctx, space.ID, "third")

	open, err := s.ListProposalsByState(ctx, core.StateOpen, 0)
	if err != nil {
		t.Fatalf("list open: %v", err)
	}
	if len(open) != 3 {
		t.Fatalf("expected 3 open proposals, got %d", len(open))
	}
	// Newest first.
	if open[0].ID != third.ID || open[2].ID != first.ID {
		t.Fatalf("expected newest first, got %d, %d, %d", open[0].ID, open[1].ID, open[2].ID)
	}

	limited, err := s.ListProposalsByState(ctx, core.StateOpen, 2)
	if err != nil {
		t.Fatalf("list limited: %v", err)
	}
	if len(limited) != 2 || limited[0].ID != third.ID {
		t.Fatalf("unexpected limited list: %+v", limited)
	}

	if err := s.RejectProposal(ctx, second.ID); err != nil {
		t.Fatalf("reject: %v", err)
	}
	rejected, err := s.ListProposalsByState(ctx, core.StateRejected, 0)
	if err != nil {
		t.Fatalf("list rejected: %v", err)
	}
	if len(rejected) != 1 || rejected[0].ID != second.ID {
		t.Fatalf("unexpected rejected list: %+v", rejected)
	}
	if rejected[0].Resolved == nil {
		t.Fatal("a rejected proposal must carry a resolved timestamp")
	}
	if rejected[0].Approval != "" || rejected[0].MergedRev != "" {
		t.Fatalf("a rejection must not record approval or a merged rev: %+v", rejected[0])
	}
	merged, err := s.ListProposalsByState(ctx, core.StateMerged, 0)
	if err != nil {
		t.Fatalf("list merged: %v", err)
	}
	if len(merged) != 0 {
		t.Fatalf("expected no merged proposals, got %d", len(merged))
	}
}

func TestProposalTransitionsAreGuarded(t *testing.T) {
	s, _, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	space := mkSpace(t, s, ctx, "bigbes", "rfcs")

	merged := mkProposal(t, s, ctx, space.ID, "to merge")
	if err := s.MarkProposalMerged(ctx, merged.ID, core.ApprovalHuman, "deadbeef"); err != nil {
		t.Fatalf("merge: %v", err)
	}
	got, err := s.GetProposal(ctx, merged.ID)
	if err != nil {
		t.Fatalf("get merged: %v", err)
	}
	if got.State != core.StateMerged || got.Approval != core.ApprovalHuman || got.MergedRev != "deadbeef" {
		t.Fatalf("unexpected merged proposal: %+v", got)
	}
	if got.Resolved == nil {
		t.Fatal("a merged proposal must carry a resolved timestamp")
	}

	// merged -> merged and merged -> rejected must both fail.
	if err := s.MarkProposalMerged(ctx, merged.ID, core.ApprovalHuman, "cafe"); !errors.Is(err, core.ErrInvalidTransition) {
		t.Fatalf("re-merge = %v, want ErrInvalidTransition", err)
	}
	if err := s.RejectProposal(ctx, merged.ID); !errors.Is(err, core.ErrInvalidTransition) {
		t.Fatalf("merged->rejected = %v, want ErrInvalidTransition", err)
	}
	// The row did not move.
	got, err = s.GetProposal(ctx, merged.ID)
	if err != nil {
		t.Fatalf("get merged: %v", err)
	}
	if got.State != core.StateMerged || got.MergedRev != "deadbeef" {
		t.Fatalf("illegal transition modified the row: %+v", got)
	}

	rejected := mkProposal(t, s, ctx, space.ID, "to reject")
	if err := s.RejectProposal(ctx, rejected.ID); err != nil {
		t.Fatalf("reject: %v", err)
	}
	if err := s.RejectProposal(ctx, rejected.ID); !errors.Is(err, core.ErrInvalidTransition) {
		t.Fatalf("re-reject = %v, want ErrInvalidTransition", err)
	}
	if err := s.MarkProposalMerged(ctx, rejected.ID, core.ApprovalPolicy, "cafe"); !errors.Is(err, core.ErrInvalidTransition) {
		t.Fatalf("rejected->merged = %v, want ErrInvalidTransition", err)
	}

	if err := s.RejectProposal(ctx, 99999); !errors.Is(err, ErrNotFound) {
		t.Fatalf("resolving a missing proposal = %v, want ErrNotFound", err)
	}
	if err := s.MarkProposalMerged(ctx, 99999, core.ApprovalHuman, "cafe"); !errors.Is(err, ErrNotFound) {
		t.Fatalf("merging a missing proposal = %v, want ErrNotFound", err)
	}
}

// TestMergeProposalIsAtomic is the reason the transaction exists: a merge that
// cannot re-point the registry must not leave the proposal reading merged.
func TestMergeProposalIsAtomic(t *testing.T) {
	s, _, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	specs := mkSpace(t, s, ctx, "bigbes", "rfcs")
	notes := mkSpace(t, s, ctx, "bigbes", "notes")
	foreign := docID(t, "NOTE-1")
	if _, err := s.RegisterDocID(ctx, notes.ID, DocRef{ID: foreign, Path: "n.md"}, "rev1"); err != nil {
		t.Fatalf("register: %v", err)
	}

	p := mkProposal(t, s, ctx, specs.ID, "claims a foreign id")
	err := s.MergeProposal(ctx, Merge{
		ProposalID: p.ID,
		SpaceID:    specs.ID,
		Approval:   core.ApprovalHuman,
		MergedRev:  "deadbeef",
		Docs: []DocRef{
			{ID: docID(t, "SPEC-1"), Path: "a.md"},
			{ID: foreign, Path: "b.md"},
		},
	})
	if !errors.Is(err, ErrDocIDTaken) {
		t.Fatalf("merge with a colliding id = %v, want ErrDocIDTaken", err)
	}
	got, err := s.GetProposal(ctx, p.ID)
	if err != nil {
		t.Fatalf("get proposal: %v", err)
	}
	if got.State != core.StateOpen {
		t.Fatalf("failed merge left the proposal %s, want open", got.State)
	}
	// The whole batch rolled back, including the ID that would have applied.
	if _, err := s.DocByID(ctx, docID(t, "SPEC-1")); !errors.Is(err, ErrNotFound) {
		t.Fatalf("failed merge registered SPEC-1 anyway: %v", err)
	}

	// The same merge without the collision commits both halves.
	if err := s.MergeProposal(ctx, Merge{
		ProposalID: p.ID,
		SpaceID:    specs.ID,
		Approval:   core.ApprovalPolicy,
		MergedRev:  "deadbeef",
		Docs:       []DocRef{{ID: docID(t, "SPEC-1"), Path: "a.md"}},
	}); err != nil {
		t.Fatalf("merge: %v", err)
	}
	got, err = s.GetProposal(ctx, p.ID)
	if err != nil {
		t.Fatalf("get proposal: %v", err)
	}
	if got.State != core.StateMerged || got.Approval != core.ApprovalPolicy {
		t.Fatalf("unexpected merged proposal: %+v", got)
	}
	doc, err := s.DocByID(ctx, docID(t, "SPEC-1"))
	if err != nil {
		t.Fatalf("lookup after merge: %v", err)
	}
	if doc.SpaceID != specs.ID || doc.Path != "a.md" || doc.UpdatedRev != "deadbeef" {
		t.Fatalf("unexpected registry row after merge: %+v", doc)
	}
}

// TestProposalCheckConstraints proves the invariants hold against SQL that does
// not go through this package — a stray UPDATE cannot produce a merged row with
// no approval, or an unknown state.
func TestProposalCheckConstraints(t *testing.T) {
	s, pool, cleanup := newTestStore(t)
	defer cleanup()
	ctx := context.Background()

	space := mkSpace(t, s, ctx, "bigbes", "rfcs")
	p := mkProposal(t, s, ctx, space.ID, "constrained")

	for _, tc := range []struct {
		name string
		q    string
		args []any
	}{
		{"unknown state", `UPDATE proposal SET state = 'abandoned' WHERE id = $1`, []any{p.ID}},
		{"unknown approval", `UPDATE proposal SET approval = 'vibes' WHERE id = $1`, []any{p.ID}},
		{
			"merged without approval",
			`UPDATE proposal SET state = 'merged', merged_rev = 'abc', resolved = now() WHERE id = $1`,
			[]any{p.ID},
		},
		{
			"merged without a merged rev",
			`UPDATE proposal SET state = 'merged', approval = 'human', resolved = now() WHERE id = $1`,
			[]any{p.ID},
		},
		{
			"open but resolved",
			`UPDATE proposal SET resolved = now() WHERE id = $1`,
			[]any{p.ID},
		},
		{
			"approval on an open proposal",
			`UPDATE proposal SET approval = 'human' WHERE id = $1`,
			[]any{p.ID},
		},
		{
			"empty provenance",
			`UPDATE proposal SET agent = '' WHERE id = $1`,
			[]any{p.ID},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			if _, err := pool.ExecContext(ctx, tc.q, tc.args...); err == nil {
				t.Fatalf("%s: expected a constraint violation, got none", tc.name)
			}
		})
	}
}