Skip to content

Commit

Permalink
Update binding variables and add configuration and deploy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OilyLime authored and RamIdeas committed Sep 26, 2023
1 parent 0fd87a7 commit c8fc636
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 2 deletions.
95 changes: 95 additions & 0 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
36 changes: 36 additions & 0 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
| {
Expand Down Expand Up @@ -216,7 +216,7 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
metadataBindings.push({
name: binding,
type: "hyperdrive",
database_id: id,
id: id,
});
});

Expand Down

0 comments on commit c8fc636

Please sign in to comment.