wrangler 0.0.0-e9a1820 → 0.0.0-e9ab55a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,17 @@
1
- import { setMock, unsetAllMocks } from "./mock-cfetch";
1
+ import { writeFileSync } from "fs";
2
+ import {
3
+ setMockResponse,
4
+ setMockRawResponse,
5
+ unsetAllMocks,
6
+ createFetchResult,
7
+ } from "./mock-cfetch";
2
8
  import { runWrangler } from "./run-wrangler";
3
9
  import { runInTempDir } from "./run-in-tmp";
4
- import { writeFileSync } from "fs";
10
+
11
+ interface KVNamespaceInfo {
12
+ title: string;
13
+ id: string;
14
+ }
5
15
 
6
16
  describe("wrangler", () => {
7
17
  runInTempDir();
@@ -13,12 +23,12 @@ describe("wrangler", () => {
13
23
  describe("kv:namespace", () => {
14
24
  describe("create", () => {
15
25
  function mockCreateRequest(expectedTitle: string) {
16
- setMock(
26
+ setMockResponse(
17
27
  "/accounts/:accountId/storage/kv/namespaces",
18
28
  "POST",
19
29
  ([_url, accountId], { body }) => {
20
30
  expect(accountId).toEqual("some-account-id");
21
- const title = JSON.parse(body).title;
31
+ const title = JSON.parse(body as string).title;
22
32
  expect(title).toEqual(expectedTitle);
23
33
  return { id: "some-namespace-id" };
24
34
  }
@@ -172,18 +182,18 @@ describe("wrangler", () => {
172
182
  describe("list", () => {
173
183
  function mockListRequest(namespaces: unknown[]) {
174
184
  const requests = { count: 0 };
175
- setMock(
176
- "/accounts/:accountId/storage/kv/namespaces\\?:qs",
177
- ([_url, accountId, query], init) => {
185
+ setMockResponse(
186
+ "/accounts/:accountId/storage/kv/namespaces",
187
+ ([_url, accountId], init, query) => {
178
188
  requests.count++;
179
189
  expect(accountId).toEqual("some-account-id");
180
- expect(query).toContain("per_page=100");
181
- expect(query).toContain("order=title");
182
- expect(query).toContain("direction=asc");
183
- expect(query).toContain("page=");
184
- expect(init).toBe(undefined);
185
- const pageSize = Number(/\bper_page=(\d+)\b/.exec(query)[1]);
186
- const page = Number(/\bpage=(\d+)/.exec(query)[1]);
190
+ expect(query.get("per_page")).toEqual("100");
191
+ expect(query.get("order")).toEqual("title");
192
+ expect(query.get("direction")).toEqual("asc");
193
+ expect(query.get("page")).toEqual(`${requests.count}`);
194
+ expect(init).toEqual({});
195
+ const pageSize = Number(query.get("per_page"));
196
+ const page = Number(query.get("page"));
187
197
  return namespaces.slice((page - 1) * pageSize, page * pageSize);
188
198
  }
189
199
  );
@@ -191,26 +201,30 @@ describe("wrangler", () => {
191
201
  }
192
202
 
193
203
  it("should list namespaces", async () => {
194
- const KVNamespaces = [
204
+ const kvNamespaces: KVNamespaceInfo[] = [
195
205
  { title: "title-1", id: "id-1" },
196
206
  { title: "title-2", id: "id-2" },
197
207
  ];
198
- mockListRequest(KVNamespaces);
199
- const { stdout } = await runWrangler("kv:namespace list");
208
+ mockListRequest(kvNamespaces);
209
+ const { error, stdout, stderr } = await runWrangler(
210
+ "kv:namespace list"
211
+ );
212
+ expect(error).toMatchInlineSnapshot(`undefined`);
213
+ expect(stderr).toMatchInlineSnapshot(`""`);
200
214
  const namespaces = JSON.parse(stdout);
201
- expect(namespaces).toEqual(KVNamespaces);
215
+ expect(namespaces).toEqual(kvNamespaces);
202
216
  });
203
217
 
204
218
  it("should make multiple requests for paginated results", async () => {
205
- // Create a lot of mock namespaces, so that the cfetch requests will be paginated
206
- const KVNamespaces = [];
219
+ // Create a lot of mock namespaces, so that the fetch requests will be paginated
220
+ const kvNamespaces: KVNamespaceInfo[] = [];
207
221
  for (let i = 0; i < 550; i++) {
208
- KVNamespaces.push({ title: "title-" + i, id: "id-" + i });
222
+ kvNamespaces.push({ title: "title-" + i, id: "id-" + i });
209
223
  }
210
- const requests = mockListRequest(KVNamespaces);
224
+ const requests = mockListRequest(kvNamespaces);
211
225
  const { stdout } = await runWrangler("kv:namespace list");
212
226
  const namespaces = JSON.parse(stdout);
213
- expect(namespaces).toEqual(KVNamespaces);
227
+ expect(namespaces).toEqual(kvNamespaces);
214
228
  expect(requests.count).toEqual(6);
215
229
  });
216
230
  });
@@ -218,7 +232,7 @@ describe("wrangler", () => {
218
232
  describe("delete", () => {
219
233
  function mockDeleteRequest(expectedNamespaceId: string) {
220
234
  const requests = { count: 0 };
221
- setMock(
235
+ setMockResponse(
222
236
  "/accounts/:accountId/storage/kv/namespaces/:namespaceId",
223
237
  "DELETE",
224
238
  ([_url, accountId, namespaceId]) => {
@@ -231,24 +245,6 @@ describe("wrangler", () => {
231
245
  return requests;
232
246
  }
233
247
 
234
- function writeWranglerConfig() {
235
- writeFileSync(
236
- "./wrangler.toml",
237
- [
238
- 'name = "otherWorker"',
239
- "kv_namespaces = [",
240
- ' { binding = "someBinding", id = "bound-id", preview_id = "preview-bound-id" }',
241
- "]",
242
- "",
243
- "[env.some-environment]",
244
- "kv_namespaces = [",
245
- ' { binding = "someBinding", id = "env-bound-id", preview_id = "preview-env-bound-id" }',
246
- "]",
247
- ].join("\n"),
248
- "utf-8"
249
- );
250
- }
251
-
252
248
  it("should delete a namespace specified by id", async () => {
253
249
  const requests = mockDeleteRequest("some-namespace-id");
254
250
  await runWrangler(
@@ -260,7 +256,9 @@ describe("wrangler", () => {
260
256
  it("should delete a namespace specified by binding name", async () => {
261
257
  writeWranglerConfig();
262
258
  const requests = mockDeleteRequest("bound-id");
263
- await runWrangler(`kv:namespace delete --binding someBinding`);
259
+ await runWrangler(
260
+ `kv:namespace delete --binding someBinding --preview false`
261
+ );
264
262
  expect(requests.count).toEqual(1);
265
263
  });
266
264
 
@@ -303,9 +301,12 @@ describe("wrangler", () => {
303
301
  it("should delete a namespace specified by binding name in a given environment", async () => {
304
302
  writeWranglerConfig();
305
303
  const requests = mockDeleteRequest("env-bound-id");
306
- await runWrangler(
307
- `kv:namespace delete --binding someBinding --env some-environment`
304
+ const { stdout, stderr, error } = await runWrangler(
305
+ `kv:namespace delete --binding someBinding --env some-environment --preview false`
308
306
  );
307
+ expect(stdout).toMatchInlineSnapshot(`""`);
308
+ expect(stderr).toMatchInlineSnapshot(`""`);
309
+ expect(error).toMatchInlineSnapshot(`undefined`);
309
310
  expect(requests.count).toEqual(1);
310
311
  });
311
312
 
@@ -319,4 +320,807 @@ describe("wrangler", () => {
319
320
  });
320
321
  });
321
322
  });
323
+
324
+ describe("kv:key", () => {
325
+ describe("put", () => {
326
+ function mockKeyPutRequest(
327
+ expectedNamespaceId: string,
328
+ expectedKey: string,
329
+ expectedValue: string,
330
+ expiration?: number,
331
+ expirationTtl?: number
332
+ ) {
333
+ const requests = { count: 0 };
334
+ setMockResponse(
335
+ "/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
336
+ "PUT",
337
+ ([_url, accountId, namespaceId, key], { body }, query) => {
338
+ requests.count++;
339
+ expect(accountId).toEqual("some-account-id");
340
+ expect(namespaceId).toEqual(expectedNamespaceId);
341
+ expect(key).toEqual(expectedKey);
342
+ expect(body).toEqual(expectedValue);
343
+ if (expiration !== undefined) {
344
+ expect(query.get("expiration")).toEqual(`${expiration}`);
345
+ } else {
346
+ expect(query.has("expiration")).toBe(false);
347
+ }
348
+ if (expirationTtl) {
349
+ expect(query.get("expiration_ttl")).toEqual(`${expirationTtl}`);
350
+ } else {
351
+ expect(query.has("expiration_ttl")).toBe(false);
352
+ }
353
+ return null;
354
+ }
355
+ );
356
+ return requests;
357
+ }
358
+
359
+ it("should put a key in a given namespace specified by namespace-id", async () => {
360
+ const requests = mockKeyPutRequest(
361
+ "some-namespace-id",
362
+ "my-key",
363
+ "my-value"
364
+ );
365
+ const { error, stderr, stdout } = await runWrangler(
366
+ "kv:key put my-key my-value --namespace-id some-namespace-id"
367
+ );
368
+ expect(requests.count).toEqual(1);
369
+ expect(stdout).toMatchInlineSnapshot(
370
+ `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id"`
371
+ );
372
+ expect(stderr).toMatchInlineSnapshot(`""`);
373
+ expect(error).toMatchInlineSnapshot(`undefined`);
374
+ });
375
+
376
+ it("should put a key in a given namespace specified by binding", async () => {
377
+ writeWranglerConfig();
378
+ const requests = mockKeyPutRequest("bound-id", "my-key", "my-value");
379
+ const { error, stderr, stdout } = await runWrangler(
380
+ "kv:key put my-key my-value --binding someBinding --preview false"
381
+ );
382
+ expect(stdout).toMatchInlineSnapshot(
383
+ `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace bound-id"`
384
+ );
385
+ expect(stderr).toMatchInlineSnapshot(`""`);
386
+ expect(error).toMatchInlineSnapshot(`undefined`);
387
+ expect(requests.count).toEqual(1);
388
+ });
389
+
390
+ it("should put a key in a given preview namespace specified by binding", async () => {
391
+ writeWranglerConfig();
392
+ const requests = mockKeyPutRequest(
393
+ "preview-bound-id",
394
+ "my-key",
395
+ "my-value"
396
+ );
397
+ const { error, stderr, stdout } = await runWrangler(
398
+ "kv:key put my-key my-value --binding someBinding --preview"
399
+ );
400
+ expect(stdout).toMatchInlineSnapshot(
401
+ `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace preview-bound-id"`
402
+ );
403
+ expect(stderr).toMatchInlineSnapshot(`""`);
404
+ expect(error).toMatchInlineSnapshot(`undefined`);
405
+ expect(requests.count).toEqual(1);
406
+ });
407
+
408
+ it("should add expiration and ttl properties when putting a key", async () => {
409
+ const requests = mockKeyPutRequest(
410
+ "some-namespace-id",
411
+ "my-key",
412
+ "my-value",
413
+ 10,
414
+ 20
415
+ );
416
+ const { error, stderr, stdout } = await runWrangler(
417
+ "kv:key put my-key my-value --namespace-id some-namespace-id --expiration 10 --ttl 20"
418
+ );
419
+ expect(requests.count).toEqual(1);
420
+ expect(stdout).toMatchInlineSnapshot(
421
+ `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace some-namespace-id"`
422
+ );
423
+ expect(stderr).toMatchInlineSnapshot(`""`);
424
+ expect(error).toMatchInlineSnapshot(`undefined`);
425
+ });
426
+
427
+ it("should put a key to the specified environment in a given namespace", async () => {
428
+ writeWranglerConfig();
429
+ const requests = mockKeyPutRequest(
430
+ "env-bound-id",
431
+ "my-key",
432
+ "my-value"
433
+ );
434
+ const { error, stderr, stdout } = await runWrangler(
435
+ "kv:key put my-key my-value --binding someBinding --env some-environment --preview false"
436
+ );
437
+ expect(stdout).toMatchInlineSnapshot(
438
+ `"writing the value \\"my-value\\" to key \\"my-key\\" on namespace env-bound-id"`
439
+ );
440
+ expect(stderr).toMatchInlineSnapshot(`""`);
441
+ expect(error).toMatchInlineSnapshot(`undefined`);
442
+ expect(requests.count).toEqual(1);
443
+ });
444
+
445
+ it("should put a key with a value loaded from a given path", async () => {
446
+ writeFileSync("foo.txt", "file-contents", "utf-8");
447
+ const requests = mockKeyPutRequest(
448
+ "some-namespace-id",
449
+ "my-key",
450
+ "file-contents"
451
+ );
452
+ const { error, stderr, stdout } = await runWrangler(
453
+ "kv:key put my-key --namespace-id some-namespace-id --path foo.txt"
454
+ );
455
+ expect(stdout).toMatchInlineSnapshot(
456
+ `"writing the contents of foo.txt to the key \\"my-key\\" on namespace some-namespace-id"`
457
+ );
458
+ expect(stderr).toMatchInlineSnapshot(`""`);
459
+ expect(error).toMatchInlineSnapshot(`undefined`);
460
+ expect(requests.count).toEqual(1);
461
+ });
462
+
463
+ it("should error if no key is provided", async () => {
464
+ const { error, stdout, stderr } = await runWrangler("kv:key put");
465
+
466
+ expect(stdout).toMatchInlineSnapshot(`""`);
467
+ expect(stderr).toMatchInlineSnapshot(`
468
+ "wrangler kv:key put <key> [value]
469
+
470
+ Writes a single key/value pair to the given namespace.
471
+
472
+ Positionals:
473
+ key The key to write to. [string] [required]
474
+ value The value to write. [string]
475
+
476
+ Flags:
477
+ -c, --config Path to .toml configuration file [string]
478
+ -h, --help Show help [boolean]
479
+ -v, --version Show version number [boolean]
480
+
481
+ Options:
482
+ -l, --local Run on my machine [boolean] [default: false]
483
+ --binding The binding of the namespace to write to. [string]
484
+ --namespace-id The id of the namespace to write to. [string]
485
+ --env Perform on a specific environment [string]
486
+ --preview Interact with a preview namespace [boolean]
487
+ --ttl Time for which the entries should be visible. [number]
488
+ --expiration Time since the UNIX epoch after which the entry expires [number]
489
+ --path Read value from the file at a given path. [string]
490
+
491
+ Not enough non-option arguments: got 0, need at least 1"
492
+ `);
493
+ expect(error).toMatchInlineSnapshot(
494
+ `[Error: Not enough non-option arguments: got 0, need at least 1]`
495
+ );
496
+ });
497
+
498
+ it("should error if no binding nor namespace is provided", async () => {
499
+ const { error, stdout, stderr } = await runWrangler(
500
+ "kv:key put foo bar"
501
+ );
502
+
503
+ expect(stdout).toMatchInlineSnapshot(`""`);
504
+ expect(stderr).toMatchInlineSnapshot(`
505
+ "wrangler kv:key put <key> [value]
506
+
507
+ Writes a single key/value pair to the given namespace.
508
+
509
+ Positionals:
510
+ key The key to write to. [string] [required]
511
+ value The value to write. [string]
512
+
513
+ Flags:
514
+ -c, --config Path to .toml configuration file [string]
515
+ -h, --help Show help [boolean]
516
+ -v, --version Show version number [boolean]
517
+
518
+ Options:
519
+ -l, --local Run on my machine [boolean] [default: false]
520
+ --binding The binding of the namespace to write to. [string]
521
+ --namespace-id The id of the namespace to write to. [string]
522
+ --env Perform on a specific environment [string]
523
+ --preview Interact with a preview namespace [boolean]
524
+ --ttl Time for which the entries should be visible. [number]
525
+ --expiration Time since the UNIX epoch after which the entry expires [number]
526
+ --path Read value from the file at a given path. [string]
527
+
528
+ Exactly one of the arguments binding and namespace-id is required"
529
+ `);
530
+ expect(error).toMatchInlineSnapshot(
531
+ `[Error: Exactly one of the arguments binding and namespace-id is required]`
532
+ );
533
+ });
534
+
535
+ it("should error if both binding and namespace is provided", async () => {
536
+ const { error, stdout, stderr } = await runWrangler(
537
+ "kv:key put foo bar --binding x --namespace-id y"
538
+ );
539
+
540
+ expect(stdout).toMatchInlineSnapshot(`""`);
541
+ expect(stderr).toMatchInlineSnapshot(`
542
+ "wrangler kv:key put <key> [value]
543
+
544
+ Writes a single key/value pair to the given namespace.
545
+
546
+ Positionals:
547
+ key The key to write to. [string] [required]
548
+ value The value to write. [string]
549
+
550
+ Flags:
551
+ -c, --config Path to .toml configuration file [string]
552
+ -h, --help Show help [boolean]
553
+ -v, --version Show version number [boolean]
554
+
555
+ Options:
556
+ -l, --local Run on my machine [boolean] [default: false]
557
+ --binding The binding of the namespace to write to. [string]
558
+ --namespace-id The id of the namespace to write to. [string]
559
+ --env Perform on a specific environment [string]
560
+ --preview Interact with a preview namespace [boolean]
561
+ --ttl Time for which the entries should be visible. [number]
562
+ --expiration Time since the UNIX epoch after which the entry expires [number]
563
+ --path Read value from the file at a given path. [string]
564
+
565
+ Arguments binding and namespace-id are mutually exclusive"
566
+ `);
567
+ expect(error).toMatchInlineSnapshot(
568
+ `[Error: Arguments binding and namespace-id are mutually exclusive]`
569
+ );
570
+ });
571
+
572
+ it("should error if no value nor path is provided", async () => {
573
+ const { error, stdout, stderr } = await runWrangler(
574
+ "kv:key put key --namespace-id 12345"
575
+ );
576
+
577
+ expect(stdout).toMatchInlineSnapshot(`""`);
578
+ expect(stderr).toMatchInlineSnapshot(`
579
+ "wrangler kv:key put <key> [value]
580
+
581
+ Writes a single key/value pair to the given namespace.
582
+
583
+ Positionals:
584
+ key The key to write to. [string] [required]
585
+ value The value to write. [string]
586
+
587
+ Flags:
588
+ -c, --config Path to .toml configuration file [string]
589
+ -h, --help Show help [boolean]
590
+ -v, --version Show version number [boolean]
591
+
592
+ Options:
593
+ -l, --local Run on my machine [boolean] [default: false]
594
+ --binding The binding of the namespace to write to. [string]
595
+ --namespace-id The id of the namespace to write to. [string]
596
+ --env Perform on a specific environment [string]
597
+ --preview Interact with a preview namespace [boolean]
598
+ --ttl Time for which the entries should be visible. [number]
599
+ --expiration Time since the UNIX epoch after which the entry expires [number]
600
+ --path Read value from the file at a given path. [string]
601
+
602
+ Exactly one of the arguments value and path is required"
603
+ `);
604
+ expect(error).toMatchInlineSnapshot(
605
+ `[Error: Exactly one of the arguments value and path is required]`
606
+ );
607
+ });
608
+
609
+ it("should error if both value and path is provided", async () => {
610
+ const { error, stdout, stderr } = await runWrangler(
611
+ "kv:key put key value --path xyz --namespace-id 12345"
612
+ );
613
+
614
+ expect(stdout).toMatchInlineSnapshot(`""`);
615
+ expect(stderr).toMatchInlineSnapshot(`
616
+ "wrangler kv:key put <key> [value]
617
+
618
+ Writes a single key/value pair to the given namespace.
619
+
620
+ Positionals:
621
+ key The key to write to. [string] [required]
622
+ value The value to write. [string]
623
+
624
+ Flags:
625
+ -c, --config Path to .toml configuration file [string]
626
+ -h, --help Show help [boolean]
627
+ -v, --version Show version number [boolean]
628
+
629
+ Options:
630
+ -l, --local Run on my machine [boolean] [default: false]
631
+ --binding The binding of the namespace to write to. [string]
632
+ --namespace-id The id of the namespace to write to. [string]
633
+ --env Perform on a specific environment [string]
634
+ --preview Interact with a preview namespace [boolean]
635
+ --ttl Time for which the entries should be visible. [number]
636
+ --expiration Time since the UNIX epoch after which the entry expires [number]
637
+ --path Read value from the file at a given path. [string]
638
+
639
+ Arguments value and path are mutually exclusive"
640
+ `);
641
+ expect(error).toMatchInlineSnapshot(
642
+ `[Error: Arguments value and path are mutually exclusive]`
643
+ );
644
+ });
645
+
646
+ it("should error if a given binding name is not in the configured kv namespaces", async () => {
647
+ writeWranglerConfig();
648
+ const { error, stdout, stderr } = await runWrangler(
649
+ `kv:key put key value --binding otherBinding`
650
+ );
651
+ expect(stdout).toMatchInlineSnapshot(`""`);
652
+ expect(stderr).toMatchInlineSnapshot(`
653
+ "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
654
+
655
+ %s
656
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
657
+ `);
658
+ expect(error).toMatchInlineSnapshot(
659
+ `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
660
+ );
661
+ });
662
+
663
+ it("should error if a given binding has both preview and non-preview and --preview is not specified", async () => {
664
+ writeWranglerConfig();
665
+ const requests = mockKeyPutRequest(
666
+ "preview-bound-id",
667
+ "my-key",
668
+ "my-value"
669
+ );
670
+ const { error, stderr, stdout } = await runWrangler(
671
+ "kv:key put my-key my-value --binding someBinding"
672
+ );
673
+ expect(stdout).toMatchInlineSnapshot(`""`);
674
+ expect(stderr).toMatchInlineSnapshot(`
675
+ "someBinding has both a namespace ID and a preview ID. Specify \\"--preview\\" or \\"--preview false\\" to avoid writing data to the wrong namespace.
676
+
677
+ %s
678
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
679
+ `);
680
+ expect(error).toMatchInlineSnapshot(
681
+ `[Error: someBinding has both a namespace ID and a preview ID. Specify "--preview" or "--preview false" to avoid writing data to the wrong namespace.]`
682
+ );
683
+ expect(requests.count).toEqual(0);
684
+ });
685
+ });
686
+
687
+ describe("list", () => {
688
+ function mockKeyListRequest(
689
+ expectedNamespaceId: string,
690
+ expectedKeys: string[],
691
+ keysPerRequest = 1000
692
+ ) {
693
+ const requests = { count: 0 };
694
+ setMockRawResponse(
695
+ "/accounts/:accountId/storage/kv/namespaces/:namespaceId/keys",
696
+ ([_url, accountId, namespaceId], _init, query) => {
697
+ requests.count++;
698
+ expect(accountId).toEqual("some-account-id");
699
+ expect(namespaceId).toEqual(expectedNamespaceId);
700
+ if (expectedKeys.length <= keysPerRequest) {
701
+ return createFetchResult(expectedKeys);
702
+ } else {
703
+ const start = parseInt(query.get("cursor") ?? "0") || 0;
704
+ const end = start + keysPerRequest;
705
+ const cursor = end < expectedKeys.length ? end : undefined;
706
+ return createFetchResult(
707
+ expectedKeys.slice(start, end),
708
+ true,
709
+ [],
710
+ [],
711
+ { cursor }
712
+ );
713
+ }
714
+ }
715
+ );
716
+ return requests;
717
+ }
718
+
719
+ it("should list the keys of a namespace specified by namespace-id", async () => {
720
+ const keys = ["key-1", "key-2", "key-3"];
721
+ mockKeyListRequest("some-namespace-id", keys);
722
+ const { error, stdout, stderr } = await runWrangler(
723
+ "kv:key list --namespace-id some-namespace-id"
724
+ );
725
+ expect(error).toMatchInlineSnapshot(`undefined`);
726
+ expect(stderr).toMatchInlineSnapshot(`""`);
727
+ expect(stdout).toMatchInlineSnapshot(`
728
+ "key-1
729
+ key-2
730
+ key-3"
731
+ `);
732
+ });
733
+
734
+ it("should list the keys of a namespace specified by binding", async () => {
735
+ writeWranglerConfig();
736
+ const keys = ["key-1", "key-2", "key-3"];
737
+ mockKeyListRequest("bound-id", keys);
738
+ const { error, stdout, stderr } = await runWrangler(
739
+ "kv:key list --binding someBinding"
740
+ );
741
+ expect(error).toMatchInlineSnapshot(`undefined`);
742
+ expect(stderr).toMatchInlineSnapshot(`""`);
743
+ expect(stdout).toMatchInlineSnapshot(`
744
+ "key-1
745
+ key-2
746
+ key-3"
747
+ `);
748
+ });
749
+
750
+ it("should list the keys of a preview namespace specified by binding", async () => {
751
+ writeWranglerConfig();
752
+ const keys = ["key-1", "key-2", "key-3"];
753
+ mockKeyListRequest("preview-bound-id", keys);
754
+ const { error, stdout, stderr } = await runWrangler(
755
+ "kv:key list --binding someBinding --preview"
756
+ );
757
+ expect(error).toMatchInlineSnapshot(`undefined`);
758
+ expect(stderr).toMatchInlineSnapshot(`""`);
759
+ expect(stdout).toMatchInlineSnapshot(`
760
+ "key-1
761
+ key-2
762
+ key-3"
763
+ `);
764
+ });
765
+
766
+ it("should list the keys of a namespace specified by binding, in a given environment", async () => {
767
+ writeWranglerConfig();
768
+ const keys = ["key-1", "key-2", "key-3"];
769
+ mockKeyListRequest("env-bound-id", keys);
770
+ const { error, stdout, stderr } = await runWrangler(
771
+ "kv:key list --binding someBinding --env some-environment"
772
+ );
773
+ expect(error).toMatchInlineSnapshot(`undefined`);
774
+ expect(stderr).toMatchInlineSnapshot(`""`);
775
+ expect(stdout).toMatchInlineSnapshot(`
776
+ "key-1
777
+ key-2
778
+ key-3"
779
+ `);
780
+ });
781
+
782
+ it("should list the keys of a preview namespace specified by binding, in a given environment", async () => {
783
+ writeWranglerConfig();
784
+ const keys = ["key-1", "key-2", "key-3"];
785
+ mockKeyListRequest("preview-env-bound-id", keys);
786
+ const { error, stdout, stderr } = await runWrangler(
787
+ "kv:key list --binding someBinding --preview --env some-environment"
788
+ );
789
+ expect(error).toMatchInlineSnapshot(`undefined`);
790
+ expect(stderr).toMatchInlineSnapshot(`""`);
791
+ expect(stdout).toMatchInlineSnapshot(`
792
+ "key-1
793
+ key-2
794
+ key-3"
795
+ `);
796
+ });
797
+
798
+ it("should make multiple requests for paginated results", async () => {
799
+ // Create a lot of mock keys, so that the fetch requests will be paginated
800
+ const keys: string[] = [];
801
+ for (let i = 0; i < 550; i++) {
802
+ keys.push("key-" + i);
803
+ }
804
+ // Ask for the keys in pages of size 100.
805
+ const requests = mockKeyListRequest("some-namespace-id", keys, 100);
806
+ const { stdout, stderr, error } = await runWrangler(
807
+ "kv:key list --namespace-id some-namespace-id --limit 100"
808
+ );
809
+ expect(error).toMatchInlineSnapshot(`undefined`);
810
+ expect(stderr).toMatchInlineSnapshot(`""`);
811
+ expect(stdout).toEqual(keys.join("\n"));
812
+ expect(requests.count).toEqual(6);
813
+ });
814
+
815
+ it("should error if a given binding name is not in the configured kv namespaces", async () => {
816
+ writeWranglerConfig();
817
+ const { error, stdout, stderr } = await runWrangler(
818
+ "kv:key list --binding otherBinding"
819
+ );
820
+ expect(error).toMatchInlineSnapshot(
821
+ `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
822
+ );
823
+ expect(stderr).toMatchInlineSnapshot(`
824
+ "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
825
+
826
+ %s
827
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
828
+ `);
829
+ expect(stdout).toMatchInlineSnapshot(`""`);
830
+ });
831
+ });
832
+
833
+ describe("get", () => {
834
+ function mockKeyGetRequest(
835
+ expectedNamespaceId: string,
836
+ expectedKey: string,
837
+ expectedValue: string
838
+ ) {
839
+ const requests = { count: 0 };
840
+ setMockRawResponse(
841
+ "/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
842
+ ([_url, accountId, namespaceId, key]) => {
843
+ requests.count++;
844
+ expect(accountId).toEqual("some-account-id");
845
+ expect(namespaceId).toEqual(expectedNamespaceId);
846
+ expect(key).toEqual(expectedKey);
847
+ return expectedValue;
848
+ }
849
+ );
850
+ return requests;
851
+ }
852
+
853
+ it("should get a key in a given namespace specified by namespace-id", async () => {
854
+ const requests = mockKeyGetRequest(
855
+ "some-namespace-id",
856
+ "my-key",
857
+ "my-value"
858
+ );
859
+ const { error, stderr, stdout } = await runWrangler(
860
+ "kv:key get my-key --namespace-id some-namespace-id"
861
+ );
862
+ expect(stdout).toMatchInlineSnapshot(`"my-value"`);
863
+ expect(stderr).toMatchInlineSnapshot(`""`);
864
+ expect(error).toMatchInlineSnapshot(`undefined`);
865
+ expect(requests.count).toEqual(1);
866
+ });
867
+
868
+ it("should get a key in a given namespace specified by binding", async () => {
869
+ writeWranglerConfig();
870
+ const requests = mockKeyGetRequest("bound-id", "my-key", "my-value");
871
+ const { error, stderr, stdout } = await runWrangler(
872
+ "kv:key get my-key --binding someBinding --preview false"
873
+ );
874
+ expect(stdout).toMatchInlineSnapshot(`"my-value"`);
875
+ expect(stderr).toMatchInlineSnapshot(`""`);
876
+ expect(error).toMatchInlineSnapshot(`undefined`);
877
+ expect(requests.count).toEqual(1);
878
+ });
879
+
880
+ it("should get a key in a given preview namespace specified by binding", async () => {
881
+ writeWranglerConfig();
882
+ const requests = mockKeyGetRequest(
883
+ "preview-bound-id",
884
+ "my-key",
885
+ "my-value"
886
+ );
887
+ const { error, stderr, stdout } = await runWrangler(
888
+ "kv:key get my-key --binding someBinding --preview"
889
+ );
890
+ expect(stdout).toMatchInlineSnapshot(`"my-value"`);
891
+ expect(stderr).toMatchInlineSnapshot(`""`);
892
+ expect(error).toMatchInlineSnapshot(`undefined`);
893
+ expect(requests.count).toEqual(1);
894
+ });
895
+
896
+ it("should get a key for the specified environment in a given namespace", async () => {
897
+ writeWranglerConfig();
898
+ const requests = mockKeyGetRequest(
899
+ "env-bound-id",
900
+ "my-key",
901
+ "my-value"
902
+ );
903
+ const { error, stderr, stdout } = await runWrangler(
904
+ "kv:key get my-key my-value --binding someBinding --env some-environment --preview false"
905
+ );
906
+ expect(stdout).toMatchInlineSnapshot(`"my-value"`);
907
+ expect(stderr).toMatchInlineSnapshot(`""`);
908
+ expect(error).toMatchInlineSnapshot(`undefined`);
909
+ expect(requests.count).toEqual(1);
910
+ });
911
+
912
+ it("should error if no key is provided", async () => {
913
+ const { error, stdout, stderr } = await runWrangler("kv:key get");
914
+
915
+ expect(stdout).toMatchInlineSnapshot(`""`);
916
+ expect(stderr).toMatchInlineSnapshot(`
917
+ "wrangler kv:key get <key>
918
+
919
+ Reads a single value by key from the given namespace.
920
+
921
+ Positionals:
922
+ key The key value to get. [string] [required]
923
+
924
+ Flags:
925
+ -c, --config Path to .toml configuration file [string]
926
+ -h, --help Show help [boolean]
927
+ -v, --version Show version number [boolean]
928
+
929
+ Options:
930
+ -l, --local Run on my machine [boolean] [default: false]
931
+ --binding The name of the namespace to get from [string]
932
+ --namespace-id The id of the namespace to get from [string]
933
+ --env Perform on a specific environment [string]
934
+ --preview Interact with a preview namespace [boolean] [default: false]
935
+
936
+ Not enough non-option arguments: got 0, need at least 1"
937
+ `);
938
+ expect(error).toMatchInlineSnapshot(
939
+ `[Error: Not enough non-option arguments: got 0, need at least 1]`
940
+ );
941
+ });
942
+
943
+ it("should error if no binding nor namespace is provided", async () => {
944
+ const { error, stdout, stderr } = await runWrangler("kv:key get foo");
945
+
946
+ expect(stdout).toMatchInlineSnapshot(`""`);
947
+ expect(stderr).toMatchInlineSnapshot(`
948
+ "wrangler kv:key get <key>
949
+
950
+ Reads a single value by key from the given namespace.
951
+
952
+ Positionals:
953
+ key The key value to get. [string] [required]
954
+
955
+ Flags:
956
+ -c, --config Path to .toml configuration file [string]
957
+ -h, --help Show help [boolean]
958
+ -v, --version Show version number [boolean]
959
+
960
+ Options:
961
+ -l, --local Run on my machine [boolean] [default: false]
962
+ --binding The name of the namespace to get from [string]
963
+ --namespace-id The id of the namespace to get from [string]
964
+ --env Perform on a specific environment [string]
965
+ --preview Interact with a preview namespace [boolean] [default: false]
966
+
967
+ Exactly one of the arguments binding and namespace-id is required"
968
+ `);
969
+ expect(error).toMatchInlineSnapshot(
970
+ `[Error: Exactly one of the arguments binding and namespace-id is required]`
971
+ );
972
+ });
973
+
974
+ it("should error if both binding and namespace is provided", async () => {
975
+ const { error, stdout, stderr } = await runWrangler(
976
+ "kv:key get foo --binding x --namespace-id y"
977
+ );
978
+
979
+ expect(stdout).toMatchInlineSnapshot(`""`);
980
+ expect(stderr).toMatchInlineSnapshot(`
981
+ "wrangler kv:key get <key>
982
+
983
+ Reads a single value by key from the given namespace.
984
+
985
+ Positionals:
986
+ key The key value to get. [string] [required]
987
+
988
+ Flags:
989
+ -c, --config Path to .toml configuration file [string]
990
+ -h, --help Show help [boolean]
991
+ -v, --version Show version number [boolean]
992
+
993
+ Options:
994
+ -l, --local Run on my machine [boolean] [default: false]
995
+ --binding The name of the namespace to get from [string]
996
+ --namespace-id The id of the namespace to get from [string]
997
+ --env Perform on a specific environment [string]
998
+ --preview Interact with a preview namespace [boolean] [default: false]
999
+
1000
+ Arguments binding and namespace-id are mutually exclusive"
1001
+ `);
1002
+ expect(error).toMatchInlineSnapshot(
1003
+ `[Error: Arguments binding and namespace-id are mutually exclusive]`
1004
+ );
1005
+ });
1006
+
1007
+ it("should error if a given binding name is not in the configured kv namespaces", async () => {
1008
+ writeWranglerConfig();
1009
+ const { error, stdout, stderr } = await runWrangler(
1010
+ `kv:key get key --binding otherBinding`
1011
+ );
1012
+ expect(stdout).toMatchInlineSnapshot(`""`);
1013
+ expect(stderr).toMatchInlineSnapshot(`
1014
+ "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1015
+
1016
+ %s
1017
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1018
+ `);
1019
+ expect(error).toMatchInlineSnapshot(
1020
+ `[Error: A namespace with binding name "otherBinding" was not found in the configured "kv_namespaces".]`
1021
+ );
1022
+ });
1023
+ });
1024
+
1025
+ describe("delete", () => {
1026
+ function mockDeleteRequest(
1027
+ expectedNamespaceId: string,
1028
+ expectedKey: string
1029
+ ) {
1030
+ const requests = { count: 0 };
1031
+ setMockResponse(
1032
+ "/accounts/:accountId/storage/kv/namespaces/:namespaceId/values/:key",
1033
+ "DELETE",
1034
+ ([_url, accountId, namespaceId, key]) => {
1035
+ requests.count++;
1036
+ expect(accountId).toEqual("some-account-id");
1037
+ expect(namespaceId).toEqual(expectedNamespaceId);
1038
+ expect(key).toEqual(expectedKey);
1039
+ return null;
1040
+ }
1041
+ );
1042
+ return requests;
1043
+ }
1044
+
1045
+ it("should delete a key in a namespace specified by id", async () => {
1046
+ const requests = mockDeleteRequest("some-namespace-id", "someKey");
1047
+ await runWrangler(
1048
+ `kv:key delete --namespace-id some-namespace-id someKey`
1049
+ );
1050
+ expect(requests.count).toEqual(1);
1051
+ });
1052
+
1053
+ it("should delete a key in a namespace specified by binding name", async () => {
1054
+ writeWranglerConfig();
1055
+ const requests = mockDeleteRequest("bound-id", "someKey");
1056
+ await runWrangler(
1057
+ `kv:key delete --binding someBinding --preview false someKey`
1058
+ );
1059
+ expect(requests.count).toEqual(1);
1060
+ });
1061
+
1062
+ it("should delete a key in a preview namespace specified by binding name", async () => {
1063
+ writeWranglerConfig();
1064
+ const requests = mockDeleteRequest("preview-bound-id", "someKey");
1065
+ await runWrangler(
1066
+ `kv:key delete --binding someBinding --preview someKey`
1067
+ );
1068
+ expect(requests.count).toEqual(1);
1069
+ });
1070
+
1071
+ it("should error if a given binding name is not in the configured kv namespaces", async () => {
1072
+ writeWranglerConfig();
1073
+ const { stderr } = await runWrangler(
1074
+ `kv:key delete --binding otherBinding someKey`
1075
+ );
1076
+ expect(stderr).toMatchInlineSnapshot(`
1077
+ "A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\".
1078
+
1079
+ %s
1080
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1081
+ `);
1082
+ });
1083
+
1084
+ it("should delete a key in a namespace specified by binding name in a given environment", async () => {
1085
+ writeWranglerConfig();
1086
+ const requests = mockDeleteRequest("env-bound-id", "someKey");
1087
+ const { stdout, stderr, error } = await runWrangler(
1088
+ `kv:key delete --binding someBinding --env some-environment --preview false someKey`
1089
+ );
1090
+ expect(stdout).toMatchInlineSnapshot(
1091
+ `"deleting the key \\"someKey\\" on namespace env-bound-id"`
1092
+ );
1093
+ expect(stderr).toMatchInlineSnapshot(`""`);
1094
+ expect(error).toMatchInlineSnapshot(`undefined`);
1095
+ expect(requests.count).toEqual(1);
1096
+ });
1097
+
1098
+ it("should delete a key in a preview namespace specified by binding name in a given environment", async () => {
1099
+ writeWranglerConfig();
1100
+ const requests = mockDeleteRequest("preview-env-bound-id", "someKey");
1101
+ await runWrangler(
1102
+ `kv:key delete --binding someBinding --env some-environment --preview someKey`
1103
+ );
1104
+ expect(requests.count).toEqual(1);
1105
+ });
1106
+ });
1107
+ });
322
1108
  });
1109
+
1110
+ function writeWranglerConfig() {
1111
+ writeFileSync(
1112
+ "./wrangler.toml",
1113
+ [
1114
+ 'name = "otherWorker"',
1115
+ "kv_namespaces = [",
1116
+ ' { binding = "someBinding", id = "bound-id", preview_id = "preview-bound-id" }',
1117
+ "]",
1118
+ "",
1119
+ "[env.some-environment]",
1120
+ "kv_namespaces = [",
1121
+ ' { binding = "someBinding", id = "env-bound-id", preview_id = "preview-env-bound-id" }',
1122
+ "]",
1123
+ ].join("\n"),
1124
+ "utf-8"
1125
+ );
1126
+ }