-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkv.test.ts
223 lines (183 loc) · 5.7 KB
/
kv.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
/* SPDX-FileCopyrightText: 2022-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import { customAlphabet } from "nanoid";
import { kv as createKv, type Namespace } from "./kv.js";
// Initialize a KV storage client
const kv = createKv({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID as string,
authKey: process.env.CLOUDFLARE_AUTH_KEY as string,
authEmail: process.env.CLOUDFLARE_AUTH_EMAIL as string,
});
// Random ID generator
const newId = customAlphabet("1234567890abcdef", 5);
test("kv.find()", async () => {
await kv.create(`test-${newId()}`);
const namespaces = kv.find();
let count = 0;
expect("size" in namespaces).toBe(false);
// Can iterate through the list of namespaces
for await (const namespace of namespaces) {
expect(anonymize(namespace)).toMatchInlineSnapshot(`
Object {
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"supports_url_encoding": true,
"title": "xxxxx",
}
`);
count++;
}
// At least one namespace found
expect(count).toBeGreaterThan(0);
});
test("kv.find() /w await", async () => {
await kv.create(`test-${newId()}`);
const namespaces = await kv.find();
let count = 0;
for await (const namespace of namespaces) {
expect(anonymize(namespace)).toMatchInlineSnapshot(`
Object {
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"supports_url_encoding": true,
"title": "xxxxx",
}
`);
count++;
}
expect(count).toBeGreaterThan(0);
});
test("kv.create(name), kv.delete(name)", async () => {
// Create a new namespace
const name = `test-${newId()}`;
const namespace = await kv.create(name);
// Ensure that the target namespace was successfully created
expect(anonymize(namespace)).toMatchInlineSnapshot(`
Object {
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"supports_url_encoding": true,
"title": "xxxxx",
}
`);
// Creating a new namespace with the same name should fail
await expect(kv.create(name)).rejects.toEqual(
expect.objectContaining({
code: 10014,
message: `create namespace: 'a namespace with this account ID and title already exists'`,
})
);
// Delete the created namespace
await kv.delete(namespace.id);
});
test("kv.update(name)", async () => {
expect.assertions(2);
const nameBefore = `test-${newId()}`;
const nameAfter = `test-${newId()}`;
// Create a new namespace
const namespace = await kv.create(nameBefore);
expect(namespace.title).toEqual(nameBefore);
// Update the namespace
await kv.update(namespace.id, nameAfter);
await new Promise((resolve) => setTimeout(resolve, 1000));
const namespaces = await kv.find();
// Look up the updated namespace and confirm
for await (const ns of namespaces) {
if (ns.id === namespace.id) {
expect(ns.title).toEqual(nameAfter);
break;
}
}
});
test("kv.create(name)", async () => {
const res = await kv.create(`test-${newId()}`);
expect(anonymize(res)).toMatchInlineSnapshot(`
Object {
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"supports_url_encoding": true,
"title": "xxxxx",
}
`);
});
test("kv.namespace(id).keys()", async () => {
// Create a temporary KV namespace
const { id } = await kv.create(`test-${newId()}`);
const ns = kv.namespace(id);
// Fetch the list of namespace's keys
let keys = await ns.keys().all();
expect(keys).toEqual([]);
// Write a new kay-value pair
await ns.set("josé", "José");
await new Promise((resolve) => setTimeout(resolve, 1000));
// Fetch the list of namespace's keys
keys = await ns.keys().all();
expect(keys).toMatchInlineSnapshot(`
Array [
Object {
"name": "josé",
},
]
`);
await ns.set("other", "other");
await ns.set("joséf", "Joséf");
await new Promise((resolve) => setTimeout(resolve, 1000));
keys = await ns.keys({ prefix: "josé" }).all();
expect(keys).toMatchInlineSnapshot(`
Array [
Object {
"name": "josé",
},
Object {
"name": "joséf",
},
]
`);
await Promise.all(
Array.from({ length: 15 }, async (_, i) => {
await ns.set(`key-${i + 1}`, i + 1);
})
);
keys = await ns.keys({ prefix: "key-", first: 10 }).all();
await new Promise((resolve) => setTimeout(resolve, 1000));
expect(Array.isArray(keys)).toBe(true);
expect(keys.length).toBe(10);
expect(keys[0].name.startsWith("key-")).toBe(true);
});
test("kv.namespace(id).get/set() [json]", async () => {
// Create a temporary KV namespace
const { id } = await kv.create(`test-${newId()}`);
// Write a new kay-value pair
await kv.namespace(id).set("josé", "José");
// Read key-value pair
const value = await kv.namespace(id).get("josé");
expect(value).toEqual("José");
// Read a non-existent key-value pair
const none = await kv.namespace(id).get("none");
expect(none).toBeUndefined();
});
test("kv.namespace(id).get/set() [text]", async () => {
// Create a temporary KV namespace
const { id } = await kv.create(`test-${newId()}`);
// Write a new kay-value pair
await kv.namespace(id).set("josé", "José", { encode: false });
// Read key-value pair
const value = await kv.namespace(id).get("josé", { decode: false });
expect(value).toEqual("José");
});
afterAll(async () => {
const namespaces = await kv.find({ per_page: 100 });
const queue: Promise<unknown>[] = [];
// Remove all the test namespaces
for await (const ns of namespaces) {
if (/^test-\w{5}(|-renamed)$/.test(ns.title)) {
queue.push(kv.delete(ns.id));
}
}
await Promise.all(queue);
});
function anonymize(ns?: Namespace): Namespace | undefined {
return (
ns && {
...ns,
id: ns.id?.replace(/\w/g, "x"),
title: ns.title?.replace(/^.*$/, "xxxxx"),
}
);
}