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