From 702801b636d303559358e05995746044a5845f03 Mon Sep 17 00:00:00 2001 From: Amaury Ricardo Date: Tue, 22 Sep 2026 16:50:49 -0300 Subject: [PATCH] fix(domain): fall back to unsuffixed API_URL in EnvConfig.apiUrl EnvConfig.apiUrl only read API_URL_, but the committed per-flavor file env/.dev (the one init.dart loads) defines plain API_URL, so the Dio base URL was always empty. Read the suffixed key first and fall back to API_URL, supporting both the per-flavor files and the single-file layout of env/.env.example. Adds the first domain unit tests (EnvConfig.apiUrl). Co-Authored-By: Claude Opus 5.5 (1M context) --- modules/domain/lib/env/env_config.dart | 7 +++- modules/domain/test/env/env_config_test.dart | 42 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 modules/domain/test/env/env_config_test.dart diff --git a/modules/domain/lib/env/env_config.dart b/modules/domain/lib/env/env_config.dart index e6e83f6..4088514 100644 --- a/modules/domain/lib/env/env_config.dart +++ b/modules/domain/lib/env/env_config.dart @@ -9,5 +9,10 @@ class EnvConfig { static String get envConfigFile => 'env/.env.example'; - static String get apiUrl => dotenv.env['API_URL_$env']?.toString() ?? ''; + /// Supports both env file layouts: + /// - one file per flavor (e.g. `env/.dev`) with an unsuffixed `API_URL`; + /// - a single file with suffixed keys (`API_URL_DEV`, `API_URL_QA`, + /// `API_URL_PROD`) as in `env/.env.example`. A suffixed key wins. + static String get apiUrl => + dotenv.env['API_URL_$env'] ?? dotenv.env['API_URL'] ?? ''; } diff --git a/modules/domain/test/env/env_config_test.dart b/modules/domain/test/env/env_config_test.dart new file mode 100644 index 0000000..4506480 --- /dev/null +++ b/modules/domain/test/env/env_config_test.dart @@ -0,0 +1,42 @@ +import 'package:domain/env/env_config.dart'; +import 'package:flutter_dotenv/flutter_dotenv.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + tearDown(() => EnvConfig.env = EnvConfig.kDevEnv); + + group('EnvConfig.apiUrl', () { + test('reads the unsuffixed API_URL of a per-flavor file', () { + dotenv.testLoad(fileInput: 'API_URL=https://dev.example.com'); + EnvConfig.env = EnvConfig.kDevEnv; + + expect(EnvConfig.apiUrl, 'https://dev.example.com'); + }); + + test('reads the key suffixed with the active flavor', () { + dotenv.testLoad( + fileInput: 'API_URL_DEV=https://dev.example.com\n' + 'API_URL_QA=https://qa.example.com', + ); + EnvConfig.env = EnvConfig.kQaEnv; + + expect(EnvConfig.apiUrl, 'https://qa.example.com'); + }); + + test('prefers the suffixed key over the unsuffixed one', () { + dotenv.testLoad( + fileInput: 'API_URL=https://fallback.example.com\n' + 'API_URL_PROD=https://prod.example.com', + ); + EnvConfig.env = EnvConfig.kProdEnv; + + expect(EnvConfig.apiUrl, 'https://prod.example.com'); + }); + + test('is empty when no API_URL key is defined', () { + dotenv.testLoad(fileInput: 'ENV=dev'); + + expect(EnvConfig.apiUrl, isEmpty); + }); + }); +}