1#![allow(missing_docs)]
12
13pub mod cmd;
14pub mod config;
15pub mod envelope;
16pub mod exit;
17pub mod trace;
18pub mod wallet_lock;
19
20pub use config::Config;
21pub use envelope::{Envelope, ErrorPayload};
22pub use exit::ExitCode;
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, clap::ValueEnum)]
27pub enum Environment {
28 Prod,
29 Staging,
30 Local,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum Network {
40 Mainnet,
41 Testnet,
42}
43
44impl Network {
45 pub fn as_str(self) -> &'static str {
46 match self {
47 Self::Mainnet => "mainnet",
48 Self::Testnet => "testnet",
49 }
50 }
51}
52
53impl Environment {
54 pub const ALL: &'static [Environment] = &[Self::Prod, Self::Staging, Self::Local];
58
59 pub fn as_str(self) -> &'static str {
60 match self {
61 Self::Prod => "production",
62 Self::Staging => "staging",
63 Self::Local => "local",
64 }
65 }
66
67 pub fn api_base(self) -> &'static str {
75 match self {
76 Self::Prod => "https://api.taskfast.app",
77 Self::Staging => "https://staging.api.taskfast.app",
78 Self::Local => "http://127.0.0.1:4000",
79 }
80 }
81
82 pub fn network(self) -> Network {
87 match self {
88 Self::Prod => Network::Mainnet,
89 Self::Staging => Network::Testnet,
90 Self::Local => Network::Testnet,
91 }
92 }
93}
94
95pub fn well_known_api_bases() -> impl Iterator<Item = &'static str> {
102 Environment::ALL.iter().map(|e| e.api_base())
103}
104
105pub fn is_well_known_api_base(url: &str) -> bool {
109 let trimmed = url.trim_end_matches('/');
110 well_known_api_bases().any(|w| w.trim_end_matches('/') == trimmed)
111}
112
113pub fn accounts_url(api_base: &str) -> String {
120 match url::Url::parse(api_base) {
121 Ok(mut u) => {
122 let rewritten = u.host_str().and_then(strip_api_label);
123 if let Some(h) = rewritten {
124 let _ = u.set_host(Some(&h));
125 }
126 u.set_path("/accounts");
127 u.set_query(None);
128 u.set_fragment(Some("personal-api-key"));
129 u.to_string()
130 }
131 Err(_) => format!(
133 "{}/accounts#personal-api-key",
134 api_base.trim_end_matches('/')
135 ),
136 }
137}
138
139fn strip_api_label(host: &str) -> Option<String> {
144 if let Some(rest) = host.strip_prefix("api.") {
145 return Some(rest.to_owned());
146 }
147 if host.contains(".api.") {
149 return Some(host.replacen(".api.", ".", 1));
150 }
151 None
152}
153
154#[cfg(test)]
155mod tests {
156 use super::{accounts_url, is_well_known_api_base, well_known_api_bases, Environment, Network};
157 use std::collections::HashSet;
158
159 #[test]
160 fn all_covers_every_variant() {
161 let n = Environment::ALL.len();
166 let unique: HashSet<_> = Environment::ALL.iter().copied().collect();
167 assert_eq!(unique.len(), n, "Environment::ALL has duplicates");
168 for env in Environment::ALL {
169 let _ = env.as_str();
172 let _ = env.api_base();
173 let _ = env.network();
174 }
175 }
176
177 #[test]
178 fn each_env_has_unique_api_base() {
179 let bases: HashSet<_> = Environment::ALL.iter().map(|e| e.api_base()).collect();
180 assert_eq!(
181 bases.len(),
182 Environment::ALL.len(),
183 "two envs share an api_base"
184 );
185 }
186
187 #[test]
188 fn env_api_base_table_is_frozen() {
189 assert_eq!(Environment::Prod.api_base(), "https://api.taskfast.app");
191 assert_eq!(
192 Environment::Staging.api_base(),
193 "https://staging.api.taskfast.app"
194 );
195 assert_eq!(Environment::Local.api_base(), "http://127.0.0.1:4000");
196 }
197
198 #[test]
199 fn env_network_table_is_frozen() {
200 assert_eq!(Environment::Prod.network(), Network::Mainnet);
201 assert_eq!(Environment::Staging.network(), Network::Testnet);
202 assert_eq!(Environment::Local.network(), Network::Testnet);
203 }
204
205 #[test]
206 fn well_known_api_bases_cover_every_environment_default() {
207 for env in Environment::ALL {
208 let url = env.api_base();
209 assert!(
210 is_well_known_api_base(url),
211 "api_base for {env:?} ({url}) must be in well_known_api_bases"
212 );
213 }
214 }
215
216 #[test]
217 fn is_well_known_api_base_accepts_exact_defaults() {
218 for url in well_known_api_bases() {
219 assert!(is_well_known_api_base(url), "expected well-known: {url}");
220 }
221 }
222
223 #[test]
224 fn is_well_known_api_base_tolerates_trailing_slash() {
225 assert!(is_well_known_api_base("https://api.taskfast.app/"));
226 assert!(is_well_known_api_base("http://127.0.0.1:4000/"));
227 }
228
229 #[test]
230 fn is_well_known_api_base_rejects_attacker_hosts() {
231 assert!(!is_well_known_api_base("https://evil.example"));
232 assert!(!is_well_known_api_base(
233 "https://api.taskfast.app.evil.example"
234 ));
235 assert!(!is_well_known_api_base("http://api.taskfast.app"));
236 assert!(!is_well_known_api_base("https://staging.taskfast.app"));
237 }
238
239 #[test]
240 fn accounts_url_strips_api_prefix_prod() {
241 assert_eq!(
242 accounts_url("https://api.taskfast.app"),
243 "https://taskfast.app/accounts#personal-api-key"
244 );
245 }
246
247 #[test]
248 fn accounts_url_strips_api_prefix_staging() {
249 assert_eq!(
250 accounts_url("https://staging.api.taskfast.app"),
251 "https://staging.taskfast.app/accounts#personal-api-key"
252 );
253 }
254
255 #[test]
256 fn accounts_url_passthrough_localhost_with_port() {
257 assert_eq!(
258 accounts_url("http://127.0.0.1:4000"),
259 "http://127.0.0.1:4000/accounts#personal-api-key"
260 );
261 }
262
263 #[test]
264 fn accounts_url_passthrough_bare_domain() {
265 assert_eq!(
266 accounts_url("https://taskfast.app"),
267 "https://taskfast.app/accounts#personal-api-key"
268 );
269 }
270
271 #[test]
272 fn accounts_url_replaces_existing_path_query_and_fragment() {
273 assert_eq!(
274 accounts_url("https://api.taskfast.app/v1?x=1#old-section"),
275 "https://taskfast.app/accounts#personal-api-key"
276 );
277 }
278
279 #[test]
280 fn accounts_url_malformed_falls_back_to_concat() {
281 assert_eq!(
282 accounts_url("not a url"),
283 "not a url/accounts#personal-api-key"
284 );
285 }
286}