forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker-namespace.test.ts
340 lines (298 loc) · 10.8 KB
/
worker-namespace.test.ts
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { http, HttpResponse } from "msw";
import { printWranglerBanner } from "../update-check";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { mockConsoleMethods } from "./helpers/mock-console";
import {
createFetchResult,
msw,
mswSuccessNamespacesHandlers,
} from "./helpers/msw";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import type { Mock } from "vitest";
describe("dispatch-namespace", () => {
const std = mockConsoleMethods();
beforeEach(() => msw.use(...mswSuccessNamespacesHandlers));
runInTempDir();
mockAccountId();
mockApiToken();
it("should display a list of available subcommands, for dispatch-namespace with no subcommand", async () => {
await runWrangler("dispatch-namespace");
// wait a tick for the help menu to be printed
await new Promise((resolve) => setImmediate(resolve));
expect(std).toMatchInlineSnapshot(`
Object {
"debug": "",
"err": "",
"info": "",
"out": "wrangler dispatch-namespace
🏗️ Manage dispatch namespaces
COMMANDS
wrangler dispatch-namespace list List all dispatch namespaces
wrangler dispatch-namespace get <name> Get information about a dispatch namespace
wrangler dispatch-namespace create <name> Create a dispatch namespace
wrangler dispatch-namespace delete <name> Delete a dispatch namespace
wrangler dispatch-namespace rename <old-name> <new-name> Rename a dispatch namespace
GLOBAL FLAGS
-j, --experimental-json-config Experimental: support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]",
"warn": "",
}
`);
});
describe("create namespace", () => {
const namespaceName = "my-namespace";
let counter = 0;
msw.use(
http.post(
"*/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
({ params }) => {
counter++;
const { namespaceNameParam } = params;
expect(counter).toBe(1);
expect(namespaceNameParam).toBe(namespaceName);
return HttpResponse.json(
createFetchResult({
namespace_id: "some-namespace-id",
namespace_name: "namespace-name",
created_on: "2022-06-29T14:30:08.16152Z",
created_by: "1fc1df98cc4420fe00367c3ab68c1639",
modified_on: "2022-06-29T14:30:08.16152Z",
modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
})
);
},
{ once: true }
)
);
it("should display help for create", async () => {
await expect(
runWrangler("dispatch-namespace create")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: Not enough non-option arguments: got 0, need at least 1]`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler dispatch-namespace create <name>
Create a dispatch namespace
POSITIONALS
name Name of the dispatch namespace [string] [required]
GLOBAL FLAGS
-j, --experimental-json-config Experimental: support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
it("should attempt to create the given namespace", async () => {
await runWrangler(`dispatch-namespace create ${namespaceName}`);
expect(std.out).toMatchInlineSnapshot(
`"Created dispatch namespace \\"my-namespace\\" with ID \\"some-namespace-id\\""`
);
});
});
describe("delete namespace", () => {
const namespaceName = "my-namespace";
let counter = 0;
msw.use(
http.delete(
"*/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
({ params }) => {
counter++;
const { namespaceNameParam } = params;
expect(counter).toBe(1);
expect(namespaceNameParam).toBe(namespaceName);
return HttpResponse.json(null);
},
{ once: true }
)
);
it("should display help for delete", async () => {
await expect(
runWrangler("dispatch-namespace create")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: Not enough non-option arguments: got 0, need at least 1]`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler dispatch-namespace create <name>
Create a dispatch namespace
POSITIONALS
name Name of the dispatch namespace [string] [required]
GLOBAL FLAGS
-j, --experimental-json-config Experimental: support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
it("should try to delete the given namespace", async () => {
await runWrangler(`dispatch-namespace delete ${namespaceName}`);
expect(std.out).toMatchInlineSnapshot(
`"Deleted dispatch namespace \\"my-namespace\\""`
);
});
});
describe("get namespace", () => {
const namespaceName = "my-namespace";
let counter = 0;
msw.use(
http.get(
"*/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
({ params }) => {
counter++;
const { namespaceNameParam } = params;
expect(counter).toBe(1);
expect(namespaceNameParam).toBe(namespaceName);
return HttpResponse.json(
createFetchResult({
namespace_id: "some-namespace-id",
namespace_name: "namespace-name",
created_on: "2022-06-29T14:30:08.16152Z",
created_by: "1fc1df98cc4420fe00367c3ab68c1639",
modified_on: "2022-06-29T14:30:08.16152Z",
modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
})
);
},
{ once: true }
)
);
it("should display help for get", async () => {
await expect(
runWrangler("dispatch-namespace get")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: Not enough non-option arguments: got 0, need at least 1]`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler dispatch-namespace get <name>
Get information about a dispatch namespace
POSITIONALS
name Name of the dispatch namespace [string] [required]
GLOBAL FLAGS
-j, --experimental-json-config Experimental: support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
it("should attempt to get info for the given namespace", async () => {
await runWrangler(`dispatch-namespace get ${namespaceName}`);
expect(std.out).toMatchInlineSnapshot(`
"{
namespace_id: 'some-namespace-id',
namespace_name: 'namespace-name',
created_on: '2022-06-29T14:30:08.16152Z',
created_by: '1fc1df98cc4420fe00367c3ab68c1639',
modified_on: '2022-06-29T14:30:08.16152Z',
modified_by: '1fc1df98cc4420fe00367c3ab68c1639'
}"
`);
});
});
describe("list namespaces", () => {
const namespaceName = "my-namespace";
let counter = 0;
msw.use(
http.get(
"*/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
({ params }) => {
counter++;
const { namespaceNameParam } = params;
expect(counter).toBe(1);
expect(namespaceNameParam).toBe(namespaceName);
return HttpResponse.json(
createFetchResult({
namespace_id: "some-namespace-id",
namespace_name: "namespace-name",
created_on: "2022-06-29T14:30:08.16152Z",
created_by: "1fc1df98cc4420fe00367c3ab68c1639",
modified_on: "2022-06-29T14:30:08.16152Z",
modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
})
);
},
{ once: true }
)
);
it("should list all namespaces", async () => {
await runWrangler("dispatch-namespace list");
expect(std.out).toMatchInlineSnapshot(`
"[
{
namespace_id: 'some-namespace-id',
namespace_name: 'namespace-name',
created_on: '2022-06-29T14:30:08.16152Z',
created_by: '1fc1df98cc4420fe00367c3ab68c1639',
modified_on: '2022-06-29T14:30:08.16152Z',
modified_by: '1fc1df98cc4420fe00367c3ab68c1639'
}
]"
`);
});
});
describe("rename namespace", () => {
const namespaceName = "my-namespace";
let counter = 0;
msw.use(
http.put(
"*/accounts/:accountId/workers/dispatch/namespaces/:namespaceNameParam",
({ params }) => {
counter++;
const { namespaceNameParam } = params;
expect(counter).toBe(1);
expect(namespaceNameParam).toBe(namespaceName);
return HttpResponse.json(
createFetchResult({
namespace_id: "some-namespace-id",
namespace_name: "namespace-name",
created_on: "2022-06-29T14:30:08.16152Z",
created_by: "1fc1df98cc4420fe00367c3ab68c1639",
modified_on: "2022-06-29T14:30:08.16152Z",
modified_by: "1fc1df98cc4420fe00367c3ab68c1639",
})
);
},
{ once: true }
)
);
it("should display help for rename", async () => {
await expect(
runWrangler("dispatch-namespace rename")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: Not enough non-option arguments: got 0, need at least 2]`
);
expect(std.out).toMatchInlineSnapshot(`
"
wrangler dispatch-namespace rename <old-name> <new-name>
Rename a dispatch namespace
POSITIONALS
old-name Name of the dispatch namespace [string] [required]
new-name New name of the dispatch namespace [string] [required]
GLOBAL FLAGS
-j, --experimental-json-config Experimental: support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
it("should attempt to rename the given namespace", async () => {
const newName = "new-namespace";
await runWrangler(
`dispatch-namespace rename ${namespaceName} ${newName}`
);
expect(std.out).toMatchInlineSnapshot(
`"Renamed dispatch namespace \\"my-namespace\\" to \\"new-namespace\\""`
);
expect((printWranglerBanner as Mock).mock.calls.length).toEqual(1);
});
});
});