Files
skillshare/internal/install/infer_tracked_kind_test.go
T
Willie 89080cf4f1 refactor(install): typed ambiguity error with counts, save one clone
The mixed-tracked-repo recovery path used a fragile substring match on
the error message and re-cloned the repo via /discover to count skills
and agents. Both problems share one root cause: the counts InferTrackedKind
already computed during the failed first install were thrown away.

- Add install.TrackKindAmbiguousError carrying the discovered Skills /
  Agents counts. InferTrackedKind returns it on the ambiguous branch.
  Error() preserves the legacy message verbatim so CLI output is unchanged.
- Server handleInstall track branch uses errors.As to detect it and emits
  writeCodedError with stable code 'install.track_kind_ambiguous' plus
  skills/agents counts in error_params.
- UI replaces the substring match with an ApiError.code check and reads
  counts straight from err.params, skipping the second /discover clone.
- Add unit tests asserting errors.As, count fidelity, and explicit-kind
  short-circuit (no clone).

Net result: 2 clones instead of 3 on the mixed-track recovery path, and a
stable cross-language contract instead of a brittle English substring.
2026-05-26 11:05:32 +08:00

97 lines
2.6 KiB
Go

package install
import (
"errors"
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestInferTrackedKind_MixedRepoReturnsTypedError(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
tmp := t.TempDir()
work := filepath.Join(tmp, "work")
remote := filepath.Join(tmp, "remote.git")
mustRunGit(t, "", "init", work)
mustRunGit(t, work, "config", "user.email", "test@test.com")
mustRunGit(t, work, "config", "user.name", "Test")
// Two skills under skills/
for _, name := range []string{"one", "two"} {
dir := filepath.Join(work, "skills", name)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("# "+name), 0644); err != nil {
t.Fatal(err)
}
}
// Three agents under agents/
agentsDir := filepath.Join(work, "agents")
if err := os.MkdirAll(agentsDir, 0755); err != nil {
t.Fatal(err)
}
for _, name := range []string{"alpha.md", "beta.md", "gamma.md"} {
if err := os.WriteFile(filepath.Join(agentsDir, name), []byte("# "+name), 0644); err != nil {
t.Fatal(err)
}
}
mustRunGit(t, work, "add", ".")
mustRunGit(t, work, "commit", "-m", "init")
mustRunGit(t, "", "clone", "--bare", work, remote)
source := &Source{
Type: SourceTypeGitHTTPS,
Raw: "file://" + remote,
CloneURL: "file://" + remote,
}
_, err := InferTrackedKind(source, "")
if err == nil {
t.Fatal("expected ambiguity error, got nil")
}
var ambig *TrackKindAmbiguousError
if !errors.As(err, &ambig) {
t.Fatalf("expected *TrackKindAmbiguousError, got %T: %v", err, err)
}
if ambig.Skills != 2 {
t.Errorf("Skills = %d, want 2", ambig.Skills)
}
if ambig.Agents != 3 {
t.Errorf("Agents = %d, want 3", ambig.Agents)
}
// Error() must preserve the legacy message verbatim so CLI output is
// unchanged and any existing string-based tests keep passing.
const wantMsg = "tracked install is ambiguous for mixed repositories; pass --kind skill or --kind agent"
if err.Error() != wantMsg {
t.Errorf("Error() = %q, want %q", err.Error(), wantMsg)
}
}
func TestInferTrackedKind_ExplicitKindSkipsDiscovery(t *testing.T) {
// No git invocation required — explicit kind short-circuits before clone.
source := &Source{
Type: SourceTypeGitHTTPS,
Raw: "https://example.invalid/repo",
CloneURL: "https://example.invalid/repo",
}
for _, kind := range []string{"skill", "agent"} {
got, err := InferTrackedKind(source, kind)
if err != nil {
t.Fatalf("kind=%q: unexpected error %v", kind, err)
}
if got != kind {
t.Errorf("kind=%q: got %q", kind, got)
}
}
}