From c8fc6361e53235a43a6aa60e7ecb2ccf7503a48e Mon Sep 17 00:00:00 2001 From: Emilio Assuncao Date: Mon, 25 Sep 2023 14:24:33 -0500 Subject: [PATCH] Update binding variables and add configuration and deploy tests --- .../src/__tests__/configuration.test.ts | 95 +++++++++++++++++++ .../wrangler/src/__tests__/deploy.test.ts | 36 +++++++ .../create-worker-upload-form.ts | 4 +- 3 files changed, 133 insertions(+), 2 deletions(-) diff --git a/packages/wrangler/src/__tests__/configuration.test.ts b/packages/wrangler/src/__tests__/configuration.test.ts index d0f76625f483..0a239568efcf 100644 --- a/packages/wrangler/src/__tests__/configuration.test.ts +++ b/packages/wrangler/src/__tests__/configuration.test.ts @@ -2077,6 +2077,101 @@ describe("normalizeAndValidateConfig()", () => { }); }); + describe("[hyperdrive]", () => { + it("should error if hyperdrive is an object", () => { + const { diagnostics } = normalizeAndValidateConfig( + { hyperdrive: {} } as unknown as RawConfig, + undefined, + { env: undefined } + ); + + expect(diagnostics.hasWarnings()).toBe(false); + expect(diagnostics.renderErrors()).toMatchInlineSnapshot(` + "Processing wrangler configuration: + - The field \\"hyperdrive\\" should be an array but got {}." + `); + }); + + it("should error if hyperdrive is a string", () => { + const { diagnostics } = normalizeAndValidateConfig( + { hyperdrive: "BAD" } as unknown as RawConfig, + undefined, + { env: undefined } + ); + + expect(diagnostics.hasWarnings()).toBe(false); + expect(diagnostics.renderErrors()).toMatchInlineSnapshot(` + "Processing wrangler configuration: + - The field \\"hyperdrive\\" should be an array but got \\"BAD\\"." + `); + }); + + it("should error if hyperdrive is a number", () => { + const { diagnostics } = normalizeAndValidateConfig( + { hyperdrive: 999 } as unknown as RawConfig, + undefined, + { env: undefined } + ); + + expect(diagnostics.hasWarnings()).toBe(false); + expect(diagnostics.renderErrors()).toMatchInlineSnapshot(` + "Processing wrangler configuration: + - The field \\"hyperdrive\\" should be an array but got 999." + `); + }); + + it("should error if hyperdrive is null", () => { + const { diagnostics } = normalizeAndValidateConfig( + { hyperdrive: null } as unknown as RawConfig, + undefined, + { env: undefined } + ); + + expect(diagnostics.hasWarnings()).toBe(false); + expect(diagnostics.renderErrors()).toMatchInlineSnapshot(` + "Processing wrangler configuration: + - The field \\"hyperdrive\\" should be an array but got null." + `); + }); + + it("should accept valid bindings", () => { + const { diagnostics } = normalizeAndValidateConfig( + { + hyperdrive: [ + { binding: "VALID", id: "343cd4f1d58c42fbb5bd082592fd7143" }, + ], + } as unknown as RawConfig, + undefined, + { env: undefined } + ); + + expect(diagnostics.hasErrors()).toBe(false); + }); + + it("should error if hyperdrive.bindings are not valid", () => { + const { diagnostics } = normalizeAndValidateConfig( + { + hyperdrive: [ + {}, + { binding: "VALID", id: "343cd4f1d58c42fbb5bd082592fd7143" }, + { binding: 2000, project: 2111 }, + ], + } as unknown as RawConfig, + undefined, + { env: undefined } + ); + + expect(diagnostics.hasWarnings()).toBe(true); + expect(diagnostics.renderErrors()).toMatchInlineSnapshot(` + "Processing wrangler configuration: + - \\"hyperdrive[0]\\" bindings should have a string \\"binding\\" field but got {}. + - \\"hyperdrive[0]\\" bindings must have a \\"id\\" field but got {}. + - \\"hyperdrive[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"project\\":2111}. + - \\"hyperdrive[2]\\" bindings must have a \\"id\\" field but got {\\"binding\\":2000,\\"project\\":2111}." + `); + }); + }); + describe("[queues]", () => { it("should error if queues is not an object", () => { const { config, diagnostics } = normalizeAndValidateConfig( diff --git a/packages/wrangler/src/__tests__/deploy.test.ts b/packages/wrangler/src/__tests__/deploy.test.ts index ec7dcfa85bc3..d348cd4cc760 100644 --- a/packages/wrangler/src/__tests__/deploy.test.ts +++ b/packages/wrangler/src/__tests__/deploy.test.ts @@ -8394,6 +8394,42 @@ export default{ }); }); + describe("hyperdrive", () => { + it("should upload hyperdrive bindings", async () => { + writeWranglerToml({ + hyperdrive: [ + { + binding: "HYPERDRIVE", + id: "343cd4f1d58c42fbb5bd082592fd7143", + }, + ], + }); + await fs.promises.writeFile("index.js", `export default {};`); + mockSubDomainRequest(); + mockUploadWorkerRequest({ + expectedBindings: [ + { + type: "hyperdrive", + name: "HYPERDRIVE", + id: "343cd4f1d58c42fbb5bd082592fd7143", + }, + ], + }); + + await runWrangler("deploy index.js"); + expect(std.out).toMatchInlineSnapshot(` + "Total Upload: xx KiB / gzip: xx KiB + Your worker has access to the following bindings: + - Hyperdrive Configs: + - HYPERDRIVE: 343cd4f1d58c42fbb5bd082592fd7143 + Uploaded test-name (TIMINGS) + Published test-name (TIMINGS) + https://test-name.test-sub-domain.workers.dev + Current Deployment ID: Galaxy-Class" + `); + }); + }); + describe("mtls_certificates", () => { it("should upload mtls_certificate bindings", async () => { writeWranglerToml({ diff --git a/packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts b/packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts index d68017939c16..2364a10d79a3 100644 --- a/packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts +++ b/packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts @@ -66,7 +66,7 @@ export type WorkerMetadataBinding = internalEnv?: string; } | { type: "constellation"; name: string; project: string } - | { type: "hyperdrive"; name: string; database_id: string } + | { type: "hyperdrive"; name: string; id: string } | { type: "service"; name: string; service: string; environment?: string } | { type: "analytics_engine"; name: string; dataset?: string } | { @@ -216,7 +216,7 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData { metadataBindings.push({ name: binding, type: "hyperdrive", - database_id: id, + id: id, }); });