test(handlers): add unit tests for buildCloneURL and setIfEmpty
Some checks failed
Block internal-flavored paths / Block forbidden paths (pull_request) Successful in 26s
CI / Detect changes (pull_request) Successful in 1m35s
E2E API Smoke Test / detect-changes (pull_request) Successful in 1m37s
CI / Shellcheck (E2E scripts) (pull_request) Successful in 5s
CI / Python Lint & Test (pull_request) Successful in 5s
E2E API Smoke Test / E2E API Smoke Test (pull_request) Failing after 1m3s
CI / Platform (Go) (pull_request) Failing after 2m25s
CI / Canvas (Next.js) (pull_request) Failing after 13m35s
CI / Canvas Deploy Reminder (pull_request) Has been skipped
CI / all-required (pull_request) Failing after 6s
sop-checklist / all-items-acked (pull_request) [info tier:low] acked: 0/7 — missing: comprehensive-testing, local-postgres-e2e, staging-smoke, +4 — body-unfilled: comprehensive-testing, l
audit-force-merge / audit (pull_request) Has been skipped
Some checks failed
Block internal-flavored paths / Block forbidden paths (pull_request) Successful in 26s
CI / Detect changes (pull_request) Successful in 1m35s
E2E API Smoke Test / detect-changes (pull_request) Successful in 1m37s
CI / Shellcheck (E2E scripts) (pull_request) Successful in 5s
CI / Python Lint & Test (pull_request) Successful in 5s
E2E API Smoke Test / E2E API Smoke Test (pull_request) Failing after 1m3s
CI / Platform (Go) (pull_request) Failing after 2m25s
CI / Canvas (Next.js) (pull_request) Failing after 13m35s
CI / Canvas Deploy Reminder (pull_request) Has been skipped
CI / all-required (pull_request) Failing after 6s
sop-checklist / all-items-acked (pull_request) [info tier:low] acked: 0/7 — missing: comprehensive-testing, local-postgres-e2e, staging-smoke, +4 — body-unfilled: comprehensive-testing, l
audit-force-merge / audit (pull_request) Has been skipped
- buildCloneURL (artifacts.go): 4 cases covering normal URL construction, special characters in token, empty token, and empty repo name. - setIfEmpty (agent_git_identity.go): 5 cases covering new key insertion, existing key preservation, empty-string value preservation, nil map safety, and multi-key interaction. Found by gap analysis across workspace-server/internal/handlers. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
718b7e6455
commit
0de7bbba19
@ -99,3 +99,55 @@ func TestSlugifyForEmail(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// setIfEmpty sets m[key] = val only when key is absent.
|
||||
|
||||
func TestSetIfEmpty_NewKey(t *testing.T) {
|
||||
m := map[string]string{}
|
||||
setIfEmpty(m, "FOO", "bar")
|
||||
if got := m["FOO"]; got != "bar" {
|
||||
t.Errorf("setIfEmpty(new key): got %q, want %q", got, "bar")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetIfEmpty_ExistingKey_NotOverwritten(t *testing.T) {
|
||||
m := map[string]string{"FOO": "original"}
|
||||
setIfEmpty(m, "FOO", "ignored")
|
||||
if got := m["FOO"]; got != "original" {
|
||||
t.Errorf("setIfEmpty(existing key): got %q, want %q", got, "original")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetIfEmpty_ExistingKey_EmptyString_NotOverwritten(t *testing.T) {
|
||||
m := map[string]string{"FOO": ""}
|
||||
setIfEmpty(m, "FOO", "ignored")
|
||||
// Key exists (even with empty value) — must not be overwritten.
|
||||
if got := m["FOO"]; got != "" {
|
||||
t.Errorf("setIfEmpty(existing, empty value): got %q, want %q", got, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetIfEmpty_NilMap_DoesNotPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("setIfEmpty panicked on nil map: %v", r)
|
||||
}
|
||||
}()
|
||||
var m map[string]string // nil map
|
||||
setIfEmpty(m, "KEY", "val")
|
||||
// Writing to a nil map is a no-op in Go; no panic, but no insert either.
|
||||
}
|
||||
|
||||
func TestSetIfEmpty_MultipleKeys(t *testing.T) {
|
||||
m := map[string]string{}
|
||||
setIfEmpty(m, "A", "1")
|
||||
setIfEmpty(m, "B", "2")
|
||||
setIfEmpty(m, "A", "ignored") // already set
|
||||
setIfEmpty(m, "C", "3")
|
||||
want := map[string]string{"A": "1", "B": "2", "C": "3"}
|
||||
for k, v := range want {
|
||||
if m[k] != v {
|
||||
t.Errorf("m[%q] = %q, want %q", k, m[k], v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -993,3 +993,40 @@ func containsCredentials(u string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// buildCloneURL constructs an authenticated Cloudflare Git CDN URL from a repo
|
||||
// name and token. The third parameter is unused (named _).
|
||||
|
||||
func TestBuildCloneURL_NormalCase(t *testing.T) {
|
||||
got := buildCloneURL("my-repo", "mytoken123", "unused")
|
||||
want := "https://x:mytoken123@artifacts.cloudflare.net/git/my-repo.git"
|
||||
if got != want {
|
||||
t.Errorf("buildCloneURL() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCloneURL_TokenWithSpecialChars(t *testing.T) {
|
||||
// Token characters are interpolated raw; URL-safe token encoding is the
|
||||
// caller's responsibility.
|
||||
got := buildCloneURL("org/repo", "tok!@#$%", "_ignored")
|
||||
want := "https://x:tok!@#$%@artifacts.cloudflare.net/git/org/repo.git"
|
||||
if got != want {
|
||||
t.Errorf("buildCloneURL() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCloneURL_EmptyToken(t *testing.T) {
|
||||
got := buildCloneURL("repo", "", "_ignored")
|
||||
want := "https://x:@artifacts.cloudflare.net/git/repo.git"
|
||||
if got != want {
|
||||
t.Errorf("buildCloneURL() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCloneURL_EmptyRepoName(t *testing.T) {
|
||||
got := buildCloneURL("", "tok", "_ignored")
|
||||
want := "https://x:tok@artifacts.cloudflare.net/git/.git"
|
||||
if got != want {
|
||||
t.Errorf("buildCloneURL() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user