beads/internal/workapi/list_comments_test.go
mtccressall d14ed67f71
fix(list): expose comment bodies in bd list/show --json (be-73x) (#6473)
* fix(list): expose comment bodies and mark the omission in bd list --json (be-73x)

`bd list --json` reported comment_count and carried no comment text and no sign
that any was missing. The consequence is not a missing field: a content search
over a listing answers with a well-formed, NON-EMPTY result set whose matching
rows are absent, and nothing about that answer invites a second look. An empty
result invites suspicion; eight rows do not.

Two halves, and the second is the one that fixes what was actually reported:

  1. --include-comments on `bd list` populates each row's comments with the
     full bodies, the list twin of `bd show --include-comments`. Opt-in: it
     costs one query per row that has comments.
  2. Every row with a nonzero comment_count whose bodies were NOT hydrated now
     carries comments_omitted=true, by default, with no flag. A flag nobody
     knows to pass does not fix a silent hole; a marker on the row does.

The bodies ride on types.Issue.Comments, which already carried the `comments`
key for export/import and was simply never populated on this path, so the only
new field is the marker.

Hydration is one shared body (workapi.HydrateListComments) called from BOTH
issueops.Reader.List implementations, the store-backed and the unit-of-work
one. Those are separate bodies that have answered one contract method
differently before; it runs in the page epilogue beside FinishPageAt, after the
trim, so a page never pays for rows it is about to drop.

The source parameter is a CONSTRUCTOR rather than a source, and that is
load-bearing. Building the unit-of-work comment source eagerly made a
capability only the opt-in path uses into a requirement every caller had to
satisfy, and turned `GET /v0/beads/issues` into a 500 on a provider without a
comment use case — on requests that had asked for no comments. A count-only
listing now constructs nothing, pinned by a test that fails if the constructor
is called.

Tests pin each implementation separately rather than duplicating: unit tests
over a fake source, an embedded-dolt test over the real CLI on the direct
route, and a proxied-server test over the real CLI on the unit-of-work route.
Sabotage-controlled three ways — no-op the shared body and all three suites go
red; unwire either reader alone and only that reader's CLI test fails, which is
what proves neither integration test is redundant.

Not addressed, deliberately: gt-mfp5 (bd comment does not advance updated_at,
the orthogonal recency axis) and be-ozp (bd ready --json carries no marker; it
does not route through Reader.Ready).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XK4B7N2CrgfKGEQvDTPM4D

Executed-By: beadsrig/polecats/dag

* fix(list): do not read a skipped comment count as proof of no comments (be-73x)

Codex review of the parent commit, correctly.

`IncludeComments: true` combined with `SkipCounts: true` returned a page with
no comment bodies at all — no error, no marker, nothing — while
`IncludeComments` documents a guarantee to populate them. The hydration loop
skipped any row whose CommentCount was zero as an optimisation, and SkipCounts
zeroes every count: `issueops.ListRequest.SkipCounts` says in its own doc that
a zero then means UNKNOWN rather than none.

That is this bead's own failure mode reintroduced one layer down by the fix for
it: a silent, well-formed, empty answer to a request that asked for content.

I had considered the interaction and resolved it wrongly, in a comment reading
"the count is authoritative here because the JSON route never sets SkipCounts".
True of today's CLI callers, and not a property of the contract — issueops
.ListRequest is a PUBLIC request type and permits both fields at once. A
comment saying "X cannot happen because of how callers behave today" on a
public type is a bug with a justification attached, and worse than no comment,
because it tells the next reader the case was checked.

countsKnown is now a parameter rather than an assumption; call sites pass
!req.SkipCounts.

  countsKnown true   a zero count proves a row has nothing; skip its query.
  countsKnown false  query every row, because nothing proves otherwise, and
                     mark no row omitted, because comments_omitted asserts a
                     nonzero count the page cannot support.

The second half is the mirror defect on the marker path — the same conflation,
the other direction — which the review did not name.

No behaviour change on any current CLI path: the JSON route never sets
SkipCounts, so it passes countsKnown true, and the text route asks for no
comments and marked nothing before either. Verified: `bd list --json` output
identical, and text output identical with and without the flag.

Regression tests HonorsIncludeWhenCountsAreSkipped and
MarksNothingWhenCountsAreSkipped. Sabotage-controlled: restoring the
count-authoritative skip fails the first with "issued 0 comment reads, want 2".

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XK4B7N2CrgfKGEQvDTPM4D

Executed-By: beadsrig/polecats/dag

* fix(list): resolve the comment plane by residence, not by the row's flags (be-73x)

Maintainer review fixup for #6473, iteration 1. Both review lanes reached the
same major independently and the synthesizer re-verified it against the tree.

The new list hydration derived the comment table from the row's own
Ephemeral/NoHistory flags - issueops.IsWisp's rule, which is right for routing a
record being WRITTEN and wrong on a read. `bd import` pins a `no_history` record
to the DURABLE table while preserving the flag on the row, because clearing it
would change the content hash and break export->import->export byte-identity,
and every clone materializes its database by importing JSONL. So a promoted
no-history bead that has been through a round-trip is a durable row whose flags
say wisp:

  bd list --json --include-comments   read wisp_comments, found nothing, and
                                      returned comment_count=N beside an empty
                                      `comments`, with no marker and no error
  bd comment, bd show                 write and read that same row's comments on
                                      the issues plane, by found-table

That is be-73x's own failure mode reintroduced under the flag whose contract is
"gets them or gets an error", and a cross-role divergence: `bd list` and
`bd show` disagreed about one row. The mirror case is real too - an import
record whose `wisp_plane` key pins it to the wisps table with neither flag set
reads back as durable - so no flag-based rule gets both directions right.

The plane now comes from the seam that knows it. HydrateListComments takes a
CommentPlanes lookup instead of inferring; the unit-of-work reader answers it
from the wisps TABLE (IssueUseCase.GetWispsByIDs, the call edgeReader.ReadEdges
already makes for the same question), once per page, inside the page's own read
transaction. The store-backed seam passes SourceRoutesCommentPlanes because its
source routes ids itself (DoltStore.GetIssueComments picks the table from
isActiveWisp) and would buy a lookup for an argument it ignores. isWispPlane is
deleted: nothing on this path infers a plane any more. A nil lookup is an error
rather than a silent "durable", so a third seam has to answer the question
rather than inherit the default this bug was.

A MISSING WISPS TABLE STAYS "NO WISPS" rather than a failed listing, classified
through the same two helpers the counts query that produced the page uses
(dberrors.MissingTableName + sqlbuild.OptionalWispTable). Any other failure of
the lookup fails the call: a guessed plane answers with the wrong table's
contents and nothing in the response shows it.

Tests. The unit table now asserts residence AGAINST the flags, with each row's
comments present only on the plane it lives on, so a wrong-table read shows up
as missing TEXT and not merely as a wrong flag in a recorded call. Restoring the
flag inference reds exactly the two rows where flags and table disagree
(durable-carrying-no_history, wisp-table-carrying-no-flags) and leaves the two
agreeing rows green. The old `override_wins_over_flags` case is gone:
WispPlaneOverride is `json:"-"` and set only by import, in memory, so no scanned
row can carry it and that branch was unreachable on this path - which is what
made the suite look sufficient (Codex's separate minor, folded in here as the
same root cause). New seam-level tests drive the real uow Reader.List over both
shapes, and pin that a listing without the flag resolves no planes at all, that
a missing wisps table is tolerated, and that any other lookup failure
propagates; emptying the residence answer reds the wisp-row leg.

Also from the review, non-gating:

- types.IssueWithCounts.CommentsOmitted and the OpenAPI property stated the
  marker rule unconditionally on a type Reader.Ready and the claim response also
  return, neither of which calls the epilogue. Both now scope the rule to the
  List surfaces and cite be-ozp, which is where the parent commit deliberately
  left the ready surfaces. (`bd ready --json` does not route through
  Reader.Ready; GET /v0/beads/ready and ClaimNextResponse.claimed do.)
- GET /v0/beads/issues gained no `include_comments` parameter, so its
  description now says so and points at the detail read, in the idiom the
  `revision` paragraph above it already uses.
- One internal/httpapi list test for the new wire field: the marker present for
  a commented row, absent for an uncommented one. It was asserted only by the
  spec file; the detail twin has get_issue_test.go. Neutering
  markCommentsOmitted reds it.

Validation: go build ./...; go vet over the changed packages and again with
CGO_ENABLED=0; make api-check clean, so the spec edits oblige no regeneration;
go test ./internal/workapi/... ./issueops/... ./internal/types/;
TestBothReaderImplementationsAgreeOnTheListEpilogue plus the new plane tests in
./internal/storage/uow/; BEADS_TEST_PROXIED_SERVER=1
TestProxiedServerListComments 3 PASS and BEADS_TEST_EMBEDDED_DOLT=1
TestEmbeddedListComments 4 PASS.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-27 02:12:13 -07:00

440 lines
19 KiB
Go

package workapi
import (
"context"
"errors"
"slices"
"testing"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
)
// fakeCommentStreamer records what was asked of it and answers from a map, so
// a test can assert on the CALLS as well as on the result. The recorded plane
// is the half no rendered output would show.
//
// It answers PER PLANE, like the storage it stands in for: comments put in byID
// are on the durable plane and comments put in wispByID on the wisp one, so a
// read of the wrong table comes back empty here exactly as it does against a
// database. That is what lets a test assert the comment TEXT rather than only
// the recorded plane flag — the defect this file's plane coverage exists for
// produced an empty array, not a wrong one.
type fakeCommentStreamer struct {
byID map[string][]*types.Comment
wispByID map[string][]*types.Comment
err error
calls []streamCall
}
type streamCall struct {
id string
isWisp bool
}
func (f *fakeCommentStreamer) IterComments(_ context.Context, id string, isWisp bool) (storage.Iter[types.Comment], error) {
f.calls = append(f.calls, streamCall{id: id, isWisp: isWisp})
if f.err != nil {
return nil, f.err
}
if isWisp {
return storage.NewSliceIter(f.wispByID[id]), nil
}
return storage.NewSliceIter(f.byID[id]), nil
}
// fakePlanes is the seam that knows where a row lives, which on the production
// unit-of-work path is a query against the wisps table. It records the ids it
// was asked about so a test can pin that the lookup is bounded to the rows a
// comment read is actually issued for — and that a page needing no read pays
// for no lookup.
type fakePlanes struct {
wisps map[string]bool
err error
asked [][]string
}
// planesFor builds the answer for a page whose wisp-table residents are the
// given ids. Called with none, it is a page of durable rows.
func planesFor(wispIDs ...string) *fakePlanes {
wisps := make(map[string]bool, len(wispIDs))
for _, id := range wispIDs {
wisps[id] = true
}
return &fakePlanes{wisps: wisps}
}
func (f *fakePlanes) resolve(_ context.Context, ids []string) (map[string]bool, error) {
f.asked = append(f.asked, ids)
if f.err != nil {
return nil, f.err
}
return f.wisps, nil
}
// noPlaneLookup fails the test if the plane is resolved at all, which is the
// same assertion the failing source constructor makes one layer up: the routing
// query is part of the READ, so a page that reads nothing must not pay for it.
func noPlaneLookup(t *testing.T) CommentPlanes {
t.Helper()
return func(_ context.Context, ids []string) (map[string]bool, error) {
t.Errorf("resolved comment planes for %v on a page that issues no comment read", ids)
return nil, nil
}
}
// srcFunc adapts a fake to the lazy constructor HydrateListComments takes.
// The laziness is itself under test in TestHydrateListCommentsCountOnlyBuildsNoSource.
func srcFunc(src CommentStreamer) func() CommentStreamer {
return func() CommentStreamer { return src }
}
func row(id string, commentCount int) *types.IssueWithCounts {
return &types.IssueWithCounts{
Issue: &types.Issue{ID: id},
CommentCount: commentCount,
}
}
func comment(id, text string) *types.Comment {
return &types.Comment{ID: id, Text: text}
}
// TestHydrateListCommentsMarksOmittedWithoutFetching pins the DEFAULT half of
// the contract, which is the half be-73x is actually about: a page nobody
// asked to hydrate must still say that its comment text is missing, and it
// must say so without paying for a single read.
func TestHydrateListCommentsMarksOmittedWithoutFetching(t *testing.T) {
src := &fakeCommentStreamer{byID: map[string][]*types.Comment{
"a-1": {comment("c1", "root cause")},
}}
items := []*types.IssueWithCounts{row("a-1", 1), row("a-2", 0)}
planes := planesFor()
if err := HydrateListComments(context.Background(), srcFunc(src), planes.resolve, items, false, true); err != nil {
t.Fatalf("HydrateListComments: %v", err)
}
if len(src.calls) != 0 {
t.Errorf("count-only mode issued %d comment reads, want 0: the marker is derived from the count already on the row", len(src.calls))
}
// The plane lookup is part of the read, so a page that reads nothing must
// not pay for it either: the marker path routes no query and so asks no
// table where anything lives.
if len(planes.asked) != 0 {
t.Errorf("count-only mode resolved comment planes %d times, want 0: nothing is being read, so nothing needs routing", len(planes.asked))
}
if items[0].CommentsOmitted == nil || !*items[0].CommentsOmitted {
t.Errorf("CommentsOmitted = %v on a row with comment_count 1, want true: without it an absent comments field reads as none", items[0].CommentsOmitted)
}
if items[0].Comments != nil {
t.Errorf("Comments = %v in count-only mode, want nil", items[0].Comments)
}
// The zero-count row is the control. A marker on it would make the flag
// meaningless: every row would carry it and none would be informative.
if items[1].CommentsOmitted != nil {
t.Errorf("CommentsOmitted = %v on a row with no comments, want unset: a true empty stays plain omission", *items[1].CommentsOmitted)
}
}
// TestHydrateListCommentsPopulatesBodies pins the opt-in half, and asserts on
// the COMMENT TEXT rather than on a length: the defect this fixes is that the
// text is unreachable, and a count of rows cannot tell a populated slice from
// a slice of empty ones.
func TestHydrateListCommentsPopulatesBodies(t *testing.T) {
src := &fakeCommentStreamer{byID: map[string][]*types.Comment{
"a-1": {comment("c1", "zzzuniquephrase"), comment("c2", "second")},
}}
items := []*types.IssueWithCounts{row("a-1", 2), row("a-2", 0)}
planes := planesFor()
if err := HydrateListComments(context.Background(), srcFunc(src), planes.resolve, items, true, true); err != nil {
t.Fatalf("HydrateListComments: %v", err)
}
if got := len(items[0].Comments); got != 2 {
t.Fatalf("hydrated %d comments, want 2", got)
}
if got := items[0].Comments[0].Text; got != "zzzuniquephrase" {
t.Errorf("comment body = %q, want %q: the bodies are the whole point", got, "zzzuniquephrase")
}
if items[0].CommentsOmitted != nil {
t.Errorf("CommentsOmitted = %v beside a populated slice, want unset: the two fields must read as one unambiguous answer", *items[0].CommentsOmitted)
}
// A row with no comments is not queried at all, which is what keeps the
// per-row cost proportional to the rows that actually have text.
for _, call := range src.calls {
if call.id == "a-2" {
t.Errorf("issued a comment read for a row with comment_count 0")
}
}
// And the plane lookup covers that same narrowed set, not the whole page.
if len(planes.asked) != 1 || !slices.Equal(planes.asked[0], []string{"a-1"}) {
t.Errorf("resolved planes for %v, want one lookup for [a-1]: routing is asked about the rows being read", planes.asked)
}
}
// TestHydrateListCommentsRoutesByResidenceNotFlags pins where the comment plane
// comes from, and the two rows in the middle of the table are the reason it is
// not the row's flags.
//
// Both of those rows are states the tree maintains on purpose. `bd import` pins
// a no_history record to the DURABLE table and preserves the flag on the row,
// because clearing it would change the content hash and break
// export→import→export byte-identity — so a promoted no-history bead that has
// been through a JSONL round-trip (which is how a clone materializes its
// database) is a durable row whose flags say wisp. The mirror case is an import
// record whose wisp_plane key pins it to the WISPS table with neither flag set.
// An earlier version of this body read Ephemeral || NoHistory, so it queried
// wisp_comments for the first and comments for the second, found nothing either
// way, and returned the row with its nonzero comment_count, no comments, no
// marker and no error — the silent hole be-73x exists to close, under the flag
// that promises the bodies or an error.
//
// The fixture puts each row's comments only on the plane it actually lives on,
// so a wrong-table read shows up as MISSING TEXT and not merely as a wrong flag
// in a recorded call.
func TestHydrateListCommentsRoutesByResidenceNotFlags(t *testing.T) {
cases := []struct {
name string
issue *types.Issue
inWisps bool
}{
{"durable row, no flags", &types.Issue{ID: "a-1"}, false},
// The flags say wisp and the table says durable: the import-pinned
// promoted bead. Residence wins.
{"durable row carrying no_history", &types.Issue{ID: "a-1", NoHistory: true}, false},
// The mirror: the flags say durable and the row is in the wisps table.
{"wisp-table row carrying no flags", &types.Issue{ID: "a-1"}, true},
{"wisp-table row carrying ephemeral", &types.Issue{ID: "a-1", Ephemeral: true}, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
src := &fakeCommentStreamer{}
planes := planesFor()
if tc.inWisps {
src.wispByID = map[string][]*types.Comment{"a-1": {comment("c1", "zzzuniquephrase")}}
planes = planesFor("a-1")
} else {
src.byID = map[string][]*types.Comment{"a-1": {comment("c1", "zzzuniquephrase")}}
}
items := []*types.IssueWithCounts{{Issue: tc.issue, CommentCount: 1}}
if err := HydrateListComments(context.Background(), srcFunc(src), planes.resolve, items, true, true); err != nil {
t.Fatalf("HydrateListComments: %v", err)
}
if len(src.calls) != 1 {
t.Fatalf("issued %d comment reads, want 1", len(src.calls))
}
if src.calls[0].isWisp != tc.inWisps {
t.Errorf("read the %v plane for a row living on the %v plane", planeName(src.calls[0].isWisp), planeName(tc.inWisps))
}
if got := len(items[0].Comments); got != 1 {
t.Fatalf("hydrated %d comments, want 1: a read of the other plane comes back empty, with a nonzero comment_count still on the row", got)
}
if got := items[0].Comments[0].Text; got != "zzzuniquephrase" {
t.Errorf("comment body = %q, want %q", got, "zzzuniquephrase")
}
if items[0].CommentsOmitted != nil {
t.Errorf("CommentsOmitted = %v on a hydrated row, want unset", *items[0].CommentsOmitted)
}
})
}
}
// TestHydrateListCommentsRequiresAPlaneAnswer pins that the routing question has
// to be ANSWERED rather than defaulted.
//
// A nil CommentPlanes would read every row on the durable plane, which is the
// wrong-table read of the test above wearing a default instead of an inference,
// and the seam that gets it wrong is the one whose source cannot detect it. A
// caller whose source routes ids itself says so by name
// (SourceRoutesCommentPlanes), so the two cases are distinguishable at the call
// site and a third seam has to make the choice rather than inherit it.
func TestHydrateListCommentsRequiresAPlaneAnswer(t *testing.T) {
src := &fakeCommentStreamer{byID: map[string][]*types.Comment{"a-1": {comment("c1", "x")}}}
items := []*types.IssueWithCounts{row("a-1", 1)}
if err := HydrateListComments(context.Background(), srcFunc(src), nil, items, true, true); err == nil {
t.Error("hydrating with no plane resolution returned nil, want an error: an unanswered plane defaults to a wrong-table read no caller can see")
}
if len(src.calls) != 0 {
t.Errorf("issued %d comment reads without knowing the plane, want 0", len(src.calls))
}
// The marker path routes nothing, so it needs no answer and must not
// demand one: a caller can pass the same arguments either way.
if err := HydrateListComments(context.Background(), srcFunc(src), nil, items, false, true); err != nil {
t.Errorf("count-only mode with no plane resolution: %v", err)
}
// SourceRoutesCommentPlanes is the explicit form of "durable for everyone,
// and the source is the one that knows better".
if err := HydrateListComments(context.Background(), srcFunc(src), SourceRoutesCommentPlanes, items, true, true); err != nil {
t.Fatalf("HydrateListComments with SourceRoutesCommentPlanes: %v", err)
}
if len(src.calls) != 1 || src.calls[0].isWisp {
t.Errorf("calls = %v, want one durable-plane read: the source is left to route the id", src.calls)
}
}
// TestHydrateListCommentsFailsWhenThePlaneIsUnknown extends hydrate-or-error to
// the routing lookup. A page that cannot find out where its rows live must not
// fall back to a guess: guessing is what returned empty comment arrays beside
// nonzero counts, and it is worse here because the failure would be invisible in
// the answer rather than reported in it.
func TestHydrateListCommentsFailsWhenThePlaneIsUnknown(t *testing.T) {
sentinel := errors.New("wisps table unreachable")
src := &fakeCommentStreamer{byID: map[string][]*types.Comment{"a-1": {comment("c1", "x")}}}
planes := planesFor()
planes.err = sentinel
items := []*types.IssueWithCounts{row("a-1", 1)}
err := HydrateListComments(context.Background(), srcFunc(src), planes.resolve, items, true, true)
if err == nil {
t.Fatal("HydrateListComments returned nil when the comment plane could not be resolved, want an error")
}
if !errors.Is(err, sentinel) {
t.Errorf("error = %v, want it to wrap %v", err, sentinel)
}
if len(src.calls) != 0 {
t.Errorf("issued %d comment reads after the plane lookup failed, want 0: a read on a guessed plane answers with the wrong table", len(src.calls))
}
}
func planeName(isWisp bool) string {
if isWisp {
return "wisp"
}
return "durable"
}
// TestHydrateListCommentsFailsRatherThanShortening pins the promise that makes
// the opt-in half trustworthy. A caller that asked for the bodies and silently
// received fewer than exist is back in the failure this whole change removes,
// with no way to detect it.
func TestHydrateListCommentsFailsRatherThanShortening(t *testing.T) {
sentinel := errors.New("comment table unreachable")
src := &fakeCommentStreamer{err: sentinel}
items := []*types.IssueWithCounts{row("a-1", 1)}
err := HydrateListComments(context.Background(), srcFunc(src), planesFor().resolve, items, true, true)
if err == nil {
t.Fatal("HydrateListComments returned nil on a failing read, want an error: a short list a caller cannot detect is the defect, not a degraded success")
}
if !errors.Is(err, sentinel) {
t.Errorf("error = %v, want it to wrap %v", err, sentinel)
}
}
// TestHydrateListCommentsCountOnlyToleratesNoSource pins that the default path
// needs no comment source at all, which is what lets a caller pass one
// unconditionally without deciding whether it will be used.
func TestHydrateListCommentsCountOnlyToleratesNoSource(t *testing.T) {
items := []*types.IssueWithCounts{row("a-1", 3)}
if err := HydrateListComments(context.Background(), nil, planesFor().resolve, items, false, true); err != nil {
t.Fatalf("count-only mode with a nil source: %v", err)
}
if items[0].CommentsOmitted == nil || !*items[0].CommentsOmitted {
t.Error("count-only mode with a nil source did not mark the row omitted")
}
if err := HydrateListComments(context.Background(), nil, planesFor().resolve, items, true, true); err == nil {
t.Error("hydrating with a nil source returned nil, want an error")
}
}
// TestHydrateListCommentsSkipsNilRows keeps the shared epilogue from panicking
// on a page shape a caller can legally hand it.
func TestHydrateListCommentsSkipsNilRows(t *testing.T) {
src := &fakeCommentStreamer{byID: map[string][]*types.Comment{}}
items := []*types.IssueWithCounts{nil, {Issue: nil, CommentCount: 2}, row("a-1", 0)}
for _, include := range []bool{false, true} {
if err := HydrateListComments(context.Background(), srcFunc(src), planesFor().resolve, items, include, true); err != nil {
t.Fatalf("include=%v: %v", include, err)
}
}
}
// TestHydrateListCommentsCountOnlyBuildsNoSource pins the laziness as a
// behaviour rather than an implementation detail.
//
// It is here because the eager version shipped and broke something: building
// the unit-of-work comment source unconditionally panicked for a provider that
// has no comment use case, on list requests that had asked for no comments.
// A count-only listing must not so much as CONSTRUCT a comment reader, so the
// constructor here fails the test if it is ever called.
func TestHydrateListCommentsCountOnlyBuildsNoSource(t *testing.T) {
built := 0
newSrc := func() CommentStreamer {
built++
t.Error("count-only hydration constructed a comment source; the dependency must be as opt-in as the feature")
return &fakeCommentStreamer{}
}
items := []*types.IssueWithCounts{row("a-1", 4), row("a-2", 0)}
if err := HydrateListComments(context.Background(), newSrc, noPlaneLookup(t), items, false, true); err != nil {
t.Fatalf("HydrateListComments: %v", err)
}
if built != 0 {
t.Errorf("constructed %d comment sources in count-only mode, want 0", built)
}
if items[0].CommentsOmitted == nil || !*items[0].CommentsOmitted {
t.Error("count-only mode did not mark the row omitted")
}
}
// TestHydrateListCommentsHonorsIncludeWhenCountsAreSkipped is the regression
// for the combination that made the count-based skip unsound.
//
// issueops.ListRequest.SkipCounts zeroes CommentCount and says in its own doc
// that a zero then means UNKNOWN. A body that reads the count as authoritative
// therefore answers IncludeComments with an entirely empty page — no error, no
// short-list marker, nothing. That is the same silent-omission failure this
// whole change exists to remove, reintroduced one layer down and reachable by
// any caller of a PUBLIC request type.
func TestHydrateListCommentsHonorsIncludeWhenCountsAreSkipped(t *testing.T) {
src := &fakeCommentStreamer{byID: map[string][]*types.Comment{
"a-1": {comment("c1", "zzzuniquephrase")},
}}
// Counts skipped: every row reports zero whether or not it has comments.
items := []*types.IssueWithCounts{row("a-1", 0), row("a-2", 0)}
planes := planesFor()
if err := HydrateListComments(context.Background(), srcFunc(src), planes.resolve, items, true, false); err != nil {
t.Fatalf("HydrateListComments: %v", err)
}
if len(src.calls) != 2 {
t.Fatalf("issued %d comment reads, want 2: with no trustworthy count, every row must be queried", len(src.calls))
}
// The plane lookup widens with them: a page whose counts prove nothing
// cannot narrow the set whose residence has to be resolved either.
if len(planes.asked) != 1 || !slices.Equal(planes.asked[0], []string{"a-1", "a-2"}) {
t.Errorf("resolved planes for %v, want one lookup for [a-1 a-2]", planes.asked)
}
if got := len(items[0].Comments); got != 1 {
t.Fatalf("hydrated %d comments for the row that has one, want 1", got)
}
if got := items[0].Comments[0].Text; got != "zzzuniquephrase" {
t.Errorf("comment body = %q, want %q", got, "zzzuniquephrase")
}
if items[1].Comments != nil {
t.Errorf("row with genuinely no comments got %v, want nil", items[1].Comments)
}
}
// TestHydrateListCommentsMarksNothingWhenCountsAreSkipped is the other side of
// the same unknown. CommentsOmitted asserts "this row HAS comments and they are
// not here"; a page whose counts were skipped cannot support that claim about
// any row, so it must make it about none rather than deriving it from a zero
// that means nothing.
func TestHydrateListCommentsMarksNothingWhenCountsAreSkipped(t *testing.T) {
items := []*types.IssueWithCounts{row("a-1", 0), row("a-2", 0)}
if err := HydrateListComments(context.Background(), nil, nil, items, false, false); err != nil {
t.Fatalf("HydrateListComments: %v", err)
}
for _, item := range items {
if item.CommentsOmitted != nil {
t.Errorf("%s carries comments_omitted on a page with no trustworthy counts, want unset: the marker would be a claim the page cannot support", item.ID)
}
}
}