|
1 | 1 | import { getGlobalDispatcher, MockAgent, setGlobalDispatcher } from "undici"; |
2 | 2 | import { afterEach, beforeEach, describe, it } from "vitest"; |
3 | 3 | import { |
| 4 | + deleteDatabase, |
| 5 | + deleteKVNamespace, |
4 | 6 | deleteProject, |
5 | 7 | deleteWorker, |
| 8 | + listTmpDatabases, |
6 | 9 | listTmpE2EProjects, |
7 | 10 | listTmpE2EWorkers, |
| 11 | + listTmpKVNamespaces, |
8 | 12 | } from "../common"; |
9 | 13 |
|
10 | 14 | const originalAccountID = process.env.CLOUDFLARE_ACCOUNT_ID; |
@@ -108,6 +112,154 @@ describe("deleteProject()", () => { |
108 | 112 | }); |
109 | 113 | }); |
110 | 114 |
|
| 115 | +describe("listTmpKVNamespaces()", () => { |
| 116 | + it("makes a REST request and returns a filtered list of kv namespaces", async ({ |
| 117 | + expect, |
| 118 | + }) => { |
| 119 | + agent |
| 120 | + .get("https://api.cloudflare.com") |
| 121 | + .intercept({ |
| 122 | + path: `/client/v4/accounts/${MOCK_CLOUDFLARE_ACCOUNT_ID}/storage/kv/namespaces`, |
| 123 | + method: "GET", |
| 124 | + query: { |
| 125 | + per_page: 10, |
| 126 | + page: 1, |
| 127 | + direction: "asc", |
| 128 | + order: "title", |
| 129 | + }, |
| 130 | + }) |
| 131 | + .reply( |
| 132 | + 200, |
| 133 | + JSON.stringify({ |
| 134 | + result: [ |
| 135 | + { id: "kv-tmp-e2e", title: "kv-1" }, |
| 136 | + { id: "kv-2", title: "kv-2" }, |
| 137 | + { id: "tmp_e2e", title: "kv-3" }, |
| 138 | + { id: "kv-4", title: "kv-4" }, |
| 139 | + { id: "kv-5", title: "kv-5" }, |
| 140 | + { id: "kv-6", title: "kv-6" }, |
| 141 | + { id: "tmp_e2e_kv", title: "kv-7" }, |
| 142 | + { id: "kv-8", title: "kv-8" }, |
| 143 | + { id: "kv-9", title: "kv-9" }, |
| 144 | + { id: "kv-10", title: "kv-10" }, |
| 145 | + ], |
| 146 | + }) |
| 147 | + ); |
| 148 | + agent |
| 149 | + .get("https://api.cloudflare.com") |
| 150 | + .intercept({ |
| 151 | + path: `/client/v4/accounts/${MOCK_CLOUDFLARE_ACCOUNT_ID}/storage/kv/namespaces`, |
| 152 | + method: "GET", |
| 153 | + query: { |
| 154 | + per_page: 10, |
| 155 | + page: 2, |
| 156 | + direction: "asc", |
| 157 | + order: "title", |
| 158 | + }, |
| 159 | + }) |
| 160 | + .reply( |
| 161 | + 200, |
| 162 | + JSON.stringify({ |
| 163 | + result: [{ id: "kv-tmp-e2e-11", title: "kv-11" }], |
| 164 | + }) |
| 165 | + ); |
| 166 | + |
| 167 | + const result = await listTmpKVNamespaces(); |
| 168 | + |
| 169 | + expect(result.map((p) => p.id)).toMatchInlineSnapshot(`[]`); |
| 170 | + }); |
| 171 | +}); |
| 172 | + |
| 173 | +describe("deleteKVNamespace()", () => { |
| 174 | + it("makes a REST request to delete the given project", async () => { |
| 175 | + const MOCK_KV = "tmp_e2e_kv"; |
| 176 | + agent |
| 177 | + .get("https://api.cloudflare.com") |
| 178 | + .intercept({ |
| 179 | + path: `/client/v4/accounts/${MOCK_CLOUDFLARE_ACCOUNT_ID}/storage/kv/namespaces/${MOCK_KV}`, |
| 180 | + method: "DELETE", |
| 181 | + }) |
| 182 | + .reply(200, JSON.stringify({ result: [] })); |
| 183 | + await deleteKVNamespace(MOCK_KV); |
| 184 | + }); |
| 185 | +}); |
| 186 | + |
| 187 | +describe("listTmpDatabases()", () => { |
| 188 | + it("makes a REST request and returns a filtered list of d1 databases", async ({ |
| 189 | + expect, |
| 190 | + }) => { |
| 191 | + agent |
| 192 | + .get("https://api.cloudflare.com") |
| 193 | + .intercept({ |
| 194 | + path: `/client/v4/accounts/${MOCK_CLOUDFLARE_ACCOUNT_ID}/d1/database`, |
| 195 | + method: "GET", |
| 196 | + query: { |
| 197 | + per_page: 10, |
| 198 | + page: 1, |
| 199 | + }, |
| 200 | + }) |
| 201 | + .reply( |
| 202 | + 200, |
| 203 | + JSON.stringify({ |
| 204 | + result: [ |
| 205 | + { uuid: "1", name: "db-1", created_at: nowStr }, |
| 206 | + { uuid: "2", name: "db-2", created_at: oldTimeStr }, |
| 207 | + { uuid: "3", name: "tmp-e2e-db-1", created_at: nowStr }, |
| 208 | + { uuid: "4", name: "tmp-e2e-db-2", created_at: oldTimeStr }, |
| 209 | + { uuid: "5", name: "db-3", created_at: nowStr }, |
| 210 | + { uuid: "6", name: "db-4", created_at: oldTimeStr }, |
| 211 | + { uuid: "7", name: "tmp-e2e-db-3", created_at: nowStr }, |
| 212 | + { uuid: "8", name: "tmp-e2e-db-4", created_at: oldTimeStr }, |
| 213 | + { uuid: "9", name: "db-5", created_at: nowStr }, |
| 214 | + { uuid: "10", name: "db-6", created_at: oldTimeStr }, |
| 215 | + ], |
| 216 | + }) |
| 217 | + ); |
| 218 | + agent |
| 219 | + .get("https://api.cloudflare.com") |
| 220 | + .intercept({ |
| 221 | + path: `/client/v4/accounts/${MOCK_CLOUDFLARE_ACCOUNT_ID}/d1/database`, |
| 222 | + method: "GET", |
| 223 | + query: { |
| 224 | + per_page: 10, |
| 225 | + page: 2, |
| 226 | + }, |
| 227 | + }) |
| 228 | + .reply( |
| 229 | + 200, |
| 230 | + JSON.stringify({ |
| 231 | + result: [ |
| 232 | + { uuid: "11", name: "db-11", created_at: nowStr }, |
| 233 | + { uuid: "12", name: "db-12", created_at: oldTimeStr }, |
| 234 | + ], |
| 235 | + }) |
| 236 | + ); |
| 237 | + |
| 238 | + const result = await listTmpDatabases(); |
| 239 | + |
| 240 | + expect(result.map((p) => p.name)).toMatchInlineSnapshot(` |
| 241 | + [ |
| 242 | + "tmp-e2e-db-2", |
| 243 | + "tmp-e2e-db-4", |
| 244 | + ] |
| 245 | + `); |
| 246 | + }); |
| 247 | +}); |
| 248 | + |
| 249 | +describe("deleteDatabase()", () => { |
| 250 | + it("makes a REST request to delete the given project", async () => { |
| 251 | + const MOCK_DB = "tmp-e2e-db"; |
| 252 | + agent |
| 253 | + .get("https://api.cloudflare.com") |
| 254 | + .intercept({ |
| 255 | + path: `/client/v4/accounts/${MOCK_CLOUDFLARE_ACCOUNT_ID}/d1/database/${MOCK_DB}`, |
| 256 | + method: "DELETE", |
| 257 | + }) |
| 258 | + .reply(200, JSON.stringify({ result: [] })); |
| 259 | + await deleteDatabase(MOCK_DB); |
| 260 | + }); |
| 261 | +}); |
| 262 | + |
111 | 263 | describe("listTmpE2EWorkers()", () => { |
112 | 264 | it("makes a REST request and returns a filtered list of workers", async ({ |
113 | 265 | expect, |
|
0 commit comments