From 0dd055297a8d4e683ef86e885b4c22d84d9dacc8 Mon Sep 17 00:00:00 2001 From: Zeb Piasecki Date: Fri, 16 Feb 2024 09:41:15 -0500 Subject: [PATCH] chore: clean up standard pricing check after legacy usage model deprecation --- .../__tests__/deprecated-usage-model.test.ts | 68 ++++++ .../src/__tests__/standard-pricing.test.ts | 224 ------------------ packages/wrangler/src/deploy/index.ts | 50 +--- packages/wrangler/src/versions/index.ts | 50 +--- 4 files changed, 80 insertions(+), 312 deletions(-) create mode 100644 packages/wrangler/src/__tests__/deprecated-usage-model.test.ts delete mode 100644 packages/wrangler/src/__tests__/standard-pricing.test.ts diff --git a/packages/wrangler/src/__tests__/deprecated-usage-model.test.ts b/packages/wrangler/src/__tests__/deprecated-usage-model.test.ts new file mode 100644 index 000000000000..9d0640f0006f --- /dev/null +++ b/packages/wrangler/src/__tests__/deprecated-usage-model.test.ts @@ -0,0 +1,68 @@ +import { mockAccountId, mockApiToken } from "./helpers/mock-account-id"; +import { mockConsoleMethods } from "./helpers/mock-console"; +import { mockUploadWorkerRequest } from "./helpers/mock-upload-worker"; +import { mockSubDomainRequest } from "./helpers/mock-workers-subdomain"; +import { + msw, + mswSuccessDeploymentScriptMetadata, +} from "./helpers/msw"; +import { runInTempDir } from "./helpers/run-in-tmp"; +import { runWrangler } from "./helpers/run-wrangler"; +import { writeWorkerSource } from "./helpers/write-worker-source"; +import writeWranglerToml from "./helpers/write-wrangler-toml"; + +describe("deprecated-usage-model", () => { + mockAccountId(); + mockApiToken(); + runInTempDir(); + const std = mockConsoleMethods(); + + // TODO: remove the fake timers and irrelevant tests after March 1st + beforeAll(() => { + jest.useFakeTimers(); + jest.setSystemTime(new Date(2024, 2, 2)); + }); + + afterAll(() => { + jest.useRealTimers(); + }); + + it("should warn user about ignored usage model if usage_model specified", async () => { + msw.use(...mswSuccessDeploymentScriptMetadata); + writeWranglerToml({ usage_model: "bundled" }); + writeWorkerSource(); + mockSubDomainRequest(); + mockUploadWorkerRequest(); + + await runWrangler("deploy ./index"); + + expect(std.warn).toMatchInlineSnapshot(` + "▲ [WARNING] The \`usage_model\` defined in wrangler.toml is deprecated and no longer used. Visit our developer docs for details: https://developers.cloudflare.com/workers/wrangler/configuration/#usage-model + + " + `); + }); + it("should not warn user about ignored usage model if usage_model not specified", async () => { + msw.use(...mswSuccessDeploymentScriptMetadata); + writeWranglerToml(); + writeWorkerSource(); + mockSubDomainRequest(); + mockUploadWorkerRequest(); + + await runWrangler("deploy ./index"); + + expect(std).toMatchInlineSnapshot(` + Object { + "debug": "", + "err": "", + "info": "", + "out": "Total Upload: xx KiB / gzip: xx KiB + Uploaded test-name (TIMINGS) + Published test-name (TIMINGS) + https://test-name.test-sub-domain.workers.dev + Current Deployment ID: Galaxy-Class", + "warn": "", + } + `); + }); +}); diff --git a/packages/wrangler/src/__tests__/standard-pricing.test.ts b/packages/wrangler/src/__tests__/standard-pricing.test.ts deleted file mode 100644 index 1634921ef02d..000000000000 --- a/packages/wrangler/src/__tests__/standard-pricing.test.ts +++ /dev/null @@ -1,224 +0,0 @@ -import { rest } from "msw"; -import { mockAccountId, mockApiToken } from "./helpers/mock-account-id"; -import { mockConsoleMethods } from "./helpers/mock-console"; -import { mockUploadWorkerRequest } from "./helpers/mock-upload-worker"; -import { mockSubDomainRequest } from "./helpers/mock-workers-subdomain"; -import { - createFetchResult, - msw, - mswSuccessDeploymentScriptMetadata, -} from "./helpers/msw"; -import { runInTempDir } from "./helpers/run-in-tmp"; -import { runWrangler } from "./helpers/run-wrangler"; -import { writeWorkerSource } from "./helpers/write-worker-source"; -import writeWranglerToml from "./helpers/write-wrangler-toml"; - -function mockStandardEnabled(enabled: boolean, enterprise = false) { - msw.use( - rest.get("*/accounts/:accountId/workers/standard", (req, res, ctx) => { - return res.once( - ctx.json( - createFetchResult( - { - standard: enabled, - reason: enterprise ? "enterprise without override" : "ignore", - }, - true - ) - ) - ); - }) - ); -} - -describe("standard-pricing", () => { - mockAccountId(); - mockApiToken(); - runInTempDir(); - const std = mockConsoleMethods(); - - // TODO: remove the fake timers and irrelevant tests after March 1st - beforeAll(() => { - jest.useFakeTimers(); - jest.setSystemTime(new Date(2024, 0, 0)); - }); - - afterAll(() => { - jest.useRealTimers(); - }); - - it("should do nothing if endpoint not available", async () => { - msw.use(...mswSuccessDeploymentScriptMetadata); - writeWranglerToml(); - writeWorkerSource(); - mockSubDomainRequest(); - mockUploadWorkerRequest(); - - await runWrangler("deploy ./index"); - - expect(std).toMatchInlineSnapshot(` - Object { - "debug": "", - "err": "", - "info": "", - "out": "Total Upload: xx KiB / gzip: xx KiB - Uploaded test-name (TIMINGS) - Published test-name (TIMINGS) - https://test-name.test-sub-domain.workers.dev - Current Deployment ID: Galaxy-Class", - "warn": "", - } - `); - }); - it("should notify user about new pricing if not enabled", async () => { - msw.use(...mswSuccessDeploymentScriptMetadata); - writeWranglerToml(); - writeWorkerSource(); - mockSubDomainRequest(); - mockUploadWorkerRequest(); - - mockStandardEnabled(false); - - await runWrangler("deploy ./index"); - - expect(std).toMatchInlineSnapshot(` - Object { - "debug": "", - "err": "", - "info": "", - "out": "🚧 New Workers Standard pricing is now available. Please visit the dashboard to view details and opt-in to new pricing: https://dash.cloudflare.com/some-account-id/workers/standard/opt-in. - Total Upload: xx KiB / gzip: xx KiB - Uploaded test-name (TIMINGS) - Published test-name (TIMINGS) - https://test-name.test-sub-domain.workers.dev - Current Deployment ID: Galaxy-Class", - "warn": "", - } - `); - }); - it("should warn user about limits set if not enabled", async () => { - msw.use(...mswSuccessDeploymentScriptMetadata); - writeWranglerToml({ limits: { cpu_ms: 20_000 } }); - writeWorkerSource(); - mockSubDomainRequest(); - mockUploadWorkerRequest(); - - mockStandardEnabled(false); - - await runWrangler("deploy ./index"); - - expect(std).toMatchInlineSnapshot(` - Object { - "debug": "", - "err": "", - "info": "", - "out": "🚧 New Workers Standard pricing is now available. Please visit the dashboard to view details and opt-in to new pricing: https://dash.cloudflare.com/some-account-id/workers/standard/opt-in. - Total Upload: xx KiB / gzip: xx KiB - Uploaded test-name (TIMINGS) - Published test-name (TIMINGS) - https://test-name.test-sub-domain.workers.dev - Current Deployment ID: Galaxy-Class", - "warn": "▲ [WARNING] The \`limits\` defined in wrangler.toml can only be applied to scripts opted into Workers Standard pricing. Agree to the new pricing details to set limits for your script. - - ", - } - `); - }); - it("should not notify user about new pricing if enterprise", async () => { - msw.use(...mswSuccessDeploymentScriptMetadata); - writeWranglerToml(); - writeWorkerSource(); - mockSubDomainRequest(); - mockUploadWorkerRequest(); - - mockStandardEnabled(false, true); - - await runWrangler("deploy ./index"); - - expect(std).toMatchInlineSnapshot(` - Object { - "debug": "", - "err": "", - "info": "", - "out": "Total Upload: xx KiB / gzip: xx KiB - Uploaded test-name (TIMINGS) - Published test-name (TIMINGS) - https://test-name.test-sub-domain.workers.dev - Current Deployment ID: Galaxy-Class", - "warn": "", - } - `); - }); - it("should warn user about new pricing if enabled and usage_model specified", async () => { - msw.use(...mswSuccessDeploymentScriptMetadata); - writeWranglerToml({ usage_model: "bundled" }); - writeWorkerSource(); - mockSubDomainRequest(); - mockUploadWorkerRequest(); - - mockStandardEnabled(true); - - await runWrangler("deploy ./index"); - - expect(std).toMatchInlineSnapshot(` - Object { - "debug": "", - "err": "", - "info": "", - "out": "Total Upload: xx KiB / gzip: xx KiB - Uploaded test-name (TIMINGS) - Published test-name (TIMINGS) - https://test-name.test-sub-domain.workers.dev - Current Deployment ID: Galaxy-Class", - "warn": "▲ [WARNING] The \`usage_model\` defined in wrangler.toml is no longer used because you have opted into Workers Standard pricing. Please remove this setting from your wrangler.toml and use the dashboard to configure the usage model for your script. - - ", - } - `); - }); - - it("should warn user about new pricing if enabled and usage_model specified after deprecation date", async () => { - jest.setSystemTime(new Date(2024, 2, 2)); - - msw.use(...mswSuccessDeploymentScriptMetadata); - writeWranglerToml({ usage_model: "bundled" }); - writeWorkerSource(); - mockSubDomainRequest(); - mockUploadWorkerRequest(); - - mockStandardEnabled(true); - - await runWrangler("deploy ./index"); - - expect(std.warn).toMatchInlineSnapshot(` - "▲ [WARNING] The \`usage_model\` defined in wrangler.toml is deprecated and no longer used. Visit our developer docs for details: https://developers.cloudflare.com/workers/wrangler/configuration/#usage-model - - " - `); - }); - it("should not warn user about new pricing if enabled and usage_model not specified", async () => { - msw.use(...mswSuccessDeploymentScriptMetadata); - writeWranglerToml(); - writeWorkerSource(); - mockSubDomainRequest(); - mockUploadWorkerRequest(); - - mockStandardEnabled(true); - - await runWrangler("deploy ./index"); - - expect(std).toMatchInlineSnapshot(` - Object { - "debug": "", - "err": "", - "info": "", - "out": "Total Upload: xx KiB / gzip: xx KiB - Uploaded test-name (TIMINGS) - Published test-name (TIMINGS) - https://test-name.test-sub-domain.workers.dev - Current Deployment ID: Galaxy-Class", - "warn": "", - } - `); - }); -}); diff --git a/packages/wrangler/src/deploy/index.ts b/packages/wrangler/src/deploy/index.ts index bc55114b28f9..a2e7a424bd2f 100644 --- a/packages/wrangler/src/deploy/index.ts +++ b/packages/wrangler/src/deploy/index.ts @@ -1,6 +1,4 @@ import path from "node:path"; -import chalk from "chalk"; -import { fetchResult } from "../cfetch"; import { findWranglerToml, readConfig } from "../config"; import { getEntry } from "../deployment-bundle/entry"; import { UserError } from "../errors"; @@ -22,48 +20,12 @@ import type { StrictYargsOptionsToInterface, } from "../yargs-types"; -async function standardPricingWarning( - accountId: string | undefined, - config: Config -) { - if (Date.now() >= Date.UTC(2024, 2, 1, 14)) { - if (config.usage_model !== undefined) { - logger.warn( - "The `usage_model` defined in wrangler.toml is deprecated and no longer used. Visit our developer docs for details: https://developers.cloudflare.com/workers/wrangler/configuration/#usage-model" - ); - } - - // TODO: After March 1st 2024 remove the code below - return; +async function standardPricingWarning(config: Config) { + if (config.usage_model !== undefined) { + logger.warn( + "The `usage_model` defined in wrangler.toml is deprecated and no longer used. Visit our developer docs for details: https://developers.cloudflare.com/workers/wrangler/configuration/#usage-model" + ); } - - try { - const { standard, reason } = await fetchResult<{ - standard: boolean; - reason: string; - }>(`/accounts/${accountId}/workers/standard`); - - if (!standard && reason !== "enterprise without override") { - logger.log( - chalk.blue( - `🚧 New Workers Standard pricing is now available. Please visit the dashboard to view details and opt-in to new pricing: https://dash.cloudflare.com/${accountId}/workers/standard/opt-in.` - ) - ); - if (config.limits?.cpu_ms !== undefined) { - logger.warn( - "The `limits` defined in wrangler.toml can only be applied to scripts opted into Workers Standard pricing. Agree to the new pricing details to set limits for your script." - ); - } - return; - } - if (standard && config.usage_model !== undefined) { - logger.warn( - "The `usage_model` defined in wrangler.toml is no longer used because you have opted into Workers Standard pricing. Please remove this setting from your wrangler.toml and use the dashboard to configure the usage model for your script." - ); - return; - } - // Ignore 404 errors for the enablement check - } catch {} } export function deployOptions(yargs: CommonYargsArgv) { @@ -305,7 +267,7 @@ export async function deployHandler( args.siteExclude ); - if (!args.dryRun) await standardPricingWarning(accountId, config); + if (!args.dryRun) await standardPricingWarning(config); await deploy({ config, diff --git a/packages/wrangler/src/versions/index.ts b/packages/wrangler/src/versions/index.ts index 253db901eda1..3f29027e7785 100644 --- a/packages/wrangler/src/versions/index.ts +++ b/packages/wrangler/src/versions/index.ts @@ -1,6 +1,4 @@ import path from "node:path"; -import chalk from "chalk"; -import { fetchResult } from "../cfetch"; import { findWranglerToml, readConfig } from "../config"; import { getEntry } from "../deployment-bundle/entry"; import { getRules, getScriptName, printWranglerBanner } from "../index"; @@ -15,48 +13,12 @@ import type { StrictYargsOptionsToInterface, } from "../yargs-types"; -async function standardPricingWarning( - accountId: string | undefined, - config: Config -) { - if (Date.now() >= Date.UTC(2024, 2, 1, 14)) { - if (config.usage_model !== undefined) { - logger.warn( - "The `usage_model` defined in wrangler.toml is deprecated and no longer used. Visit our developer docs for details: https://developers.cloudflare.com/workers/wrangler/configuration/#usage-model" - ); - } - - // TODO: After March 1st 2024 remove the code below - return; +async function standardPricingWarning(config: Config) { + if (config.usage_model !== undefined) { + logger.warn( + "The `usage_model` defined in wrangler.toml is deprecated and no longer used. Visit our developer docs for details: https://developers.cloudflare.com/workers/wrangler/configuration/#usage-model" + ); } - - try { - const { standard, reason } = await fetchResult<{ - standard: boolean; - reason: string; - }>(`/accounts/${accountId}/workers/standard`); - - if (!standard && reason !== "enterprise without override") { - logger.log( - chalk.blue( - `🚧 New Workers Standard pricing is now available. Please visit the dashboard to view details and opt-in to new pricing: https://dash.cloudflare.com/${accountId}/workers/standard/opt-in.` - ) - ); - if (config.limits?.cpu_ms !== undefined) { - logger.warn( - "The `limits` defined in wrangler.toml can only be applied to scripts opted into Workers Standard pricing. Agree to the new pricing details to set limits for your script." - ); - } - return; - } - if (standard && config.usage_model !== undefined) { - logger.warn( - "The `usage_model` defined in wrangler.toml is no longer used because you have opted into Workers Standard pricing. Please remove this setting from your wrangler.toml and use the dashboard to configure the usage model for your script." - ); - return; - } - // Ignore 404 errors for the enablement check - } catch {} } export function versionsUploadOptions(yargs: CommonYargsArgv) { @@ -225,7 +187,7 @@ export async function versionsUploadHandler( const accountId = args.dryRun ? undefined : await requireAuth(config); - await standardPricingWarning(accountId, config); + await standardPricingWarning(config); await versionsUpload({ config, accountId,