diff --git a/packages/void/src/cli/platform-lifecycle.ts b/packages/void/src/cli/platform-lifecycle.ts index 9ea553695..1573782b4 100644 --- a/packages/void/src/cli/platform-lifecycle.ts +++ b/packages/void/src/cli/platform-lifecycle.ts @@ -32,6 +32,7 @@ import { PlatformCloudflareApiError, PlatformRuntimeTokenEmailPermissionError, verifyPlatformRuntimeTokenPermissions, + PlatformCloudflarePrerequisiteError, } from './platform-cloudflare.ts'; import type { CloudflareAccount } from './platform-cloudflare.ts'; import { platformCredentialHelp } from './platform-credential-label.ts'; @@ -392,6 +393,18 @@ export async function runPlatformLifecycleCommand(commandArgs: PlatformArgs): Pr ) { throw new Error('platform: The installation belongs to a different Cloudflare account.'); } + // Fail before the domain questions when Workers for Platforms is off. Planning + // repeats this read and reports permission denials together, so only the + // missing-product error stops here. + try { + await withPlatformProgress('Checking Workers for Platforms', () => + client.listDispatchNamespaces(accountId), + ); + } catch (error) { + if (error instanceof PlatformCloudflarePrerequisiteError) { + throw error; + } + } const recorded = existing?.deployment; if ( existing && diff --git a/packages/void/test/unit/platform-install-prompts.test.ts b/packages/void/test/unit/platform-install-prompts.test.ts index 063f2377a..c61f9b07c 100644 --- a/packages/void/test/unit/platform-install-prompts.test.ts +++ b/packages/void/test/unit/platform-install-prompts.test.ts @@ -90,6 +90,7 @@ beforeEach(() => { { id: 'account', name: 'Account' }, ]); vi.spyOn(PlatformCloudflareClient.prototype, 'listHyperdriveConfigs').mockResolvedValue([]); + vi.spyOn(PlatformCloudflareClient.prototype, 'listDispatchNamespaces').mockResolvedValue([]); mocks.text.mockImplementation(async ({ defaultValue }) => defaultValue ?? ''); mocks.select.mockImplementation(async ({ message, options }) => message === 'How should Void set up Hyperdrive?' || diff --git a/packages/void/test/unit/platform-setup-flow.test.ts b/packages/void/test/unit/platform-setup-flow.test.ts index e6f60383e..a7efefa40 100644 --- a/packages/void/test/unit/platform-setup-flow.test.ts +++ b/packages/void/test/unit/platform-setup-flow.test.ts @@ -18,6 +18,7 @@ import { parsePlatformArgs } from '../../src/cli/platform-args.ts'; import { PlatformCloudflareApiError, PlatformCloudflareClient, + PlatformCloudflarePrerequisiteError, verifyPlatformRuntimeTokenEmailPermissions, } from '../../src/cli/platform-cloudflare.ts'; import { formatPlatformCredentialDenials } from '../../src/cli/platform-credential-label.ts'; @@ -179,6 +180,7 @@ beforeEach(() => { { id: 'account', name: 'Account' }, ]); vi.spyOn(PlatformCloudflareClient.prototype, 'listHyperdriveConfigs').mockResolvedValue([]); + vi.spyOn(PlatformCloudflareClient.prototype, 'listDispatchNamespaces').mockResolvedValue([]); mocks.confirm.mockResolvedValue(true); mocks.spinner.mockReturnValue(mocks.progress); mocks.select.mockImplementation(async ({ message, options }) => @@ -393,6 +395,40 @@ function accessManagementPlan() { return [...args, '--login-methods', 'cloudflare-access', '--access-protection']; } +describe('early Workers for Platforms check', () => { + it('stops right after the account is chosen when Workers for Platforms is off', async () => { + const listNamespaces = vi.mocked(PlatformCloudflareClient.prototype.listDispatchNamespaces); + listNamespaces.mockRejectedValue( + new PlatformCloudflarePrerequisiteError( + 'Cloudflare Workers for Platforms requires a paid subscription for this account (code 10121).', + new PlatformCloudflareApiError( + 'denied', + 403, + '/accounts/account/workers/dispatch/namespaces', + ), + ), + ); + + await expect( + runPlatformLifecycleCommand( + parsePlatformArgs(['install', '--name', 'test-setup', '--account', 'account']), + ), + ).rejects.toThrow('Workers for Platforms requires a paid subscription'); + + expect(listNamespaces).toHaveBeenCalledWith('account'); + expect(mocks.progress.start).toHaveBeenLastCalledWith('Checking Workers for Platforms'); + const [namespaceRead] = listNamespaces.mock.invocationCallOrder; + expect(mocks.progress.start.mock.invocationCallOrder.at(-1)).toBeLessThan(namespaceRead); + expect(mocks.progress.clear.mock.invocationCallOrder.at(-1)).toBeGreaterThan(namespaceRead); + expect(mocks.select).not.toHaveBeenCalledWith( + expect.objectContaining({ message: 'Where should your apps live?' }), + ); + expect(mocks.plan).not.toHaveBeenCalled(); + expect(listSavedPlatformInstallPlans()).toEqual([]); + expect(listInstallationManifests()).toEqual([]); + }); +}); + describe('platform credential onboarding', () => { it('shows discovery progress around remote reads without covering authentication', async () => { const steps = vi.spyOn(log, 'step').mockImplementation(() => {});