~bigbes/sr-ht-dolt

ref: f91a2e80c6848098c00ef56d75f205e8eb88f744 sr-ht-dolt/beads/truncation_test.go -rw-r--r-- 10.4 KiB
f91a2e80 — Eugene Blikh docs: read_rows answers strings or null 5 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package beads

import (
	"context"
	"fmt"
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"sourcecraft.dev/bigbes/sr-ht-dolt/browse"
)

// --- a session that clips the way the store does -----------------------------

// clippingSession is a BrowseSession that honours offset/limit and reports the
// table's true row count, which is what a real read at limit=Max does: the
// first Max rows, and a Total that says the rest exists.
//
// The clip is performed by the seam rather than written into a fixture by hand.
// A page whose Total simply disagrees with its own Rows would let a test assert
// the flag while never producing the situation the flag is about — and the tail
// row, which is the whole point of the "not in what was read" case, has to be
// genuinely absent from the rows handed to the projection.
type clippingSession struct {
	rowsByTable map[string]*browse.RowPage
}

func (s *clippingSession) Rows(_ context.Context, _, table string, offset, limit int) (*browse.RowPage, error) {
	p, ok := s.rowsByTable[table]
	if !ok {
		return nil, fmt.Errorf("%w: %s", browse.ErrTableNotFound, table)
	}
	total := len(p.Rows)
	lo := offset
	if lo > total {
		lo = total
	}
	hi := total
	if limit > 0 && lo+limit < hi {
		hi = lo + limit
	}
	return &browse.RowPage{Columns: p.Columns, Rows: p.Rows[lo:hi], Offset: lo, Total: total}, nil
}

// --- fixtures past the cap ---------------------------------------------------

// manyIssues builds n issue rows, ids i-0000… in order, cycling through the
// three status categories. With n > Max the ids from index Max on are the tail
// a capped read never sees.
func manyIssues(n int) *browse.RowPage {
	statuses := []string{"open", "in_progress", "closed"}
	rows := make([][]string, 0, n)
	for i := 0; i < n; i++ {
		rows = append(rows, []string{
			issueID(i),
			fmt.Sprintf("Issue %d", i),
			statuses[i%len(statuses)],
			"1",
			"task",
			"alice",
			fmt.Sprintf("2024-01-01 00:00:%02d", i%60),
			"0",
		})
	}
	return &browse.RowPage{
		Columns: []string{"id", "title", "status", "priority", "issue_type", "assignee", "created_at", "is_blocked"},
		Rows:    rows,
		Total:   n,
	}
}

func issueID(i int) string { return fmt.Sprintf("i-%04d", i) }

func statusRows() *browse.RowPage {
	return &browse.RowPage{
		Columns: []string{"name", "category"},
		Rows: [][]string{
			{"open", "open"},
			{"in_progress", "in_progress"},
			{"closed", "closed"},
		},
		Total: 3,
	}
}

// bigTracker is a tracker of Max+5 issues: five of them exist only past the cap.
// Every other table stays small, so a flag raised over this fixture can only
// have come from the issues read.
//
// Two issues carry milestone:m1 — i-0003, which a capped read sees, and
// i-2003, which it does not. The rollup over a clipped read therefore reports
// half a milestone, which is the arithmetic this fixture is here to catch.
func bigTracker() *clippingSession {
	return &clippingSession{rowsByTable: map[string]*browse.RowPage{
		"issues": manyIssues(Max + 5),
		"dependencies": {
			Columns: []string{"id", "issue_id", "depends_on_issue_id", "type"},
			Rows:    [][]string{{"d1", "i-0007", "i-0001", "blocks"}},
			Total:   1,
		},
		"labels": {
			Columns: []string{"issue_id", "label"},
			Rows: [][]string{
				{"i-0003", "milestone:m1"},
				{"i-2003", "milestone:m1"},
				{"i-0007", "backend"},
			},
			Total: 3,
		},
		"custom_statuses": statusRows(),
	}}
}

