forked from heliohq/anycli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanycli_test.go
More file actions
168 lines (156 loc) · 4.76 KB
/
Copy pathanycli_test.go
File metadata and controls
168 lines (156 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package anycli
import (
"context"
"slices"
"strings"
"testing"
"time"
"github.com/heliohq/anycli/internal/registry"
)
// staticResolver is a minimal CredentialResolver for wiring tests.
type staticResolver struct {
data map[string]string
}
func (r staticResolver) Resolve(ctx context.Context, tool Tool, account string) (*Credential, error) {
return &Credential{Data: r.data, CacheUntil: time.Now().Add(time.Hour)}, nil
}
func TestExecuteWith_UnknownTool(t *testing.T) {
e, err := New(Config{})
if err != nil {
t.Fatalf("New failed: %v", err)
}
exitCode, err := e.ExecuteWith(context.Background(), Tool("definitely-not-a-shipped-tool"), nil, staticResolver{}, ExecOptions{Account: "a1"})
if err == nil {
t.Fatal("expected an error for an unknown tool")
}
if exitCode != 1 {
t.Errorf("exit code = %d, want 1", exitCode)
}
}
func TestExecute_DelegatesToExecuteWith(t *testing.T) {
e, err := New(Config{})
if err != nil {
t.Fatalf("New failed: %v", err)
}
// Execute is the default-account short form: same behavior as ExecuteWith
// with empty options for an unknown tool.
exitCode, err := e.Execute(context.Background(), Tool("definitely-not-a-shipped-tool"), nil, staticResolver{})
if err == nil {
t.Fatal("expected an error for an unknown tool")
}
if exitCode != 1 {
t.Errorf("exit code = %d, want 1", exitCode)
}
}
func TestExecuteFigmaCapabilitiesThroughEmbeddedService(t *testing.T) {
engine, err := New(Config{})
if err != nil {
t.Fatalf("New failed: %v", err)
}
resolver := staticResolver{data: map[string]string{"access_token": "figd_test_token"}}
exitCode, err := engine.Execute(context.Background(), Tool("figma"), []string{"capabilities"}, resolver)
if err != nil {
t.Fatalf("Execute failed: %v", err)
}
if exitCode != 0 {
t.Errorf("exit code = %d, want 0", exitCode)
}
}
func TestListToolsReturnsValidatedManifests(t *testing.T) {
manifests, err := ListTools()
if err != nil {
t.Fatalf("ListTools failed: %v", err)
}
if len(manifests) == 0 {
t.Fatal("ListTools returned no tools")
}
for i, manifest := range manifests {
if manifest.Name == "" || manifest.Description == "" {
t.Errorf("manifest %d is incomplete: %+v", i, manifest)
}
if manifest.Kind != ToolKindCLI && manifest.Kind != ToolKindService {
t.Errorf("manifest %q has invalid kind %q", manifest.Name, manifest.Kind)
}
if i > 0 && manifests[i-1].Name >= manifest.Name {
t.Errorf("manifests are not strictly name-sorted: %q then %q", manifests[i-1].Name, manifest.Name)
}
}
}
func TestListToolsGitHubManifest(t *testing.T) {
manifests, err := ListTools()
if err != nil {
t.Fatalf("ListTools failed: %v", err)
}
for _, manifest := range manifests {
if manifest.Name != Tool("github") {
continue
}
if manifest.Kind != ToolKindCLI {
t.Errorf("github kind = %q, want %q", manifest.Kind, ToolKindCLI)
}
if !slices.Equal(manifest.CredentialFields, []string{"access_token"}) {
t.Errorf("github credential fields = %v, want [access_token]", manifest.CredentialFields)
}
return
}
t.Fatal("github manifest not found")
}
func TestListToolsFigmaManifest(t *testing.T) {
manifests, err := ListTools()
if err != nil {
t.Fatalf("ListTools failed: %v", err)
}
for _, manifest := range manifests {
if manifest.Name != Tool("figma") {
continue
}
if manifest.Kind != ToolKindService {
t.Errorf("figma kind = %q, want %q", manifest.Kind, ToolKindService)
}
if !slices.Equal(manifest.CredentialFields, []string{"access_token"}) {
t.Errorf("figma credential fields = %v, want [access_token]", manifest.CredentialFields)
}
return
}
t.Fatal("figma manifest not found")
}
func TestManifestForRejectsIncompleteExecutionContracts(t *testing.T) {
cases := []struct {
name string
definition *registry.Definition
wantError string
}{
{
name: "CLI without binary",
definition: ®istry.Definition{Name: "missing-cli"},
wantError: "has no binary",
},
{
name: "service without implementation",
definition: ®istry.Definition{Name: "missing-service", Type: "service"},
wantError: "has no registered implementation",
},
{
name: "unsupported execution kind",
definition: ®istry.Definition{Name: "unknown", Type: "remote"},
wantError: "unsupported type",
},
{
name: "credential binding without source field",
definition: ®istry.Definition{
Name: "figma",
Type: "service",
Auth: ®istry.AuthConfig{Credentials: []registry.CredentialBinding{{}}},
},
wantError: "credential binding 0 has no source field",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := manifestFor(tc.definition)
if err == nil || !strings.Contains(err.Error(), tc.wantError) {
t.Fatalf("manifestFor error = %v, want substring %q", err, tc.wantError)
}
})
}
}