test(cloudflare): use wrangler dev proxy (#518)

This commit is contained in:
Pooya Parsa 2024-12-10 14:26:16 +01:00 committed by GitHub
parent 6caa7d6bb8
commit 13da5e58a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 643 additions and 66 deletions

1
.gitignore vendored
View File

@ -12,3 +12,4 @@ __*
.netlify
test/fs-storage/**
.env
.wrangler

View File

@ -95,7 +95,8 @@
"unbuild": "^2.0.0",
"vite": "^6.0.3",
"vitest": "^2.1.8",
"vue": "^3.5.13"
"vue": "^3.5.13",
"wrangler": "^3.94.0"
},
"peerDependencies": {
"@azure/app-configuration": "^1.8.0",

603
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,38 +1,25 @@
/// <reference types="@cloudflare/workers-types" />
import { describe, expect, test } from "vitest";
import { describe, expect, test, afterAll } from "vitest";
import { createStorage, snapshot } from "../../src";
import CloudflareKVBinding from "../../src/drivers/cloudflare-kv-binding";
import { testDriver } from "./utils";
import { getPlatformProxy } from "wrangler";
const mockStorage = createStorage();
// https://developers.cloudflare.com/workers/runtime-apis/kv/
const mockBinding: KVNamespace = {
get(key) {
return mockStorage.getItem(key) as any;
},
getWithMetadata(key: string) {
return mockStorage.getItem(key) as any;
},
put(key, value) {
return mockStorage.setItem(key, value) as any;
},
delete(key) {
return mockStorage.removeItem(key) as any;
},
list(opts) {
return mockStorage
.getKeys(opts?.prefix || undefined)
.then((keys) => ({ keys: keys.map((name) => ({ name })) })) as any;
},
};
describe("drivers: cloudflare-kv", () => {
describe("drivers: cloudflare-kv", async () => {
const cfProxy = await getPlatformProxy();
globalThis.__env__ = cfProxy.env;
afterAll(async () => {
globalThis.__env__ = undefined;
await cfProxy.dispose();
});
testDriver({
driver: CloudflareKVBinding({ binding: mockBinding, base: "base" }),
driver: CloudflareKVBinding({ base: "base" }),
async additionalTests() {
test("snapshot", async () => {
expect(await snapshot(mockStorage, "")).toMatchInlineSnapshot(`
const storage = createStorage({
driver: CloudflareKVBinding({}),
});
expect(await snapshot(storage, "")).toMatchInlineSnapshot(`
{
"base:data:raw.bin": "base64:AQID",
"base:data:serialized1.json": "SERIALIZED",

View File

@ -1,50 +1,26 @@
/// <reference types="@cloudflare/workers-types" />
import { describe, test, expect } from "vitest";
import { describe, test, expect, afterAll } from "vitest";
import { createStorage, snapshot } from "../../src";
import CloudflareR2Binding from "../../src/drivers/cloudflare-r2-binding";
import { testDriver } from "./utils";
import { getPlatformProxy } from "wrangler";
const mockStorage = createStorage();
describe.skip("drivers: cloudflare-r2-binding", async () => {
const cfProxy = await getPlatformProxy();
globalThis.__env__ = cfProxy.env;
afterAll(async () => {
globalThis.__env__ = undefined;
await cfProxy.dispose();
});
// https://developers.cloudflare.com/workers/runtime-apis/kv/
const mockBinding: R2Bucket = {
async head(key) {
return (await mockStorage.hasItem(key)) ? ({ key } as any) : null;
},
async get(key) {
return {
text: () => mockStorage.getItem(key),
arrayBuffer: () => mockStorage.getItemRaw(key),
} as any;
},
put(key, value) {
return mockStorage.setItemRaw(key, value) as any;
},
delete(key) {
if (Array.isArray(key)) {
return Promise.all(key.map((k) => mockStorage.removeItem(k))) as any;
}
return mockStorage.removeItem(key as string) as any;
},
list(opts) {
return mockStorage
.getKeys(opts?.prefix || undefined)
.then((keys) => ({ objects: keys.map((key) => ({ key })) })) as any;
},
createMultipartUpload() {
throw new Error("Not implemented");
},
resumeMultipartUpload() {
throw new Error("Not implemented");
},
};
describe("drivers: cloudflare-r2-binding", () => {
testDriver({
driver: CloudflareR2Binding({ binding: mockBinding, base: "base" }),
driver: CloudflareR2Binding({ base: "base" }),
async additionalTests() {
test("snapshot", async () => {
expect(await snapshot(mockStorage, "")).toMatchInlineSnapshot(`
const storage = createStorage({
driver: CloudflareR2Binding({}),
});
expect(await snapshot(storage, "")).toMatchInlineSnapshot(`
{
"base:data:raw.bin": Uint8Array [
1,

9
wrangler.toml Normal file
View File

@ -0,0 +1,9 @@
compatibility_date = "2024-12-01"
kv_namespaces = [
{ binding = "STORAGE", id = "<KV_ID>" }
]
r2_buckets = [
{ binding = "BUCKET", bucket_name = "default" },
]