// smallTrackerWith is three issues and no clip anywhere, plus whatever extra
// tables a test wants to oversize. It isolates the clip to one optional table.
func smallTrackerWith(extra map[string]*browse.RowPage) *clippingSession {
	tables := map[string]*browse.RowPage{
		"issues": manyIssues(3),
		"dependencies": {
			Columns: []string{"id", "issue_id", "depends_on_issue_id", "type"},
			Rows:    [][]string{{"d1", "i-0001", "i-0000", "blocks"}},
			Total:   1,
		},
		"custom_statuses": statusRows(),
	}
	for name, page := range extra {
		tables[name] = page
	}
	return &clippingSession{rowsByTable: tables}
}

// manyComments builds n comment rows, all on i-0000.
func manyComments(n int) *browse.RowPage {
	rows := make([][]string, 0, n)
	for i := 0; i < n; i++ {
		rows = append(rows, []string{"i-0000", "alice", fmt.Sprintf("comment %d", i), "2024-01-05 00:00:00"})
	}
	return &browse.RowPage{
		Columns: []string{"issue_id", "author", "text", "created_at"},
		Rows:    rows,
		Total:   n,
	}
}

func detail(t *testing.T, sess BrowseSession, id string) *Data {
	t.Helper()
	d, err := Build(context.Background(), sess, "main", url.Values{"issue": {id}})
	require.NoError(t, err)
	require.Contains(t, []string{"detail", "epic"}, d.Mode)
	return d
}

// --- the detail branch -------------------------------------------------------

func TestDetailReportsAClippedIssuesRead(t *testing.T) {
	d := detail(t, bigTracker(), "i-0007")

	require.NotNil(t, d.Issue, "i-0007 is inside the first Max rows")
	assert.Equal(t, "i-0007", d.Issue.ID)
	assert.True(t, d.Truncated, "the issues table exceeded Max")
	assert.True(t, d.IssuesClipped())
	assert.Equal(t, Max+5, d.ShownOf, "the tracker's true issue count, not the number read")
	assert.False(t, d.Missing())
	assert.False(t, d.MissingBeyondCap())
}

func TestDetailIssueInTheTailIsNotAbsence(t *testing.T) {
	sess := bigTracker()
	d := detail(t, sess, "i-2003")

	// The issue exists in the tracker...
	rows := sess.rowsByTable["issues"].Rows
	assert.Equal(t, "i-2003", rows[2003][0])
	// ...but not in the rows this projection read, and it says so.
	assert.Nil(t, d.Issue)
	assert.True(t, d.Missing())
	assert.True(t, d.MissingBeyondCap(), "the read was clipped, so absence is not established")
	assert.True(t, d.IssuesClipped())
	assert.Equal(t, Max+5, d.ShownOf)
}

func TestDetailAbsentIssueOnACompleteReadIsAbsence(t *testing.T) {
	d := detail(t, beadsFixture(), "i-nope")

	assert.Nil(t, d.Issue)
	assert.True(t, d.Missing())
	assert.False(t, d.MissingBeyondCap(), "nothing was clipped, so the id genuinely does not exist")
	assert.False(t, d.Truncated)
	assert.False(t, d.IssuesClipped())
	assert.Equal(t, 4, d.ShownOf)
}

func TestDetailReportsAClippedCommentsRead(t *testing.T) {
	d := detail(t, smallTrackerWith(map[string]*browse.RowPage{
		"comments": manyComments(Max + 3),
	}), "i-0000")

	require.NotNil(t, d.Issue)
	assert.Len(t, d.Comments, Max, "the thread is the comments that were read")
	assert.True(t, d.Truncated, "the comments table was clipped, so the thread is partial")
	assert.False(t, d.IssuesClipped(), "the issue set itself is complete")
	assert.Equal(t, 3, d.ShownOf)
}

func TestDetailReportsAClippedLabelsRead(t *testing.T) {
	labels := make([][]string, 0, Max+2)
	for i := 0; i < Max+2; i++ {
		labels = append(labels, []string{"i-0002", fmt.Sprintf("label-%04d", i)})
	}
	sess := smallTrackerWith(map[string]*browse.RowPage{
		"labels": {Columns: []string{"issue_id", "label"}, Rows: labels, Total: len(labels)},
	})

	d := detail(t, sess, "i-0000")
	require.NotNil(t, d.Issue)
	assert.True(t, d.Truncated, "labels feed the detail pane, so a clipped labels read is partial too")
	assert.False(t, d.IssuesClipped())
}

