From 824bdd02ddd0d72e0f712dcbcf26d7e9727e85a2 Mon Sep 17 00:00:00 2001 From: Carmen Popoviciu Date: Thu, 12 Dec 2024 19:47:40 +0100 Subject: [PATCH] WIP: hyperdrive resource e2e test --- .../wrangler/e2e/dev-with-resources.test.ts | 38 +++++++++++++++- packages/wrangler/e2e/dev.test.ts | 43 ------------------- .../wrangler/e2e/helpers/e2e-wrangler-test.ts | 23 ++++++++++ 3 files changed, 60 insertions(+), 44 deletions(-) diff --git a/packages/wrangler/e2e/dev-with-resources.test.ts b/packages/wrangler/e2e/dev-with-resources.test.ts index 1910d82e7d18..421570aad453 100644 --- a/packages/wrangler/e2e/dev-with-resources.test.ts +++ b/packages/wrangler/e2e/dev-with-resources.test.ts @@ -6,6 +6,7 @@ import { Agent, fetch } from "undici"; import { beforeEach, describe, expect, it } from "vitest"; import WebSocket from "ws"; import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test"; +import { fetchText } from "./helpers/fetch-text"; import { generateResourceName } from "./helpers/generate-resource-name"; const RUNTIMES = [ @@ -561,6 +562,42 @@ describe.sequential.each(RUNTIMES)("Bindings: $flags", ({ runtime, flags }) => { ); }); + it("exposes Hyperdrive bindings", async () => { + const { id } = await helper.hyperdrive(false); + + await helper.seed({ + "wrangler.toml": dedent` + name = "${workerName}" + main = "src/index.ts" + compatibility_date = "2023-10-25" + + [[hyperdrive]] + binding = "HYPERDRIVE" + id = "${id}" + `, + "src/index.ts": dedent` + export default { + async fetch(request, env) { + if (request.url.includes("connect")) { + const conn = env.HYPERDRIVE.connect(); + await conn.writable.getWriter().write(new TextEncoder().encode("test string")); + } + return new Response(env.HYPERDRIVE?.connectionString ?? "no") + } + }`, + }); + + const worker = helper.runLongLived(`wrangler dev ${flags}`); + const { url } = await worker.waitForReady(); + const text = await fetchText(url); + + const hyperdrive = new URL(text); + expect(hyperdrive.pathname).toBe("/some_db"); + expect(hyperdrive.username).toBe("user"); + expect(hyperdrive.password).toBe("!pass"); + expect(hyperdrive.host).not.toBe("localhost"); + }); + it.skipIf(!isLocal).fails("exposes Pipelines bindings", async () => { await helper.seed({ "wrangler.toml": dedent` @@ -667,7 +704,6 @@ describe.sequential.each(RUNTIMES)("Bindings: $flags", ({ runtime, flags }) => { }); // TODO(soon): implement E2E tests for other bindings - it.todo("exposes hyperdrive bindings"); it.skipIf(isLocal).todo("exposes send email bindings"); it.skipIf(isLocal).todo("exposes browser bindings"); it.skipIf(isLocal).todo("exposes Workers AI bindings"); diff --git a/packages/wrangler/e2e/dev.test.ts b/packages/wrangler/e2e/dev.test.ts index 3e353be5d65d..c71737ea96ce 100644 --- a/packages/wrangler/e2e/dev.test.ts +++ b/packages/wrangler/e2e/dev.test.ts @@ -555,49 +555,6 @@ describe("hyperdrive dev tests", () => { await socketMsgPromise; }); - it("does not require local connection string when running `wrangler dev --remote`", async () => { - const helper = new WranglerE2ETestHelper(); - await helper.seed({ - "wrangler.toml": dedent` - name = "${workerName}" - main = "src/index.ts" - compatibility_date = "2023-10-25" - - [[hyperdrive]] - binding = "HYPERDRIVE" - id = "hyperdrive_id" - `, - "src/index.ts": dedent` - export default { - async fetch(request, env) { - if (request.url.includes("connect")) { - const conn = env.HYPERDRIVE.connect(); - await conn.writable.getWriter().write(new TextEncoder().encode("test string")); - } - return new Response(env.HYPERDRIVE?.connectionString ?? "no") - } - }`, - "package.json": dedent` - { - "name": "worker", - "version": "0.0.0", - "private": true - } - `, - }); - - const worker = helper.runLongLived("wrangler dev --remote"); - - const { url } = await worker.waitForReady(); - const text = await fetchText(url); - - const hyperdrive = new URL(text); - expect(hyperdrive.pathname).toBe("/some_db"); - expect(hyperdrive.username).toBe("user"); - expect(hyperdrive.password).toBe("!pass"); - expect(hyperdrive.host).not.toBe("localhost"); - }); - afterEach(() => { if (server.listening) { server.close(); diff --git a/packages/wrangler/e2e/helpers/e2e-wrangler-test.ts b/packages/wrangler/e2e/helpers/e2e-wrangler-test.ts index 293ff896b796..d659efa79347 100644 --- a/packages/wrangler/e2e/helpers/e2e-wrangler-test.ts +++ b/packages/wrangler/e2e/helpers/e2e-wrangler-test.ts @@ -124,4 +124,27 @@ export class WranglerE2ETestHelper { return name; } + + async hyperdrive(isLocal: boolean): Promise<{ id: string; name: string }> { + const name = generateResourceName("hyperdrive"); + + if (isLocal) { + return { id: crypto.randomUUID(), name }; + } + + const result = await this.run( + `wrangler hyperdrive create ${name} --connection-string=""` + ); + const tomlMatch = /id = "([0-9a-f]{32})"/.exec(result.stdout); + const jsonMatch = /"id": "([0-9a-f]{32})"/.exec(result.stdout); + const match = jsonMatch ?? tomlMatch; + assert(match !== null, `Cannot find ID in ${JSON.stringify(result)}`); + const id = match[1]; + + onTestFinished(async () => { + await this.run(`wrangler hyperdrive delete ${name}`); + }); + + return { id, name }; + } }