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)
}