@@ -11,6 +11,7 @@ import (
11
11
"github.com/stretchr/testify/assert"
12
12
"github.com/stretchr/testify/require"
13
13
"github.com/supabase/cli/pkg/api"
14
+ "github.com/supabase/cli/pkg/cast"
14
15
"github.com/supabase/cli/pkg/config"
15
16
)
16
17
@@ -29,19 +30,24 @@ func (b *MockBundler) Bundle(ctx context.Context, slug, entrypoint, importMap st
29
30
}, nil
30
31
}
31
32
33
+ func mockClient (t * testing.T ) EdgeRuntimeAPI {
34
+ apiClient , err := api .NewClientWithResponses (mockApiHost )
35
+ require .NoError (t , err )
36
+ return NewEdgeRuntimeAPI (mockProject , * apiClient , func (era * EdgeRuntimeAPI ) {
37
+ era .eszip = & MockBundler {}
38
+ })
39
+ }
40
+
32
41
const (
33
42
mockApiHost = "https://api.supabase.com"
34
43
mockProject = "test-project"
35
44
)
36
45
37
46
func TestUpsertFunctions (t * testing.T ) {
38
- apiClient , err := api .NewClientWithResponses (mockApiHost )
39
- require .NoError (t , err )
40
- client := NewEdgeRuntimeAPI (mockProject , * apiClient , func (era * EdgeRuntimeAPI ) {
41
- era .eszip = & MockBundler {}
42
- })
47
+ client := mockClient (t )
43
48
44
49
t .Run ("deploys with bulk update" , func (t * testing.T ) {
50
+ // t.Cleanup(mockClock(100 * time.Millisecond))
45
51
// Setup mock api
46
52
defer gock .OffAll ()
47
53
gock .New (mockApiHost ).
@@ -53,8 +59,8 @@ func TestUpsertFunctions(t *testing.T) {
53
59
Reply (http .StatusOK ).
54
60
JSON (api.FunctionResponse {Slug : "test-a" })
55
61
gock .New (mockApiHost ).
56
- Post ("/v1/projects/" + mockProject + "/functions/test-b " ).
57
- Reply (http .StatusOK ).
62
+ Post ("/v1/projects/" + mockProject + "/functions" ).
63
+ Reply (http .StatusCreated ).
58
64
JSON (api.FunctionResponse {Slug : "test-b" })
59
65
gock .New (mockApiHost ).
60
66
Put ("/v1/projects/" + mockProject + "/functions" ).
@@ -68,8 +74,30 @@ func TestUpsertFunctions(t *testing.T) {
68
74
JSON (api.V1BulkUpdateFunctionsResponse {})
69
75
// Run test
70
76
err := client .UpsertFunctions (context .Background (), config.FunctionConfig {
71
- "test-a" : {},
72
- "test-b" : {},
77
+ "test-a" : {Enabled : true },
78
+ "test-b" : {Enabled : true },
79
+ })
80
+ // Check error
81
+ assert .NoError (t , err )
82
+ assert .Empty (t , gock .Pending ())
83
+ assert .Empty (t , gock .GetUnmatchedRequests ())
84
+ })
85
+
86
+ t .Run ("skips disabled and unchanged" , func (t * testing.T ) {
87
+ // Setup mock api
88
+ defer gock .OffAll ()
89
+ gock .New (mockApiHost ).
90
+ Get ("/v1/projects/" + mockProject + "/functions" ).
91
+ Reply (http .StatusOK ).
92
+ JSON ([]api.FunctionResponse {{
93
+ Slug : "test-a" ,
94
+ VerifyJwt : cast .Ptr (true ),
95
+ EzbrSha256 : cast .Ptr ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ),
96
+ }})
97
+ // Run test
98
+ err := client .UpsertFunctions (context .Background (), config.FunctionConfig {
99
+ "test-a" : {Enabled : true , VerifyJWT : true },
100
+ "test-b" : {Enabled : false },
73
101
})
74
102
// Check error
75
103
assert .NoError (t , err )
@@ -94,7 +122,7 @@ func TestUpsertFunctions(t *testing.T) {
94
122
JSON (api.FunctionResponse {Slug : "test" })
95
123
// Run test
96
124
err := client .UpsertFunctions (context .Background (), config.FunctionConfig {
97
- "test" : {},
125
+ "test" : {Enabled : true },
98
126
})
99
127
// Check error
100
128
assert .NoError (t , err )
@@ -121,58 +149,60 @@ func TestUpsertFunctions(t *testing.T) {
121
149
assert .Empty (t , gock .Pending ())
122
150
assert .Empty (t , gock .GetUnmatchedRequests ())
123
151
})
152
+ }
124
153
125
- t .Run ("retries on create failure" , func (t * testing.T ) {
126
- // Setup mock api
127
- defer gock .OffAll ()
128
- gock .New (mockApiHost ).
129
- Get ("/v1/projects/" + mockProject + "/functions" ).
130
- Reply (http .StatusOK ).
131
- JSON ([]api.FunctionResponse {})
132
- gock .New (mockApiHost ).
133
- Post ("/v1/projects/" + mockProject + "/functions" ).
134
- ReplyError (errors .New ("network error" ))
135
- gock .New (mockApiHost ).
136
- Post ("/v1/projects/" + mockProject + "/functions" ).
137
- Reply (http .StatusServiceUnavailable )
138
- gock .New (mockApiHost ).
139
- Post ("/v1/projects/" + mockProject + "/functions" ).
140
- Reply (http .StatusCreated ).
141
- JSON (api.FunctionResponse {Slug : "test" })
142
- // Run test
143
- err := client .UpsertFunctions (context .Background (), config.FunctionConfig {
144
- "test" : {},
145
- })
146
- // Check error
147
- assert .NoError (t , err )
148
- assert .Empty (t , gock .Pending ())
149
- assert .Empty (t , gock .GetUnmatchedRequests ())
154
+ func TestCreateFunction (t * testing.T ) {
155
+ client := mockClient (t )
156
+ // Setup mock api
157
+ defer gock .OffAll ()
158
+ gock .New (mockApiHost ).
159
+ Get ("/v1/projects/" + mockProject + "/functions" ).
160
+ Reply (http .StatusOK ).
161
+ JSON ([]api.FunctionResponse {})
162
+ gock .New (mockApiHost ).
163
+ Post ("/v1/projects/" + mockProject + "/functions" ).
164
+ ReplyError (errors .New ("network error" ))
165
+ gock .New (mockApiHost ).
166
+ Post ("/v1/projects/" + mockProject + "/functions" ).
167
+ Reply (http .StatusServiceUnavailable )
168
+ gock .New (mockApiHost ).
169
+ Post ("/v1/projects/" + mockProject + "/functions" ).
170
+ Reply (http .StatusCreated ).
171
+ JSON (api.FunctionResponse {Slug : "test" })
172
+ // Run test
173
+ err := client .UpsertFunctions (context .Background (), config.FunctionConfig {
174
+ "test" : {Enabled : true },
150
175
})
176
+ // Check error
177
+ assert .NoError (t , err )
178
+ assert .Empty (t , gock .Pending ())
179
+ assert .Empty (t , gock .GetUnmatchedRequests ())
180
+ }
151
181
152
- t .Run ("retries on update failure" , func (t * testing.T ) {
153
- // Setup mock api
154
- defer gock .OffAll ()
155
- gock .New (mockApiHost ).
156
- Get ("/v1/projects/" + mockProject + "/functions" ).
157
- Reply (http .StatusOK ).
158
- JSON ([]api.FunctionResponse {{Slug : "test" }})
159
- gock .New (mockApiHost ).
160
- Patch ("/v1/projects/" + mockProject + "/functions/test" ).
161
- ReplyError (errors .New ("network error" ))
162
- gock .New (mockApiHost ).
163
- Patch ("/v1/projects/" + mockProject + "/functions/test" ).
164
- Reply (http .StatusServiceUnavailable )
165
- gock .New (mockApiHost ).
166
- Patch ("/v1/projects/" + mockProject + "/functions/test" ).
167
- Reply (http .StatusOK ).
168
- JSON (api.FunctionResponse {Slug : "test" })
169
- // Run test
170
- err := client .UpsertFunctions (context .Background (), config.FunctionConfig {
171
- "test" : {},
172
- })
173
- // Check error
174
- assert .NoError (t , err )
175
- assert .Empty (t , gock .Pending ())
176
- assert .Empty (t , gock .GetUnmatchedRequests ())
182
+ func TestUpdateFunction (t * testing.T ) {
183
+ client := mockClient (t )
184
+ // Setup mock api
185
+ defer gock .OffAll ()
186
+ gock .New (mockApiHost ).
187
+ Get ("/v1/projects/" + mockProject + "/functions" ).
188
+ Reply (http .StatusOK ).
189
+ JSON ([]api.FunctionResponse {{Slug : "test" }})
190
+ gock .New (mockApiHost ).
191
+ Patch ("/v1/projects/" + mockProject + "/functions/test" ).
192
+ ReplyError (errors .New ("network error" ))
193
+ gock .New (mockApiHost ).
194
+ Patch ("/v1/projects/" + mockProject + "/functions/test" ).
195
+ Reply (http .StatusServiceUnavailable )
196
+ gock .New (mockApiHost ).
197
+ Patch ("/v1/projects/" + mockProject + "/functions/test" ).
198
+ Reply (http .StatusOK ).
199
+ JSON (api.FunctionResponse {Slug : "test" })
200
+ // Run test
201
+ err := client .UpsertFunctions (context .Background (), config.FunctionConfig {
202
+ "test" : {Enabled : true },
177
203
})
204
+ // Check error
205
+ assert .NoError (t , err )
206
+ assert .Empty (t , gock .Pending ())
207
+ assert .Empty (t , gock .GetUnmatchedRequests ())
178
208
}
0 commit comments