forked from guigui42/mcp-vosdroits
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
155 lines (134 loc) · 3.35 KB
/
client_test.go
File metadata and controls
155 lines (134 loc) · 3.35 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
package client
import (
"context"
"testing"
"time"
)
func TestNew(t *testing.T) {
timeout := 30 * time.Second
client := New(timeout)
if client == nil {
t.Fatal("New() returned nil")
}
if client.collector == nil {
t.Error("collector should not be nil")
}
if client.timeout != timeout {
t.Errorf("timeout = %v, want %v", client.timeout, timeout)
}
if client.baseURL == "" {
t.Error("baseURL should not be empty")
}
}
func TestSearchProcedures(t *testing.T) {
tests := []struct {
name string
query string
limit int
wantErr bool
}{
{
name: "valid search",
query: "carte d'identité",
limit: 10,
wantErr: false, // Should return fallback results
},
{
name: "empty query",
query: "",
limit: 10,
wantErr: false, // Will use fallback search
},
{
name: "limit too high",
query: "passeport",
limit: 200,
wantErr: false, // Will be clamped to 10
},
}
client := New(30 * time.Second)
ctx := context.Background()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
results, err := client.SearchProcedures(ctx, tt.query, tt.limit)
if (err != nil) != tt.wantErr {
t.Errorf("SearchProcedures() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && results == nil {
t.Error("SearchProcedures() returned nil results")
}
// Should always return at least fallback results
if len(results) == 0 {
t.Error("SearchProcedures() returned empty results, expected at least fallback")
}
})
}
}
func TestSearchProceduresContextCancellation(t *testing.T) {
client := New(30 * time.Second)
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately
_, err := client.SearchProcedures(ctx, "test", 10)
if err == nil {
t.Error("SearchProcedures() should return error when context is cancelled")
}
}
func TestGetArticle(t *testing.T) {
tests := []struct {
name string
url string
wantErr bool
}{
{
name: "valid service-public.gouv.fr url",
url: "https://www.service-public.gouv.fr/particuliers/vosdroits/F1234",
// May or may not error depending on whether the page exists and has content
wantErr: false,
},
{
name: "invalid url",
url: "not-a-url",
wantErr: true,
},
{
name: "wrong domain",
url: "https://example.com/page",
wantErr: true,
},
}
client := New(5 * time.Second) // Shorter timeout for tests
ctx := context.Background()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
article, err := client.GetArticle(ctx, tt.url)
if (err != nil) != tt.wantErr {
// For the service-public test, we just log the result
if tt.name == "valid service-public.gouv.fr url" {
t.Logf("GetArticle() returned article=%v, err=%v", article != nil, err)
return
}
t.Errorf("GetArticle() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && article == nil {
t.Error("GetArticle() returned nil article")
}
})
}
}
func TestListCategories(t *testing.T) {
client := New(30 * time.Second)
ctx := context.Background()
categories, err := client.ListCategories(ctx)
if err != nil {
t.Errorf("ListCategories() error = %v", err)
return
}
if categories == nil {
t.Error("ListCategories() returned nil")
}
if len(categories) == 0 {
t.Error("ListCategories() returned empty list")
}
}