cli/internal/ghinstance/host_test.go
William Martin 998b6212b3
Add skills specific telemetry
* Add skills specific telemetry

* Remove VisibilityFuture, inline goroutine at call sites

The VisibilityFuture/FetchRepoVisibilityAsync/Wait wrapper was an
unidiomatic async abstraction built for a single pattern used in
exactly two call sites. In Go the channel is already the future;
wrapping it in a struct with a Wait(timeout) method adds no value.

Delete the abstraction and inline a local visResult struct,
buffered channel, goroutine, and select at each call site. Behavior
is preserved exactly: err -> "unknown", timeout -> "unknown",
success+public -> include skill_names.

FetchRepoVisibility (synchronous) is kept as-is.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Fix nonsense copilot tests

* Update telemetry tests for public-only dims and search event removal

Production telemetry emission changed:
- preview: skill_owner/skill_repo/skill_name (renamed from skill_names)
  are now emitted only when repo_visibility=public.
- install: skill_owner/skill_repo/skill_names are now emitted only
  when repo_visibility=public.
- search: the initial skill_search event was removed entirely; the
  skill_search_install event no longer carries query/owner dims.

Update tests to match: rename skill_names -> skill_name in preview,
make owner/repo assertions conditional on public visibility in both
preview and install, and reduce the search test to a single event
with explicit Empty assertions for the removed query/owner dims so a
privacy regression cannot pass silently.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Test CategorizeHost and switch telemetry to skill_host_type

Add TestCategorizeHost covering all four classification branches
(github.com, ghes, tenancy, uncategorized) with cases verified
against the real ghauth implementation rather than guessed.

Update install and preview unit tests to assert the new
skill_host_type dimension name, and fix a typo in the preview
acceptance txtar (skill_hos_type -> skill_host_type).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Shrink visibility wait and test unknown visibility

The 2s visibilityWaitTimeout was wildly overprovisioned: by the time
telemetry emission reaches the select, the command has already done
several serial GitHub REST calls (and for install, a git sparse-checkout
plus possibly interactive prompts), so the one-call visibility fetch
has almost always completed. Drop the timeout to 200ms — a short safety
net for a stalled REST call, not a wait budget for a healthy one.

Also adds a table-driven case to TestFetchRepoVisibility covering an
unknown/future visibility value from the API, addressing @babakks'
review nitpick.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-17 19:58:59 +02:00

213 lines
4 KiB
Go

package ghinstance
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTenantName(t *testing.T) {
tests := []struct {
host string
wantTenant string
wantFound bool
}{
{
host: "github.com",
wantTenant: "github.com",
},
{
host: "github.localhost",
wantTenant: "github.localhost",
},
{
host: "garage.github.com",
wantTenant: "github.com",
},
{
host: "ghe.com",
wantTenant: "ghe.com",
},
{
host: "tenant.ghe.com",
wantTenant: "tenant",
wantFound: true,
},
{
host: "api.tenant.ghe.com",
wantTenant: "tenant",
wantFound: true,
},
}
for _, tt := range tests {
t.Run(tt.host, func(t *testing.T) {
if tenant, found := TenantName(tt.host); tenant != tt.wantTenant || found != tt.wantFound {
t.Errorf("TenantName(%v) = %v %v, want %v %v", tt.host, tenant, found, tt.wantTenant, tt.wantFound)
}
})
}
}
func TestHostnameValidator(t *testing.T) {
tests := []struct {
name string
input string
wantsErr bool
}{
{
name: "valid hostname",
input: "internal.instance",
wantsErr: false,
},
{
name: "hostname with slashes",
input: "//internal.instance",
wantsErr: true,
},
{
name: "empty hostname",
input: " ",
wantsErr: true,
},
{
name: "hostname with colon",
input: "internal.instance:2205",
wantsErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := HostnameValidator(tt.input)
if tt.wantsErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
})
}
}
func TestGraphQLEndpoint(t *testing.T) {
tests := []struct {
host string
want string
}{
{
host: "github.com",
want: "https://api.github.com/graphql",
},
{
host: "github.localhost",
want: "http://api.github.localhost/graphql",
},
{
host: "garage.github.com",
want: "https://garage.github.com/api/graphql",
},
{
host: "ghe.io",
want: "https://ghe.io/api/graphql",
},
{
host: "tenant.ghe.com",
want: "https://api.tenant.ghe.com/graphql",
},
}
for _, tt := range tests {
t.Run(tt.host, func(t *testing.T) {
if got := GraphQLEndpoint(tt.host); got != tt.want {
t.Errorf("GraphQLEndpoint() = %v, want %v", got, tt.want)
}
})
}
}
func TestRESTPrefix(t *testing.T) {
tests := []struct {
host string
want string
}{
{
host: "github.com",
want: "https://api.github.com/",
},
{
host: "github.localhost",
want: "http://api.github.localhost/",
},
{
host: "garage.github.com",
want: "https://garage.github.com/api/v3/",
},
{
host: "ghe.io",
want: "https://ghe.io/api/v3/",
},
{
host: "tenant.ghe.com",
want: "https://api.tenant.ghe.com/",
},
}
for _, tt := range tests {
t.Run(tt.host, func(t *testing.T) {
if got := RESTPrefix(tt.host); got != tt.want {
t.Errorf("RESTPrefix() = %v, want %v", got, tt.want)
}
})
}
}
func TestCategorizeHost(t *testing.T) {
tests := []struct {
name string
host string
want string
}{
{
name: "github.com returns github.com",
host: "github.com",
want: "github.com",
},
{
name: "classic GHES hostname returns ghes",
host: "ghe.io",
want: "ghes",
},
{
name: "arbitrary enterprise hostname returns ghes",
host: "enterprise.example.com",
want: "ghes",
},
{
name: "tenant subdomain of ghe.com returns tenancy",
host: "tenant.ghe.com",
want: "tenancy",
},
{
name: "api subdomain under tenant returns tenancy",
host: "api.tenant.ghe.com",
want: "tenancy",
},
{
name: "bare ghe.com returns ghes",
host: "ghe.com",
want: "ghes",
},
{
name: "github.localhost returns uncategorized",
host: "github.localhost",
want: "uncategorized",
},
{
name: "github.com subdomain returns uncategorized",
host: "garage.github.com",
want: "uncategorized",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, CategorizeHost(tt.host))
})
}
}