* test(cli): scope workspace and Git caches together Add resetRepoCachesForTest for serial fixtures that change directories. Reset both successful and failed discoveries on entry and cleanup, and use it in the three remaining beads-only context/serve reset sites. Exercise non-repo to nested repository to non-repo discovery through both cache owners. Preserve runInDir's callback lifetime and the two original fixture repairs already owned by PR5747. Fixes #6207 Reported-by: bee-ghosttrack Bead: saa-cgh.92 Agent-Signature: unknown-model/unknown-reasoning * test(cache): leave conventional helper filename available (saa-cgh.92) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * test(cli): apply review fixes — one reset invariant, gazelle-synced srcs Maintainer review fixup for #6320 (iteration 1). Both minors from the review synthesis; no blocker or major was filed. Architectural Consistency (minor, both lanes): runInDir reset only the Git cache, the inverse of the leak this PR fixes — a workspace context primed by an earlier fixture survived its chdir. Extract the pair into resetRepoCaches() so one definition owns the invariant, and route both resetRepoCachesForTest and runInDir through it. Deviation from the synthesis's parenthetical "(or resetRepoCachesForTest)": calling that helper inside runInDir would move the exit reset from runInDir's own defer to a t.Cleanup at end of test, dropping the before/after guarantee its doc comment states, so the shared closure is extracted instead. The doc comment is updated to match the widened behaviour. TestRunInDirScopesWorkspaceDiscovery pins both halves. Restoring the git-only entry reset reds it at :94 ("no .beads directory found" — the finding's own observable); restoring the git-only exit reset reds it at :110 with the leaked context. In both arms the existing TestResetRepoCachesForTestScopesDiscovery stays green, so the new test covers an uncovered site rather than duplicating it. Test Evidence Quality (minor, codex): the new test file was absent from the gazelle-managed go_test srcs list, so bazel test never ran it. Ran make bazel-sync (gazelle, go_srcs.py, mod tidy) rather than hand-editing. Its entire output was three srcs lines: ours, plus two base-inherited absentees that landed in this PR's own base commit. Only the line for the file this PR adds is carried here, because shipping all three conflicts with origin/main. Measured, not assumed: git merge-tree --write-tree against origin/main puts BUILD.bazel in the conflict set with all three lines and merges it cleanly with one, while the merged tree's bd_test srcs holds the same 648 entries — including all three files — either way, since origin/main has already gazelle-synced the two base-inherited ones. The one-line form is therefore what gazelle emits at the merge target, and it leaves the base's own drift as found rather than fixing it in this PR. Validation: 32 tests covering all 22 package-main runInDir call sites pass before and after with an identical name-level PASS set; the PR's four affected tests plus the new pin are 5/5 anchored PASS; go vet, gofmt, git diff --check, golangci-lint (0 issues) and go_srcs.py --check all clean. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
140 lines
4.7 KiB
Go
140 lines
4.7 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/beads"
|
|
"github.com/steveyegge/beads/internal/configfile"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/storage/backends"
|
|
"github.com/steveyegge/beads/internal/storage/contextinfo"
|
|
"github.com/steveyegge/beads/internal/storage/domain"
|
|
)
|
|
|
|
// TestContextRoutesNameOneWorkspaceTheSameWay.
|
|
//
|
|
// There are two routes to a workspace snapshot and they are not
|
|
// interchangeable: `bd context` reads the config files itself, because it has
|
|
// to answer in degraded states where no database opens, while the proxied route
|
|
// and GET /v0/beads/context go through the contextinfo provider. Both hand the
|
|
// result to domain.PublishedContext, which exists so that the identity an
|
|
// automation client is given and the identity printed on the operator's
|
|
// terminal beside it are the same values read the same way.
|
|
//
|
|
// Nothing tested that. Both routes carried their own `Backend:
|
|
// configfile.BackendDolt`, so a workspace on a registered backend was described
|
|
// as embedded Dolt on database "beads" by both — the same lie twice, which is
|
|
// indistinguishable from agreement.
|
|
func TestContextRoutesNameOneWorkspaceTheSameWay(t *testing.T) {
|
|
const registered = "context-identity-backend"
|
|
backends.Register(registered, backends.Backend{
|
|
Open: func(context.Context, string) (storage.DoltStorage, error) {
|
|
return nil, errors.ErrUnsupported
|
|
},
|
|
OpenReadOnly: func(context.Context, string) (storage.DoltStorage, error) {
|
|
return nil, errors.ErrUnsupported
|
|
},
|
|
WorkspaceIsBeadsDir: true,
|
|
})
|
|
t.Cleanup(func() { backends.Deregister(registered) })
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
cfg *configfile.Config
|
|
want domain.PublishedContextFields
|
|
}{
|
|
{
|
|
// The case that was wrong. This workspace configures no Dolt
|
|
// anything, and both Dolt values default rather than fail.
|
|
name: "a registered backend",
|
|
cfg: &configfile.Config{Backend: registered, ProjectID: "proj-registered"},
|
|
want: domain.PublishedContextFields{Backend: registered, ProjectID: "proj-registered"},
|
|
},
|
|
{
|
|
name: "embedded dolt",
|
|
cfg: &configfile.Config{
|
|
Backend: configfile.BackendDolt,
|
|
DoltMode: configfile.DoltModeEmbedded,
|
|
ProjectID: "proj-dolt",
|
|
},
|
|
want: domain.PublishedContextFields{
|
|
Backend: configfile.BackendDolt,
|
|
DoltMode: configfile.DoltModeEmbedded,
|
|
Database: configfile.DefaultDoltDatabase,
|
|
ProjectID: "proj-dolt",
|
|
},
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
initGitRepoAt(t, dir)
|
|
beadsDir := filepath.Join(dir, ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
|
|
t.Fatalf("mkdir .beads: %v", err)
|
|
}
|
|
if err := tc.cfg.Save(beadsDir); err != nil {
|
|
t.Fatalf("save metadata.json: %v", err)
|
|
}
|
|
t.Chdir(dir)
|
|
t.Setenv("BEADS_DIR", beadsDir)
|
|
resetRepoCachesForTest(t)
|
|
|
|
provider, err := contextinfo.NewContextProvider(dir, Version).ContextUseCase().GetContextInfo(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("provider route: %v", err)
|
|
}
|
|
direct := directContextSnapshot(t, beadsDir)
|
|
|
|
// The paths and the version come from the workspace either way and
|
|
// are not what this compares; the identity is.
|
|
want := tc.want
|
|
want.BdVersion, want.BeadsDir, want.RepoRoot = Version, provider.BeadsDir, provider.RepoRoot
|
|
|
|
for _, got := range []struct {
|
|
route string
|
|
fields domain.PublishedContextFields
|
|
}{
|
|
{"the contextinfo provider (GET /v0/beads/context, bd context in proxied mode)", domain.PublishedContext(provider)},
|
|
{"the direct route (bd context)", domain.PublishedContext(direct)},
|
|
} {
|
|
if got.fields != want {
|
|
t.Errorf("%s published\n %+v\nwant\n %+v", got.route, got.fields, want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// NOT COVERED: this drives applyContextBackend, not contextCmd's RunE, so it
|
|
// pins the shared projection rather than the command's own wiring. A change
|
|
// that bypassed applyContextBackend inside RunE would not be caught here.
|
|
//
|
|
// directContextSnapshot assembles the snapshot `bd context`'s direct route
|
|
// answers from, off the same config files the command reads.
|
|
func directContextSnapshot(t *testing.T, beadsDir string) domain.ContextInfo {
|
|
t.Helper()
|
|
rc, err := beads.GetRepoContext()
|
|
if err != nil {
|
|
t.Fatalf("repo context: %v", err)
|
|
}
|
|
cfg, err := configfile.Load(beadsDir)
|
|
if err != nil || cfg == nil {
|
|
t.Fatalf("load metadata.json: %v", err)
|
|
}
|
|
|
|
snapshot := domain.ContextInfo{
|
|
BdVersion: Version,
|
|
BeadsDir: rc.BeadsDir,
|
|
RepoRoot: rc.RepoRoot,
|
|
}
|
|
if err := applyContextBackend(&snapshot, rc.BeadsDir, cfg); err != nil {
|
|
t.Fatalf("applyContextBackend: %v", err)
|
|
}
|
|
return snapshot
|
|
}
|