76 lines
2.7 KiB
Dart
76 lines
2.7 KiB
Dart
import 'package:cyberhybridhub_server/alpaca/alpaca_env.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
void main() {
|
|
group('AlpacaEnv.fromMap', () {
|
|
test('defaults to paper trading and data URLs', () {
|
|
final AlpacaEnv env = AlpacaEnv.fromMap(<String, String>{});
|
|
expect(env.tradingBaseUrl, AlpacaEnv.defaultPaperTradingUrl);
|
|
expect(env.dataBaseUrl, AlpacaEnv.defaultDataUrl);
|
|
expect(env.dataFeed, 'iex');
|
|
expect(env.allowLive, isFalse);
|
|
expect(env.isPaperUrl, isTrue);
|
|
});
|
|
|
|
test('assertPaperOnly blocks live host when allowLive is false', () {
|
|
final AlpacaEnv env = AlpacaEnv.fromMap(<String, String>{
|
|
'ALPACA_TRADING_BASE_URL': 'https://api.alpaca.markets',
|
|
'ALPACA_ALLOW_LIVE': 'false',
|
|
});
|
|
expect(env.assertPaperOnly, throwsStateError);
|
|
});
|
|
|
|
test('assertPaperOnly allows live host when allowLive is true', () {
|
|
final AlpacaEnv env = AlpacaEnv.fromMap(<String, String>{
|
|
'ALPACA_TRADING_BASE_URL': 'https://api.alpaca.markets',
|
|
'ALPACA_ALLOW_LIVE': 'true',
|
|
});
|
|
expect(() => env.assertPaperOnly(), returnsNormally);
|
|
});
|
|
|
|
test('requireCredentials throws when keys missing', () {
|
|
final AlpacaEnv env = AlpacaEnv.fromMap(<String, String>{});
|
|
expect(env.hasCredentials, isFalse);
|
|
expect(env.requireCredentials, throwsStateError);
|
|
});
|
|
|
|
test('requireCredentials passes when keys present', () {
|
|
final AlpacaEnv env = AlpacaEnv.fromMap(<String, String>{
|
|
'ALPACA_API_KEY_ID': 'key',
|
|
'ALPACA_API_SECRET_KEY': 'secret',
|
|
});
|
|
expect(() => env.requireCredentials(), returnsNormally);
|
|
});
|
|
|
|
test('strips trailing /v2 and trailing slashes from base URLs', () {
|
|
final List<({String input, String expected})> cases =
|
|
<({String input, String expected})>[
|
|
(
|
|
input: 'https://paper-api.alpaca.markets/v2',
|
|
expected: 'https://paper-api.alpaca.markets',
|
|
),
|
|
(
|
|
input: 'https://paper-api.alpaca.markets/v2/',
|
|
expected: 'https://paper-api.alpaca.markets',
|
|
),
|
|
(
|
|
input: 'https://paper-api.alpaca.markets/',
|
|
expected: 'https://paper-api.alpaca.markets',
|
|
),
|
|
(
|
|
input: 'https://paper-api.alpaca.markets',
|
|
expected: 'https://paper-api.alpaca.markets',
|
|
),
|
|
];
|
|
for (final ({String input, String expected}) c in cases) {
|
|
final AlpacaEnv env = AlpacaEnv.fromMap(<String, String>{
|
|
'ALPACA_TRADING_BASE_URL': c.input,
|
|
'ALPACA_DATA_BASE_URL': c.input,
|
|
});
|
|
expect(env.tradingBaseUrl, c.expected, reason: 'input=${c.input}');
|
|
expect(env.dataBaseUrl, c.expected, reason: 'input=${c.input}');
|
|
}
|
|
});
|
|
});
|
|
}
|