* feat(ids): say when a partial ID resolved to something else ResolvePartialID tries an exact match first and only then falls back to leading-prefix abbreviation (a3f8 -> a3f8e9). Ambiguity is already handled well: two or more candidates return ErrAmbiguousID naming all of them. But a *sole* abbreviation match was returned silently, so a caller could act on an issue it never named with nothing to indicate the substitution. The case that motivates it: a hierarchical child is a valid leading-prefix match for its own parent's ID, because "." is a legal ID character and the prefix test has no boundary. So once a parent ID is vacated — renamed away, or deleted — `bd show <parent-id>` returns the child, exit 0, no signal. With exactly one descendant it is silent; with two or more the existing ambiguity error catches it. The silent case is the dangerous one. Rather than narrow resolution (e.g. requiring a boundary so a child stops abbreviating its parent), which changes semantics and risks breaking legitimate abbreviations, this announces the substitution and leaves the documented abbreviation UX alone. Emitted from the resolver, not from individual commands: ResolvePartialID has 64 non-test call sites, so a per-command notice would be applied inconsistently. One emission point covers all of them, and it sits at the only return that hands back an issue the caller did not name. stderr, never stdout, so --json payloads and piped output are byte-for-byte unchanged. Gated on --quiet and BD_NO_PARTIAL_ID_NOTICE, following the BD_NO_DEP_TYPE_WARNING precedent in cmd/bd/dep.go. Deliberately not gated on stderr being a terminal, which is where it departs from that precedent: the callers most exposed to acting on the wrong issue are scripts, hooks and agents, and those are exactly the non-TTY ones. Tests: the gate matrix (quiet, env opt-out, identical/empty resolution), the message text, and end-to-end resolution over a small in-package fake store — the existing resolver tests need a live Dolt server and self-skip without one, so they cannot pin this in CI. Verified the end-to-end test fails without the emission. Co-Authored-By: Claude <noreply@anthropic.com> * docs(changelog): link the partial-ID notice entry to its PR Matches the surrounding entries, which each cite the PR or issue they came from. No code change. Co-Authored-By: Claude <noreply@anthropic.com> * test(ids): cover the second silent return; disclose the notice's breadth Review follow-up on #6302. MAJOR — CHANGELOG understated the scope. The notice fires on every leading-prefix abbreviation, not only the vacated-parent shape, so the ordinary documented a3f8 -> bd-a3f8e9 path emits a line too, once per resolved argument. The entry now says so, and says why the breadth is deliberate: a .-boundary rule would fix parent/child and leave bd-x.1 -> bd-x.11 just as silent. MINOR — an emission planted at the in-loop exact-hash return survived the suite. The suggested repro (prefix-less a3f8e9 -> bd-a3f8e9) does not reach that return: resolution has TWO fast paths, one on the raw input and one on the prefix-normalized input, and the normalized one catches that case first. The in-loop return is reached only CROSS-PREFIX, where the issue's real prefix is an allowed one other than the configured default, so both fast paths miss and the search loop matches on hash alone. TestResolvePartialIDSilentOnCrossPrefixExactHashMatch covers it (configured prefix bd, allowed hq, store holds hq-a3f8e9, input a3f8e9) and was verified to fail with that mutant planted and pass without it. The prefix-less case is kept as its own test, since it pins the normalized fast path staying silent. TestResolvePartialIDNotifiesOnOrdinaryAbbreviation pins the breadth the CHANGELOG now discloses. Also: captureStderr restores os.Stderr from a defer so a panicking test cannot swallow later tests' output, and closes the read end. Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
254 lines
7.7 KiB
Go
254 lines
7.7 KiB
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
func TestPartialIDSearchPartUsesLastHyphenSuffix(t *testing.T) {
|
|
got, ok := partialIDSearchPart("hacker-news-ko4")
|
|
if !ok {
|
|
t.Fatal("partialIDSearchPart returned ok=false")
|
|
}
|
|
if got != "ko4" {
|
|
t.Fatalf("search part = %q, want %q", got, "ko4")
|
|
}
|
|
}
|
|
|
|
func TestPartialIDSearchPartKeepsPlainAndHierarchicalIDs(t *testing.T) {
|
|
tests := []string{"abc123", "abc123.1", "bd-abc123.1"}
|
|
for _, input := range tests {
|
|
got, ok := partialIDSearchPart(input)
|
|
if !ok {
|
|
t.Fatalf("partialIDSearchPart(%q) returned ok=false", input)
|
|
}
|
|
want := input
|
|
if input == "bd-abc123.1" {
|
|
want = "abc123.1"
|
|
}
|
|
if got != want {
|
|
t.Fatalf("partialIDSearchPart(%q) = %q, want %q", input, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPartialIDSearchPartRejectsInvalidSearchText(t *testing.T) {
|
|
for _, input := range []string{"", "bd abc", "bd:abc", "bd/abc"} {
|
|
if got, ok := partialIDSearchPart(input); ok {
|
|
t.Fatalf("partialIDSearchPart(%q) = %q, true; want false", input, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- partial-resolution notice -------------------------------------------------
|
|
|
|
func TestShouldNotifyPartialResolution(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
resolved string
|
|
quiet bool
|
|
noNotify string
|
|
wantNotify bool
|
|
}{
|
|
{"non-exact match notifies", "bd-x.11", "bd-x.11.1", false, "", true},
|
|
{"identical resolution is silent", "bd-x.11", "bd-x.11", false, "", false},
|
|
{"empty resolution is silent", "bd-x.11", "", false, "", false},
|
|
{"quiet suppresses", "bd-x.11", "bd-x.11.1", true, "", false},
|
|
{"env opt-out suppresses", "bd-x.11", "bd-x.11.1", false, "1", false},
|
|
{"quiet and env together suppress", "bd-x.11", "bd-x.11.1", true, "1", false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := shouldNotifyPartialResolution(tt.input, tt.resolved, tt.quiet, tt.noNotify)
|
|
if got != tt.wantNotify {
|
|
t.Fatalf("shouldNotifyPartialResolution(%q, %q, quiet=%v, env=%q) = %v, want %v",
|
|
tt.input, tt.resolved, tt.quiet, tt.noNotify, got, tt.wantNotify)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEmitPartialResolutionNoticeNamesBothIDs(t *testing.T) {
|
|
out := captureStderr(t, func() {
|
|
emitPartialResolutionNotice("bd-x.11", "bd-x.11.1")
|
|
})
|
|
for _, want := range []string{"bd-x.11", "bd-x.11.1", "not an exact issue ID", "BD_NO_PARTIAL_ID_NOTICE"} {
|
|
if !strings.Contains(out, want) {
|
|
t.Fatalf("notice %q does not contain %q", out, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// fakeResolverStore is a minimal PartialIDResolverStore over an in-memory ID
|
|
// list. It avoids the live-Dolt dependency in id_parser_test.go, which skips
|
|
// when no test server is running and so cannot pin this behaviour in CI.
|
|
type fakeResolverStore struct {
|
|
ids []string
|
|
prefix string
|
|
allowed string // allowed_prefixes config, for cross-prefix cases
|
|
}
|
|
|
|
func (f *fakeResolverStore) SearchIssues(_ context.Context, _ string, filter types.IssueFilter) ([]*types.Issue, error) {
|
|
var out []*types.Issue
|
|
for _, want := range filter.IDs {
|
|
for _, id := range f.ids {
|
|
if id == want {
|
|
out = append(out, &types.Issue{ID: id})
|
|
}
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// SearchIssueIDs mimics the storage layer's `id LIKE %query%` filtering.
|
|
func (f *fakeResolverStore) SearchIssueIDs(_ context.Context, query string, _ types.IssueFilter) ([]string, error) {
|
|
var out []string
|
|
for _, id := range f.ids {
|
|
if query == "" || strings.Contains(id, query) {
|
|
out = append(out, id)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeResolverStore) GetConfig(_ context.Context, key string) (string, error) {
|
|
switch key {
|
|
case "issue_prefix":
|
|
return f.prefix, nil
|
|
case "allowed_prefixes":
|
|
return f.allowed, nil
|
|
}
|
|
return "", nil
|
|
}
|
|
|
|
// A renamed parent leaves its child carrying the old prefix, so the vacated
|
|
// parent ID resolves to that child by leading-prefix abbreviation. The caller
|
|
// must be told it did not get the ID it named.
|
|
func TestResolvePartialIDNotifiesWhenParentIDResolvesToItsChild(t *testing.T) {
|
|
store := &fakeResolverStore{ids: []string{"bd-x.11.1"}, prefix: "bd"}
|
|
|
|
var resolved string
|
|
var err error
|
|
out := captureStderr(t, func() {
|
|
resolved, err = ResolvePartialID(context.Background(), store, "bd-x.11")
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ResolvePartialID returned error: %v", err)
|
|
}
|
|
if resolved != "bd-x.11.1" {
|
|
t.Fatalf("resolved = %q, want %q", resolved, "bd-x.11.1")
|
|
}
|
|
if !strings.Contains(out, "bd-x.11.1") || !strings.Contains(out, "not an exact issue ID") {
|
|
t.Fatalf("expected a partial-resolution notice on stderr, got %q", out)
|
|
}
|
|
}
|
|
|
|
// Reaching the in-loop exact-hash return needs a CROSS-PREFIX input: both
|
|
// fast paths (raw input, then prefix-normalized input) miss, because the
|
|
// issue's real prefix is an allowed one that is not the configured default, so
|
|
// resolution falls through to the search loop and matches on hash alone. That
|
|
// return is a second silent exit, and an emission planted there survives any
|
|
// test that only exercises the fast paths.
|
|
func TestResolvePartialIDSilentOnCrossPrefixExactHashMatch(t *testing.T) {
|
|
store := &fakeResolverStore{ids: []string{"hq-a3f8e9"}, prefix: "bd", allowed: "hq"}
|
|
|
|
var resolved string
|
|
var err error
|
|
out := captureStderr(t, func() {
|
|
resolved, err = ResolvePartialID(context.Background(), store, "a3f8e9")
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ResolvePartialID returned error: %v", err)
|
|
}
|
|
if resolved != "hq-a3f8e9" {
|
|
t.Fatalf("resolved = %q, want %q", resolved, "hq-a3f8e9")
|
|
}
|
|
if out != "" {
|
|
t.Fatalf("expected no notice for an exact hash match, got %q", out)
|
|
}
|
|
}
|
|
|
|
// A prefix-less exact input is still silent, via the normalized fast path.
|
|
func TestResolvePartialIDSilentOnPrefixlessExactMatch(t *testing.T) {
|
|
store := &fakeResolverStore{ids: []string{"bd-a3f8e9"}, prefix: "bd"}
|
|
|
|
var resolved string
|
|
var err error
|
|
out := captureStderr(t, func() {
|
|
resolved, err = ResolvePartialID(context.Background(), store, "a3f8e9")
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ResolvePartialID returned error: %v", err)
|
|
}
|
|
if resolved != "bd-a3f8e9" {
|
|
t.Fatalf("resolved = %q, want %q", resolved, "bd-a3f8e9")
|
|
}
|
|
if out != "" {
|
|
t.Fatalf("expected no notice for an exact match, got %q", out)
|
|
}
|
|
}
|
|
|
|
// The documented abbreviation path notifies too — this is not limited to the
|
|
// parent/child shape, and the CHANGELOG says so.
|
|
func TestResolvePartialIDNotifiesOnOrdinaryAbbreviation(t *testing.T) {
|
|
store := &fakeResolverStore{ids: []string{"bd-a3f8e9"}, prefix: "bd"}
|
|
|
|
out := captureStderr(t, func() {
|
|
if _, err := ResolvePartialID(context.Background(), store, "a3f8"); err != nil {
|
|
t.Errorf("ResolvePartialID returned error: %v", err)
|
|
}
|
|
})
|
|
if !strings.Contains(out, "bd-a3f8e9") {
|
|
t.Fatalf("expected a notice naming the resolved ID, got %q", out)
|
|
}
|
|
}
|
|
|
|
func TestResolvePartialIDSilentOnExactMatch(t *testing.T) {
|
|
store := &fakeResolverStore{ids: []string{"bd-x.11", "bd-x.11.1"}, prefix: "bd"}
|
|
|
|
var resolved string
|
|
var err error
|
|
out := captureStderr(t, func() {
|
|
resolved, err = ResolvePartialID(context.Background(), store, "bd-x.11")
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ResolvePartialID returned error: %v", err)
|
|
}
|
|
if resolved != "bd-x.11" {
|
|
t.Fatalf("resolved = %q, want %q", resolved, "bd-x.11")
|
|
}
|
|
if out != "" {
|
|
t.Fatalf("expected no notice for an exact match, got %q", out)
|
|
}
|
|
}
|
|
|
|
func captureStderr(t *testing.T, fn func()) string {
|
|
t.Helper()
|
|
orig := os.Stderr
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("os.Pipe: %v", err)
|
|
}
|
|
// Restore even if fn panics, so one failure cannot swallow every later
|
|
// test's stderr. Both pipe ends are closed before returning.
|
|
defer func() {
|
|
os.Stderr = orig
|
|
_ = r.Close()
|
|
}()
|
|
os.Stderr = w
|
|
done := make(chan string, 1)
|
|
go func() {
|
|
var sb strings.Builder
|
|
_, _ = io.Copy(&sb, r)
|
|
done <- sb.String()
|
|
}()
|
|
fn()
|
|
_ = w.Close()
|
|
return <-done
|
|
}
|