// --- the board's flag is what it always was ----------------------------------

func TestBoardTruncationIsUnchanged(t *testing.T) {
	t.Run("clipped issues", func(t *testing.T) {
		d, err := Build(context.Background(), bigTracker(), "main", url.Values{})
		require.NoError(t, err)
		require.Equal(t, "board", d.Mode)
		assert.True(t, d.Truncated)
		assert.Equal(t, Max+5, d.ShownOf)
		assert.Equal(t, Max, d.Counts.Total, "the board holds the rows that were read")
		assert.False(t, d.Missing(), "a board is never a miss")
	})

	t.Run("clipped dependencies", func(t *testing.T) {
		deps := make([][]string, 0, Max+1)
		for i := 0; i < Max+1; i++ {
			deps = append(deps, []string{fmt.Sprintf("d%d", i), "i-0001", "i-0000", "blocks"})
		}
		sess := smallTrackerWith(map[string]*browse.RowPage{
			"dependencies": {
				Columns: []string{"id", "issue_id", "depends_on_issue_id", "type"},
				Rows:    deps,
				Total:   len(deps),
			},
		})
		d, err := Build(context.Background(), sess, "main", url.Values{})
		require.NoError(t, err)
		assert.True(t, d.Truncated, "dependencies is one of the two tables the board buckets from")
		assert.False(t, d.IssuesClipped(), "and Truncated is not the same fact as a clipped issue set")
		assert.Equal(t, 3, d.ShownOf)
	})

	t.Run("a clip in a table the board does not bucket from", func(t *testing.T) {
		// The board's flag has always named issues and dependencies. Widening it to
		// labels or comments would change what the board reports; the detail pane
		// covers them because it reads them for this one issue.
		d, err := Build(context.Background(), smallTrackerWith(map[string]*browse.RowPage{
			"comments": manyComments(Max + 3),
		}), "main", url.Values{})
		require.NoError(t, err)
		assert.False(t, d.Truncated)
		assert.False(t, d.IssuesClipped())
	})
}

// --- the milestone rollup ----------------------------------------------------

func TestMilestonesReportAClippedRead(t *testing.T) {
	v, err := BuildMilestones(context.Background(), bigTracker(), "main")
	require.NoError(t, err)

	assert.True(t, v.Truncated, "the rollup below is arithmetic over a partial read")
	assert.True(t, v.IssuesClipped())
	assert.Equal(t, Max, v.Total, "issues read")
	assert.Equal(t, Max+5, v.ShownOf, "issues that exist")

	require.Len(t, v.Milestones, 1)
	assert.Equal(t, 1, v.Milestones[0].Total,
		"m1's other member is i-2003, which sits past the cap — the count is short and the view says why")
}

func TestMilestonesOnACompleteReadReportNoClip(t *testing.T) {
	v, err := BuildMilestones(context.Background(), milestoneFixture(), "main")
	require.NoError(t, err)

	assert.False(t, v.Truncated)
	assert.False(t, v.IssuesClipped())
	assert.Equal(t, 6, v.Total)
	assert.Equal(t, v.Total, v.ShownOf, "nothing was left behind, so the two agree")
}

func TestMilestonesReportAClippedLabelsRead(t *testing.T) {
	labels := make([][]string, 0, Max+2)
	labels = append(labels, []string{"i-0000", "milestone:m1"})
	for i := 0; i < Max+1; i++ {
		labels = append(labels, []string{"i-0002", fmt.Sprintf("label-%04d", i)})
	}
	sess := smallTrackerWith(map[string]*browse.RowPage{
		"labels": {Columns: []string{"issue_id", "label"}, Rows: labels, Total: len(labels)},
	})

	v, err := BuildMilestones(context.Background(), sess, "main")
	require.NoError(t, err)
	assert.True(t, v.Truncated, "membership itself was read partially")
	assert.False(t, v.IssuesClipped(), "though every issue was read")
	assert.Equal(t, 3, v.Total)
	assert.Equal(t, 3, v.ShownOf)
}