From f02d8333cf0503ddcbbf3bc56402bdf69fc1836c Mon Sep 17 00:00:00 2001 From: Tom Riglar Date: Thu, 12 Nov 2026 16:52:28 +0000 Subject: [PATCH] fix: treat dev-api.devicecloud.dev as the dev environment The dev API is now at dev-api.devicecloud.dev. Make it the dev apiUrl and keep api.dev.devicecloud.dev as a legacy URL, so saved configs and CI flags that name it still resolve to dev. inferEnvFromApiUrl only recognised 'api.dev.', so a dev-api URL fell through to prod and the Supabase copy of an upload would target the prod project. Both hostnames now map to dev. Co-Authored-By: Claude Opus 5.5 --- src/config/environments.ts | 33 +++++++++++++++++++++----- test/unit/environments.test.ts | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 test/unit/environments.test.ts diff --git a/src/config/environments.ts b/src/config/environments.ts index 1d79cd7..07b8509 100644 --- a/src/config/environments.ts +++ b/src/config/environments.ts @@ -13,6 +13,12 @@ export type DcdEnvName = 'dev' | 'prod'; export interface DcdEnvironment { apiUrl: string; + /** + * Older API URLs that still reach this environment. A config saved by an + * earlier `dcd login`, or a `--api-url` in someone's CI, may still name one, + * so it must keep resolving to this environment. + */ + legacyApiUrls?: string[]; frontendUrl: string; supabase: { url: string; @@ -30,7 +36,8 @@ export const ENVIRONMENTS: Record = { }, }, dev: { - apiUrl: 'https://api.dev.devicecloud.dev', + apiUrl: 'https://dev-api.devicecloud.dev', + legacyApiUrls: ['https://api.dev.devicecloud.dev'], frontendUrl: 'https://dev.console.devicecloud.dev', supabase: { url: 'https://lbmsowehtjwnqlurpemb.supabase.co', @@ -39,18 +46,32 @@ export const ENVIRONMENTS: Record = { }, }; -/** Exact-match lookup of a known environment by API URL (trailing slashes ignored). */ +/** + * Exact-match lookup of a known environment by API URL, current or legacy + * (trailing slashes ignored). + */ export function findEnvByApiUrl(apiUrl: string): DcdEnvironment | undefined { const normalize = (u: string) => u.replace(/\/+$/, ''); const needle = normalize(apiUrl); - return Object.values(ENVIRONMENTS).find( - (env) => normalize(env.apiUrl) === needle, + return Object.values(ENVIRONMENTS).find((env) => + [env.apiUrl, ...(env.legacyApiUrls ?? [])].some((u) => normalize(u) === needle), ); } -/** Map a caller-supplied API URL to one of the known environments. */ +/** + * Map a caller-supplied API URL to one of the known environments. This picks + * the Supabase project uploads go to, so a dev URL must never fall through to + * prod: both dev hostnames (`dev-api.` and the older `api.dev.`) and + * localhost are dev. + */ export function inferEnvFromApiUrl(apiUrl: string): DcdEnvName { - if (apiUrl.includes('api.dev.') || apiUrl.includes('localhost')) return 'dev'; + if ( + apiUrl.includes('dev-api.') || + apiUrl.includes('api.dev.') || + apiUrl.includes('localhost') + ) { + return 'dev'; + } return 'prod'; } diff --git a/test/unit/environments.test.ts b/test/unit/environments.test.ts new file mode 100644 index 0000000..d5461ac --- /dev/null +++ b/test/unit/environments.test.ts @@ -0,0 +1,42 @@ +import { expect } from 'chai'; +import { + ENVIRONMENTS, + findEnvByApiUrl, + inferEnvFromApiUrl, + resolveFrontendUrl, +} from '../../src/config/environments.js'; + +/** + * The dev API answers on two hostnames: `dev-api.devicecloud.dev` (current) + * and `api.dev.devicecloud.dev` (older). Both must resolve to dev. A dev URL + * that fell through to prod would send uploads to the prod Supabase project. + */ +describe('environments', () => { + const DEV_URLS = [ + 'https://dev-api.devicecloud.dev', + 'https://dev-api.devicecloud.dev/', + 'https://api.dev.devicecloud.dev', + 'https://api.dev.devicecloud.dev/', + ]; + + it('treats both dev hostnames as dev', () => { + for (const url of DEV_URLS) { + expect(inferEnvFromApiUrl(url), url).to.equal('dev'); + expect(findEnvByApiUrl(url), url).to.equal(ENVIRONMENTS.dev); + expect(resolveFrontendUrl(url), url).to.equal('https://dev.console.devicecloud.dev'); + } + }); + + it('uses dev-api as the dev API URL', () => { + expect(ENVIRONMENTS.dev.apiUrl).to.equal('https://dev-api.devicecloud.dev'); + }); + + it('keeps prod as prod', () => { + expect(inferEnvFromApiUrl('https://api.devicecloud.dev')).to.equal('prod'); + expect(findEnvByApiUrl('https://api.devicecloud.dev')).to.equal(ENVIRONMENTS.prod); + }); + + it('treats localhost as dev', () => { + expect(inferEnvFromApiUrl('http://localhost:8000')).to.equal('dev'); + }); +});