forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_test.go
More file actions
91 lines (79 loc) · 2.75 KB
/
types_test.go
File metadata and controls
91 lines (79 loc) · 2.75 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
package copilot
import (
"encoding/json"
"testing"
)
func TestPermissionRequestResultKind_Constants(t *testing.T) {
tests := []struct {
name string
kind PermissionRequestResultKind
expected string
}{
{"Approved", PermissionRequestResultKindApproved, "approved"},
{"DeniedByRules", PermissionRequestResultKindDeniedByRules, "denied-by-rules"},
{"DeniedCouldNotRequestFromUser", PermissionRequestResultKindDeniedCouldNotRequestFromUser, "denied-no-approval-rule-and-could-not-request-from-user"},
{"DeniedInteractivelyByUser", PermissionRequestResultKindDeniedInteractivelyByUser, "denied-interactively-by-user"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if string(tt.kind) != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, string(tt.kind))
}
})
}
}
func TestPermissionRequestResultKind_CustomValue(t *testing.T) {
custom := PermissionRequestResultKind("custom-kind")
if string(custom) != "custom-kind" {
t.Errorf("expected %q, got %q", "custom-kind", string(custom))
}
}
func TestPermissionRequestResult_JSONRoundTrip(t *testing.T) {
tests := []struct {
name string
kind PermissionRequestResultKind
}{
{"Approved", PermissionRequestResultKindApproved},
{"DeniedByRules", PermissionRequestResultKindDeniedByRules},
{"DeniedCouldNotRequestFromUser", PermissionRequestResultKindDeniedCouldNotRequestFromUser},
{"DeniedInteractivelyByUser", PermissionRequestResultKindDeniedInteractivelyByUser},
{"Custom", PermissionRequestResultKind("custom")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
original := PermissionRequestResult{Kind: tt.kind}
data, err := json.Marshal(original)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
var decoded PermissionRequestResult
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
if decoded.Kind != tt.kind {
t.Errorf("expected kind %q, got %q", tt.kind, decoded.Kind)
}
})
}
}
func TestPermissionRequestResult_JSONDeserialize(t *testing.T) {
jsonStr := `{"kind":"denied-by-rules"}`
var result PermissionRequestResult
if err := json.Unmarshal([]byte(jsonStr), &result); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
if result.Kind != PermissionRequestResultKindDeniedByRules {
t.Errorf("expected %q, got %q", PermissionRequestResultKindDeniedByRules, result.Kind)
}
}
func TestPermissionRequestResult_JSONSerialize(t *testing.T) {
result := PermissionRequestResult{Kind: PermissionRequestResultKindApproved}
data, err := json.Marshal(result)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
expected := `{"kind":"approved"}`
if string(data) != expected {
t.Errorf("expected %s, got %s", expected, string(data))
}
}