GitLabgitlab-org

cli

main
ply9 git calls350 ms99 KiB6 cachedprotocol v2
175 lines6.0 KiBRaw
1//go:build !integration
3package claudehooks
5import (
6 "encoding/json"
7 "os"
8 "path/filepath"
9 "testing"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
15func TestWriteSettings_PreservesFilePermissions(t *testing.T) {
16 dir := t.TempDir()
17 path := dir + "/settings.json"
19 // Create file with 0644
20 require.NoError(t, os.WriteFile(path, []byte(`{"model":"test"}`), 0o644))
21 info, _ := os.Stat(path)
22 before := info.Mode()
24 raw := map[string]json.RawMessage{"model": json.RawMessage(`"test"`)}
25 require.NoError(t, WriteSettings(path, raw))
27 info, _ = os.Stat(path)
28 after := info.Mode()
30 assert.Equal(t, before, after, "file permissions should be preserved")
33func TestSyncHookInstalled(t *testing.T) {
34 t.Parallel()
36 path := filepath.Join(t.TempDir(), "settings.json")
38 installed, err := SyncHookInstalled(path)
39 require.NoError(t, err)
40 assert.False(t, installed, "a missing settings file has no hooks")
42 hooks := map[string][]HookGroup{}
43 AddHook(hooks, "Stop", StopHookCommand)
44 hooksJSON, err := json.Marshal(hooks)
45 require.NoError(t, err)
46 require.NoError(t, WriteSettings(path, map[string]json.RawMessage{"hooks": hooksJSON}))
48 installed, err = SyncHookInstalled(path)
49 require.NoError(t, err)
50 assert.True(t, installed)
52 hooks = map[string][]HookGroup{}
53 AddHook(hooks, "Stop", legacyStopHookCommand)
54 hooksJSON, err = json.Marshal(hooks)
55 require.NoError(t, err)
56 require.NoError(t, WriteSettings(path, map[string]json.RawMessage{"hooks": hooksJSON}))
58 installed, err = SyncHookInstalled(path)
59 require.NoError(t, err)
60 assert.True(t, installed, "a Stop hook from before the rename to glab minder still syncs")
62 require.NoError(t, os.WriteFile(path, []byte("{not json"), 0o644))
63 _, err = SyncHookInstalled(path)
64 require.Error(t, err)
67func TestHooks_RoundTripKeepsUnknownFields(t *testing.T) {
68 t.Parallel()
70 input := `{"PostToolUse":[{"matcher":"Bash","futureGroupField":{"x":1},"hooks":[{"type":"command","command":"~/.claude/hooks/print-mr-link.sh","if":"Bash(git push*)","statusMessage":"Checking for MR link","timeout":0}]}]}`
72 var hooks map[string][]HookGroup
73 require.NoError(t, json.Unmarshal([]byte(input), &hooks))
74 assert.True(t, AddHook(hooks, "Stop", StopHookCommand))
76 out, err := MarshalHooks(hooks)
77 require.NoError(t, err)
79 var got map[string][]struct {
80 Matcher string `json:"matcher"`
81 FutureField json.RawMessage `json:"futureGroupField"`
82 Hooks []map[string]json.RawMessage `json:"hooks"`
83 }
84 require.NoError(t, json.Unmarshal(out, &got))
85 require.Len(t, got["PostToolUse"], 1)
86 group := got["PostToolUse"][0]
87 assert.Equal(t, "Bash", group.Matcher)
88 assert.JSONEq(t, `{"x":1}`, string(group.FutureField))
89 require.Len(t, group.Hooks, 1)
90 entry := group.Hooks[0]
91 assert.JSONEq(t, `"Bash(git push*)"`, string(entry["if"]))
92 assert.JSONEq(t, `"Checking for MR link"`, string(entry["statusMessage"]))
93 assert.JSONEq(t, `0`, string(entry["timeout"]), "an explicit zero value is written back")
94 assert.Len(t, got["Stop"], 1)
97func TestMarshalHooks_KeepsShellCharactersReadable(t *testing.T) {
98 t.Parallel()
100 hooks := map[string][]HookGroup{}
101 AddHook(hooks, "Stop", StopHookCommand)
103 out, err := MarshalHooks(hooks)
104 require.NoError(t, err)
105 assert.Contains(t, string(out), `>/dev/null 2>&1 &`)
106 assert.NotContains(t, string(out), `\u003e`)
109func TestRemoveHook(t *testing.T) {
110 t.Parallel()
112 input := `{
113 "Stop": [
114 {"hooks": [{"type": "command", "command": "` + StopHookCommand + `"}]},
115 {"matcher": "Bash", "futureGroupField": {"x": 1}, "hooks": [
116 {"type": "command", "command": "` + StopHookCommand + `"},
117 {"type": "command", "command": "echo user-hook", "if": "Bash(git push*)"}
118 ]}
119 ],
120 "SessionEnd": [{"hooks": [{"type": "command", "command": "` + SessionEndHookCommand + `"}]}]
121 }`
122 var hooks map[string][]HookGroup
123 require.NoError(t, json.Unmarshal([]byte(input), &hooks))
125 assert.True(t, RemoveHook(hooks, "Stop", StopHookCommand))
126 assert.True(t, RemoveHook(hooks, "SessionEnd", SessionEndHookCommand))
127 assert.False(t, RemoveHook(hooks, "SessionEnd", SessionEndHookCommand), "removing twice changes nothing")
128 assert.False(t, RemoveHook(hooks, "PreToolUse", StopHookCommand), "an absent event changes nothing")
130 out, err := MarshalHooks(hooks)
131 require.NoError(t, err)
132 assert.JSONEq(t, `{
133 "Stop": [{"matcher": "Bash", "futureGroupField": {"x": 1}, "hooks": [
134 {"type": "command", "command": "echo user-hook", "if": "Bash(git push*)"}
135 ]}]
136 }`, string(out), "other hooks keep their group, matcher, and unknown fields, and emptied events are dropped")
139func TestInstallGlabHooks_ReplacesLegacyHooks(t *testing.T) {
140 t.Parallel()
142 hooks := map[string][]HookGroup{}
143 AddHook(hooks, "Stop", legacyStopHookCommand)
144 AddHook(hooks, "SessionEnd", legacySessionEndHookCommand)
145 AddHook(hooks, "Stop", "echo user-hook")
146 assert.True(t, LegacyHookPresent(hooks))
147 assert.True(t, GlabHookPresent(hooks, "Stop"), "legacy hooks still sync")
149 assert.True(t, InstallGlabHooks(hooks))
150 assert.False(t, InstallGlabHooks(hooks), "installing again changes nothing")
152 assert.False(t, LegacyHookPresent(hooks))
153 assert.True(t, HookPresent(hooks, "Stop", StopHookCommand))
154 assert.True(t, HookPresent(hooks, "SessionEnd", SessionEndHookCommand))
155 assert.True(t, HookPresent(hooks, "Stop", "echo user-hook"))
156 assert.Len(t, hooks["Stop"], 2, "the legacy Stop hook is replaced, not kept next to the new one")
157 assert.Len(t, hooks["SessionEnd"], 1)
160func TestRemoveGlabHooks_RemovesCurrentAndLegacyHooks(t *testing.T) {
161 t.Parallel()
163 hooks := map[string][]HookGroup{}
164 AddHook(hooks, "Stop", StopHookCommand)
165 AddHook(hooks, "Stop", legacyStopHookCommand)
166 AddHook(hooks, "SessionEnd", legacySessionEndHookCommand)
167 AddHook(hooks, "Stop", "echo user-hook")
169 assert.True(t, RemoveGlabHooks(hooks))
170 assert.False(t, RemoveGlabHooks(hooks), "removing twice changes nothing")
172 assert.False(t, GlabHookPresent(hooks, "Stop"))
173 assert.False(t, GlabHookPresent(hooks, "SessionEnd"))
174 assert.True(t, HookPresent(hooks, "Stop", "echo user-hook"))
Diagnostics9 git calls · 350 ms
CallStatusSizeTime0350 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=UPDATING
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200261 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=261
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=3bb9946301aa8c0dc8d202b83bc9f84bf0400f6d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=261
git.pkt.payload.max=234
git.sideband.band1.bytes=234
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=234
git.pack.trailer=e0a52f164f79506da6b0dcc275ed62911f5383bf
Open pack map
fetchtree:0200273 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=5fa160737ad30857ff606d64de1d596bb211ac3d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=273
git.pkt.payload.max=246
git.sideband.band1.bytes=246
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=246
git.pack.trailer=06dd39b7545811625e04a7645a0a64c9a62c99c6
Open pack map
fetchtree:0200172 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=172
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=fad6a354864efa1f4e58b0ebf3a892d3e063d43c
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=172
git.pkt.payload.max=145
git.sideband.band1.bytes=145
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=145
git.pack.trailer=e56fd3e20c405e8a9d9512bc4e8366864e9111b7
Open pack map
fetch2001.6 KiB331 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=331
git.response.bytes=1685
git.response.cache=MISS
git.response.duration_ms=331
git.fetch.wants=9c0d32113c2e42e9459eba974d5cb4580e24e49b
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1685
git.pkt.payload.max=1658
git.sideband.band1.bytes=1658
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e32480581a98f1
Request
bf798305…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format