git-remote-entire: test makeBindHook bind-on-success behavior · Entire

git-remote-entire: test makeBindHook bind-on-success behavior

4f44cc8→main·

toothbrush·1mo ago·1 file·+61 added/-0 removed

Covers the three paths: nil hook for an empty context name, a binding persisted after the first call for an existing context, and no binding written when the named context is missing (BindCluster fails, logged not fatal).

Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com

Sessions

9e787e814af7View transcript

Changes

1

4 unmodified lines

5
6
7
8
9
10
11
12
23 unmodified lines

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

4 unmodified lines

"net/http"
    "net/http/httptest"
    "testing"

"github.com/entireio/cli/internal/entireclient/contexts"
}

func TestGitActionFromRequest(t *testing.T) {
23 unmodified lines

})
    }
}

func TestMakeBindHook(t *testing.T) {
    t.Parallel()

t.Run("nil when no context name", func(t *testing.T) {
        t.Parallel()
        if makeBindHook(t.TempDir(), "host.example", "") != nil {
            t.Fatal("expected nil hook for empty context name")
        }
    })

t.Run("binds cluster after first call", func(t *testing.T) {
        t.Parallel()
        dir := t.TempDir()
        // BindCluster only writes a binding for a context that exists.
        if err := contexts.Modify(dir, func(f *contexts.File) (bool, error) {
            f.Upsert(&contexts.Context{
                Name:            "ctx",
                CoreURL:         "https://core.example",
                Handle:          "h",
                KeychainService: "svc",
            })
            return true, nil
        }); err != nil {
            t.Fatalf("seed context: %v", err)
        }

hook := makeBindHook(dir, "cluster.example", "ctx")
        if hook == nil {
            t.Fatal("expected non-nil hook")
        }
        hook()
        hook() // second call is a no-op (sync.Once); must not error or change state

f, err := contexts.Load(dir)
        if err != nil {
            t.Fatalf("load contexts: %v", err)
        }
        if got := f.ClusterContexts["cluster.example"]; got != "ctx" {
            t.Fatalf("binding for cluster.example = %q, want %q", got, "ctx")
        }
    })

t.Run("no binding when context is missing", func(t *testing.T) {
        t.Parallel()
        dir := t.TempDir()
        // No context named "ghost" exists, so BindCluster fails and the hook
        // logs without persisting anything.
        makeBindHook(dir, "cluster.example", "ghost")()

f, err := contexts.Load(dir)
        if err != nil {
            t.Fatalf("load contexts: %v", err)
        }
        if _, ok := f.ClusterContexts["cluster.example"]; ok {
            t.Fatal("expected no binding to be written for a missing context")
        }
    })
}