* feat(search): re-cut SearchIssueSummaries atop Lite as searchProjection[T]
Re-cuts release/be-nu4.3-stack onto current main, resolved against the
merged Lite foundation (#3906) instead of the stale pre-Lite conflict set.
SearchIssueSummaries moves from a standalone search_summary.go into a
summaryProjection literal alongside issueProjection/issueLiteProjection/
idProjection in issueops/search.go, sharing the generic searchInTx core
instead of a parallel top-level function — retiring the anti-pattern
coffeegoddd's CHANGES_REQUESTED flagged. LessSummary shares lessFields
with Less via a summarySortFields adapter so the two comparators can't
drift on NULL-handling or tie-break rules.
Adds parity coverage (SearchIssues vs SearchIssueSummaries agree on ID
set/order and on every IssueSummary field) and the three-way wide/lite/
summary A/B benchmark, extending dolt_benchmark_test.go — which still
only compiles together with the projection method, the fact that
retired the earlier "land the bench file first" plan.
* test(feat): red — fix parity test to compare order directly + add comparator/wisp-limit coverage (refs be-be3pj)
- TestSearchIssuesAndSearchIssueSummaries_Parity now compares
SearchIssues/SearchIssueSummaries IDs order-sensitively (slices.Equal)
instead of via the sort-then-compare equalStringSlices helper, which
would silently pass an ordering/top-N regression.
- Add TestLessSummaryMatchesLessAcrossSortKeys: Less vs LessSummary
produce identical order across every SortDefs key + "id", both
directions.
- Add TestSearchIssueSummariesInTx_MergesAcrossIssuesAndWisps: be-yrtwi
regression pattern for summaryProjection specifically, covering
wisp-merge top-N truncation under a limit.
* fix(types): give IssueSummary the same JSON tags as Issue
IssueSummary carried no struct tags, so encoding/json emitted Go's default
field names ({"ID":…,"Title":…}). The type exists to back list-shaped
rendering and `bd list --json` is one of that command's primary modes, so
the wiring follow-up would have silently changed the wire shape for every
consumer parsing bd output. Zero production callers today makes this a
zero-risk moment to fix it.
Tags mirror the same-named fields on types.Issue exactly, including
priority carrying no omitempty (0 is a valid P0). Three tests pin it: a
reflection test comparing every tag against Issue's so the two cannot
drift, an exact-key-set marshal test, and an omitempty test.
Reviewer item 1 (maphew, 2026-08-03).
* docs(storage): pin the --sort id contract on SearchIssueSummaries
The interface said SortBy/SortDesc are honored identically to SearchIssues,
which is true but left the id case looking like a gap in the summary path.
It is not a gap, and the doc now says why: sqlbuild.OrderBy emits no ORDER BY
for id (sqlbuild.IsGoSideSort), so *neither* path gets a SQL id order, and
both carry a comparator used only where the issues+wisps merge re-sorts
(sqlbuild.Less / sqlbuild.LessSummary).
The user-facing id order is cmd/bd's own, applied after the call via
utils.NaturalCompareIDs (cmd/bd/list.go sortIssues -> compareIssuesBy) —
numeric-aware, so bd-2 sorts before bd-10. Adding a lexicographic
storage-side summary sorter would therefore diverge from SearchIssues and
still not match what bd list renders.
Reviewer question (maphew, 2026-08-03): the contract no longer depends on
an unfiled follow-up to be readable.
* fix(backend): alias IssueSummary into the public backend contract
#5321 landed the public backend surface and its reflection drift guard on
2026-08-03, about two hours after this branch's last push, so the 08-11
maintenance rebase brought the branch under a gate that did not exist when
the engine interface was widened. TestPublicSurfaceComplete fails on both
platforms at the rebased head:
internal type github.com/steveyegge/beads/internal/types.IssueSummary
is reachable from the public backend contract but has no alias in
package backend
The gate is right and the rebase broke nothing. SearchIssueSummaries
returns []*types.IssueSummary on storage.Storage, which backend.DoltStorage
aliases whole, so the row type is now part of the contract an out-of-tree
backend writes against — and without the alias that backend cannot name it.
Verified this is ours rather than base breakage: the same test passes on
origin/main and fails only on this branch.
* ci: retrigger after infra-flake red on dc92c3c3
Run 34381971686 went red on two failures, neither of them in this PR's
15-file diff:
- Test (storage domain + uow): internal/tracker lost its Dolt
testcontainer mid-suite after a Ryuk reaper handshake reset
(54 failed, 1994 passed). Known parallel-binary reaper flake; the
fix is PR #5770, open and unmerged upstream since 2026-08-15. Third
recorded recurrence, same package and same mapped port as the prior
two.
- PR Core (wrapper timing): TestDoltServer_LogFile_CapturesOutput in
internal/storage/dbproxy/server, a package this PR does not touch.
Not reproducible locally in 55 attempts against clean main, 30 of
them under 2x CPU load with the CI-pinned dolt 2.2.0.
We are pull-only on the upstream repo and cannot rerun jobs, so an
empty commit is the only retrigger lever available.
* test(issueops): tie the summary column list to its scanner, delete a third copy
Review point (minor 2): IssueSummaryColumns, ScanIssueSummaryFrom's dest list
and search_summary_merge_test.go's summaryColumnNames were three hand-synced
copies of the same list, with only the real-database field-parity test to
catch a drift between them.
Two changes rather than one guard over three lists:
- summaryColumnNames now derives from IssueSummaryColumns instead of
restating it, so the fixture cannot drift at all. summaryMergeRow places
its values by column NAME against that list rather than by literal
position, so adding a column widens the row automatically instead of
silently shifting every value one slot left.
- TestIssueSummaryColumnsMatchScanner pins the remaining pair by count,
using a counting scanner that needs no database. This is the summary
counterpart of TestIssueSelectColumns_LitePlusHeavyEqualsFull.
TestIssueSummaryColumnsAreRealIssueColumns covers what a count cannot: a
misspelled column keeps the widths equal and fails only at query time against
a real database, as an opaque unknown-column error from whichever caller runs
first. Requiring membership in IssueSelectColumns catches it in a unit test.
Membership, not order: unlike IssueSelectColumnsLite, IssueSummaryColumns is
deliberately not a subsequence of the full list, so there is no analogue of
TestIssueSelectColumnsLite_IsFullMinusHeavyInOrder.
Both guards verified red-before-green by mutation: adding a column without a
dest fails the first, and misspelling one fails the second.
* fix(types): carry the wisp-plane markers through the summary projection
Review point (MAJOR): IssueSummary's doc comment promises a summary-backed
list serializes to the same wire shape a full-Issue-backed one does, and
issueops.searchInTx merges wisp rows into every result whose filter does not
set SkipWisps. But IssueSummary had no Ephemeral, NoHistory, WispType or
StorageClass, so a wisp rendered through it dropped ephemeral, no_history,
wisp_type and storage_class from `bd list --json` while every other key stayed
identical -- a wisp and a durable bead became indistinguishable on the wire.
maphew raised this on 2026-08-03 and it was still open.
Adding the fields, not excluding wisps. Excluding them would break the
same-wisp-merge-semantics claim that is this PR's central design argument, and
would make the wiring follow-up silently drop wisps from `bd list` entirely --
a strictly worse outcome than the missing keys.
Correcting our own 2026-08-03 reasoning, which argued the other way. That
comment observed that no list *renderer* reads Ephemeral or WispType back off
the row, which is true and beside the point: `--json` does not read fields,
it marshals the struct. It also treated the cost as the one D3 exists to
remove. These four are narrow scalars -- two TINYINT(1), two short VARCHARs,
already present on both the issues and wisps tables and already scanned by
ScanIssueFrom -- not the multi-KB TEXT bodies and JSON parse the projection is
narrow to avoid. Zero callers today makes this free; after the wiring lands it
is a wire-format change.
Verified red-before-green at both levels:
- TestIssueSummaryCarriesWispMarkers failed on all four fields before the
change (reflection against Issue, asserting tag AND type -- WispType and
StorageClass are named string types that a plain string would impersonate
under a tag-only check).
- TestIssueSummaryWispKeysMatchIssue marshals the same marker values through
both types and compares the four keys byte for byte, including a durable
row that must emit none of them.
- TestSearchIssueSummaries_FieldsMatchSearchIssues now seeds an ephemeral
wisp and a no-history wisp (mutually exclusive, so one row each) and
compares all four markers against SearchIssues on a real Dolt database. It
refuses to pass vacuously: fewer than two wisp rows in the result is a
fatal, since zero-vs-zero marker comparisons would otherwise succeed if
the merge stopped returning wisps. Mutating ScanIssueSummaryFrom to stop
mapping wisp_type fails it on both seeded rows.
ScanIssueSummaryFrom maps the four exactly as ScanIssueFrom does, including a
NULL storage_class staying empty for EffectiveStorageClass to resolve per
plane rather than being pinned to a literal.
* perf(sqlbuild): compare sortFields through pointers, not two copies per call
Review point (minor 3): the shared-comparator refactor made Less build two
sortFields values per comparison and pass them by value, a small regression on
the pre-existing issues+wisps merge sort inside a perf PR.
lessFields and sortKeyCompareFields now take *sortFields. sortFields is a
~150-byte struct, so passing by value copied it twice into lessFields and
twice more into sortKeyCompareFields on every comparison; that is now zero
copies after the adapters run. The adapters still build one value per row --
that normalization is exactly what buys Less and LessSummary a single shared
body, and giving it up would reintroduce the drift this PR exists to remove.
No allocation either way, before or after: escape analysis reports nothing in
Less/LessSummary/lessFields escaping to heap (the only escapes in sort.go are
the pre-existing SortDefs map and OrderBy's string formatting), and both arms
measure 0 B/op, 0 allocs/op. A throwaway A/B in one binary, -benchtime=3s
-count=3 on the priority key, put the by-value median at 26.33 ns/op and the
pointer median at 25.35 ns/op, with non-overlapping ranges -- a consistent but
small ~4% win.
Stating that plainly rather than claiming more: the value here is removing four
struct copies per comparison from the hot path, not the wall-clock number,
which is within a couple of nanoseconds. Behavior is unchanged; the sqlbuild
suite, including the parity and LessID-honors-sortDesc tests, passes.
* test(dolt): vary SortBy across the summary parity table
Review point (minor 4): none of the nine parity cases set SortBy, so
LessSummary-versus-SQL ORDER BY agreement was proven by unit test alone
(TestLessSummaryMatchesLessAcrossSortKeys) and never against a real database.
Every case ran at the default order, priority ASC.
Nine rows added, covering created, title, status, updated and assignee -- the
first two in both directions -- plus id in both directions. Each carries a
Limit, so the merge's sort-before-trim actually decides which rows survive
rather than the ordering being cosmetic: an ordering bug that only shows up in
which rows a top-N keeps is the failure mode this table exists to catch.
id is included deliberately even though sqlbuild.OrderBy emits no ORDER BY for
it. It is the one Go-side key, so these two rows pin that both paths still
agree on the order the merge comparator produces where SQL contributes none.
All nine pass against a real Dolt database.
* docs(storage): document the summary projection's caller contract
Two review points, both about the contract being legible to a caller who has
only the interface and the extension guide.
Minor 1 -- IncludeDependencies is a silent no-op on the summary path, because
types.IssueSummary has no Dependencies field to populate. hydrateSummaryLabels
says why in its own comment; the interface a caller actually reads did not.
SearchIssueSummaries' doc now states it, alongside the neighbouring facts that
SkipLabels IS honored identically and that wisps are not excluded.
Minor 5 -- engdocs/EXTENDING.md documented the Lite projection's contract and
never mentioned the third one. It now carries a parallel section: what a caller
may read, why there is no IsLitePartial equivalent (a narrower type makes a
missing field a compile error rather than a silent zero value, which is the
substantive difference from Lite), the dependency and wisp rules above, the
ordering guarantee, and where each is enforced -- including the note that
IssueSummaryColumns is deliberately not an ordered subsequence of the full
list, so it has no analogue of the lite path's in-order guard.
The Lite section is left intact, its own Backend coverage subsection included;
the new section follows it rather than interleaving.
* chore(bazel): enumerate the two new test files in their go_test srcs
The PR adds internal/types/issue_summary_test.go and
internal/storage/issueops/search_summary_merge_test.go. Both are untagged,
and both packages carry a base-added BUILD.bazel whose go_test enumerates
srcs explicitly, so bazel silently would not have compiled or run them:
measured on //internal/types:types_test, the unrepaired binary lists 0 of
the 5 new tests (68 total) and the repaired one lists 5 of 5 (73), with
both builds exiting 0 -- a fake green, since bazel reports "1 test passes"
either way.
Base parity at 43a526b828 is clean (102 packages / 1192 untagged test files
enumerated, 0 srcs gaps), so the omission is PR-introduced rather than an
unenforced list. make bazel-sync-check and tools/bazel/go_srcs.py --check
both exit 0 on the broken head; generated with make bazel-sync (gazelle),
which is idempotent here.
Maintainer fixup applied during adopt-pr rebase (PR #3458).
* docs: announce the required SearchIssueSummaries method and inventory its span
Review follow-through for #3458 (maintainer fixup, iteration 1).
CHANGELOG.md: backend/backend.go promises that a required method added to
the engine interface is called out in CHANGELOG.md. SearchIssueSummaries is
one and had no entry. Adds an [Unreleased] "Changed" bullet in the #4911
form, next to the #6731 GetReadyWorkWithCountsAndTotal announcement: the
method and its signature, that callers are unaffected while implementers
must add it to compile, and its contract (SortBy/SortDesc and SkipLabels as
in SearchIssues, IncludeDependencies a silent no-op, wisps merged with their
markers set).
engdocs/design/otel/otel-data-model.md: the direct-operations block calls
itself a complete inventory and was exact at the PR base. The PR's new
storage.SearchIssueSummaries span was the one missing name. Inserted in
alphabetical order and bumped the count to 74. A set-diff against the
s.op(ctx, "...") names in internal/telemetry/storage.go now finds 74 == 74
with no drift in either direction.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
615 lines
23 KiB
Go
615 lines
23 KiB
Go
package sqlbuild
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
func TestOrderByKnownKeys(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
sortBy string
|
|
sortDesc bool
|
|
table string
|
|
want string
|
|
}{
|
|
{"", false, "", "ORDER BY priority ASC, created_at DESC, id ASC"},
|
|
{"priority", true, "", "ORDER BY priority DESC, created_at DESC, id ASC"},
|
|
{"created", false, "", "ORDER BY created_at DESC, id ASC"},
|
|
{"created", true, "", "ORDER BY created_at ASC, id ASC"},
|
|
{"title", false, "i", "ORDER BY LOWER(i.title) ASC, i.id ASC"},
|
|
{"updated", false, "i", "ORDER BY i.updated_at DESC, i.id ASC"},
|
|
{"bogus-key", false, "", "ORDER BY priority ASC, created_at DESC, id ASC"},
|
|
{"id", false, "", ""}, // Go-side sort
|
|
}
|
|
for _, tc := range cases {
|
|
if got := OrderBy(tc.sortBy, tc.sortDesc, tc.table); got != tc.want {
|
|
t.Errorf("OrderBy(%q, %v, %q) = %q, want %q", tc.sortBy, tc.sortDesc, tc.table, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestUnionSortColumnsCoverSortDefs pins that every SQL-side sort key has a
|
|
// sort_* alias in UnionSortColumnsSQL, so UNION consumers can order by any
|
|
// key OrderByForColumns may emit.
|
|
func TestUnionSortColumnsCoverSortDefs(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for key := range SortDefs {
|
|
alias := "sort_" + key
|
|
if key == "" {
|
|
alias = "sort_priority"
|
|
}
|
|
if !strings.Contains(UnionSortColumnsSQL, alias) {
|
|
t.Errorf("UnionSortColumnsSQL missing alias %q for sort key %q", alias, key)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestLessMirrorsOrderBy spot-checks that the Go-side comparator agrees with
|
|
// the SQL default ordering on the documented tie-break chain: priority ASC,
|
|
// then created_at DESC, then id ASC.
|
|
func TestLessMirrorsOrderBy(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
now := time.Now().UTC()
|
|
older := now.Add(-time.Hour)
|
|
a := &types.Issue{ID: "a", Priority: 1, CreatedAt: now}
|
|
b := &types.Issue{ID: "b", Priority: 2, CreatedAt: now}
|
|
if !Less(a, b, "", false) || Less(b, a, "", false) {
|
|
t.Error("default sort must order priority 1 before priority 2")
|
|
}
|
|
c := &types.Issue{ID: "c", Priority: 1, CreatedAt: older}
|
|
if !Less(a, c, "", false) {
|
|
t.Error("equal priority must order newer created_at first (created_at DESC)")
|
|
}
|
|
d := &types.Issue{ID: "d", Priority: 1, CreatedAt: now}
|
|
if !Less(a, d, "", false) || Less(d, a, "", false) {
|
|
t.Error("full tie must break by id ASC")
|
|
}
|
|
}
|
|
|
|
// TestLessIDHonorsSortDesc pins the bd-jao3t half of the reversed-id bug:
|
|
// Less ignored sortDesc for "id" (the one Go-side sort key), so every merge
|
|
// sort that fed a reversed id page kept the byte-FIRST rows.
|
|
func TestLessIDHonorsSortDesc(t *testing.T) {
|
|
a := &types.Issue{ID: "bd-001"}
|
|
b := &types.Issue{ID: "bd-002"}
|
|
|
|
if !Less(a, b, "id", false) || Less(b, a, "id", false) {
|
|
t.Error("id ascending must be byte order")
|
|
}
|
|
if !Less(b, a, "id", true) || Less(a, b, "id", true) {
|
|
t.Error("id descending must be reversed byte order, not the ascending order sortDesc used to be ignored into")
|
|
}
|
|
}
|
|
|
|
// TestLessSummaryMirrorsOrderBy is TestLessMirrorsOrderBy's sibling for
|
|
// LessSummary/types.IssueSummary. Less and LessSummary share the lessFields
|
|
// core, so this mostly guards against a future adapter (summarySortFields)
|
|
// dropping or mis-mapping a field rather than the tie-break logic itself.
|
|
func TestLessSummaryMirrorsOrderBy(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
now := time.Now().UTC()
|
|
older := now.Add(-time.Hour)
|
|
a := &types.IssueSummary{ID: "a", Priority: 1, CreatedAt: now}
|
|
b := &types.IssueSummary{ID: "b", Priority: 2, CreatedAt: now}
|
|
if !LessSummary(a, b, "", false) || LessSummary(b, a, "", false) {
|
|
t.Error("default sort must order priority 1 before priority 2")
|
|
}
|
|
c := &types.IssueSummary{ID: "c", Priority: 1, CreatedAt: older}
|
|
if !LessSummary(a, c, "", false) {
|
|
t.Error("equal priority must order newer created_at first (created_at DESC)")
|
|
}
|
|
d := &types.IssueSummary{ID: "d", Priority: 1, CreatedAt: now}
|
|
if !LessSummary(a, d, "", false) || LessSummary(d, a, "", false) {
|
|
t.Error("full tie must break by id ASC")
|
|
}
|
|
}
|
|
|
|
// TestLessSummaryMatchesLessAcrossSortKeys is TestLessSummaryMirrorsOrderBy's
|
|
// broader sibling: instead of spot-checking only the default tie-break chain,
|
|
// it sorts a parallel Issue/IssueSummary fixture with Less and LessSummary
|
|
// for every SortDefs key (plus "id") in both directions and asserts the two
|
|
// produce identical ID order. Less and LessSummary already share the
|
|
// lessFields core, so this mainly guards summarySortFields: if a future field
|
|
// is added to sortFields without updating summarySortFields, the two
|
|
// adapters silently diverge and this is what would catch it.
|
|
func TestLessSummaryMatchesLessAcrossSortKeys(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
day := func(d int) time.Time { return time.Date(2026, 1, d, 0, 0, 0, 0, time.UTC) }
|
|
closedOn := func(d int) *time.Time { c := day(d); return &c }
|
|
|
|
type row struct {
|
|
id string
|
|
priority int
|
|
createdAt time.Time
|
|
updatedAt time.Time
|
|
closedAt *time.Time
|
|
status types.Status
|
|
issueType types.IssueType
|
|
assignee string
|
|
title string
|
|
}
|
|
// Distinct (and, for priority, partially tied) values on every field so
|
|
// each sort key — including the tie-break chain the "" and "priority"
|
|
// keys share — produces a non-trivial, checkable order.
|
|
rows := []row{
|
|
{"p1", 3, day(1), day(5), nil, types.StatusOpen, types.TypeBug, "alice", "Zebra"},
|
|
{"p2", 1, day(3), day(2), closedOn(10), types.StatusBlocked, types.TypeEpic, "bob", "apple"},
|
|
{"p3", 2, day(5), day(4), closedOn(8), types.StatusClosed, types.TypeTask, "carol", "Mango"},
|
|
{"p4", 1, day(2), day(1), nil, types.StatusInProgress, types.TypeChore, "dave", "banana"},
|
|
{"p5", 4, day(4), day(3), closedOn(6), types.StatusOpen, types.TypeBug, "eve", "Orange"},
|
|
}
|
|
|
|
issues := make([]*types.Issue, len(rows))
|
|
summaries := make([]*types.IssueSummary, len(rows))
|
|
for i, r := range rows {
|
|
issues[i] = &types.Issue{
|
|
ID: r.id, Priority: r.priority, CreatedAt: r.createdAt, UpdatedAt: r.updatedAt,
|
|
ClosedAt: r.closedAt, Status: r.status, IssueType: r.issueType, Assignee: r.assignee, Title: r.title,
|
|
}
|
|
summaries[i] = &types.IssueSummary{
|
|
ID: r.id, Priority: r.priority, CreatedAt: r.createdAt, UpdatedAt: r.updatedAt,
|
|
ClosedAt: r.closedAt, Status: r.status, IssueType: r.issueType, Assignee: r.assignee, Title: r.title,
|
|
}
|
|
}
|
|
|
|
keys := make([]string, 0, len(SortDefs)+1)
|
|
for key := range SortDefs {
|
|
keys = append(keys, key)
|
|
}
|
|
keys = append(keys, "id")
|
|
|
|
for _, key := range keys {
|
|
for _, desc := range []bool{false, true} {
|
|
name := key
|
|
if name == "" {
|
|
name = "default"
|
|
}
|
|
if desc {
|
|
name += "/desc"
|
|
} else {
|
|
name += "/asc"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
sortedIssues := append([]*types.Issue(nil), issues...)
|
|
sort.Slice(sortedIssues, func(i, j int) bool {
|
|
return Less(sortedIssues[i], sortedIssues[j], key, desc)
|
|
})
|
|
sortedSummaries := append([]*types.IssueSummary(nil), summaries...)
|
|
sort.Slice(sortedSummaries, func(i, j int) bool {
|
|
return LessSummary(sortedSummaries[i], sortedSummaries[j], key, desc)
|
|
})
|
|
|
|
gotIssues := make([]string, len(sortedIssues))
|
|
for i, is := range sortedIssues {
|
|
gotIssues[i] = is.ID
|
|
}
|
|
gotSummaries := make([]string, len(sortedSummaries))
|
|
for i, s := range sortedSummaries {
|
|
gotSummaries[i] = s.ID
|
|
}
|
|
|
|
if !slices.Equal(gotIssues, gotSummaries) {
|
|
t.Errorf("Less vs LessSummary order diverge for sortBy=%q sortDesc=%v:\n Less: %v\n LessSummary: %v",
|
|
key, desc, gotIssues, gotSummaries)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuildReadyWorkOrderPriorityFIFO(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
policy types.SortPolicy
|
|
want string
|
|
}{
|
|
{
|
|
name: "priority",
|
|
policy: types.SortPolicyPriority,
|
|
want: "ORDER BY priority ASC, created_at ASC, id ASC",
|
|
},
|
|
{
|
|
name: "fallback",
|
|
policy: types.SortPolicy("unknown"),
|
|
want: "ORDER BY priority ASC, created_at ASC, id ASC",
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got := BuildReadyWorkOrder(tc.policy, "created_at", "priority")
|
|
if got.SQL != tc.want {
|
|
t.Fatalf("BuildReadyWorkOrder(%q).SQL = %q, want %q", tc.policy, got.SQL, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReadyWorkExcludeTypes(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
base := ReadyWorkExcludeTypes(nil)
|
|
seen := make(map[types.IssueType]bool, len(base))
|
|
for _, typ := range base {
|
|
if seen[typ] {
|
|
t.Errorf("duplicate type %q in default exclude list", typ)
|
|
}
|
|
seen[typ] = true
|
|
}
|
|
for _, want := range []types.IssueType{"merge-request", types.TypeGate, types.TypeMolecule, "agent", "rig", "role", "message"} {
|
|
if !seen[want] {
|
|
t.Errorf("default exclude list missing %q", want)
|
|
}
|
|
}
|
|
|
|
extended := ReadyWorkExcludeTypes([]types.IssueType{"custom", "", types.TypeGate})
|
|
if got, want := len(extended), len(base)+1; got != want {
|
|
t.Errorf("extras must dedupe and drop empties: len = %d, want %d", got, want)
|
|
}
|
|
}
|
|
|
|
func TestBuildReadyWorkWhereBatchesIDSets(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ids := make([]string, QueryBatchSize+1)
|
|
for i := range ids {
|
|
ids[i] = "x-" + strings.Repeat("a", 3)
|
|
}
|
|
where, args, err := BuildReadyWorkWhere(types.WorkFilter{}, IssuesFilterTables, ReadyWorkWhereInputs{DeferredChildIDs: ids})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got := strings.Count(where, "id NOT IN ("); got != 2 {
|
|
t.Errorf("expected 2 batched NOT IN clauses for %d IDs, got %d", len(ids), got)
|
|
}
|
|
wantArgs := len(ids) + len(ReadyWorkExcludeTypes(nil))
|
|
if len(args) != wantArgs {
|
|
t.Errorf("args = %d, want %d", len(args), wantArgs)
|
|
}
|
|
}
|
|
|
|
func TestBuildReadyWorkWhereAppliesPolicyIDExclusions(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ids := make([]string, QueryBatchSize+1)
|
|
for i := range ids {
|
|
ids[i] = fmt.Sprintf("be-external-%d", i)
|
|
}
|
|
where, args, err := BuildReadyWorkWhere(
|
|
types.WorkFilter{ExcludeIDs: ids},
|
|
IssuesFilterTables,
|
|
ReadyWorkWhereInputs{},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("BuildReadyWorkWhere: %v", err)
|
|
}
|
|
if got := strings.Count(where, "id NOT IN ("); got != 2 {
|
|
t.Fatalf("external policy exclusions produced %d NOT IN clauses, want 2", got)
|
|
}
|
|
wantArgs := len(ids) + len(ReadyWorkExcludeTypes(nil))
|
|
if len(args) != wantArgs {
|
|
t.Fatalf("args = %d, want %d", len(args), wantArgs)
|
|
}
|
|
}
|
|
|
|
// wy-jpd3.2: --label-any was silently dropped on the ready/claim path (with or
|
|
// without --parent). BuildReadyWorkWhere must now emit an OR-set membership
|
|
// clause for LabelsAny that AND-combines with the AND-set Labels and the parent
|
|
// filter, so an atomic claim is actually fenced by the label filter it names.
|
|
func TestBuildReadyWorkWhereLabelsAny(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
parent := "wy-jpd3"
|
|
filter := types.WorkFilter{
|
|
Labels: []string{"tier:opus"},
|
|
LabelsAny: []string{"lane-a", "lane-c"},
|
|
ParentID: &parent,
|
|
}
|
|
where, args, err := BuildReadyWorkWhere(filter, IssuesFilterTables, ReadyWorkWhereInputs{})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
// OR-set: one subquery with an IN over all --label-any values.
|
|
wantAny := "id IN (SELECT issue_id FROM " + IssuesFilterTables.Labels + " WHERE label IN (?, ?))"
|
|
if !strings.Contains(where, wantAny) {
|
|
t.Errorf("LabelsAny OR clause missing.\n where = %s\n want substring = %s", where, wantAny)
|
|
}
|
|
// AND-set still emits its own per-label subquery (equality, not IN).
|
|
wantAnd := "id IN (SELECT issue_id FROM " + IssuesFilterTables.Labels + " WHERE label = ?)"
|
|
if !strings.Contains(where, wantAnd) {
|
|
t.Errorf("Labels AND clause missing.\n where = %s\n want substring = %s", where, wantAnd)
|
|
}
|
|
// Parent filter must survive alongside the label clauses.
|
|
if !strings.Contains(where, "LIKE CONCAT(?, '.%')") {
|
|
t.Errorf("parent filter clause missing when combined with labels.\n where = %s", where)
|
|
}
|
|
// The label + parent args land in filter order (AND labels, then LabelsAny
|
|
// values, then the parent LIKE arg) as the tail of the arg list — the
|
|
// default issue_type exclusion prepends its own args.
|
|
wantTail := []interface{}{"tier:opus", "lane-a", "lane-c", parent}
|
|
if len(args) < len(wantTail) {
|
|
t.Fatalf("args = %v, want at least %d trailing values %v", args, len(wantTail), wantTail)
|
|
}
|
|
tail := args[len(args)-len(wantTail):]
|
|
for i := range wantTail {
|
|
if tail[i] != wantTail[i] {
|
|
t.Errorf("tail[%d] = %v, want %v (full args: %v)", i, tail[i], wantTail[i], args)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ga-hqchm: filter.LabelPattern was parsed from --label-pattern and stored
|
|
// on WorkFilter, but BuildReadyWorkWhere never read it — the flag was dead
|
|
// code on the ready path and silently returned unfiltered results.
|
|
// BuildIssueFilterClauses coverage for LabelPattern/LabelRegex lives with
|
|
// the list path (be402a022, #3971); this covers only the ready path's own
|
|
// clause shape, which mirrors filter.go's LIKE ... ESCAPE '|' pattern.
|
|
func TestBuildReadyWorkWhereLabelPattern(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
where, args, err := BuildReadyWorkWhere(types.WorkFilter{LabelPattern: "tech-*"}, IssuesFilterTables, ReadyWorkWhereInputs{})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
wantClause := "id IN (SELECT issue_id FROM " + IssuesFilterTables.Labels + " WHERE label LIKE ? ESCAPE '|')"
|
|
if !strings.Contains(where, wantClause) {
|
|
t.Errorf("LabelPattern clause missing.\n where = %s\n want substring = %s", where, wantClause)
|
|
}
|
|
if len(args) == 0 || args[len(args)-1] != "tech-%" {
|
|
t.Errorf("args tail = %v, want last arg %q", args, "tech-%")
|
|
}
|
|
}
|
|
|
|
// #4884 intent: filter.LabelRegex was likewise dead on the ready path.
|
|
// BuildReadyWorkWhere must emit a REGEXP subquery mirroring filter.go's
|
|
// LabelRegex clause.
|
|
func TestBuildReadyWorkWhereLabelRegex(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
where, args, err := BuildReadyWorkWhere(types.WorkFilter{LabelRegex: "^tech-"}, IssuesFilterTables, ReadyWorkWhereInputs{})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
wantClause := "id IN (SELECT issue_id FROM " + IssuesFilterTables.Labels + " WHERE label REGEXP ?)"
|
|
if !strings.Contains(where, wantClause) {
|
|
t.Errorf("LabelRegex clause missing.\n where = %s\n want substring = %s", where, wantClause)
|
|
}
|
|
if len(args) == 0 || args[len(args)-1] != "^tech-" {
|
|
t.Errorf("args tail = %v, want last arg %q", args, "^tech-")
|
|
}
|
|
}
|
|
|
|
func TestSearchCountsSQLShape(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
sql, args := SearchCountsSQL(WispsFilterTables, nil, "WHERE x = ?", "ORDER BY y", "LIMIT 5", true, CountsHydration{})
|
|
if args != nil {
|
|
t.Errorf("predicate form must not return generated args, got %d", len(args))
|
|
}
|
|
for _, want := range []string{
|
|
"FROM wisps i",
|
|
"FROM wisp_dependencies",
|
|
"FROM wisp_comments",
|
|
"FROM wisp_labels",
|
|
"UNION ALL", // wisp reverse deps included
|
|
"WHERE x = ?",
|
|
"ORDER BY y",
|
|
"LIMIT 5",
|
|
} {
|
|
if !strings.Contains(sql, want) {
|
|
t.Errorf("counts SQL missing %q", want)
|
|
}
|
|
}
|
|
|
|
// Filter-before-join structure (predicate form): whereSQL is applied to the
|
|
// main table in a derived table that closes before the first aggregate LEFT
|
|
// JOIN. Locking this prevents a regression back to the filter-after-join
|
|
// shape that materialized every aggregate before pruning. "SELECT i.*" is
|
|
// the derived driver's marker — "FROM (" alone is ambiguous, since the
|
|
// reverse-blocker subquery also nests one.
|
|
driverEnd := strings.Index(sql, ") i")
|
|
firstJoin := strings.Index(sql, "LEFT JOIN")
|
|
if !strings.Contains(sql, "SELECT i.*") || driverEnd < 0 {
|
|
t.Fatalf("counts SQL must wrap the main table in a derived subquery; got:\n%s", sql)
|
|
}
|
|
if firstJoin < 0 || driverEnd > firstJoin {
|
|
t.Fatalf("derived driver must close before the first aggregate LEFT JOIN")
|
|
}
|
|
if idx := strings.Index(sql, "WHERE x = ?"); idx < 0 || idx > driverEnd {
|
|
t.Errorf("WHERE filter must appear inside the derived driver (before %q)", ") i")
|
|
}
|
|
// ORDER BY and LIMIT must stay AFTER the joins, and appear exactly once —
|
|
// the ready-work path passes a parameterized ORDER BY, so duplicating it
|
|
// would desync the placeholder/arg counts.
|
|
for _, outer := range []string{"ORDER BY y", "LIMIT 5"} {
|
|
if strings.Count(sql, outer) != 1 {
|
|
t.Errorf("%q must appear exactly once, got %d", outer, strings.Count(sql, outer))
|
|
}
|
|
if idx := strings.Index(sql, outer); idx < firstJoin {
|
|
t.Errorf("%q must appear after the aggregate joins", outer)
|
|
}
|
|
}
|
|
|
|
noWispDeps, _ := SearchCountsSQL(IssuesFilterTables, nil, "", "", "", false, CountsHydration{SkipLabels: true})
|
|
if strings.Contains(noWispDeps, "UNION ALL") {
|
|
t.Error("counts SQL must not union wisp reverse deps when probe says absent")
|
|
}
|
|
// An empty whereSQL keeps the plain driver: there is no predicate to push
|
|
// down, so the derived-table wrapper would be pure overhead for the
|
|
// unfiltered list/counts path.
|
|
if strings.Contains(noWispDeps, "SELECT i.*") {
|
|
t.Errorf("empty-where counts SQL must keep the plain driver, not a derived table:\n%s", noWispDeps)
|
|
}
|
|
if strings.Contains(noWispDeps, "JSON_ARRAYAGG(label)") {
|
|
t.Error("counts SQL must skip the labels join when skipLabels is set")
|
|
}
|
|
if !strings.Contains(noWispDeps, "NULL AS labels_json") {
|
|
t.Error("counts SQL must project NULL labels_json when skipLabels is set")
|
|
}
|
|
|
|
// By-IDs form: driver and every subquery are constrained to the ids, and the
|
|
// arg count matches the placeholder injection points (labels, dc, rc-deps,
|
|
// rc-wisp, cc, pc, d, driver = 8 for the wisp family with labels).
|
|
byIDs, idArgs := SearchCountsSQL(WispsFilterTables, []string{"a", "b"}, "", "", "", true, CountsHydration{})
|
|
if !strings.Contains(byIDs, "WHERE i.id IN (?,?)") {
|
|
t.Errorf("by-IDs counts SQL missing driver id filter:\n%s", byIDs)
|
|
}
|
|
// The by-IDs form keeps the plain "FROM wisps i" driver: its subqueries are
|
|
// already id-constrained, so there is no join-then-filter blowup to avoid and
|
|
// no reason to interpose a derived table.
|
|
if strings.Contains(byIDs, "SELECT i.*") {
|
|
t.Errorf("by-IDs counts SQL must keep the plain driver, not a derived table:\n%s", byIDs)
|
|
}
|
|
if strings.Contains(byIDs, "ORDER BY") || strings.Contains(byIDs, "LIMIT") {
|
|
t.Error("by-IDs counts SQL must not carry ORDER BY / LIMIT (order restored in Go)")
|
|
}
|
|
if len(idArgs) != 8*2 {
|
|
t.Errorf("by-IDs args = %d, want %d", len(idArgs), 8*2)
|
|
}
|
|
|
|
// skipLabels drops the labels point and !includeWispReverseDeps drops the
|
|
// rc-wisp point, leaving 6 injection points (dc, rc-deps, cc, pc, d, driver).
|
|
_, idArgsNoLabels := SearchCountsSQL(IssuesFilterTables, []string{"a", "b"}, "", "", "", false, CountsHydration{SkipLabels: true})
|
|
if len(idArgsNoLabels) != 6*2 {
|
|
t.Errorf("by-IDs args (skipLabels, no wisp deps) = %d, want %d", len(idArgsNoLabels), 6*2)
|
|
}
|
|
|
|
// SkipCounts drops the three cardinality joins and projects constants in
|
|
// their place, so the six extra columns the scan reads positionally stay
|
|
// six and in the same order.
|
|
skipCounts, _ := SearchCountsSQL(IssuesFilterTables, nil, "", "", "", true, CountsHydration{SkipCounts: true})
|
|
for _, gone := range []string{"dc ON dc.issue_id", "rc ON rc.dep_id", "cc ON cc.issue_id", "COALESCE(dc.cnt, 0)", "UNION ALL"} {
|
|
if strings.Contains(skipCounts, gone) {
|
|
t.Errorf("counts SQL must drop %q when SkipCounts is set:\n%s", gone, skipCounts)
|
|
}
|
|
}
|
|
for _, want := range []string{"0 AS dep_count", "0 AS rdep_count", "0 AS comment_count", "pc.parent_id AS parent_id", "d.deps_json AS deps_json"} {
|
|
if !strings.Contains(skipCounts, want) {
|
|
t.Errorf("counts SQL missing %q under SkipCounts:\n%s", want, skipCounts)
|
|
}
|
|
}
|
|
// Labels survive SkipCounts: the two opt-outs are independent.
|
|
if !strings.Contains(skipCounts, "JSON_ARRAYAGG(label)") {
|
|
t.Error("SkipCounts must not drop the labels join")
|
|
}
|
|
|
|
// By-IDs: SkipCounts removes the dc, rc-deps, rc-wisp and cc injection
|
|
// points, leaving labels, pc, d and the driver.
|
|
_, idArgsNoCounts := SearchCountsSQL(WispsFilterTables, []string{"a", "b"}, "", "", "", true, CountsHydration{SkipCounts: true})
|
|
if len(idArgsNoCounts) != 4*2 {
|
|
t.Errorf("by-IDs args (skipCounts) = %d, want %d", len(idArgsNoCounts), 4*2)
|
|
}
|
|
// Both opt-outs together leave pc, d and the driver.
|
|
_, idArgsNeither := SearchCountsSQL(WispsFilterTables, []string{"a", "b"}, "", "", "", true, CountsHydration{SkipLabels: true, SkipCounts: true})
|
|
if len(idArgsNeither) != 3*2 {
|
|
t.Errorf("by-IDs args (skipLabels+skipCounts) = %d, want %d", len(idArgsNeither), 3*2)
|
|
}
|
|
}
|
|
|
|
func TestBuildReadyWorkWhereStatusFilter(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
filter types.WorkFilter
|
|
wantClause string
|
|
rejectClause string // must NOT appear in the WHERE; "" skips the check
|
|
wantLeadingArgs []any
|
|
}{
|
|
{
|
|
name: "StatusesInClause",
|
|
filter: types.WorkFilter{Statuses: []types.Status{"open", "blocked", "pinned"}},
|
|
wantClause: "status IN (?,?,?)",
|
|
wantLeadingArgs: []any{"open", "blocked", "pinned"},
|
|
},
|
|
{
|
|
name: "SingularStatusWinsOverStatuses",
|
|
filter: types.WorkFilter{Status: "open", Statuses: []types.Status{"blocked", "pinned"}},
|
|
wantClause: "(status = ? OR status IN (SELECT name FROM custom_statuses WHERE category = 'active'))",
|
|
rejectClause: "status IN (?",
|
|
wantLeadingArgs: []any{"open"},
|
|
},
|
|
{
|
|
name: "OpenIncludesCustomActiveCategory",
|
|
filter: types.WorkFilter{Status: types.StatusOpen},
|
|
wantClause: "status IN (SELECT name FROM custom_statuses WHERE category = 'active')",
|
|
rejectClause: "status IN ('open', 'in_progress')",
|
|
wantLeadingArgs: []any{"open"},
|
|
},
|
|
{
|
|
name: "NonOpenSingularStatusStaysExact",
|
|
filter: types.WorkFilter{Status: types.StatusInProgress},
|
|
wantClause: "status = ?",
|
|
rejectClause: "custom_statuses",
|
|
wantLeadingArgs: []any{"in_progress"},
|
|
},
|
|
{
|
|
name: "EmptyFilterLegacyDefault",
|
|
filter: types.WorkFilter{},
|
|
wantClause: "status IN ('open', 'in_progress')",
|
|
rejectClause: "status = ?",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
where, args, err := BuildReadyWorkWhere(tt.filter, IssuesFilterTables, ReadyWorkWhereInputs{})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !strings.Contains(where, tt.wantClause) {
|
|
t.Errorf("status clause %q missing.\n where = %s", tt.wantClause, where)
|
|
}
|
|
if tt.rejectClause != "" && strings.Contains(where, tt.rejectClause) {
|
|
t.Errorf("unexpected status clause %q present.\n where = %s", tt.rejectClause, where)
|
|
}
|
|
if len(args) < len(tt.wantLeadingArgs) {
|
|
t.Fatalf("args = %v, want at least the %d leading status args %v", args, len(tt.wantLeadingArgs), tt.wantLeadingArgs)
|
|
}
|
|
for i, want := range tt.wantLeadingArgs {
|
|
if args[i] != want {
|
|
t.Errorf("args[%d] = %v, want %v (status args must lead)", i, args[i], want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestOptionalWispTable pins the set a wisp query may treat as "no wisps".
|
|
// leases and wisp_labels are joined or hydrated by that query but are not the
|
|
// wisp plane being absent, so tolerating them turns a broken database into an
|
|
// empty result.
|
|
func TestOptionalWispTable(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, name := range []string{"wisps", "wisp_dependencies", "WISPS"} {
|
|
if !OptionalWispTable(name) {
|
|
t.Errorf("OptionalWispTable(%q) = false, want true", name)
|
|
}
|
|
}
|
|
for _, name := range []string{"leases", "wisp_labels", "issues", "labels", "", "wisp"} {
|
|
if OptionalWispTable(name) {
|
|
t.Errorf("OptionalWispTable(%q) = true, want false", name)
|
|
}
|
|
}
|
|
}
|