Skip to main content

snip_sync/
lib.rs

1// The `format!("{}", var)` patterns below are inherited from the
2// pre-existing main.rs implementation. They were not flagged by clippy
3// when the code lived in a binary crate's `#[cfg(test)] mod tests`
4// (binary test targets are not always checked by `cargo clippy
5// --all-targets`), but moving the code into the library makes them
6// visible to lib test compilation. The lint suppression keeps the
7// refactor minimal; a follow-up should inline the args.
8//
9// Similarly, `RateLimiter::new()` and a few other constructors predate
10// the refactor and never carried a `Default` impl. Suppressing
11// `new_without_default` here keeps the diff focused.
12#![allow(clippy::uninlined_format_args, clippy::new_without_default)]
13
14pub mod bootstrap;
15pub mod cert;
16pub mod cli;
17pub mod db;
18pub mod editor;
19pub mod http;
20pub mod metrics;
21pub mod orchestration;
22pub mod paths;
23pub mod premade;
24pub mod process;
25pub mod rate_limiter;
26pub mod server_lock;
27pub mod startup;
28#[cfg(any(test, feature = "test-helpers"))]
29pub mod test_helpers;
30#[cfg(any(test, feature = "test-helpers"))]
31pub mod test_observer;
32pub mod toml_escapes;
33
34pub use db::Database;
35pub use metrics::Metrics;
36pub use premade::PremadeManager;
37pub use rate_limiter::RateLimiter;
38
39use prost::Message;
40use serde::Deserialize;
41use snip_proto::{
42    CreateLibraryRequest, CreateLibraryResponse, DeleteLibraryRequest, DeleteLibraryResponse,
43    GetPremadeLibraryRequest, GetPremadeLibraryResponse, GetSnippetsRequest, HealthRequest,
44    HealthResponse, Library, ListLibrariesRequest, ListLibrariesResponse,
45    ListPremadeLibrariesRequest, ListPremadeLibrariesResponse,
46    PremadeLibrary as ProtoPremadeLibrary, PushSnippetsRequest, PushSnippetsResponse,
47    RegisterRequest, RegisterResponse, SearchPremadeLibrariesRequest,
48    SearchPremadeLibrariesResponse, Snippet as ProtoSnippet, SnippetList, SyncRequest,
49    SyncResponse, snippet_sync_server::SnippetSync,
50};
51use std::path::PathBuf;
52use std::sync::Arc;
53use std::time::Duration;
54use tonic::{Request, Response, Status};
55
56/// Extract the API key from a gRPC request's `authorization` metadata.
57/// Falls back to the body `api_key` field for backward compatibility.
58///
59/// The auth-scheme token is case-insensitive (RFC 9110 §11.1), so `Bearer`,
60/// `bearer`, and `BEARER` are all accepted. Only the scheme is matched
61/// leniently; the credential itself is used verbatim (minus surrounding
62/// whitespace) and compared exactly by the authentication path.
63pub fn extract_api_key<T>(request: &Request<T>, body_api_key: &str) -> String {
64    request
65        .metadata()
66        .get("authorization")
67        .and_then(|v| v.to_str().ok())
68        .and_then(|v| {
69            let (scheme, credential) = v.split_once(' ')?;
70            scheme
71                .eq_ignore_ascii_case("bearer")
72                .then(|| credential.trim().to_owned())
73        })
74        .filter(|v| !v.is_empty())
75        .unwrap_or_else(|| body_api_key.to_string())
76}
77
78/// Strip a trailing `:port` suffix from a peer address.
79///
80/// The value can be attacker-influenced (it comes from the `x-forwarded-for`
81/// header behind a trusted proxy), so malformed input is returned unchanged
82/// rather than panicking — the result is only ever used as a rate-limit
83/// bucket key.
84fn strip_port(addr: &str) -> String {
85    if let Some(close_bracket) = addr.find("]:") {
86        // Only a bracketed IPv6 literal (`[::1]:443`) has a `]:` separator.
87        // Anything else is malformed: slicing it would produce a bogus
88        // address, and slicing at offset 0 would panic outright.
89        if close_bracket > 0 && addr.starts_with('[') {
90            return addr[1..close_bracket].to_string();
91        }
92        return addr.to_string();
93    }
94    // Bare IPv6 addresses contain multiple colons but no brackets.
95    // They have no port to strip, so return as-is.
96    if !addr.starts_with('[') && addr.matches(':').count() > 1 {
97        return addr.to_string();
98    }
99    if let Some(colon_pos) = addr.rfind(':')
100        && !addr.starts_with('[')
101    {
102        return addr[..colon_pos].to_string();
103    }
104    addr.to_string()
105}
106
107pub const DEFAULT_MAX_COMMAND_LENGTH: usize = 1024;
108// Encrypted records carry description + command + tags as one AES-256-GCM
109// payload in `command`, base64-expanded (~1.37x the plaintext JSON). The cap
110// is deliberately far above `max_command_length` so ordinary snippets are
111// unaffected; its job is to make storage bounded and to guarantee a single
112// record can always be transported, since anything larger than the gRPC
113// message ceiling could be stored but never downloaded again.
114pub const DEFAULT_MAX_ENCRYPTED_PAYLOAD_LENGTH: usize = 1024 * 1024; // 1 MiB
115pub const DEFAULT_MAX_DESCRIPTION_LENGTH: usize = 1024;
116pub const DEFAULT_MAX_TAGS: usize = 50;
117pub const DEFAULT_MAX_TAG_LENGTH: usize = 100;
118pub const DEFAULT_MAX_ID_LENGTH: usize = 128;
119pub const DEFAULT_MAX_DEVICE_ID_LENGTH: usize = 128;
120pub const DEFAULT_MAX_API_KEY_LENGTH: usize = 512;
121pub const DEFAULT_MAX_SYNC_SNIPPETS: usize = 10000;
122pub const DEFAULT_REQUEST_TIMEOUT_SECS: u64 = 30;
123pub const DEFAULT_RATE_LIMIT_PER_MINUTE: u32 = 120;
124/// Global ceiling on successful account creations per minute, independent of
125/// the caller's address. The per-peer `register:<peer>` budget alone lets an
126/// attacker with many addresses grow the user table without bound.
127pub const DEFAULT_REGISTER_PER_MINUTE: u32 = 10;
128/// Global ceiling on **failed** `/metrics` authentications per minute. Correct
129/// credentials never consume it, so an attacker cannot deny the operator their
130/// own scrape by burning the budget.
131pub const DEFAULT_METRICS_AUTH_FAILURES_PER_MINUTE: u32 = 10;
132/// Multiplier applied to the per-peer budget for requests that have **not**
133/// yet authenticated. The pre-authentication budget only has to bound the cost
134/// of an API-key lookup, so it is generous by design: an unauthenticated
135/// caller must not be able to exhaust the budget that legitimate callers
136/// spend on the authenticated surface.
137pub const PRE_AUTH_RATE_LIMIT_MULTIPLIER: u32 = 10;
138pub const DEFAULT_DB_MAX_CONNECTIONS: u32 = 5;
139pub const DEFAULT_GRPC_MAX_MESSAGE_SIZE: u32 = 4 * 1024 * 1024; // 4 MiB
140/// Hard upper bound for any single request's page size. Endpoints choose
141/// their own default page size when the client omits one (`sync` → 1000,
142/// `get_snippets` → 100, `list_libraries` → 50); all are clamped here.
143pub const MAX_REQUEST_LIMIT: i32 = 1000;
144
145/// Bytes held back from the gRPC encode ceiling for the response envelope
146/// (status message, `skipped_ids`, scalar fields, and protobuf framing).
147const RESPONSE_ENVELOPE_RESERVE_BYTES: usize = 64 * 1024;
148
149/// Encoded-byte budget available to the `snippets` array of one response,
150/// given the configured gRPC ceiling and any other variable-length payload the
151/// envelope already carries.
152fn page_byte_ceiling(grpc_ceiling: usize, envelope_bytes: usize) -> usize {
153    grpc_ceiling
154        .saturating_sub(RESPONSE_ENVELOPE_RESERVE_BYTES)
155        .saturating_sub(envelope_bytes)
156}
157
158/// Drop trailing rows from a response page until its encoded size fits
159/// `ceiling`; returns whether anything was dropped.
160///
161/// Downloads are paged by *row count*, but tonic hard-fails an over-size
162/// encoding with `OUT_OF_RANGE` instead of truncating. Without this bound a
163/// library whose rows happen to be large can be pushed successfully and then
164/// never downloaded again, by any client, for any page size. The caller sets
165/// `has_more` from the return value so the remainder is still fetched.
166fn fit_page_to_byte_ceiling(snippets: &mut Vec<ProtoSnippet>, ceiling: usize) -> bool {
167    let mut used = 0usize;
168    let mut kept = 0usize;
169    for snippet in snippets.iter() {
170        let len = snippet.encoded_len();
171        // Always keep at least one row. A single record larger than the whole
172        // ceiling cannot be made to fit, and returning an empty page would
173        // stall the client's pagination loop rather than make progress.
174        if kept > 0 && used.saturating_add(len) > ceiling {
175            break;
176        }
177        used = used.saturating_add(len);
178        kept += 1;
179    }
180    let truncated = kept < snippets.len();
181    if truncated {
182        tracing::info!(
183            dropped = snippets.len() - kept,
184            kept_bytes = used,
185            ceiling_bytes = ceiling,
186            "Response page trimmed to fit the gRPC encode ceiling"
187        );
188        snippets.truncate(kept);
189    }
190    truncated
191}
192
193#[derive(Deserialize, Default)]
194pub struct ConfigFile {
195    pub server: Option<ServerConfig>,
196}
197
198#[derive(Deserialize, Default)]
199pub struct ServerConfig {
200    pub grpc_host: Option<String>,
201    pub grpc_port: Option<u16>,
202    pub http_host: Option<String>,
203    pub http_port: Option<u16>,
204    pub database: Option<DatabaseConfig>,
205    pub premade: Option<PremadeConfig>,
206    pub limits: Option<LimitsConfig>,
207    pub rate_limit: Option<RateLimitConfig>,
208    pub metrics: Option<MetricsConfig>,
209    pub cors: Option<CorsConfig>,
210}
211
212#[derive(Deserialize, Default)]
213pub struct DatabaseConfig {
214    pub path: Option<String>,
215    pub max_connections: Option<u32>,
216}
217
218#[derive(Deserialize, Default)]
219pub struct PremadeConfig {
220    pub directory: Option<String>,
221}
222
223#[derive(Deserialize, Default)]
224pub struct LimitsConfig {
225    pub max_command_length: Option<usize>,
226    pub max_encrypted_payload_length: Option<usize>,
227    pub max_description_length: Option<usize>,
228    pub max_tags: Option<usize>,
229    pub max_tag_length: Option<usize>,
230    pub max_id_length: Option<usize>,
231    pub max_device_id_length: Option<usize>,
232    pub max_api_key_length: Option<usize>,
233    pub request_timeout_secs: Option<u64>,
234    pub grpc_max_message_size: Option<u32>,
235}
236
237#[derive(Deserialize, Default)]
238pub struct RateLimitConfig {
239    pub requests_per_minute: Option<u32>,
240    /// Global per-minute ceiling on account creation (all peers combined).
241    pub register_per_minute: Option<u32>,
242    /// Global per-minute ceiling on failed `/metrics` authentications.
243    pub metrics_auth_failures_per_minute: Option<u32>,
244    pub trusted_proxies: Option<Vec<String>>,
245}
246
247#[derive(Deserialize, Default)]
248pub struct MetricsConfig {
249    pub username: Option<String>,
250    pub password: Option<String>,
251}
252
253#[derive(Deserialize, Default)]
254pub struct CorsConfig {
255    pub allowed_origins: Option<String>,
256}
257
258#[derive(Clone, Debug)]
259pub struct Config {
260    pub grpc_host: String,
261    pub grpc_port: u16,
262    pub http_host: String,
263    pub http_port: u16,
264    pub db_path: String,
265    pub db_max_connections: u32,
266    pub premade_dir: PathBuf,
267    pub max_command_length: usize,
268    pub max_encrypted_payload_length: usize,
269    pub max_description_length: usize,
270    pub max_tags: usize,
271    pub max_tag_length: usize,
272    pub max_id_length: usize,
273    pub max_device_id_length: usize,
274    pub max_api_key_length: usize,
275    pub request_timeout_secs: u64,
276    pub grpc_max_message_size: u32,
277    pub rate_limit_per_minute: u32,
278    /// Global per-minute ceiling on account creation, across all peers.
279    pub register_per_minute: u32,
280    /// Global per-minute ceiling on failed `/metrics` authentications.
281    pub metrics_auth_failures_per_minute: u32,
282    pub trusted_proxies: Vec<String>,
283    pub metrics_username: Option<String>,
284    pub metrics_password: Option<String>,
285    pub cors_allowed_origins: Vec<String>,
286}
287
288#[derive(Debug, thiserror::Error)]
289pub enum ConfigLoadError {
290    #[error("failed to read configuration file {path}: {source}")]
291    Read {
292        path: PathBuf,
293        source: std::io::Error,
294    },
295    #[error("failed to parse configuration file {path}: {source}")]
296    Parse {
297        path: PathBuf,
298        source: toml::de::Error,
299    },
300    #[error("invalid environment variable {name}={value:?}: {reason}")]
301    InvalidEnvironment {
302        name: &'static str,
303        value: String,
304        reason: String,
305    },
306    #[error("invalid configuration value {name}={value:?}: {reason}")]
307    InvalidRange {
308        name: &'static str,
309        value: String,
310        reason: String,
311    },
312}
313
314/// Parse an optional environment variable, returning the typed value if present and valid.
315///
316/// Missing variable → `Ok(None)`. Present and valid → `Ok(Some(value))`.
317/// Present and invalid → `Err(ConfigLoadError::InvalidEnvironment)`.
318pub fn parse_env<T>(
319    env: &impl Fn(&str) -> Option<String>,
320    name: &'static str,
321) -> Result<Option<T>, ConfigLoadError>
322where
323    T: std::str::FromStr,
324    T::Err: std::fmt::Display,
325{
326    match env(name) {
327        Some(val) => match val.parse::<T>() {
328            Ok(parsed) => Ok(Some(parsed)),
329            Err(e) => Err(ConfigLoadError::InvalidEnvironment {
330                name,
331                value: val,
332                reason: e.to_string(),
333            }),
334        },
335        None => Ok(None),
336    }
337}
338
339/// Parse an optional boolean environment variable.
340///
341/// Accepted true values (case-insensitive): `true`, `1`, `yes`, `on`
342/// Accepted false values (case-insensitive): `false`, `0`, `no`, `off`
343///
344/// Missing variable → `Ok(None)`. Present and valid → `Ok(Some(bool))`.
345/// Present and invalid → `Err(ConfigLoadError::InvalidEnvironment)`.
346pub fn parse_bool_env(
347    env: &impl Fn(&str) -> Option<String>,
348    name: &'static str,
349) -> Result<Option<bool>, ConfigLoadError> {
350    match env(name) {
351        Some(val) => match val.to_lowercase().as_str() {
352            "true" | "1" | "yes" | "on" => Ok(Some(true)),
353            "false" | "0" | "no" | "off" => Ok(Some(false)),
354            _ => Err(ConfigLoadError::InvalidEnvironment {
355                name,
356                value: val,
357                reason: "expected one of: true, 1, yes, on, false, 0, no, off".to_string(),
358            }),
359        },
360        None => Ok(None),
361    }
362}
363
364/// Warn if a config file containing credentials is world-readable.
365///
366/// On Unix, a world-readable config file (mode bits `0o004` set) allows
367/// any local user to read plaintext metrics credentials.  This helper
368/// logs a warning so the operator can `chmod 600` the file.
369#[cfg(unix)]
370fn check_config_permissions(path: &std::path::Path) {
371    use std::os::unix::fs::PermissionsExt;
372    match std::fs::metadata(path) {
373        Ok(meta) => {
374            let mode = meta.permissions().mode() & 0o777;
375            if mode & 0o004 != 0 {
376                tracing::warn!(
377                    "Config file {} is world-readable (mode {:#05o}); \
378                     other local users can read metrics credentials. \
379                     Run: chmod 600 {}",
380                    path.display(),
381                    mode,
382                    path.display()
383                );
384            }
385        }
386        Err(e) => {
387            tracing::debug!("Unable to check permissions on {}: {}", path.display(), e);
388        }
389    }
390}
391
392impl Config {
393    pub fn load() -> Result<Self, ConfigLoadError> {
394        let config_path = paths::config_path();
395        Self::load_from(&config_path, &|name| std::env::var(name).ok())
396    }
397
398    fn load_from(
399        config_path: &std::path::Path,
400        env: &impl Fn(&str) -> Option<String>,
401    ) -> Result<Self, ConfigLoadError> {
402        let config_file = match std::fs::read_to_string(config_path) {
403            Ok(content) => {
404                let parsed: ConfigFile =
405                    toml::from_str(&content).map_err(|source| ConfigLoadError::Parse {
406                        path: config_path.to_path_buf(),
407                        source,
408                    })?;
409
410                #[cfg(unix)]
411                if let Some(server) = &parsed.server
412                    && server
413                        .metrics
414                        .as_ref()
415                        .is_some_and(|m| m.username.is_some() || m.password.is_some())
416                {
417                    check_config_permissions(config_path);
418                }
419
420                parsed
421            }
422            Err(e) if e.kind() == std::io::ErrorKind::NotFound => ConfigFile::default(),
423            Err(source) => {
424                return Err(ConfigLoadError::Read {
425                    path: config_path.to_path_buf(),
426                    source,
427                });
428            }
429        };
430
431        let server = config_file.server.unwrap_or_default();
432
433        let config = Self {
434            grpc_host: env("GRPC_HOST")
435                .or_else(|| server.grpc_host.clone())
436                .unwrap_or_else(|| "127.0.0.1".to_string()),
437            grpc_port: parse_env(env, "GRPC_PORT")?
438                .or(server.grpc_port)
439                .unwrap_or(50051),
440            http_host: env("HTTP_HOST")
441                .or_else(|| server.http_host.clone())
442                .unwrap_or_else(|| "127.0.0.1".to_string()),
443            http_port: parse_env(env, "HTTP_PORT")?
444                .or(server.http_port)
445                .unwrap_or(50050),
446            db_path: env("DATABASE_URL")
447                .or_else(|| server.database.as_ref().and_then(|d| d.path.clone()))
448                .unwrap_or_else(|| paths::default_db_path().to_string_lossy().into_owned()),
449            db_max_connections: parse_env(env, "DB_MAX_CONNECTIONS")?
450                .or(server.database.as_ref().and_then(|d| d.max_connections))
451                .unwrap_or(DEFAULT_DB_MAX_CONNECTIONS),
452            premade_dir: env("PREMADE_DIR")
453                .map(PathBuf::from)
454                .or_else(|| {
455                    server
456                        .premade
457                        .as_ref()
458                        .and_then(|p| p.directory.clone())
459                        .map(PathBuf::from)
460                })
461                .unwrap_or_else(paths::default_premade_dir),
462            max_command_length: parse_env(env, "MAX_COMMAND_LENGTH")?
463                .or(server.limits.as_ref().and_then(|l| l.max_command_length))
464                .unwrap_or(DEFAULT_MAX_COMMAND_LENGTH),
465            max_encrypted_payload_length: parse_env(env, "MAX_ENCRYPTED_PAYLOAD_LENGTH")?
466                .or(server
467                    .limits
468                    .as_ref()
469                    .and_then(|l| l.max_encrypted_payload_length))
470                .unwrap_or(DEFAULT_MAX_ENCRYPTED_PAYLOAD_LENGTH),
471            max_description_length: parse_env(env, "MAX_DESCRIPTION_LENGTH")?
472                .or(server
473                    .limits
474                    .as_ref()
475                    .and_then(|l| l.max_description_length))
476                .unwrap_or(DEFAULT_MAX_DESCRIPTION_LENGTH),
477            max_tags: parse_env(env, "MAX_TAGS")?
478                .or(server.limits.as_ref().and_then(|l| l.max_tags))
479                .unwrap_or(DEFAULT_MAX_TAGS),
480            max_tag_length: parse_env(env, "MAX_TAG_LENGTH")?
481                .or(server.limits.as_ref().and_then(|l| l.max_tag_length))
482                .unwrap_or(DEFAULT_MAX_TAG_LENGTH),
483            max_id_length: parse_env(env, "MAX_ID_LENGTH")?
484                .or(server.limits.as_ref().and_then(|l| l.max_id_length))
485                .unwrap_or(DEFAULT_MAX_ID_LENGTH),
486            max_device_id_length: parse_env(env, "MAX_DEVICE_ID_LENGTH")?
487                .or(server.limits.as_ref().and_then(|l| l.max_device_id_length))
488                .unwrap_or(DEFAULT_MAX_DEVICE_ID_LENGTH),
489            max_api_key_length: parse_env(env, "MAX_API_KEY_LENGTH")?
490                .or(server.limits.as_ref().and_then(|l| l.max_api_key_length))
491                .unwrap_or(DEFAULT_MAX_API_KEY_LENGTH),
492            request_timeout_secs: parse_env(env, "REQUEST_TIMEOUT_SECS")?
493                .or(server.limits.as_ref().and_then(|l| l.request_timeout_secs))
494                .unwrap_or(DEFAULT_REQUEST_TIMEOUT_SECS),
495            grpc_max_message_size: parse_env(env, "GRPC_MAX_MESSAGE_SIZE")?
496                .or(server.limits.as_ref().and_then(|l| l.grpc_max_message_size))
497                .unwrap_or(DEFAULT_GRPC_MAX_MESSAGE_SIZE),
498            rate_limit_per_minute: parse_env(env, "RATE_LIMIT_PER_MINUTE")?
499                .or(server
500                    .rate_limit
501                    .as_ref()
502                    .and_then(|r| r.requests_per_minute))
503                .unwrap_or(DEFAULT_RATE_LIMIT_PER_MINUTE),
504            register_per_minute: parse_env(env, "REGISTER_PER_MINUTE")?
505                .or(server
506                    .rate_limit
507                    .as_ref()
508                    .and_then(|r| r.register_per_minute))
509                .unwrap_or(DEFAULT_REGISTER_PER_MINUTE),
510            metrics_auth_failures_per_minute: parse_env(env, "METRICS_AUTH_FAILURES_PER_MINUTE")?
511                .or(server
512                    .rate_limit
513                    .as_ref()
514                    .and_then(|r| r.metrics_auth_failures_per_minute))
515                .unwrap_or(DEFAULT_METRICS_AUTH_FAILURES_PER_MINUTE),
516            trusted_proxies: env("TRUSTED_PROXIES")
517                .map(|v| {
518                    v.split(',')
519                        .map(|s| s.trim().to_string())
520                        .filter(|s| !s.is_empty() && s.parse::<std::net::IpAddr>().is_ok())
521                        .collect()
522                })
523                .or_else(|| server.rate_limit.as_ref()?.trusted_proxies.clone())
524                .unwrap_or_default(),
525            metrics_username: env("METRICS_USERNAME")
526                .or_else(|| server.metrics.as_ref().and_then(|m| m.username.clone())),
527            metrics_password: env("METRICS_PASSWORD")
528                .or_else(|| server.metrics.as_ref().and_then(|m| m.password.clone())),
529            cors_allowed_origins: env("CORS_ALLOWED_ORIGINS")
530                .or_else(|| server.cors.as_ref().and_then(|c| c.allowed_origins.clone()))
531                .map(|v| {
532                    v.split(',')
533                        .map(str::trim)
534                        .filter(|origin| !origin.is_empty())
535                        .map(ToOwned::to_owned)
536                        .collect()
537                })
538                .unwrap_or_default(),
539        };
540
541        config.validate()?;
542        Ok(config)
543    }
544
545    fn validate(&self) -> Result<(), ConfigLoadError> {
546        if self.grpc_port == 0 {
547            return Err(ConfigLoadError::InvalidRange {
548                name: "GRPC_PORT",
549                value: self.grpc_port.to_string(),
550                reason: "port must be nonzero".into(),
551            });
552        }
553        if self.http_port == 0 {
554            return Err(ConfigLoadError::InvalidRange {
555                name: "HTTP_PORT",
556                value: self.http_port.to_string(),
557                reason: "port must be nonzero".into(),
558            });
559        }
560        if self.db_max_connections == 0 {
561            return Err(ConfigLoadError::InvalidRange {
562                name: "DB_MAX_CONNECTIONS",
563                value: self.db_max_connections.to_string(),
564                reason: "must be at least 1".into(),
565            });
566        }
567        if self.request_timeout_secs == 0 {
568            return Err(ConfigLoadError::InvalidRange {
569                name: "REQUEST_TIMEOUT_SECS",
570                value: self.request_timeout_secs.to_string(),
571                reason: "must be at least 1".into(),
572            });
573        }
574        if self.grpc_max_message_size == 0 {
575            return Err(ConfigLoadError::InvalidRange {
576                name: "GRPC_MAX_MESSAGE_SIZE",
577                value: self.grpc_max_message_size.to_string(),
578                reason: "must be greater than 0".into(),
579            });
580        }
581        if self.max_command_length == 0 {
582            return Err(ConfigLoadError::InvalidRange {
583                name: "MAX_COMMAND_LENGTH",
584                value: self.max_command_length.to_string(),
585                reason: "must be nonzero".into(),
586            });
587        }
588        if self.max_encrypted_payload_length == 0 {
589            return Err(ConfigLoadError::InvalidRange {
590                name: "MAX_ENCRYPTED_PAYLOAD_LENGTH",
591                value: self.max_encrypted_payload_length.to_string(),
592                reason: "must be nonzero".into(),
593            });
594        }
595        if self.max_description_length == 0 {
596            return Err(ConfigLoadError::InvalidRange {
597                name: "MAX_DESCRIPTION_LENGTH",
598                value: self.max_description_length.to_string(),
599                reason: "must be nonzero".into(),
600            });
601        }
602        if self.max_tags == 0 {
603            return Err(ConfigLoadError::InvalidRange {
604                name: "MAX_TAGS",
605                value: self.max_tags.to_string(),
606                reason: "must be nonzero".into(),
607            });
608        }
609        if self.max_tag_length == 0 {
610            return Err(ConfigLoadError::InvalidRange {
611                name: "MAX_TAG_LENGTH",
612                value: self.max_tag_length.to_string(),
613                reason: "must be nonzero".into(),
614            });
615        }
616        if self.max_id_length == 0 {
617            return Err(ConfigLoadError::InvalidRange {
618                name: "MAX_ID_LENGTH",
619                value: self.max_id_length.to_string(),
620                reason: "must be nonzero".into(),
621            });
622        }
623        if self.max_device_id_length == 0 {
624            return Err(ConfigLoadError::InvalidRange {
625                name: "MAX_DEVICE_ID_LENGTH",
626                value: self.max_device_id_length.to_string(),
627                reason: "must be nonzero".into(),
628            });
629        }
630        if self.max_api_key_length == 0 {
631            return Err(ConfigLoadError::InvalidRange {
632                name: "MAX_API_KEY_LENGTH",
633                value: self.max_api_key_length.to_string(),
634                reason: "must be nonzero".into(),
635            });
636        }
637        if self.rate_limit_per_minute == 0 {
638            return Err(ConfigLoadError::InvalidRange {
639                name: "RATE_LIMIT_PER_MINUTE",
640                value: self.rate_limit_per_minute.to_string(),
641                reason: "must be nonzero".into(),
642            });
643        }
644        // Zero is a hard error, not "unlimited": a zero registration budget
645        // makes account creation impossible, and a zero metrics-failure budget
646        // makes the second failed scrape return 429 forever. Both are almost
647        // certainly operator mistakes.
648        if self.register_per_minute == 0 {
649            return Err(ConfigLoadError::InvalidRange {
650                name: "REGISTER_PER_MINUTE",
651                value: self.register_per_minute.to_string(),
652                reason: "must be nonzero".into(),
653            });
654        }
655        if self.metrics_auth_failures_per_minute == 0 {
656            return Err(ConfigLoadError::InvalidRange {
657                name: "METRICS_AUTH_FAILURES_PER_MINUTE",
658                value: self.metrics_auth_failures_per_minute.to_string(),
659                reason: "must be nonzero".into(),
660            });
661        }
662        Ok(())
663    }
664
665    pub fn ensure_config_file() {
666        if let Err(e) = bootstrap::ensure_config_file() {
667            tracing::warn!("{}", e);
668        }
669    }
670}
671
672#[derive(Clone)]
673pub struct AppState {
674    pub config: Config,
675    pub metrics: Metrics,
676    pub db: Arc<Database>,
677    /// The same process-local limiter the gRPC service uses. Sharing it keeps
678    /// one bounded structure (and one `MAX_ENTRIES` budget) for both listeners
679    /// instead of inventing a second one for the HTTP leaf.
680    pub rate_limiter: Arc<RateLimiter>,
681}
682
683pub struct SnipSyncService {
684    pub db: Arc<Database>,
685    pub rate_limiter: Arc<RateLimiter>,
686    pub config: Config,
687    pub metrics: Metrics,
688    pub premade_manager: PremadeManager,
689    /// Test-only: most recent `authorization` header value observed by
690    /// the gRPC handlers. Used by integration tests to assert that the
691    /// sync client actually sends a non-empty bearer token in metadata
692    /// (catching the auth-header regression where `request.api_key` was
693    /// read instead of the caller's API key).
694    #[cfg(any(test, feature = "test-helpers"))]
695    pub captured_auth_header: Arc<std::sync::Mutex<Option<String>>>,
696    /// Test-only: optional request observer for telemetry tests.
697    /// When set, `record_request` and `record_request_finished` emit
698    /// sanitized events to the observer.
699    #[cfg(any(test, feature = "test-helpers"))]
700    pub test_observer: Option<Arc<dyn crate::test_observer::TestRequestObserver>>,
701    /// Test-only: failure-injection threshold for `PushSnippets`.
702    ///
703    /// The value is the 1-indexed push call that must fail: once
704    /// `push_fail_counter` reaches it, the push is rejected with an
705    /// internal error. `u32::MAX` (the default) disables injection. This
706    /// field is only read — the call counter is tracked separately in
707    /// `push_fail_counter`.
708    #[cfg(any(test, feature = "test-helpers"))]
709    pub push_fail_after: Arc<std::sync::atomic::AtomicU32>,
710    /// Test-only: internal counter for push failure injection.
711    /// Starts at 0 and increments on each PushSnippets call.
712    #[cfg(any(test, feature = "test-helpers"))]
713    pub push_fail_counter: Arc<std::sync::atomic::AtomicU32>,
714}
715
716impl SnipSyncService {
717    pub fn record_request(&self, method: &str) {
718        self.metrics
719            .requests_total
720            .with_label_values(&[method])
721            .inc();
722        tracing::trace!("Request: {}", method);
723        #[cfg(feature = "test-helpers")]
724        if let Some(ref obs) = self.test_observer {
725            use crate::test_observer::{RequestStarted, now_millis, operation_name_for_method};
726            let sequence = obs.allocate_sequence();
727            let concurrent = obs.current_concurrent();
728            obs.request_started(RequestStarted {
729                sequence,
730                started_at_unix_ms: now_millis(),
731                method: method.to_string(),
732                operation: operation_name_for_method(method).to_string(),
733                authenticated_user_id: None,
734                authenticated_device_id: None,
735                target_library_id: None,
736                request_revision: None,
737                payload_len: 0,
738                payload_sha256: String::new(),
739                payload_contains_plaintext_sentinel: false,
740                concurrent_at_start: concurrent,
741            });
742        }
743    }
744
745    #[cfg_attr(not(feature = "test-helpers"), allow(unused_variables))]
746    pub fn record_request_finished(&self, method: &str, success: bool) {
747        #[cfg(feature = "test-helpers")]
748        if let Some(ref obs) = self.test_observer {
749            use crate::test_observer::RequestFinished;
750            let sequence = obs.last_started_sequence_for_method(method);
751            obs.request_finished(RequestFinished {
752                sequence,
753                finished_at_unix_ms: crate::test_observer::now_millis(),
754                success,
755                response_revision: None,
756            });
757        }
758    }
759
760    /// Update the last started request's authenticated IDs.
761    ///
762    /// Called after authentication resolves the user and device IDs.
763    /// Only active with `test-helpers` feature.
764    #[cfg_attr(not(feature = "test-helpers"), allow(unused_variables))]
765    pub fn update_request_ids(
766        &self,
767        method: &str,
768        user_id: Option<String>,
769        device_id: Option<String>,
770        library_id: Option<String>,
771    ) {
772        #[cfg(feature = "test-helpers")]
773        if let Some(ref obs) = self.test_observer {
774            obs.update_ids_for_last_method(method, user_id, device_id, library_id);
775        }
776    }
777
778    pub fn record_request_duration(&self, method: &str, start: std::time::Instant) {
779        let duration = start.elapsed().as_secs_f64();
780        self.metrics
781            .request_duration_seconds
782            .with_label_values(&[method])
783            .observe(duration);
784        tracing::debug!("Request {} completed in {:.3}s", method, duration);
785    }
786
787    pub fn record_rate_limit(&self) {
788        self.metrics.rate_limit_hits.inc();
789    }
790
791    pub fn record_auth_failure(&self) {
792        self.metrics.auth_failures.inc();
793    }
794
795    /// Record a database failure (for example an upsert that failed inside a
796    /// write transaction). Database failures are infrastructure errors, not
797    /// content-level skips, so they are counted separately from the
798    /// `skipped_count` a client sees.
799    pub fn record_db_error(&self) {
800        self.metrics.db_errors_total.inc();
801    }
802
803    pub fn record_sync(&self, direction: &str) {
804        self.metrics
805            .sync_operations_total
806            .with_label_values(&[direction])
807            .inc();
808    }
809
810    pub fn record_library_op(&self, operation: &str) {
811        self.metrics
812            .library_operations_total
813            .with_label_values(&[operation])
814            .inc();
815    }
816
817    fn request_api_key<T>(&self, request: &Request<T>, body_api_key: &str) -> String {
818        let api_key = extract_api_key(request, body_api_key);
819        #[cfg(any(test, feature = "test-helpers"))]
820        self.capture_test_auth_header(&api_key);
821        api_key
822    }
823
824    #[cfg(any(test, feature = "test-helpers"))]
825    fn capture_test_auth_header(&self, api_key: &str) {
826        if !api_key.is_empty() {
827            let mut slot = self
828                .captured_auth_header
829                .lock()
830                .unwrap_or_else(|e| e.into_inner());
831            if slot.is_none() {
832                *slot = Some(format!("Bearer {api_key}"));
833            }
834        }
835    }
836
837    /// Derive the rate-limit bucket identity for an inbound request.
838    ///
839    /// Deliberately **not** the API key. The key is request-supplied and is
840    /// not validated until `db.get_user_by_api_key` runs, so keying the limiter
841    /// on it meant every distinct attacker-chosen key got its own budget of
842    /// `rate_limit_per_minute` and the limiter imposed no aggregate request
843    /// ceiling on the authenticated gRPC surface. The peer address is bounded
844    /// by the number of real callers, and `x-forwarded-for` is honoured only
845    /// from a configured trusted proxy.
846    ///
847    /// Tonic inserts a [`tonic::transport::server::TcpConnectInfo`] into the
848    /// request extensions for every TCP connection; the peer address is read
849    /// from there. A bare [`std::net::SocketAddr`] is never inserted, so
850    /// looking for one yields `None` for every request and collapses every
851    /// caller onto a single global bucket.
852    fn peer_rate_limit_key<T>(&self, request: &Request<T>) -> String {
853        let peer_ip = request
854            .extensions()
855            .get::<tonic::transport::server::TcpConnectInfo>()
856            .and_then(|info| info.remote_addr())
857            .map(|addr| addr.ip().to_string());
858
859        match peer_ip {
860            Some(ref ip) if self.config.trusted_proxies.contains(ip) => request
861                .metadata()
862                .get("x-forwarded-for")
863                .and_then(|v| v.to_str().ok())
864                .and_then(|header| header.split(',').next())
865                .map(|s| strip_port(s.trim()))
866                .filter(|s| !s.is_empty())
867                .unwrap_or_else(|| ip.clone()),
868            Some(ip) => ip,
869            None => "unknown".to_string(),
870        }
871    }
872
873    pub async fn authenticate_and_rate_limit(
874        &self,
875        api_key: &str,
876        rate_limit_key: &str,
877    ) -> Result<String, Status> {
878        self.authenticate_and_rate_limit_with_duration(api_key, rate_limit_key, None)
879            .await
880    }
881
882    /// Like `authenticate_and_rate_limit`, but records request duration on
883    /// failure so the histogram captures latency for error responses.
884    ///
885    /// `rate_limit_key` must come from [`Self::peer_rate_limit_key`].
886    pub async fn authenticate_and_rate_limit_with_duration(
887        &self,
888        api_key: &str,
889        rate_limit_key: &str,
890        start: Option<std::time::Instant>,
891    ) -> Result<String, Status> {
892        if api_key.is_empty() {
893            if let Some(s) = start {
894                self.record_request_duration("auth", s);
895            }
896            return Err(Status::unauthenticated("API key is required"));
897        }
898        if api_key.len() > self.config.max_api_key_length {
899            if let Some(s) = start {
900                self.record_request_duration("auth", s);
901            }
902            return Err(Status::invalid_argument(format!(
903                "API key exceeds maximum length of {} bytes",
904                self.config.max_api_key_length
905            )));
906        }
907
908        // Pre-authentication budget. Its only job is to bound the cost of the
909        // API-key lookup below, so it is deliberately far larger than the
910        // per-caller budget: an unauthenticated caller must never be able to
911        // exhaust the budget that legitimate callers spend.
912        let pre_auth_key = format!("pre-auth:{rate_limit_key}");
913        let pre_auth_limit = self
914            .config
915            .rate_limit_per_minute
916            .saturating_mul(PRE_AUTH_RATE_LIMIT_MULTIPLIER) as usize;
917        if !self
918            .rate_limiter
919            .allow(&pre_auth_key, pre_auth_limit, Duration::from_secs(60))
920            .await
921        {
922            self.record_rate_limit();
923            if let Some(s) = start {
924                self.record_request_duration("auth", s);
925            }
926            return Err(Status::resource_exhausted("Rate limit exceeded"));
927        }
928
929        let user_id = self
930            .db
931            .get_user_by_api_key(api_key)
932            .await
933            .map_err(|_| {
934                if let Some(s) = start {
935                    self.record_request_duration("auth", s);
936                }
937                Status::internal("Internal error")
938            })?
939            .ok_or_else(|| {
940                self.record_auth_failure();
941                if let Some(s) = start {
942                    self.record_request_duration("auth", s);
943                }
944                Status::unauthenticated("Invalid API key")
945            })?;
946
947        // Authenticated budget. Only a request that presented a valid API key
948        // can spend it, so an invalid key cannot lock out a legitimate caller
949        // that shares the same peer address.
950        if !self
951            .rate_limiter
952            .allow(
953                rate_limit_key,
954                self.config.rate_limit_per_minute as usize,
955                Duration::from_secs(60),
956            )
957            .await
958        {
959            self.record_rate_limit();
960            if let Some(s) = start {
961                self.record_request_duration("auth", s);
962            }
963            return Err(Status::resource_exhausted("Rate limit exceeded"));
964        }
965
966        Ok(user_id)
967    }
968
969    pub fn validate_snippet(&self, snippet: &ProtoSnippet) -> Result<(), Status> {
970        if snippet.id.is_empty() {
971            return Err(Status::invalid_argument("Snippet ID is required"));
972        }
973        if snippet.id.len() > self.config.max_id_length {
974            return Err(Status::invalid_argument(format!(
975                "Snippet ID exceeds maximum length of {} bytes",
976                self.config.max_id_length
977            )));
978        }
979
980        if snippet.device_id.is_empty() {
981            return Err(Status::invalid_argument("Device ID is required"));
982        }
983        if snippet.device_id.len() > self.config.max_device_id_length {
984            return Err(Status::invalid_argument(format!(
985                "Device ID exceeds maximum length of {} bytes",
986                self.config.max_device_id_length
987            )));
988        }
989
990        if !snippet.encrypted && snippet.command.len() > self.config.max_command_length {
991            return Err(Status::invalid_argument(format!(
992                "Command exceeds maximum length of {} bytes",
993                self.config.max_command_length
994            )));
995        }
996
997        if !snippet.encrypted && snippet.command.trim().is_empty() {
998            return Err(Status::invalid_argument("Snippet command is required"));
999        }
1000
1001        if snippet.encrypted && snippet.command.len() > self.config.max_encrypted_payload_length {
1002            return Err(Status::invalid_argument(format!(
1003                "Encrypted payload exceeds maximum length of {} bytes",
1004                self.config.max_encrypted_payload_length
1005            )));
1006        }
1007
1008        if snippet.encrypted && (snippet.command.is_empty() || snippet.command.trim().is_empty()) {
1009            return Err(Status::invalid_argument(
1010                "Encrypted snippet payload is required",
1011            ));
1012        }
1013
1014        if snippet.description.len() > self.config.max_description_length {
1015            return Err(Status::invalid_argument(format!(
1016                "Description exceeds maximum length of {} bytes",
1017                self.config.max_description_length
1018            )));
1019        }
1020
1021        if snippet.tags.len() > self.config.max_tags {
1022            return Err(Status::invalid_argument(format!(
1023                "Too many tags (max {})",
1024                self.config.max_tags
1025            )));
1026        }
1027
1028        for tag in &snippet.tags {
1029            if tag.len() > self.config.max_tag_length {
1030                return Err(Status::invalid_argument(format!(
1031                    "Tag '{}' exceeds maximum length of {} bytes",
1032                    tag, self.config.max_tag_length
1033                )));
1034            }
1035        }
1036
1037        // Sample server time once to avoid micro-boundary disagreement
1038        // between consecutive timestamp checks.
1039        let now = time::OffsetDateTime::now_utc().unix_timestamp();
1040        let tolerance = 300; // 5 minutes
1041
1042        if snippet.updated_at > now + tolerance {
1043            let skew = snippet.updated_at - now;
1044            return Err(Status::invalid_argument(format!(
1045                "CLOCK_SKEW: updated_at is {} seconds ahead of server time; \
1046                 synchronize the client clock and retry",
1047                skew
1048            )));
1049        }
1050        if snippet.created_at > now + tolerance {
1051            let skew = snippet.created_at - now;
1052            return Err(Status::invalid_argument(format!(
1053                "CLOCK_SKEW: created_at is {} seconds ahead of server time; \
1054                 synchronize the client clock and retry",
1055                skew
1056            )));
1057        }
1058        if snippet.created_at < 0 || snippet.updated_at < 0 {
1059            return Err(Status::invalid_argument("Timestamps must be non-negative"));
1060        }
1061        // Lower clock-skew bound: a zero timestamp means the client clock was
1062        // unset (epoch). Such rows would be invisible to every delta query
1063        // (the cursor filter is inclusive, so a zero timestamp never reaches
1064        // a client whose cursor is positive), so reject them instead of
1065        // storing them.
1066        if snippet.created_at == 0 || snippet.updated_at == 0 {
1067            return Err(Status::invalid_argument(
1068                "Timestamps must be positive; zero indicates an unset client clock",
1069            ));
1070        }
1071        if snippet.created_at > snippet.updated_at {
1072            return Err(Status::invalid_argument(
1073                "created_at must not be greater than updated_at",
1074            ));
1075        }
1076
1077        Ok(())
1078    }
1079}
1080
1081#[tonic::async_trait]
1082impl SnippetSync for SnipSyncService {
1083    async fn health(
1084        &self,
1085        _request: Request<HealthRequest>,
1086    ) -> Result<Response<HealthResponse>, Status> {
1087        let request_id = uuid::Uuid::new_v4();
1088        let start = std::time::Instant::now();
1089        self.record_request("health");
1090        tracing::debug!(request_id = %request_id, "Health check");
1091        let healthy = self.db.ping().await.is_ok();
1092        self.record_request_duration("health", start);
1093        self.record_request_finished("health", healthy);
1094        Ok(Response::new(HealthResponse {
1095            healthy,
1096            version: env!("CARGO_PKG_VERSION").to_string(),
1097        }))
1098    }
1099
1100    async fn register(
1101        &self,
1102        request: Request<RegisterRequest>,
1103    ) -> Result<Response<RegisterResponse>, Status> {
1104        let request_id = uuid::Uuid::new_v4();
1105        let start = std::time::Instant::now();
1106        self.record_request("register");
1107        tracing::info!(request_id = %request_id, "Register request");
1108
1109        // Use peer IP address for rate limiting (device_id is client-controlled).
1110        // Tonic populates request.extensions() with a `TcpConnectInfo` holding
1111        // the peer socket address.
1112        let rate_limit_key = self.peer_rate_limit_key(&request);
1113
1114        // Registration is unauthenticated, so it gets its own bucket: it must
1115        // not be able to spend the budget that authenticated callers use.
1116        let register_limit_key = format!("register:{rate_limit_key}");
1117        if !self
1118            .rate_limiter
1119            .allow(
1120                &register_limit_key,
1121                self.config.rate_limit_per_minute as usize,
1122                Duration::from_secs(60),
1123            )
1124            .await
1125        {
1126            self.record_rate_limit();
1127            self.record_request_duration("register", start);
1128            self.record_request_finished("register", false);
1129            return Err(Status::resource_exhausted(
1130                "Rate limit exceeded for registration",
1131            ));
1132        }
1133
1134        // Per-peer budgets do not bound a caller that rotates addresses, and
1135        // every registration inserts a user plus a default library. The global
1136        // budget is deliberately a separate key so a legitimate caller is not
1137        // charged for an attacker's traffic on another address: it caps total
1138        // account creation, not per-caller fairness.
1139        if !self
1140            .rate_limiter
1141            .allow(
1142                "register-global",
1143                self.config.register_per_minute as usize,
1144                Duration::from_secs(60),
1145            )
1146            .await
1147        {
1148            self.record_rate_limit();
1149            self.record_request_duration("register", start);
1150            self.record_request_finished("register", false);
1151            tracing::warn!(
1152                request_id = %request_id,
1153                "Rejected registration: global per-minute ceiling reached"
1154            );
1155            return Err(Status::resource_exhausted(
1156                "Registration is temporarily unavailable: global account-creation limit reached",
1157            ));
1158        }
1159
1160        let _req = request.into_inner();
1161
1162        let api_key = uuid::Uuid::new_v4().to_string();
1163
1164        match self.db.create_user(&api_key).await {
1165            Ok(device_id) => {
1166                if uuid::Uuid::parse_str(&device_id).is_err() {
1167                    tracing::error!(
1168                        request_id = %request_id,
1169                        "Generated device_id is not a valid UUID: {}",
1170                        device_id
1171                    );
1172                    self.record_request_duration("register", start);
1173                    self.record_request_finished("register", false);
1174                    return Err(Status::internal(
1175                        "Internal error: invalid device ID generated",
1176                    ));
1177                }
1178                tracing::info!(
1179                    request_id = %request_id,
1180                    device_id = %device_id,
1181                    "Created new user"
1182                );
1183                self.record_request_duration("register", start);
1184                self.record_request_finished("register", true);
1185                Ok(Response::new(RegisterResponse {
1186                    success: true,
1187                    api_key,
1188                    message: "Account created successfully".to_string(),
1189                    device_id,
1190                }))
1191            }
1192            Err(e) => {
1193                tracing::error!(request_id = %request_id, "Failed to create user: {}", e);
1194                self.record_request_duration("register", start);
1195                self.record_request_finished("register", false);
1196                Err(Status::internal("Internal error"))
1197            }
1198        }
1199    }
1200
1201    async fn get_snippets(
1202        &self,
1203        request: Request<GetSnippetsRequest>,
1204    ) -> Result<Response<SnippetList>, Status> {
1205        let request_id = uuid::Uuid::new_v4();
1206        let start = std::time::Instant::now();
1207        self.record_request("get_snippets");
1208        tracing::info!(request_id = %request_id, "GetSnippets request");
1209        let rate_limit_key = self.peer_rate_limit_key(&request);
1210        let api_key = self.request_api_key(&request, &request.get_ref().api_key);
1211        let req = request.into_inner();
1212
1213        let user_id = self
1214            .authenticate_and_rate_limit_with_duration(&api_key, &rate_limit_key, Some(start))
1215            .await
1216            .inspect_err(|_| {
1217                self.record_request_finished("get_snippets", false);
1218            })?;
1219
1220        let library_id = if req.library_id.is_empty() {
1221            self.db.get_default_library(&user_id).await.map_err(|e| {
1222                self.record_request_duration("get_snippets", start);
1223                self.record_request_finished("get_snippets", false);
1224                tracing::error!("Internal error: {}", e);
1225                Status::internal("Internal error")
1226            })?
1227        } else {
1228            if !self
1229                .db
1230                .verify_library_ownership(&user_id, &req.library_id)
1231                .await
1232                .map_err(|e| {
1233                    self.record_request_duration("get_snippets", start);
1234                    self.record_request_finished("get_snippets", false);
1235                    tracing::error!("Internal error: {}", e);
1236                    Status::internal("Internal error")
1237                })?
1238            {
1239                tracing::warn!(
1240                    "User {} attempted to access library {} without ownership",
1241                    user_id,
1242                    req.library_id
1243                );
1244                self.record_request_duration("get_snippets", start);
1245                self.record_request_finished("get_snippets", false);
1246                return Err(Status::not_found("Library not found"));
1247            }
1248            req.library_id
1249        };
1250
1251        // Update observer with authenticated identity after resolution.
1252        self.update_request_ids(
1253            "get_snippets",
1254            Some(user_id.clone()),
1255            None,
1256            Some(library_id.clone()),
1257        );
1258
1259        let limit = if req.limit > 0 {
1260            req.limit.clamp(1, MAX_REQUEST_LIMIT)
1261        } else {
1262            100
1263        };
1264        let offset = if req.offset > 0 { req.offset } else { 0 };
1265
1266        let (snippets, total) = self
1267            .db
1268            .get_snippets(&user_id, &library_id, req.since, limit, offset, false)
1269            .await
1270            .map_err(|e| {
1271                self.record_request_duration("get_snippets", start);
1272                self.record_request_finished("get_snippets", false);
1273                tracing::error!("Internal error: {}", e);
1274                Status::internal("Internal error")
1275            })?;
1276
1277        let page_len = snippets.len() as i32;
1278        let has_more_by_rows = offset.saturating_add(page_len) < total;
1279
1280        let mut proto_snippets: Vec<ProtoSnippet> = snippets
1281            .into_iter()
1282            .map(|s| ProtoSnippet {
1283                id: s.id,
1284                description: s.description,
1285                command: s.command,
1286                tags: s.tags,
1287                created_at: s.created_at,
1288                updated_at: s.updated_at,
1289                device_id: s.device_id,
1290                deleted: s.deleted,
1291                encrypted: s.encrypted,
1292            })
1293            .collect();
1294
1295        let truncated = fit_page_to_byte_ceiling(
1296            &mut proto_snippets,
1297            page_byte_ceiling(self.config.grpc_max_message_size as usize, 0),
1298        );
1299        let has_more = has_more_by_rows || truncated;
1300
1301        self.record_request_duration("get_snippets", start);
1302
1303        let result = Ok(Response::new(SnippetList {
1304            snippets: proto_snippets,
1305            total_count: total,
1306            has_more,
1307        }));
1308        self.record_request_finished("get_snippets", result.is_ok());
1309        result
1310    }
1311
1312    async fn push_snippets(
1313        &self,
1314        request: Request<PushSnippetsRequest>,
1315    ) -> Result<Response<PushSnippetsResponse>, Status> {
1316        let request_id = uuid::Uuid::new_v4();
1317        let start = std::time::Instant::now();
1318        self.record_request("push_snippets");
1319        tracing::info!(request_id = %request_id, "PushSnippets request");
1320        let rate_limit_key = self.peer_rate_limit_key(&request);
1321        let api_key = self.request_api_key(&request, &request.get_ref().api_key);
1322        let req = request.into_inner();
1323
1324        let user_id = self
1325            .authenticate_and_rate_limit_with_duration(&api_key, &rate_limit_key, Some(start))
1326            .await
1327            .inspect_err(|_| {
1328                self.record_request_finished("push_snippets", false);
1329            })?;
1330
1331        let library_id = if req.library_id.is_empty() {
1332            self.db.get_default_library(&user_id).await.map_err(|e| {
1333                self.record_request_duration("push_snippets", start);
1334                self.record_request_finished("push_snippets", false);
1335                tracing::error!("Internal error: {}", e);
1336                Status::internal("Internal error")
1337            })?
1338        } else {
1339            if !self
1340                .db
1341                .verify_library_ownership(&user_id, &req.library_id)
1342                .await
1343                .map_err(|e| {
1344                    self.record_request_duration("push_snippets", start);
1345                    self.record_request_finished("push_snippets", false);
1346                    tracing::error!("Internal error: {}", e);
1347                    Status::internal("Internal error")
1348                })?
1349            {
1350                tracing::warn!(
1351                    "User {} attempted to push to library {} without ownership",
1352                    user_id,
1353                    req.library_id
1354                );
1355                self.record_request_duration("push_snippets", start);
1356                self.record_request_finished("push_snippets", false);
1357                return Err(Status::not_found("Library not found"));
1358            }
1359            req.library_id
1360        };
1361
1362        // Update observer with authenticated identity after resolution.
1363        self.update_request_ids(
1364            "push_snippets",
1365            Some(user_id.clone()),
1366            req.snippets.first().map(|s| s.device_id.clone()),
1367            Some(library_id.clone()),
1368        );
1369
1370        if req.snippets.len() > DEFAULT_MAX_SYNC_SNIPPETS {
1371            self.record_request_duration("push_snippets", start);
1372            self.record_request_finished("push_snippets", false);
1373            return Err(Status::invalid_argument(format!(
1374                "Too many snippets in push request (max {}), got {}",
1375                DEFAULT_MAX_SYNC_SNIPPETS,
1376                req.snippets.len()
1377            )));
1378        }
1379
1380        // Test-only failure injection: fail on the Nth PushSnippets call.
1381        // push_fail_after stores the 1-indexed failure point. A value of
1382        // u32::MAX means "disabled" (never fail). The internal counter
1383        // starts at 0 and increments on each push; when it reaches the
1384        // threshold the push is rejected.
1385        #[cfg(feature = "test-helpers")]
1386        {
1387            let threshold = self
1388                .push_fail_after
1389                .load(std::sync::atomic::Ordering::SeqCst);
1390            if threshold != u32::MAX {
1391                let count = self
1392                    .push_fail_counter
1393                    .fetch_add(1, std::sync::atomic::Ordering::SeqCst)
1394                    + 1;
1395                if count >= threshold {
1396                    self.record_request_duration("push_snippets", start);
1397                    self.record_request_finished("push_snippets", false);
1398                    return Err(Status::internal("test-injected push failure"));
1399                }
1400            }
1401        }
1402
1403        let mut accepted = 0;
1404        let mut rejected = 0;
1405
1406        // The write transaction deliberately spans every upsert of the
1407        // request: a push is all-or-nothing, and a failed upsert rolls the
1408        // whole batch back (see the error arm below). Committing in batches
1409        // would shorten the SQLite write-lock window, but it would also turn
1410        // a mid-batch failure into a partially applied upload — a contract
1411        // change that needs its own design decision, not a silent tweak.
1412        let mut tx = self.db.pool().begin().await.map_err(|e| {
1413            self.record_request_duration("push_snippets", start);
1414            self.record_request_finished("push_snippets", false);
1415            tracing::error!(request_id = %request_id, "Failed to begin transaction: {}", e);
1416            Status::internal("Internal error")
1417        })?;
1418
1419        for snippet in req.snippets {
1420            if let Err(e) = self.validate_snippet(&snippet) {
1421                rejected += 1;
1422                tracing::warn!(request_id = %request_id, snippet_id = %snippet.id, reason = %e, "Snippet validation failed");
1423                continue;
1424            }
1425
1426            let db_snippet = db::Snippet {
1427                id: snippet.id.clone(),
1428                description: snippet.description,
1429                command: snippet.command,
1430                tags: snippet.tags,
1431                created_at: snippet.created_at,
1432                updated_at: snippet.updated_at,
1433                device_id: snippet.device_id,
1434                deleted: snippet.deleted,
1435                encrypted: snippet.encrypted,
1436            };
1437
1438            match self
1439                .db
1440                .upsert_snippet_in_tx(&mut tx, &db_snippet, &user_id, &library_id)
1441                .await
1442            {
1443                Ok(db::UpsertOutcome::Written | db::UpsertOutcome::Stale) => accepted += 1,
1444                Ok(db::UpsertOutcome::ForeignScope) => {
1445                    // `snippets.id` is a global primary key, so this snippet
1446                    // cannot be stored alongside the identically-identified one
1447                    // in another user or library. Reporting it as accepted
1448                    // would tell the client the upload succeeded while the
1449                    // snippet is silently absent from the server.
1450                    rejected += 1;
1451                    tracing::error!(
1452                        request_id = %request_id,
1453                        snippet_id = %snippet.id,
1454                        user_id = %user_id,
1455                        library_id = %library_id,
1456                        "Snippet rejected: id already exists under a different user or library"
1457                    );
1458                }
1459                Err(e) => {
1460                    tracing::warn!(
1461                        request_id = %request_id,
1462                        snippet_id = %snippet.id,
1463                        reason = %e,
1464                        "Snippet upsert failed"
1465                    );
1466                    self.record_db_error();
1467                    // Rollback failure must not leak a dirty connection back
1468                    // into the pool. sqlx 0.9 guarantees this on release: a
1469                    // returned PoolConnection is pinged, which flushes any
1470                    // queued/failed ROLLBACK work, and a connection that
1471                    // fails the ping is hard-closed instead of pooled
1472                    // (sqlx-core `pool/connection.rs::return_to_pool`).
1473                    if let Err(rollback_error) = tx.rollback().await {
1474                        tracing::error!(
1475                            request_id = %request_id,
1476                            error = %rollback_error,
1477                            "Failed to roll back push transaction"
1478                        );
1479                    }
1480                    self.record_request_duration("push_snippets", start);
1481                    self.record_request_finished("push_snippets", false);
1482                    return Err(Status::internal("Internal error"));
1483                }
1484            }
1485        }
1486
1487        tx.commit().await.map_err(|e| {
1488            self.record_request_duration("push_snippets", start);
1489            self.record_request_finished("push_snippets", false);
1490            tracing::error!(request_id = %request_id, "Failed to commit transaction: {}", e);
1491            Status::internal("Internal error")
1492        })?;
1493
1494        self.record_request_duration("push_snippets", start);
1495
1496        let result = Ok(Response::new(PushSnippetsResponse {
1497            success: rejected == 0,
1498            message: format!("Accepted {}, rejected {}", accepted, rejected),
1499            accepted_count: accepted,
1500            rejected_count: rejected,
1501        }));
1502        self.record_request_finished("push_snippets", result.is_ok());
1503        result
1504    }
1505
1506    async fn sync(&self, request: Request<SyncRequest>) -> Result<Response<SyncResponse>, Status> {
1507        let request_id = uuid::Uuid::new_v4();
1508        let start = std::time::Instant::now();
1509        self.record_request("sync");
1510        tracing::info!(request_id = %request_id, "Sync request");
1511        let rate_limit_key = self.peer_rate_limit_key(&request);
1512        let api_key = self.request_api_key(&request, &request.get_ref().api_key);
1513        let req = request.into_inner();
1514
1515        let user_id = self
1516            .authenticate_and_rate_limit_with_duration(&api_key, &rate_limit_key, Some(start))
1517            .await
1518            .inspect_err(|_| {
1519                self.record_request_finished("sync", false);
1520            })?;
1521
1522        if req.local_snippets.len() > DEFAULT_MAX_SYNC_SNIPPETS {
1523            self.record_request_duration("sync", start);
1524            self.record_request_finished("sync", false);
1525            return Err(Status::invalid_argument(format!(
1526                "Too many snippets in sync request (max {}), got {}",
1527                DEFAULT_MAX_SYNC_SNIPPETS,
1528                req.local_snippets.len()
1529            )));
1530        }
1531
1532        self.record_sync("bidirectional");
1533
1534        let library_id = if req.library_id.is_empty() {
1535            self.db.get_default_library(&user_id).await.map_err(|e| {
1536                self.record_request_duration("sync", start);
1537                self.record_request_finished("sync", false);
1538                tracing::error!("Internal error: {}", e);
1539                Status::internal("Internal error")
1540            })?
1541        } else {
1542            if !self
1543                .db
1544                .verify_library_ownership(&user_id, &req.library_id)
1545                .await
1546                .map_err(|e| {
1547                    self.record_request_duration("sync", start);
1548                    self.record_request_finished("sync", false);
1549                    tracing::error!("Internal error: {}", e);
1550                    Status::internal("Internal error")
1551                })?
1552            {
1553                tracing::warn!(
1554                    "User {} attempted to sync with library {} without ownership",
1555                    user_id,
1556                    req.library_id
1557                );
1558                self.record_request_duration("sync", start);
1559                self.record_request_finished("sync", false);
1560                return Err(Status::not_found("Library not found"));
1561            }
1562            req.library_id
1563        };
1564
1565        // Update observer with authenticated identity after resolution.
1566        self.update_request_ids(
1567            "sync",
1568            Some(user_id.clone()),
1569            req.local_snippets.first().map(|s| s.device_id.clone()),
1570            Some(library_id.clone()),
1571        );
1572
1573        let mut skipped_ids = Vec::new();
1574
1575        // The write transaction deliberately spans every upsert of the
1576        // request, for the same reason as `push_snippets`: a sync is
1577        // all-or-nothing and a failed upsert rolls the whole batch back.
1578        // Batching the commits would shorten the SQLite write-lock window
1579        // but would turn a mid-batch failure into a partially applied
1580        // upload, which needs its own design decision.
1581        let mut tx = self.db.pool().begin().await.map_err(|e| {
1582            self.record_request_duration("sync", start);
1583            self.record_request_finished("sync", false);
1584            tracing::error!(request_id = %request_id, "Failed to begin transaction: {}", e);
1585            Status::internal("Internal error")
1586        })?;
1587
1588        for snippet in &req.local_snippets {
1589            if let Err(e) = self.validate_snippet(snippet) {
1590                tracing::warn!(
1591                    request_id = %request_id,
1592                    snippet_id = %snippet.id,
1593                    reason = %e,
1594                    "Snippet skipped: validation failed"
1595                );
1596                skipped_ids.push(snippet.id.clone());
1597                continue;
1598            }
1599
1600            let db_snippet = db::Snippet {
1601                id: snippet.id.clone(),
1602                description: snippet.description.clone(),
1603                command: snippet.command.clone(),
1604                tags: snippet.tags.clone(),
1605                created_at: snippet.created_at,
1606                updated_at: snippet.updated_at,
1607                device_id: snippet.device_id.clone(),
1608                deleted: snippet.deleted,
1609                encrypted: snippet.encrypted,
1610            };
1611
1612            match self
1613                .db
1614                .upsert_snippet_in_tx(&mut tx, &db_snippet, &user_id, &library_id)
1615                .await
1616            {
1617                Ok(db::UpsertOutcome::Written | db::UpsertOutcome::Stale) => {}
1618                Ok(db::UpsertOutcome::ForeignScope) => {
1619                    // See `push_snippets`: the global primary key makes this
1620                    // write impossible. Report it in `skipped_ids` so the
1621                    // client sees a partial failure instead of a clean sync.
1622                    tracing::error!(
1623                        request_id = %request_id,
1624                        snippet_id = %snippet.id,
1625                        user_id = %user_id,
1626                        library_id = %library_id,
1627                        "Snippet skipped: id already exists under a different user or library"
1628                    );
1629                    skipped_ids.push(snippet.id.clone());
1630                }
1631                Err(e) => {
1632                    // A database error is an infrastructure failure, not a
1633                    // content-level skip. Reporting it as a skip would tell
1634                    // the client the sync succeeded while the snippet was
1635                    // never stored. Roll the transaction back and answer
1636                    // with an internal error so the client retries.
1637                    tracing::error!(
1638                        request_id = %request_id,
1639                        snippet_id = %snippet.id,
1640                        reason = %e,
1641                        "Snippet upsert failed: rolling back the sync transaction"
1642                    );
1643                    self.record_db_error();
1644                    // Rollback failure must not leak a dirty connection back
1645                    // into the pool (sqlx hard-closes a connection whose
1646                    // rollback cannot be flushed).
1647                    if let Err(rollback_error) = tx.rollback().await {
1648                        tracing::error!(
1649                            request_id = %request_id,
1650                            error = %rollback_error,
1651                            "Failed to roll back sync transaction"
1652                        );
1653                    }
1654                    self.record_request_duration("sync", start);
1655                    self.record_request_finished("sync", false);
1656                    return Err(Status::internal("Internal error"));
1657                }
1658            }
1659        }
1660
1661        tx.commit().await.map_err(|e| {
1662            self.record_request_duration("sync", start);
1663            self.record_request_finished("sync", false);
1664            tracing::error!(request_id = %request_id, "Failed to commit transaction: {}", e);
1665            Status::internal("Internal error")
1666        })?;
1667
1668        let limit = if req.limit > 0 {
1669            req.limit.clamp(1, MAX_REQUEST_LIMIT)
1670        } else {
1671            1000
1672        };
1673
1674        let offset = if req.offset > 0 { req.offset } else { 0 };
1675
1676        // The sequence cursor is the exact delta cursor; the timestamp cursor
1677        // stays the filter for clients that send no sequence cursor. A
1678        // non-zero sequence in the request is the whole negotiation: there is
1679        // no separate round trip.
1680        let sequence_cursor = req.last_sync_sequence;
1681        let cursor = if sequence_cursor != 0 {
1682            db::DeltaCursor::Sequence(sequence_cursor)
1683        } else {
1684            db::DeltaCursor::Timestamp(req.last_sync_timestamp)
1685        };
1686
1687        // One page plus a `LIMIT limit + 1` probe row: `has_more` is derived
1688        // from the rows themselves, so the common single-page sync no longer
1689        // pays for a second `COUNT(*)` scan over the same range. `total`
1690        // keeps its exact meaning (see `Database::get_snippets_page`).
1691        let page = self
1692            .db
1693            .get_snippets_page(&user_id, &library_id, cursor, limit, offset, true)
1694            .await
1695            .map_err(|e| {
1696                self.record_request_duration("sync", start);
1697                self.record_request_finished("sync", false);
1698                tracing::error!("Internal error: {}", e);
1699                Status::internal("Internal error")
1700            })?;
1701
1702        let total = page.total;
1703        let has_more_by_rows = page.has_more;
1704
1705        let mut proto_snippets: Vec<ProtoSnippet> = page
1706            .snippets
1707            .into_iter()
1708            .map(|s| ProtoSnippet {
1709                id: s.id,
1710                description: s.description,
1711                command: s.command,
1712                tags: s.tags,
1713                created_at: s.created_at,
1714                updated_at: s.updated_at,
1715                device_id: s.device_id,
1716                deleted: s.deleted,
1717                encrypted: s.encrypted,
1718            })
1719            .collect();
1720
1721        // Bound the page by encoded bytes before deciding `has_more`: the
1722        // timestamp watermark below must be withheld whenever rows remain to
1723        // be fetched, including rows dropped here for size.
1724        let skipped_bytes: usize = skipped_ids.iter().map(|id| id.len() + 8).sum();
1725        let truncated = fit_page_to_byte_ceiling(
1726            &mut proto_snippets,
1727            page_byte_ceiling(self.config.grpc_max_message_size as usize, skipped_bytes),
1728        );
1729        let has_more = has_more_by_rows || truncated;
1730
1731        let timestamp = if has_more {
1732            // Don't advance timestamp when paginating — client needs to fetch remaining pages
1733            req.last_sync_timestamp
1734        } else {
1735            self.db
1736                .get_latest_timestamp(&user_id, &library_id)
1737                .await
1738                .map_err(|e| {
1739                    self.record_request_duration("sync", start);
1740                    self.record_request_finished("sync", false);
1741                    tracing::error!("Internal error: {}", e);
1742                    Status::internal("Internal error")
1743                })?
1744        };
1745
1746        // The sequence watermark is the server's answer to "which cursor am I
1747        // serving?". It is answered in exactly two cases:
1748        //
1749        // - the request carried a sequence cursor (the exact path), and
1750        // - the request carried no cursor at all (the bootstrap full sync),
1751        //   because that request delivers the complete library, so its
1752        //   watermark is adoptable by the client.
1753        //
1754        // A timestamp-only delta is answered with zero: a delta does not
1755        // deliver the library's whole history, so the client cannot adopt the
1756        // watermark without skipping rows it never received. Like the
1757        // timestamp, the sequence does not advance while paginating.
1758        //
1759        // A zero watermark is not reported as zero: in the protocol zero
1760        // means "no sequence", which a client cannot tell apart from a
1761        // server that does not serve one. The inclusive `sequence >=` filter
1762        // makes the reported position safe — a library that has issued
1763        // nothing yet reports the position before its first row, so that row
1764        // is still delivered when it appears.
1765        let sequence = if has_more {
1766            sequence_cursor
1767        } else if sequence_cursor != 0 || req.last_sync_timestamp == 0 {
1768            self.db
1769                .get_latest_sequence(&user_id, &library_id)
1770                .await
1771                .map_err(|e| {
1772                    self.record_request_duration("sync", start);
1773                    self.record_request_finished("sync", false);
1774                    tracing::error!("Internal error: {}", e);
1775                    Status::internal("Internal error")
1776                })?
1777                .max(1)
1778        } else {
1779            0
1780        };
1781
1782        let skipped_count = skipped_ids.len() as i32;
1783
1784        tracing::info!(
1785            request_id = %request_id,
1786            snippets_synced = proto_snippets.len(),
1787            skipped_count = skipped_count,
1788            has_more = has_more,
1789            "Sync completed"
1790        );
1791
1792        self.record_request_duration("sync", start);
1793
1794        let result = Ok(Response::new(SyncResponse {
1795            success: true,
1796            message: if skipped_count > 0 {
1797                format!("Sync completed, {} snippets skipped", skipped_count)
1798            } else {
1799                "Sync completed".to_string()
1800            },
1801            snippets: proto_snippets,
1802            server_timestamp: timestamp,
1803            server_sequence: sequence,
1804            skipped_count,
1805            skipped_ids,
1806            has_more,
1807            total_count: total,
1808        }));
1809        self.record_request_finished("sync", result.is_ok());
1810        result
1811    }
1812
1813    async fn create_library(
1814        &self,
1815        request: Request<CreateLibraryRequest>,
1816    ) -> Result<Response<CreateLibraryResponse>, Status> {
1817        let request_id = uuid::Uuid::new_v4();
1818        let start = std::time::Instant::now();
1819        self.record_request("create_library");
1820        self.record_library_op("create");
1821        tracing::info!(request_id = %request_id, "CreateLibrary request");
1822        let rate_limit_key = self.peer_rate_limit_key(&request);
1823        let api_key = self.request_api_key(&request, &request.get_ref().api_key);
1824        let req = request.into_inner();
1825
1826        let user_id = self
1827            .authenticate_and_rate_limit_with_duration(&api_key, &rate_limit_key, Some(start))
1828            .await
1829            .inspect_err(|_| {
1830                self.record_request_finished("create_library", false);
1831            })?;
1832
1833        let result = match self.db.create_library(&user_id, &req.name).await {
1834            Ok(lib_id) => {
1835                tracing::info!("Created library '{}' for user {}", req.name, user_id);
1836                self.record_request_duration("create_library", start);
1837                Ok(Response::new(CreateLibraryResponse {
1838                    success: true,
1839                    library_id: lib_id,
1840                    message: format!("Library '{}' created successfully", req.name),
1841                }))
1842            }
1843            Err(db::DbError::Conflict(msg)) => {
1844                self.record_request_duration("create_library", start);
1845                Err(Status::already_exists(msg))
1846            }
1847            Err(db::DbError::Validation(msg)) => {
1848                self.record_request_duration("create_library", start);
1849                Err(Status::invalid_argument(msg))
1850            }
1851            Err(db::DbError::NotFound(msg)) => {
1852                self.record_request_duration("create_library", start);
1853                Err(Status::invalid_argument(msg))
1854            }
1855            Err(db::DbError::Internal(msg)) => {
1856                tracing::error!("Internal error creating library: {}", msg);
1857                self.record_request_duration("create_library", start);
1858                Err(Status::internal("Internal error"))
1859            }
1860            Err(db::DbError::Database(e)) => {
1861                tracing::error!("Database error creating library: {}", e);
1862                self.record_request_duration("create_library", start);
1863                Err(Status::internal("Internal error"))
1864            }
1865        };
1866        self.record_request_finished("create_library", result.is_ok());
1867        result
1868    }
1869
1870    async fn list_libraries(
1871        &self,
1872        request: Request<ListLibrariesRequest>,
1873    ) -> Result<Response<ListLibrariesResponse>, Status> {
1874        let request_id = uuid::Uuid::new_v4();
1875        let start = std::time::Instant::now();
1876        self.record_request("list_libraries");
1877        self.record_library_op("list");
1878        tracing::info!(request_id = %request_id, "ListLibraries request");
1879        let rate_limit_key = self.peer_rate_limit_key(&request);
1880        let api_key = self.request_api_key(&request, &request.get_ref().api_key);
1881        let req = request.into_inner();
1882
1883        let user_id = self
1884            .authenticate_and_rate_limit_with_duration(&api_key, &rate_limit_key, Some(start))
1885            .await
1886            .inspect_err(|_| {
1887                self.record_request_finished("list_libraries", false);
1888            })?;
1889
1890        let limit = if req.limit > 0 {
1891            req.limit.clamp(1, MAX_REQUEST_LIMIT)
1892        } else {
1893            50
1894        };
1895        let offset = if req.offset > 0 { req.offset } else { 0 };
1896
1897        let (libraries, total) = self
1898            .db
1899            .list_libraries(&user_id, limit, offset)
1900            .await
1901            .map_err(|e| {
1902                self.record_request_duration("list_libraries", start);
1903                self.record_request_finished("list_libraries", false);
1904                tracing::error!("Internal error: {}", e);
1905                Status::internal("Internal error")
1906            })?;
1907
1908        let has_more = offset.saturating_add(libraries.len() as i32) < total;
1909
1910        let proto_libraries: Vec<Library> = libraries
1911            .into_iter()
1912            .map(|l| Library {
1913                id: l.id,
1914                name: l.name,
1915                created_at: l.created_at,
1916                snippet_count: l.snippet_count,
1917            })
1918            .collect();
1919
1920        self.record_request_duration("list_libraries", start);
1921
1922        let result = Ok(Response::new(ListLibrariesResponse {
1923            libraries: proto_libraries,
1924            total_count: total,
1925            has_more,
1926        }));
1927        self.record_request_finished("list_libraries", result.is_ok());
1928        result
1929    }
1930
1931    async fn delete_library(
1932        &self,
1933        request: Request<DeleteLibraryRequest>,
1934    ) -> Result<Response<DeleteLibraryResponse>, Status> {
1935        let request_id = uuid::Uuid::new_v4();
1936        let start = std::time::Instant::now();
1937        self.record_request("delete_library");
1938        self.record_library_op("delete");
1939        tracing::info!(request_id = %request_id, "DeleteLibrary request");
1940        let rate_limit_key = self.peer_rate_limit_key(&request);
1941        let api_key = self.request_api_key(&request, &request.get_ref().api_key);
1942        let req = request.into_inner();
1943
1944        let user_id = self
1945            .authenticate_and_rate_limit_with_duration(&api_key, &rate_limit_key, Some(start))
1946            .await
1947            .inspect_err(|_| {
1948                self.record_request_finished("delete_library", false);
1949            })?;
1950
1951        // Prevent deleting default library — resolve the actual UUID, not
1952        // the literal string "default" which is just the library name.
1953        if req.library_id.is_empty() || req.library_id == "default" {
1954            self.record_request_duration("delete_library", start);
1955            self.record_request_finished("delete_library", false);
1956            return Err(Status::invalid_argument("Cannot delete default library"));
1957        }
1958        if let Ok(default_id) = self.db.get_default_library(&user_id).await
1959            && req.library_id == default_id
1960        {
1961            self.record_request_duration("delete_library", start);
1962            self.record_request_finished("delete_library", false);
1963            return Err(Status::invalid_argument("Cannot delete default library"));
1964        }
1965
1966        let result = match self.db.delete_library(&user_id, &req.library_id).await {
1967            Ok(_) => {
1968                tracing::info!("Deleted library {} for user {}", req.library_id, user_id);
1969                Ok(Response::new(DeleteLibraryResponse {
1970                    success: true,
1971                    message: "Library deleted successfully".to_string(),
1972                }))
1973            }
1974            Err(db::DbError::NotFound(msg)) => Err(Status::not_found(msg)),
1975            Err(db::DbError::Conflict(msg)) => Err(Status::failed_precondition(msg)),
1976            Err(db::DbError::Validation(msg)) => Err(Status::invalid_argument(msg)),
1977            Err(db::DbError::Internal(msg)) => {
1978                tracing::error!("Internal error deleting library: {}", msg);
1979                Err(Status::internal("Internal error"))
1980            }
1981            Err(db::DbError::Database(e)) => {
1982                tracing::error!("Failed to delete library: {}", e);
1983                Err(Status::internal("Internal error"))
1984            }
1985        };
1986        self.record_request_duration("delete_library", start);
1987        self.record_request_finished("delete_library", result.is_ok());
1988        result
1989    }
1990
1991    async fn list_premade_libraries(
1992        &self,
1993        request: Request<ListPremadeLibrariesRequest>,
1994    ) -> Result<Response<ListPremadeLibrariesResponse>, Status> {
1995        let request_id = uuid::Uuid::new_v4();
1996        let start = std::time::Instant::now();
1997        self.record_request("list_premade_libraries");
1998        tracing::info!(request_id = %request_id, "ListPremadeLibraries request");
1999        let rate_limit_key = self.peer_rate_limit_key(&request);
2000        let api_key = self.request_api_key(&request, &request.get_ref().api_key);
2001        let _req = request.into_inner();
2002
2003        let user_id = self
2004            .authenticate_and_rate_limit_with_duration(&api_key, &rate_limit_key, Some(start))
2005            .await
2006            .inspect_err(|_| {
2007                self.record_request_finished("list_premade_libraries", false);
2008            })?;
2009
2010        let libraries = self.premade_manager.list();
2011
2012        let proto_libraries: Vec<ProtoPremadeLibrary> = libraries
2013            .into_iter()
2014            .map(|l| ProtoPremadeLibrary {
2015                name: l.name,
2016                filename: l.filename,
2017                description: l.description,
2018                snippet_count: l.snippet_count,
2019                tags: l.tags,
2020            })
2021            .collect();
2022
2023        tracing::info!(
2024            request_id = %request_id,
2025            user_id = %user_id,
2026            count = proto_libraries.len(),
2027            "ListPremadeLibraries completed"
2028        );
2029
2030        let count = proto_libraries.len() as i32;
2031
2032        self.record_request_duration("list_premade_libraries", start);
2033
2034        let result = Ok(Response::new(ListPremadeLibrariesResponse {
2035            libraries: proto_libraries,
2036            has_more: false,
2037            total_count: count,
2038        }));
2039        self.record_request_finished("list_premade_libraries", result.is_ok());
2040        result
2041    }
2042
2043    async fn get_premade_library(
2044        &self,
2045        request: Request<GetPremadeLibraryRequest>,
2046    ) -> Result<Response<GetPremadeLibraryResponse>, Status> {
2047        let request_id = uuid::Uuid::new_v4();
2048        let start = std::time::Instant::now();
2049        self.record_request("get_premade_library");
2050        tracing::info!(request_id = %request_id, "GetPremadeLibrary request");
2051        let rate_limit_key = self.peer_rate_limit_key(&request);
2052        let api_key = self.request_api_key(&request, &request.get_ref().api_key);
2053        let req = request.into_inner();
2054
2055        let user_id = self
2056            .authenticate_and_rate_limit_with_duration(&api_key, &rate_limit_key, Some(start))
2057            .await
2058            .inspect_err(|_| {
2059                self.record_request_finished("get_premade_library", false);
2060            })?;
2061
2062        if req.filename.is_empty() {
2063            self.record_request_duration("get_premade_library", start);
2064            self.record_request_finished("get_premade_library", false);
2065            return Err(Status::invalid_argument("Filename is required"));
2066        }
2067
2068        if req.filename.contains("..") || req.filename.contains('/') || req.filename.contains('\\')
2069        {
2070            self.record_request_duration("get_premade_library", start);
2071            self.record_request_finished("get_premade_library", false);
2072            return Err(Status::invalid_argument("Invalid filename"));
2073        }
2074
2075        let sanitized: String = req
2076            .filename
2077            .chars()
2078            .filter(|c| c.is_alphanumeric() || *c == '-' || *c == '_' || *c == '.')
2079            .collect();
2080
2081        if sanitized.is_empty() || sanitized.len() > 64 {
2082            self.record_request_duration("get_premade_library", start);
2083            self.record_request_finished("get_premade_library", false);
2084            return Err(Status::invalid_argument("Invalid filename"));
2085        }
2086        if sanitized != req.filename {
2087            self.record_request_duration("get_premade_library", start);
2088            self.record_request_finished("get_premade_library", false);
2089            return Err(Status::invalid_argument("Invalid filename"));
2090        }
2091
2092        let content = match self.premade_manager.get(&sanitized) {
2093            Ok(c) => c,
2094            Err(e) => {
2095                self.record_request_duration("get_premade_library", start);
2096                self.record_request_finished("get_premade_library", false);
2097                return Err(e);
2098            }
2099        };
2100
2101        tracing::info!("User {} fetched premade library '{}'", user_id, sanitized);
2102
2103        self.record_request_duration("get_premade_library", start);
2104
2105        let result = Ok(Response::new(GetPremadeLibraryResponse {
2106            success: true,
2107            name: sanitized.clone(),
2108            content,
2109            message: "Library fetched successfully".to_string(),
2110        }));
2111        self.record_request_finished("get_premade_library", result.is_ok());
2112        result
2113    }
2114
2115    async fn search_premade_libraries(
2116        &self,
2117        request: Request<SearchPremadeLibrariesRequest>,
2118    ) -> Result<Response<SearchPremadeLibrariesResponse>, Status> {
2119        let request_id = uuid::Uuid::new_v4();
2120        let start = std::time::Instant::now();
2121        self.record_request("search_premade_libraries");
2122        tracing::info!(request_id = %request_id, "SearchPremadeLibraries request");
2123        let rate_limit_key = self.peer_rate_limit_key(&request);
2124        let api_key = self.request_api_key(&request, &request.get_ref().api_key);
2125        let req = request.into_inner();
2126
2127        let _user_id = self
2128            .authenticate_and_rate_limit_with_duration(&api_key, &rate_limit_key, Some(start))
2129            .await
2130            .inspect_err(|_| {
2131                self.record_request_finished("search_premade_libraries", false);
2132            })?;
2133
2134        if req.query.is_empty() {
2135            self.record_request_duration("search_premade_libraries", start);
2136            self.record_request_finished("search_premade_libraries", false);
2137            return Err(Status::invalid_argument("Query is required"));
2138        }
2139
2140        let results = self.premade_manager.search(&req.query);
2141
2142        let proto_libraries: Vec<ProtoPremadeLibrary> = results
2143            .into_iter()
2144            .map(|l| ProtoPremadeLibrary {
2145                name: l.name,
2146                filename: l.filename,
2147                description: l.description,
2148                snippet_count: l.snippet_count,
2149                tags: l.tags,
2150            })
2151            .collect();
2152
2153        let count = proto_libraries.len() as i32;
2154
2155        tracing::info!(
2156            request_id = %request_id,
2157            query = %req.query,
2158            count = count,
2159            "SearchPremadeLibraries completed"
2160        );
2161
2162        self.record_request_duration("search_premade_libraries", start);
2163
2164        let result = Ok(Response::new(SearchPremadeLibrariesResponse {
2165            libraries: proto_libraries,
2166            total_count: count,
2167        }));
2168        self.record_request_finished("search_premade_libraries", result.is_ok());
2169        result
2170    }
2171}
2172
2173#[cfg(test)]
2174mod tests {
2175    use super::*;
2176    use snip_proto::{
2177        CreateLibraryRequest, GetPremadeLibraryRequest, GetSnippetsRequest, HealthRequest,
2178        ListLibrariesRequest, PushSnippetsRequest, RegisterRequest, Snippet as ProtoSnippet,
2179        SyncRequest,
2180    };
2181
2182    pub async fn setup_test_service() -> SnipSyncService {
2183        setup_test_service_with_rate_limit(DEFAULT_RATE_LIMIT_PER_MINUTE).await
2184    }
2185
2186    async fn setup_test_service_with_rate_limit(rate_limit_per_minute: u32) -> SnipSyncService {
2187        let db = Arc::new(Database::connect("sqlite::memory:", 5).await.unwrap());
2188        let config = Config {
2189            grpc_host: "127.0.0.1".to_string(),
2190            grpc_port: 50051,
2191            http_host: "127.0.0.1".to_string(),
2192            http_port: 50050,
2193            db_path: "sqlite::memory:".to_string(),
2194            db_max_connections: 5,
2195            premade_dir: PathBuf::from("premade-libraries"),
2196            max_command_length: 1024,
2197            max_encrypted_payload_length: DEFAULT_MAX_ENCRYPTED_PAYLOAD_LENGTH,
2198            max_description_length: 1024,
2199            max_tags: 50,
2200            max_tag_length: 100,
2201            max_id_length: 128,
2202            max_device_id_length: 128,
2203            max_api_key_length: 512,
2204            request_timeout_secs: 30,
2205            grpc_max_message_size: 4 * 1024 * 1024,
2206            rate_limit_per_minute,
2207            register_per_minute: DEFAULT_REGISTER_PER_MINUTE,
2208            metrics_auth_failures_per_minute: DEFAULT_METRICS_AUTH_FAILURES_PER_MINUTE,
2209            trusted_proxies: vec![],
2210            metrics_username: None,
2211            metrics_password: None,
2212            cors_allowed_origins: vec![],
2213        };
2214        let metrics = Metrics::fallback();
2215        let rate_limiter = Arc::new(RateLimiter::new());
2216        let premade_manager = PremadeManager::new(PathBuf::from("premade-libraries"));
2217
2218        SnipSyncService {
2219            db,
2220            rate_limiter,
2221            config,
2222            metrics,
2223            premade_manager,
2224            #[cfg(any(test, feature = "test-helpers"))]
2225            captured_auth_header: Arc::new(std::sync::Mutex::new(None)),
2226            test_observer: None,
2227            push_fail_after: Arc::new(std::sync::atomic::AtomicU32::new(u32::MAX)),
2228            push_fail_counter: Arc::new(std::sync::atomic::AtomicU32::new(0)),
2229        }
2230    }
2231
2232    pub async fn register_test_user(service: &SnipSyncService) -> String {
2233        let req = Request::new(RegisterRequest {
2234            device_id: "test-device".to_string(),
2235        });
2236        let resp = service.register(req).await.unwrap();
2237        resp.into_inner().api_key
2238    }
2239
2240    #[tokio::test]
2241    async fn test_health_check() {
2242        let service = setup_test_service().await;
2243        let req = Request::new(HealthRequest {});
2244        let resp = service.health(req).await.unwrap();
2245        let health = resp.into_inner();
2246        assert!(health.healthy);
2247        assert_eq!(health.version, env!("CARGO_PKG_VERSION"));
2248    }
2249
2250    #[tokio::test]
2251    async fn test_register_creates_user() {
2252        let service = setup_test_service().await;
2253        let req = Request::new(RegisterRequest {
2254            device_id: "test-device".to_string(),
2255        });
2256        let resp = service.register(req).await.unwrap();
2257        let reg = resp.into_inner();
2258        assert!(reg.success);
2259        assert!(!reg.api_key.is_empty());
2260        assert!(!reg.device_id.is_empty());
2261        assert!(uuid::Uuid::parse_str(&reg.device_id).is_ok());
2262    }
2263
2264    #[tokio::test]
2265    async fn test_register_rate_limiting() {
2266        let service = setup_test_service_with_rate_limit(3).await;
2267        for _ in 0..3 {
2268            let req = Request::new(RegisterRequest {
2269                device_id: "test-device".to_string(),
2270            });
2271            let _ = service.register(req).await;
2272        }
2273        let req = Request::new(RegisterRequest {
2274            device_id: "test-device".to_string(),
2275        });
2276        let resp = service.register(req).await;
2277        assert!(resp.is_err());
2278        assert_eq!(resp.unwrap_err().code(), tonic::Code::ResourceExhausted);
2279    }
2280
2281    #[tokio::test]
2282    async fn peer_rate_limit_key_uses_the_peer_socket_address() {
2283        let service = setup_test_service().await;
2284        let mut req = Request::new(RegisterRequest {
2285            device_id: "test-device".to_string(),
2286        });
2287        assert_eq!(service.peer_rate_limit_key(&req), "unknown");
2288        req.extensions_mut()
2289            .insert(tonic::transport::server::TcpConnectInfo {
2290                local_addr: None,
2291                remote_addr: Some("203.0.113.7:44321".parse().unwrap()),
2292            });
2293        assert_eq!(service.peer_rate_limit_key(&req), "203.0.113.7");
2294    }
2295
2296    #[tokio::test]
2297    async fn peer_rate_limit_key_honours_x_forwarded_for_from_a_trusted_proxy() {
2298        let mut service = setup_test_service().await;
2299        service.config.trusted_proxies = vec!["198.51.100.1".to_string()];
2300
2301        let mut req = Request::new(RegisterRequest {
2302            device_id: "test-device".to_string(),
2303        });
2304        req.extensions_mut()
2305            .insert(tonic::transport::server::TcpConnectInfo {
2306                local_addr: None,
2307                remote_addr: Some("198.51.100.1:44321".parse().unwrap()),
2308            });
2309        req.metadata_mut()
2310            .insert("x-forwarded-for", "203.0.113.9:5555".parse().unwrap());
2311        assert_eq!(service.peer_rate_limit_key(&req), "203.0.113.9");
2312    }
2313
2314    #[test]
2315    fn strip_port_handles_malformed_input_without_panicking() {
2316        // A `]:` at offset 0 used to slice `addr[1..0]` and panic.
2317        assert_eq!(strip_port("]:"), "]:");
2318        assert_eq!(strip_port("1.2.3.4]:80"), "1.2.3.4]:80");
2319        assert_eq!(strip_port("[::1]:443"), "::1");
2320        assert_eq!(strip_port("[::1]"), "[::1]");
2321        assert_eq!(strip_port("1.2.3.4:80"), "1.2.3.4");
2322        assert_eq!(strip_port("::1"), "::1");
2323        assert_eq!(strip_port(""), "");
2324    }
2325
2326    #[tokio::test]
2327    async fn test_create_and_list_libraries() {
2328        let service = setup_test_service().await;
2329        let api_key = register_test_user(&service).await;
2330
2331        let req = Request::new(CreateLibraryRequest {
2332            api_key: api_key.clone(),
2333            name: "test-lib".to_string(),
2334        });
2335        let resp = service.create_library(req).await.unwrap();
2336        let create = resp.into_inner();
2337        assert!(create.success);
2338        assert!(!create.library_id.is_empty());
2339
2340        let req = Request::new(ListLibrariesRequest {
2341            api_key: api_key.clone(),
2342            limit: 100,
2343            offset: 0,
2344        });
2345        let resp = service.list_libraries(req).await.unwrap();
2346        let list = resp.into_inner();
2347        assert_eq!(list.libraries.len(), 2);
2348        assert!(list.libraries.iter().any(|l| l.name == "test-lib"));
2349    }
2350
2351    #[tokio::test]
2352    async fn test_create_duplicate_library_name() {
2353        let service = setup_test_service().await;
2354        let api_key = register_test_user(&service).await;
2355
2356        let req = Request::new(CreateLibraryRequest {
2357            api_key: api_key.clone(),
2358            name: "test-lib".to_string(),
2359        });
2360        let _ = service.create_library(req).await.unwrap();
2361
2362        let req = Request::new(CreateLibraryRequest {
2363            api_key: api_key.clone(),
2364            name: "test-lib".to_string(),
2365        });
2366        let resp = service.create_library(req).await;
2367        assert!(resp.is_err());
2368        assert_eq!(resp.unwrap_err().code(), tonic::Code::AlreadyExists);
2369    }
2370
2371    #[tokio::test]
2372    async fn test_invalid_api_key() {
2373        let service = setup_test_service().await;
2374        let req = Request::new(GetSnippetsRequest {
2375            api_key: "invalid-key".to_string(),
2376            library_id: String::new(),
2377            limit: 100,
2378            offset: 0,
2379            since: 0,
2380        });
2381        let resp = service.get_snippets(req).await;
2382        assert!(resp.is_err());
2383        assert_eq!(resp.unwrap_err().code(), tonic::Code::Unauthenticated);
2384    }
2385
2386    #[tokio::test]
2387    async fn test_validate_snippet_empty_id() {
2388        let service = setup_test_service().await;
2389        let snippet = ProtoSnippet {
2390            id: String::new(),
2391            description: "test".to_string(),
2392            command: "echo hello".to_string(),
2393            tags: vec![],
2394            created_at: 0,
2395            updated_at: 0,
2396            device_id: "device".to_string(),
2397            deleted: false,
2398            encrypted: false,
2399        };
2400        let result = service.validate_snippet(&snippet);
2401        assert!(result.is_err());
2402        assert!(result.unwrap_err().message().contains("ID is required"));
2403    }
2404
2405    #[tokio::test]
2406    async fn test_validate_snippet_oversized_command() {
2407        let service = setup_test_service().await;
2408        let snippet = ProtoSnippet {
2409            id: "test-id".to_string(),
2410            description: "test".to_string(),
2411            command: "x".repeat(2000),
2412            tags: vec![],
2413            created_at: 0,
2414            updated_at: 0,
2415            device_id: "device".to_string(),
2416            deleted: false,
2417            encrypted: false,
2418        };
2419        let result = service.validate_snippet(&snippet);
2420        assert!(result.is_err());
2421        assert!(result.unwrap_err().message().contains("maximum length"));
2422    }
2423
2424    #[tokio::test]
2425    async fn test_validate_snippet_future_timestamp() {
2426        let service = setup_test_service().await;
2427        let future = time::OffsetDateTime::now_utc().unix_timestamp() + 600;
2428        let snippet = ProtoSnippet {
2429            id: "test-id".to_string(),
2430            description: "test".to_string(),
2431            command: "echo hello".to_string(),
2432            tags: vec![],
2433            created_at: future,
2434            updated_at: future,
2435            device_id: "device".to_string(),
2436            deleted: false,
2437            encrypted: false,
2438        };
2439        let result = service.validate_snippet(&snippet);
2440        assert!(result.is_err());
2441        let msg = result.unwrap_err().message().to_string();
2442        assert!(
2443            msg.contains("ahead of server time"),
2444            "Expected clock skew message, got: {msg}"
2445        );
2446    }
2447
2448    #[tokio::test]
2449    async fn test_validate_snippet_negative_timestamp() {
2450        let service = setup_test_service().await;
2451        let snippet = ProtoSnippet {
2452            id: "test-id".to_string(),
2453            description: "test".to_string(),
2454            command: "echo hello".to_string(),
2455            tags: vec![],
2456            created_at: -1,
2457            updated_at: -1,
2458            device_id: "device".to_string(),
2459            deleted: false,
2460            encrypted: false,
2461        };
2462        let result = service.validate_snippet(&snippet);
2463        assert!(result.is_err());
2464        assert!(result.unwrap_err().message().contains("non-negative"));
2465    }
2466
2467    #[tokio::test]
2468    async fn test_validate_snippet_clock_skew_reports_magnitude() {
2469        let service = setup_test_service().await;
2470        let now = time::OffsetDateTime::now_utc().unix_timestamp();
2471        let snippet = ProtoSnippet {
2472            id: "skew-id".to_string(),
2473            description: "test".to_string(),
2474            command: "echo hello".to_string(),
2475            tags: vec![],
2476            created_at: now,
2477            updated_at: now + 742, // 742 seconds ahead
2478            device_id: "device".to_string(),
2479            deleted: false,
2480            encrypted: false,
2481        };
2482        let result = service.validate_snippet(&snippet);
2483        assert!(result.is_err());
2484        let msg = result.unwrap_err().message().to_string();
2485        assert!(
2486            msg.contains("742"),
2487            "Clock skew message should contain magnitude, got: {msg}"
2488        );
2489        assert!(
2490            msg.contains("synchronize the client clock"),
2491            "Clock skew message should contain corrective action, got: {msg}"
2492        );
2493    }
2494
2495    #[tokio::test]
2496    async fn test_validate_snippet_created_ahead_reports_skew() {
2497        let service = setup_test_service().await;
2498        let now = time::OffsetDateTime::now_utc().unix_timestamp();
2499        let snippet = ProtoSnippet {
2500            id: "skew-created".to_string(),
2501            description: "test".to_string(),
2502            command: "echo hello".to_string(),
2503            tags: vec![],
2504            created_at: now + 500,
2505            updated_at: now, // updated_at is fine, only created_at is ahead
2506            device_id: "device".to_string(),
2507            deleted: false,
2508            encrypted: false,
2509        };
2510        let result = service.validate_snippet(&snippet);
2511        assert!(result.is_err());
2512        let msg = result.unwrap_err().message().to_string();
2513        assert!(
2514            msg.contains("created_at"),
2515            "Should mention created_at: {msg}"
2516        );
2517        assert!(msg.contains("500"), "Should contain skew magnitude: {msg}");
2518    }
2519
2520    #[tokio::test]
2521    async fn test_validate_snippet_too_many_tags() {
2522        let service = setup_test_service().await;
2523        let snippet = ProtoSnippet {
2524            id: "test-id".to_string(),
2525            description: "test".to_string(),
2526            command: "echo hello".to_string(),
2527            tags: (0..100).map(|i| format!("tag{}", i)).collect(),
2528            created_at: 0,
2529            updated_at: 0,
2530            device_id: "device".to_string(),
2531            deleted: false,
2532            encrypted: false,
2533        };
2534        let result = service.validate_snippet(&snippet);
2535        assert!(result.is_err());
2536        assert!(result.unwrap_err().message().contains("Too many tags"));
2537    }
2538
2539    #[tokio::test]
2540    async fn test_validate_snippet_valid() {
2541        let service = setup_test_service().await;
2542        let now = time::OffsetDateTime::now_utc().unix_timestamp();
2543        let snippet = ProtoSnippet {
2544            id: "test-id".to_string(),
2545            description: "test".to_string(),
2546            command: "echo hello".to_string(),
2547            tags: vec!["bash".to_string()],
2548            created_at: now,
2549            updated_at: now,
2550            device_id: "device".to_string(),
2551            deleted: false,
2552            encrypted: false,
2553        };
2554        assert!(service.validate_snippet(&snippet).is_ok());
2555    }
2556
2557    #[tokio::test]
2558    async fn test_validate_snippet_zero_timestamp_rejected() {
2559        let service = setup_test_service().await;
2560        let snippet = ProtoSnippet {
2561            id: "epoch-id".to_string(),
2562            description: "test".to_string(),
2563            command: "echo hello".to_string(),
2564            tags: vec![],
2565            created_at: 0,
2566            updated_at: 0,
2567            device_id: "device".to_string(),
2568            deleted: false,
2569            encrypted: false,
2570        };
2571        let result = service.validate_snippet(&snippet);
2572        assert!(result.is_err());
2573        let msg = result.unwrap_err().message().to_string();
2574        assert!(
2575            msg.contains("unset client clock"),
2576            "Expected zero-timestamp message, got: {msg}"
2577        );
2578    }
2579
2580    #[tokio::test]
2581    async fn test_cross_user_library_access_denied() {
2582        let service = setup_test_service().await;
2583        let api_key1 = register_test_user(&service).await;
2584
2585        let req = Request::new(CreateLibraryRequest {
2586            api_key: api_key1.clone(),
2587            name: "user1-lib".to_string(),
2588        });
2589        let resp = service.create_library(req).await.unwrap();
2590        let library_id = resp.into_inner().library_id;
2591
2592        let api_key2 = register_test_user(&service).await;
2593
2594        let req = Request::new(GetSnippetsRequest {
2595            api_key: api_key2,
2596            library_id,
2597            limit: 100,
2598            offset: 0,
2599            since: 0,
2600        });
2601        let resp = service.get_snippets(req).await;
2602        assert!(resp.is_err());
2603        assert_eq!(resp.unwrap_err().code(), tonic::Code::NotFound);
2604    }
2605
2606    #[tokio::test]
2607    async fn test_duplicate_id_across_libraries_is_rejected_not_silently_dropped() {
2608        // `snippets.id` is a global primary key. Pushing the same id into a
2609        // second library of the same user writes zero rows, and SQLite raises
2610        // no error for the discarded `DO UPDATE`. Counting that as accepted
2611        // would tell the client the upload succeeded while the snippet is
2612        // absent from the server forever.
2613        let service = setup_test_service().await;
2614        let api_key = register_test_user(&service).await;
2615
2616        let work = service
2617            .create_library(Request::new(CreateLibraryRequest {
2618                api_key: api_key.clone(),
2619                name: "work".to_string(),
2620            }))
2621            .await
2622            .unwrap()
2623            .into_inner()
2624            .library_id;
2625        let personal = service
2626            .create_library(Request::new(CreateLibraryRequest {
2627                api_key: api_key.clone(),
2628                name: "personal".to_string(),
2629            }))
2630            .await
2631            .unwrap()
2632            .into_inner()
2633            .library_id;
2634
2635        let snippet = ProtoSnippet {
2636            id: "legacy-abc123".to_string(),
2637            description: "git status".to_string(),
2638            command: "git status".to_string(),
2639            tags: vec![],
2640            created_at: time::OffsetDateTime::now_utc().unix_timestamp(),
2641            updated_at: time::OffsetDateTime::now_utc().unix_timestamp(),
2642            device_id: "device-1".to_string(),
2643            deleted: false,
2644            encrypted: false,
2645        };
2646
2647        let first = service
2648            .push_snippets(Request::new(PushSnippetsRequest {
2649                api_key: api_key.clone(),
2650                library_id: work.clone(),
2651                snippets: vec![snippet.clone()],
2652            }))
2653            .await
2654            .unwrap()
2655            .into_inner();
2656        assert_eq!(first.accepted_count, 1);
2657        assert_eq!(first.rejected_count, 0);
2658
2659        let second = service
2660            .push_snippets(Request::new(PushSnippetsRequest {
2661                api_key: api_key.clone(),
2662                library_id: personal.clone(),
2663                snippets: vec![snippet],
2664            }))
2665            .await
2666            .unwrap()
2667            .into_inner();
2668        assert_eq!(
2669            second.rejected_count, 1,
2670            "cross-library id collision must be rejected, not accepted"
2671        );
2672        assert_eq!(
2673            second.accepted_count, 0,
2674            "the dropped write must not be counted as accepted"
2675        );
2676        assert!(!second.success, "a partial upload must not report success");
2677
2678        // The snippet really is only stored under the first library.
2679        let other = service
2680            .get_snippets(Request::new(GetSnippetsRequest {
2681                api_key: api_key.clone(),
2682                library_id: personal.clone(),
2683                limit: 100,
2684                offset: 0,
2685                since: 0,
2686            }))
2687            .await
2688            .unwrap()
2689            .into_inner();
2690        assert!(other.snippets.is_empty());
2691
2692        // And `sync` reports the same collision in `skipped_ids`.
2693        let sync = service
2694            .sync(Request::new(SyncRequest {
2695                api_key: api_key.clone(),
2696                local_snippets: vec![ProtoSnippet {
2697                    id: "legacy-abc123".to_string(),
2698                    description: "git status".to_string(),
2699                    command: "git status".to_string(),
2700                    tags: vec![],
2701                    created_at: time::OffsetDateTime::now_utc().unix_timestamp(),
2702                    updated_at: time::OffsetDateTime::now_utc().unix_timestamp(),
2703                    device_id: "device-1".to_string(),
2704                    deleted: false,
2705                    encrypted: false,
2706                }],
2707                last_sync_timestamp: 0,
2708                last_sync_sequence: 0,
2709                library_id: personal,
2710                limit: 100,
2711                offset: 0,
2712            }))
2713            .await
2714            .unwrap()
2715            .into_inner();
2716        assert_eq!(
2717            sync.skipped_ids,
2718            vec!["legacy-abc123".to_string()],
2719            "sync must surface the skipped id instead of reporting a clean run"
2720        );
2721        assert_eq!(sync.skipped_count, 1);
2722    }
2723
2724    #[tokio::test]
2725    async fn test_older_version_in_same_library_is_accepted_as_stale() {
2726        // A genuine last-write-wins dedupe also writes zero rows, but it must
2727        // NOT be reported as a rejection — only a foreign-scope collision is.
2728        let service = setup_test_service().await;
2729        let api_key = register_test_user(&service).await;
2730        let library_id = service
2731            .create_library(Request::new(CreateLibraryRequest {
2732                api_key: api_key.clone(),
2733                name: "work".to_string(),
2734            }))
2735            .await
2736            .unwrap()
2737            .into_inner()
2738            .library_id;
2739
2740        let now = time::OffsetDateTime::now_utc().unix_timestamp();
2741        let newer = ProtoSnippet {
2742            id: "snippet-1".to_string(),
2743            description: "newer".to_string(),
2744            command: "echo newer".to_string(),
2745            tags: vec![],
2746            created_at: now,
2747            updated_at: now,
2748            device_id: "device-1".to_string(),
2749            deleted: false,
2750            encrypted: false,
2751        };
2752        let older = ProtoSnippet {
2753            description: "older".to_string(),
2754            command: "echo older".to_string(),
2755            created_at: now - 500,
2756            updated_at: now - 500,
2757            ..newer.clone()
2758        };
2759
2760        for snippet in [newer, older] {
2761            let push = service
2762                .push_snippets(Request::new(PushSnippetsRequest {
2763                    api_key: api_key.clone(),
2764                    library_id: library_id.clone(),
2765                    snippets: vec![snippet],
2766                }))
2767                .await
2768                .unwrap()
2769                .into_inner();
2770            assert_eq!(push.accepted_count, 1);
2771            assert_eq!(push.rejected_count, 0, "stale write is not a rejection");
2772        }
2773
2774        let list = service
2775            .get_snippets(Request::new(GetSnippetsRequest {
2776                api_key,
2777                library_id,
2778                limit: 100,
2779                offset: 0,
2780                since: 0,
2781            }))
2782            .await
2783            .unwrap()
2784            .into_inner();
2785        assert_eq!(list.snippets.len(), 1);
2786        assert_eq!(list.snippets[0].command, "echo newer");
2787    }
2788
2789    #[test]
2790    fn test_fit_page_to_byte_ceiling_keeps_page_within_budget() {
2791        // Each row is ~1 KiB of command. With a 4 KiB budget only some rows
2792        // fit; the rest must be dropped *and reported* so the client still
2793        // paginates. Dropping them silently would lose records.
2794        let row = |i: usize| ProtoSnippet {
2795            id: format!("snippet-{i}"),
2796            description: "d".repeat(1024),
2797            command: "c".repeat(1024),
2798            tags: vec![],
2799            created_at: 1,
2800            updated_at: 1,
2801            device_id: "dev".to_string(),
2802            deleted: false,
2803            encrypted: false,
2804        };
2805        let mut page: Vec<ProtoSnippet> = (0..10).map(row).collect();
2806        let budget = 4 * 1024usize;
2807
2808        let truncated = fit_page_to_byte_ceiling(&mut page, budget);
2809
2810        assert!(truncated, "page must report that rows were dropped");
2811        assert!(!page.is_empty(), "at least one row must always be kept");
2812        assert!(
2813            page.len() < 10,
2814            "rows beyond the byte budget must be dropped, got {}",
2815            page.len()
2816        );
2817        assert!(
2818            page.iter().map(|s| s.encoded_len()).sum::<usize>() <= budget,
2819            "kept rows must fit the budget"
2820        );
2821        // Dropping is a tail-trim, so the prefix is preserved and the client
2822        // can resume from `offset + kept`.
2823        assert_eq!(page[0].id, "snippet-0");
2824    }
2825
2826    #[test]
2827    fn test_fit_page_to_byte_ceiling_keeps_at_least_one_oversized_row() {
2828        // A single row larger than the whole budget cannot be made to fit.
2829        // Returning an empty page would stall the client forever, so the row is
2830        // returned anyway and left for the transport to report.
2831        let mut page = vec![ProtoSnippet {
2832            id: "huge".to_string(),
2833            description: String::new(),
2834            command: "x".repeat(64 * 1024),
2835            tags: vec![],
2836            created_at: 1,
2837            updated_at: 1,
2838            device_id: "dev".to_string(),
2839            deleted: false,
2840            encrypted: true,
2841        }];
2842        let truncated = fit_page_to_byte_ceiling(&mut page, 1024);
2843        assert_eq!(page.len(), 1, "an oversized row must still be returned");
2844        assert!(!truncated, "nothing was actually dropped");
2845    }
2846
2847    #[test]
2848    fn test_fit_page_to_byte_ceiling_noop_when_page_fits() {
2849        let mut page = vec![ProtoSnippet {
2850            id: "small".to_string(),
2851            description: "d".to_string(),
2852            command: "echo hi".to_string(),
2853            tags: vec![],
2854            created_at: 1,
2855            updated_at: 1,
2856            device_id: "dev".to_string(),
2857            deleted: false,
2858            encrypted: false,
2859        }];
2860        assert!(!fit_page_to_byte_ceiling(&mut page, 1024 * 1024));
2861        assert_eq!(page.len(), 1);
2862    }
2863
2864    #[test]
2865    fn test_page_byte_ceiling_reserves_envelope_space() {
2866        assert_eq!(
2867            page_byte_ceiling(4 * 1024 * 1024, 0),
2868            4 * 1024 * 1024 - RESPONSE_ENVELOPE_RESERVE_BYTES
2869        );
2870        assert_eq!(
2871            page_byte_ceiling(4 * 1024 * 1024, 2048),
2872            4 * 1024 * 1024 - RESPONSE_ENVELOPE_RESERVE_BYTES - 2048
2873        );
2874        // Absurd envelope payloads must saturate, never wrap into a huge budget.
2875        assert_eq!(page_byte_ceiling(1024, 1024 * 1024), 0);
2876    }
2877
2878    #[tokio::test]
2879    async fn test_oversized_encrypted_payload_is_rejected() {
2880        // Encrypted records previously skipped every size check, so storage
2881        // was unbounded and a record larger than the gRPC ceiling could be
2882        // stored but never downloaded.
2883        let service = setup_test_service().await;
2884        let api_key = register_test_user(&service).await;
2885        let library_id = service
2886            .create_library(Request::new(CreateLibraryRequest {
2887                api_key: api_key.clone(),
2888                name: "work".to_string(),
2889            }))
2890            .await
2891            .unwrap()
2892            .into_inner()
2893            .library_id;
2894
2895        let push = service
2896            .push_snippets(Request::new(PushSnippetsRequest {
2897                api_key,
2898                library_id,
2899                snippets: vec![ProtoSnippet {
2900                    id: "huge".to_string(),
2901                    description: String::new(),
2902                    command: "x".repeat(DEFAULT_MAX_ENCRYPTED_PAYLOAD_LENGTH + 1),
2903                    tags: vec![],
2904                    created_at: 1,
2905                    updated_at: 1,
2906                    device_id: "dev".to_string(),
2907                    deleted: false,
2908                    encrypted: true,
2909                }],
2910            }))
2911            .await
2912            .unwrap()
2913            .into_inner();
2914        assert_eq!(push.rejected_count, 1, "oversized payload must be rejected");
2915        assert_eq!(push.accepted_count, 0);
2916    }
2917
2918    #[tokio::test]
2919    async fn test_push_and_get_snippets() {
2920        let service = setup_test_service().await;
2921        let api_key = register_test_user(&service).await;
2922
2923        let req = Request::new(CreateLibraryRequest {
2924            api_key: api_key.clone(),
2925            name: "test-lib".to_string(),
2926        });
2927        let resp = service.create_library(req).await.unwrap();
2928        let library_id = resp.into_inner().library_id;
2929
2930        let snippet = ProtoSnippet {
2931            id: "snippet-1".to_string(),
2932            description: "Test snippet".to_string(),
2933            command: "echo hello".to_string(),
2934            tags: vec!["test".to_string()],
2935            created_at: time::OffsetDateTime::now_utc().unix_timestamp(),
2936            updated_at: time::OffsetDateTime::now_utc().unix_timestamp(),
2937            device_id: "device-1".to_string(),
2938            deleted: false,
2939            encrypted: false,
2940        };
2941        let req = Request::new(PushSnippetsRequest {
2942            api_key: api_key.clone(),
2943            library_id: library_id.clone(),
2944            snippets: vec![snippet],
2945        });
2946        let resp = service.push_snippets(req).await.unwrap();
2947        let push = resp.into_inner();
2948        assert_eq!(push.accepted_count, 1);
2949        assert_eq!(push.rejected_count, 0);
2950
2951        let req = Request::new(GetSnippetsRequest {
2952            api_key: api_key.clone(),
2953            library_id,
2954            limit: 100,
2955            offset: 0,
2956            since: 0,
2957        });
2958        let resp = service.get_snippets(req).await.unwrap();
2959        let list = resp.into_inner();
2960        assert_eq!(list.snippets.len(), 1);
2961        assert_eq!(list.snippets[0].id, "snippet-1");
2962        assert_eq!(list.snippets[0].command, "echo hello");
2963    }
2964
2965    #[tokio::test]
2966    async fn test_delete_library() {
2967        let service = setup_test_service().await;
2968        let api_key = register_test_user(&service).await;
2969
2970        let req = Request::new(CreateLibraryRequest {
2971            api_key: api_key.clone(),
2972            name: "to-delete".to_string(),
2973        });
2974        let resp = service.create_library(req).await.unwrap();
2975        let library_id = resp.into_inner().library_id;
2976
2977        let req = Request::new(snip_proto::DeleteLibraryRequest {
2978            api_key: api_key.clone(),
2979            library_id: library_id.clone(),
2980        });
2981        let resp = service.delete_library(req).await.unwrap();
2982        assert!(resp.into_inner().success);
2983
2984        let req = Request::new(ListLibrariesRequest {
2985            api_key: api_key.clone(),
2986            limit: 100,
2987            offset: 0,
2988        });
2989        let resp = service.list_libraries(req).await.unwrap();
2990        let list = resp.into_inner();
2991        assert_eq!(list.libraries.len(), 1);
2992        assert_eq!(list.libraries[0].name, "default");
2993    }
2994
2995    #[tokio::test]
2996    async fn test_delete_default_library_blocked() {
2997        let service = setup_test_service().await;
2998        let api_key = register_test_user(&service).await;
2999
3000        let req = Request::new(snip_proto::DeleteLibraryRequest {
3001            api_key: api_key.clone(),
3002            library_id: "default".to_string(),
3003        });
3004        let resp = service.delete_library(req).await;
3005        assert!(resp.is_err());
3006        assert!(resp.unwrap_err().message().contains("Cannot delete"));
3007    }
3008
3009    #[tokio::test]
3010    async fn test_sync_basic() {
3011        let service = setup_test_service().await;
3012        let api_key = register_test_user(&service).await;
3013
3014        let req = Request::new(CreateLibraryRequest {
3015            api_key: api_key.clone(),
3016            name: "sync-test".to_string(),
3017        });
3018        let resp = service.create_library(req).await.unwrap();
3019        let library_id = resp.into_inner().library_id;
3020
3021        let req = Request::new(SyncRequest {
3022            api_key: api_key.clone(),
3023            local_snippets: vec![],
3024            last_sync_timestamp: 0,
3025            last_sync_sequence: 0,
3026            library_id,
3027            limit: 1000,
3028            offset: 0,
3029        });
3030        let resp = service.sync(req).await.unwrap();
3031        let sync = resp.into_inner();
3032        assert_eq!(sync.snippets.len(), 0);
3033    }
3034
3035    fn proto_snippet(id: &str, updated_at: i64) -> ProtoSnippet {
3036        ProtoSnippet {
3037            id: id.to_string(),
3038            description: "Test snippet".to_string(),
3039            command: "echo hello".to_string(),
3040            tags: vec![],
3041            created_at: updated_at,
3042            updated_at,
3043            device_id: "device-1".to_string(),
3044            deleted: false,
3045            encrypted: false,
3046        }
3047    }
3048
3049    /// A client that sends no sequence cursor is served by the timestamp
3050    /// filter, exactly as before, and the response carries no sequence.
3051    #[tokio::test]
3052    async fn test_sync_without_a_sequence_cursor_serves_the_timestamp_rows() {
3053        let service = setup_test_service().await;
3054        let api_key = register_test_user(&service).await;
3055
3056        let req = Request::new(CreateLibraryRequest {
3057            api_key: api_key.clone(),
3058            name: "cursor-lib".to_string(),
3059        });
3060        let library_id = service
3061            .create_library(req)
3062            .await
3063            .unwrap()
3064            .into_inner()
3065            .library_id;
3066
3067        for (id, updated_at) in [("a", 100_i64), ("b", 200)] {
3068            let req = Request::new(PushSnippetsRequest {
3069                api_key: api_key.clone(),
3070                library_id: library_id.clone(),
3071                snippets: vec![proto_snippet(id, updated_at)],
3072            });
3073            service.push_snippets(req).await.unwrap();
3074        }
3075
3076        // A timestamp-only request: a non-zero timestamp cursor and no
3077        // sequence cursor. This is the shape an old client sends.
3078        let req = Request::new(SyncRequest {
3079            api_key: api_key.clone(),
3080            local_snippets: vec![],
3081            last_sync_timestamp: 1,
3082            last_sync_sequence: 0,
3083            library_id: library_id.clone(),
3084            limit: 1000,
3085            offset: 0,
3086        });
3087        let sync = service.sync(req).await.unwrap().into_inner();
3088
3089        assert_eq!(sync.snippets.len(), 2);
3090        // A delta does not deliver the library's whole history, so its
3091        // watermark is not adoptable by this client and none is returned:
3092        // an old client never receives a value it cannot interpret.
3093        assert_eq!(sync.server_sequence, 0);
3094        assert!(sync.server_timestamp > 0);
3095    }
3096
3097    /// A request with no cursor at all is the bootstrap: it delivers the
3098    /// complete library, so the client may adopt the sequence watermark.
3099    #[tokio::test]
3100    async fn test_sync_without_any_cursor_bootstraps_the_sequence_watermark() {
3101        let service = setup_test_service().await;
3102        let api_key = register_test_user(&service).await;
3103
3104        let req = Request::new(CreateLibraryRequest {
3105            api_key: api_key.clone(),
3106            name: "cursor-lib".to_string(),
3107        });
3108        let library_id = service
3109            .create_library(req)
3110            .await
3111            .unwrap()
3112            .into_inner()
3113            .library_id;
3114
3115        for (id, updated_at) in [("a", 100_i64), ("b", 200)] {
3116            let req = Request::new(PushSnippetsRequest {
3117                api_key: api_key.clone(),
3118                library_id: library_id.clone(),
3119                snippets: vec![proto_snippet(id, updated_at)],
3120            });
3121            service.push_snippets(req).await.unwrap();
3122        }
3123
3124        let req = Request::new(SyncRequest {
3125            api_key: api_key.clone(),
3126            local_snippets: vec![],
3127            last_sync_timestamp: 0,
3128            last_sync_sequence: 0,
3129            library_id: library_id.clone(),
3130            limit: 1000,
3131            offset: 0,
3132        });
3133        let sync = service.sync(req).await.unwrap().into_inner();
3134
3135        assert_eq!(sync.snippets.len(), 2);
3136        assert_eq!(
3137            sync.server_sequence, 2,
3138            "a cursorless request is the bootstrap and reports the watermark"
3139        );
3140    }
3141
3142    /// An empty library still reports a sequence, so a client can tell a
3143    /// server that serves the sequence from one that does not. Zero means
3144    /// "no sequence" in the protocol, so the reported position is the one
3145    /// before the library's first row; the inclusive `sequence >=` filter
3146    /// keeps that first row deliverable.
3147    #[tokio::test]
3148    async fn test_sync_bootstrap_on_an_empty_library_reports_a_sequence() {
3149        let service = setup_test_service().await;
3150        let api_key = register_test_user(&service).await;
3151
3152        let req = Request::new(CreateLibraryRequest {
3153            api_key: api_key.clone(),
3154            name: "empty-lib".to_string(),
3155        });
3156        let library_id = service
3157            .create_library(req)
3158            .await
3159            .unwrap()
3160            .into_inner()
3161            .library_id;
3162
3163        let req = Request::new(SyncRequest {
3164            api_key: api_key.clone(),
3165            local_snippets: vec![],
3166            last_sync_timestamp: 0,
3167            last_sync_sequence: 0,
3168            library_id: library_id.clone(),
3169            limit: 1000,
3170            offset: 0,
3171        });
3172        let sync = service.sync(req).await.unwrap().into_inner();
3173
3174        assert!(sync.snippets.is_empty());
3175        assert_eq!(
3176            sync.server_sequence, 1,
3177            "an empty library must still report a sequence, so the client can \
3178             tell a sequence-capable server from one that does not serve it"
3179        );
3180
3181        // The first row written to the library is still delivered to a client
3182        // that adopted the reported position.
3183        let req = Request::new(PushSnippetsRequest {
3184            api_key: api_key.clone(),
3185            library_id: library_id.clone(),
3186            snippets: vec![proto_snippet("first", 100)],
3187        });
3188        service.push_snippets(req).await.unwrap();
3189
3190        let req = Request::new(SyncRequest {
3191            api_key: api_key.clone(),
3192            local_snippets: vec![],
3193            last_sync_timestamp: 0,
3194            last_sync_sequence: 1,
3195            library_id,
3196            limit: 1000,
3197            offset: 0,
3198        });
3199        let sync = service.sync(req).await.unwrap().into_inner();
3200        assert_eq!(sync.snippets.len(), 1);
3201        assert_eq!(sync.snippets[0].id, "first");
3202    }
3203
3204    /// A client that holds a sequence cursor is served by the sequence
3205    /// filter and receives the sequence watermark back.
3206    #[tokio::test]
3207    async fn test_sync_with_a_sequence_cursor_serves_the_rows_after_it() {
3208        let service = setup_test_service().await;
3209        let api_key = register_test_user(&service).await;
3210
3211        let req = Request::new(CreateLibraryRequest {
3212            api_key: api_key.clone(),
3213            name: "cursor-lib".to_string(),
3214        });
3215        let library_id = service
3216            .create_library(req)
3217            .await
3218            .unwrap()
3219            .into_inner()
3220            .library_id;
3221
3222        for (id, updated_at) in [("a", 100_i64), ("b", 200), ("c", 300)] {
3223            let req = Request::new(PushSnippetsRequest {
3224                api_key: api_key.clone(),
3225                library_id: library_id.clone(),
3226                snippets: vec![proto_snippet(id, updated_at)],
3227            });
3228            service.push_snippets(req).await.unwrap();
3229        }
3230
3231        // The bootstrap request returns everything and the watermark.
3232        let req = Request::new(SyncRequest {
3233            api_key: api_key.clone(),
3234            local_snippets: vec![],
3235            last_sync_timestamp: 0,
3236            last_sync_sequence: 0,
3237            library_id: library_id.clone(),
3238            limit: 1000,
3239            offset: 0,
3240        });
3241        let sync = service.sync(req).await.unwrap().into_inner();
3242        assert_eq!(sync.snippets.len(), 3);
3243        assert_eq!(sync.server_sequence, 3);
3244
3245        // A client that knows the watermark asks with it and receives only
3246        // the rows written after it.
3247        let user_id = service
3248            .db
3249            .get_user_by_api_key(&api_key)
3250            .await
3251            .unwrap()
3252            .unwrap();
3253        let watermark = service
3254            .db
3255            .get_latest_sequence(&user_id, &library_id)
3256            .await
3257            .unwrap();
3258        assert_eq!(watermark, 3);
3259
3260        let req = Request::new(SyncRequest {
3261            api_key: api_key.clone(),
3262            local_snippets: vec![],
3263            last_sync_timestamp: 0,
3264            last_sync_sequence: watermark,
3265            library_id: library_id.clone(),
3266            limit: 1000,
3267            offset: 0,
3268        });
3269        let sync = service.sync(req).await.unwrap().into_inner();
3270        assert_eq!(sync.snippets.len(), 1);
3271        assert_eq!(sync.snippets[0].id, "c");
3272        assert_eq!(sync.server_sequence, watermark);
3273    }
3274
3275    #[tokio::test]
3276    async fn test_sync_accepts_local_record_older_than_pull_watermark() {
3277        let service = setup_test_service().await;
3278        let api_key = register_test_user(&service).await;
3279
3280        let req = Request::new(CreateLibraryRequest {
3281            api_key: api_key.clone(),
3282            name: "clock-skew-test".to_string(),
3283        });
3284        let library_id = service
3285            .create_library(req)
3286            .await
3287            .unwrap()
3288            .into_inner()
3289            .library_id;
3290
3291        let req = Request::new(SyncRequest {
3292            api_key: api_key.clone(),
3293            local_snippets: vec![ProtoSnippet {
3294                id: "clock-skewed".to_string(),
3295                description: "local mutation".to_string(),
3296                command: "echo local".to_string(),
3297                tags: vec![],
3298                created_at: 10,
3299                updated_at: 10,
3300                device_id: "device".to_string(),
3301                deleted: false,
3302                encrypted: false,
3303            }],
3304            last_sync_timestamp: 1_000,
3305            last_sync_sequence: 0,
3306            library_id: library_id.clone(),
3307            limit: 100,
3308            offset: 0,
3309        });
3310        service.sync(req).await.unwrap();
3311
3312        let response = service
3313            .get_snippets(Request::new(GetSnippetsRequest {
3314                api_key,
3315                library_id,
3316                limit: 100,
3317                offset: 0,
3318                since: 0,
3319            }))
3320            .await
3321            .unwrap()
3322            .into_inner();
3323        assert_eq!(response.total_count, 1);
3324        assert_eq!(response.snippets[0].id, "clock-skewed");
3325    }
3326
3327    #[tokio::test]
3328    async fn test_premade_filename_with_unsafe_characters_is_rejected() {
3329        let service = setup_test_service().await;
3330        let api_key = register_test_user(&service).await;
3331
3332        let req = Request::new(GetPremadeLibraryRequest {
3333            api_key,
3334            filename: "valid-name!.toml".to_string(),
3335        });
3336        let error = service.get_premade_library(req).await.unwrap_err();
3337
3338        assert_eq!(error.code(), tonic::Code::InvalidArgument);
3339        assert_eq!(error.message(), "Invalid filename");
3340    }
3341
3342    #[test]
3343    fn config_load_missing_file_uses_defaults() {
3344        let dir = tempfile::tempdir().unwrap();
3345        let path = dir.path().join("missing.toml");
3346        let config = Config::load_from(&path, &|_| None).unwrap();
3347        assert_eq!(config.grpc_port, 50051);
3348        assert_eq!(config.http_port, 50050);
3349    }
3350
3351    #[test]
3352    fn config_load_merges_file_and_environment_overrides() {
3353        let dir = tempfile::tempdir().unwrap();
3354        let path = dir.path().join("config.toml");
3355        std::fs::write(
3356            &path,
3357            "[server]\ngrpc_port = 51000\nhttp_host = \"192.0.2.10\"\n",
3358        )
3359        .unwrap();
3360        let config = Config::load_from(&path, &|name| {
3361            (name == "GRPC_PORT").then(|| "52000".to_string())
3362        })
3363        .unwrap();
3364        assert_eq!(config.grpc_port, 52000);
3365        assert_eq!(config.http_host, "192.0.2.10");
3366    }
3367
3368    #[test]
3369    fn config_load_rejects_malformed_existing_file() {
3370        let dir = tempfile::tempdir().unwrap();
3371        let path = dir.path().join("config.toml");
3372        std::fs::write(&path, "[server\n").unwrap();
3373        let error = match Config::load_from(&path, &|_| None) {
3374            Ok(_) => panic!("malformed config unexpectedly loaded"),
3375            Err(error) => error,
3376        };
3377        let message = error.to_string();
3378        assert!(message.contains(&path.display().to_string()));
3379        assert!(message.contains("parse"));
3380    }
3381
3382    #[test]
3383    fn config_load_rejects_unreadable_existing_path() {
3384        let dir = tempfile::tempdir().unwrap();
3385        let error = match Config::load_from(dir.path(), &|_| None) {
3386            Ok(_) => panic!("directory unexpectedly loaded as config"),
3387            Err(error) => error,
3388        };
3389        assert!(
3390            error
3391                .to_string()
3392                .contains(&dir.path().display().to_string())
3393        );
3394    }
3395
3396    #[test]
3397    fn parse_env_valid_numeric_override() {
3398        let env = |name: &str| match name {
3399            "GRPC_PORT" => Some("60000".to_string()),
3400            _ => None,
3401        };
3402        let val: Option<u16> = parse_env(&env, "GRPC_PORT").unwrap();
3403        assert_eq!(val, Some(60000));
3404    }
3405
3406    #[test]
3407    fn parse_env_malformed_numeric_returns_invalid_environment() {
3408        let env = |name: &str| match name {
3409            "GRPC_PORT" => Some("not-a-number".to_string()),
3410            _ => None,
3411        };
3412        let err = parse_env::<u16>(&env, "GRPC_PORT").unwrap_err();
3413        match err {
3414            ConfigLoadError::InvalidEnvironment { name, value, .. } => {
3415                assert_eq!(name, "GRPC_PORT");
3416                assert_eq!(value, "not-a-number");
3417            }
3418            other => panic!("expected InvalidEnvironment, got: {other}"),
3419        }
3420    }
3421
3422    #[test]
3423    fn parse_env_missing_variable_returns_none() {
3424        let env = |_name: &str| -> Option<String> { None };
3425        let val: Option<u16> = parse_env(&env, "GRPC_PORT").unwrap();
3426        assert_eq!(val, None);
3427    }
3428
3429    #[test]
3430    fn parse_bool_env_accepted_true_spellings() {
3431        for input in &["true", "1", "yes", "on", "TRUE", "Yes", "ON"] {
3432            let env = |_: &str| Some(input.to_string());
3433            let val = parse_bool_env(&env, "FLAG").unwrap();
3434            assert_eq!(val, Some(true), "expected true for {input:?}");
3435        }
3436    }
3437
3438    #[test]
3439    fn parse_bool_env_accepted_false_spellings() {
3440        for input in &["false", "0", "no", "off", "FALSE", "No", "OFF"] {
3441            let env = |_: &str| Some(input.to_string());
3442            let val = parse_bool_env(&env, "FLAG").unwrap();
3443            assert_eq!(val, Some(false), "expected false for {input:?}");
3444        }
3445    }
3446
3447    #[test]
3448    fn parse_bool_env_unknown_value_fails() {
3449        let env = |_: &str| Some("maybe".to_string());
3450        let err = parse_bool_env(&env, "FLAG").unwrap_err();
3451        match err {
3452            ConfigLoadError::InvalidEnvironment { name, value, .. } => {
3453                assert_eq!(name, "FLAG");
3454                assert_eq!(value, "maybe");
3455            }
3456            other => panic!("expected InvalidEnvironment, got: {other}"),
3457        }
3458    }
3459
3460    #[test]
3461    fn parse_bool_env_missing_variable_returns_none() {
3462        let env = |_name: &str| -> Option<String> { None };
3463        let val = parse_bool_env(&env, "FLAG").unwrap();
3464        assert_eq!(val, None);
3465    }
3466
3467    #[test]
3468    fn config_validate_rejects_zero_grpc_port() {
3469        let dir = tempfile::tempdir().unwrap();
3470        let path = dir.path().join("config.toml");
3471        std::fs::write(&path, "[server]\ngrpc_port = 0\n").unwrap();
3472        let err = Config::load_from(&path, &|_| None).unwrap_err();
3473        match err {
3474            ConfigLoadError::InvalidRange { name, .. } => assert_eq!(name, "GRPC_PORT"),
3475            other => panic!("expected InvalidRange, got: {other}"),
3476        }
3477    }
3478
3479    #[test]
3480    fn config_validate_rejects_zero_db_max_connections() {
3481        let dir = tempfile::tempdir().unwrap();
3482        let path = dir.path().join("config.toml");
3483        std::fs::write(&path, "[server.database]\nmax_connections = 0\n").unwrap();
3484        let err = Config::load_from(&path, &|_| None).unwrap_err();
3485        match err {
3486            ConfigLoadError::InvalidRange { name, .. } => assert_eq!(name, "DB_MAX_CONNECTIONS"),
3487            other => panic!("expected InvalidRange, got: {other}"),
3488        }
3489    }
3490
3491    #[test]
3492    fn config_validate_rejects_zero_request_timeout() {
3493        let dir = tempfile::tempdir().unwrap();
3494        let path = dir.path().join("config.toml");
3495        std::fs::write(&path, "[server.limits]\nrequest_timeout_secs = 0\n").unwrap();
3496        let err = Config::load_from(&path, &|_| None).unwrap_err();
3497        match err {
3498            ConfigLoadError::InvalidRange { name, .. } => assert_eq!(name, "REQUEST_TIMEOUT_SECS"),
3499            other => panic!("expected InvalidRange, got: {other}"),
3500        }
3501    }
3502
3503    #[test]
3504    fn config_validate_rejects_zero_register_per_minute() {
3505        let dir = tempfile::tempdir().unwrap();
3506        let path = dir.path().join("config.toml");
3507        std::fs::write(&path, "[server.rate_limit]\nregister_per_minute = 0\n").unwrap();
3508        let err = Config::load_from(&path, &|_| None).unwrap_err();
3509        match err {
3510            ConfigLoadError::InvalidRange { name, .. } => assert_eq!(name, "REGISTER_PER_MINUTE"),
3511            other => panic!("expected InvalidRange, got: {other}"),
3512        }
3513    }
3514
3515    #[test]
3516    fn config_validate_rejects_zero_metrics_auth_failures_per_minute() {
3517        let dir = tempfile::tempdir().unwrap();
3518        let path = dir.path().join("config.toml");
3519        std::fs::write(
3520            &path,
3521            "[server.rate_limit]\nmetrics_auth_failures_per_minute = 0\n",
3522        )
3523        .unwrap();
3524        let err = Config::load_from(&path, &|_| None).unwrap_err();
3525        match err {
3526            ConfigLoadError::InvalidRange { name, .. } => {
3527                assert_eq!(name, "METRICS_AUTH_FAILURES_PER_MINUTE")
3528            }
3529            other => panic!("expected InvalidRange, got: {other}"),
3530        }
3531    }
3532
3533    #[test]
3534    fn config_defaults_new_rate_limit_keys_when_absent() {
3535        // An existing config that predates the keys must keep loading, and the
3536        // defaults must be documented values rather than zero.
3537        let dir = tempfile::tempdir().unwrap();
3538        let path = dir.path().join("config.toml");
3539        std::fs::write(&path, "[server.rate_limit]\nrequests_per_minute = 60\n").unwrap();
3540        let config = Config::load_from(&path, &|_| None).unwrap();
3541        assert_eq!(config.rate_limit_per_minute, 60);
3542        assert_eq!(config.register_per_minute, DEFAULT_REGISTER_PER_MINUTE);
3543        assert_eq!(
3544            config.metrics_auth_failures_per_minute,
3545            DEFAULT_METRICS_AUTH_FAILURES_PER_MINUTE
3546        );
3547    }
3548
3549    #[test]
3550    fn config_reads_new_rate_limit_keys_from_file_and_env() {
3551        let dir = tempfile::tempdir().unwrap();
3552        let path = dir.path().join("config.toml");
3553        std::fs::write(
3554            &path,
3555            "[server.rate_limit]\nregister_per_minute = 3\nmetrics_auth_failures_per_minute = 4\n",
3556        )
3557        .unwrap();
3558        let config = Config::load_from(&path, &|_| None).unwrap();
3559        assert_eq!(config.register_per_minute, 3);
3560        assert_eq!(config.metrics_auth_failures_per_minute, 4);
3561
3562        // Environment overrides win over the file.
3563        let env = |name: &str| match name {
3564            "REGISTER_PER_MINUTE" => Some("7".to_string()),
3565            "METRICS_AUTH_FAILURES_PER_MINUTE" => Some("8".to_string()),
3566            _ => None,
3567        };
3568        let config = Config::load_from(&path, &env).unwrap();
3569        assert_eq!(config.register_per_minute, 7);
3570        assert_eq!(config.metrics_auth_failures_per_minute, 8);
3571    }
3572
3573    #[tokio::test]
3574    async fn register_global_budget_rejects_without_writing_rows() {
3575        let mut service = setup_test_service().await;
3576        service.config.register_per_minute = 2;
3577
3578        let count_users = |db: Arc<Database>| async move {
3579            let (count,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM users")
3580                .fetch_one(db.pool())
3581                .await
3582                .expect("counting users must succeed");
3583            count
3584        };
3585
3586        for attempt in 1..=2 {
3587            let response = service
3588                .register(Request::new(RegisterRequest {
3589                    device_id: String::new(),
3590                }))
3591                .await
3592                .unwrap_or_else(|status| {
3593                    panic!("registration {attempt} should succeed, got {status:?}")
3594                });
3595            assert!(response.get_ref().success);
3596        }
3597        assert_eq!(count_users(service.db.clone()).await, 2);
3598
3599        let rejection = service
3600            .register(Request::new(RegisterRequest {
3601                device_id: String::new(),
3602            }))
3603            .await
3604            .expect_err("the third registration must exceed the global budget");
3605        assert_eq!(rejection.code(), tonic::Code::ResourceExhausted);
3606        assert!(
3607            rejection.message().contains("global"),
3608            "the rejection must be distinguishable from the per-peer budget: {}",
3609            rejection.message()
3610        );
3611        assert_eq!(
3612            count_users(service.db.clone()).await,
3613            2,
3614            "a rejected registration must not insert a user row"
3615        );
3616    }
3617
3618    #[test]
3619    fn config_load_rejects_malformed_numeric_env() {
3620        let dir = tempfile::tempdir().unwrap();
3621        let path = dir.path().join("config.toml");
3622        std::fs::write(&path, "").unwrap();
3623        let env = |name: &str| match name {
3624            "GRPC_PORT" => Some("abc".to_string()),
3625            _ => None,
3626        };
3627        let err = Config::load_from(&path, &env).unwrap_err();
3628        match err {
3629            ConfigLoadError::InvalidEnvironment { name, .. } => assert_eq!(name, "GRPC_PORT"),
3630            other => panic!("expected InvalidEnvironment, got: {other}"),
3631        }
3632    }
3633
3634    #[test]
3635    fn config_validate_rejects_zero_max_id_length() {
3636        let dir = tempfile::tempdir().unwrap();
3637        let path = dir.path().join("config.toml");
3638        std::fs::write(&path, "[server.limits]\nmax_id_length = 0\n").unwrap();
3639        let err = Config::load_from(&path, &|_| None).unwrap_err();
3640        match err {
3641            ConfigLoadError::InvalidRange { name, .. } => assert_eq!(name, "MAX_ID_LENGTH"),
3642            other => panic!("expected InvalidRange, got: {other}"),
3643        }
3644    }
3645
3646    #[test]
3647    fn config_validate_rejects_zero_max_device_id_length() {
3648        let dir = tempfile::tempdir().unwrap();
3649        let path = dir.path().join("config.toml");
3650        std::fs::write(&path, "[server.limits]\nmax_device_id_length = 0\n").unwrap();
3651        let err = Config::load_from(&path, &|_| None).unwrap_err();
3652        match err {
3653            ConfigLoadError::InvalidRange { name, .. } => assert_eq!(name, "MAX_DEVICE_ID_LENGTH"),
3654            other => panic!("expected InvalidRange, got: {other}"),
3655        }
3656    }
3657
3658    #[test]
3659    fn config_validate_rejects_zero_max_api_key_length() {
3660        let dir = tempfile::tempdir().unwrap();
3661        let path = dir.path().join("config.toml");
3662        std::fs::write(&path, "[server.limits]\nmax_api_key_length = 0\n").unwrap();
3663        let err = Config::load_from(&path, &|_| None).unwrap_err();
3664        match err {
3665            ConfigLoadError::InvalidRange { name, .. } => assert_eq!(name, "MAX_API_KEY_LENGTH"),
3666            other => panic!("expected InvalidRange, got: {other}"),
3667        }
3668    }
3669
3670    #[test]
3671    fn config_validate_rejects_zero_rate_limit_per_minute() {
3672        let dir = tempfile::tempdir().unwrap();
3673        let path = dir.path().join("config.toml");
3674        std::fs::write(&path, "[server.rate_limit]\nrequests_per_minute = 0\n").unwrap();
3675        let err = Config::load_from(&path, &|_| None).unwrap_err();
3676        match err {
3677            ConfigLoadError::InvalidRange { name, .. } => {
3678                assert_eq!(name, "RATE_LIMIT_PER_MINUTE")
3679            }
3680            other => panic!("expected InvalidRange, got: {other}"),
3681        }
3682    }
3683
3684    #[test]
3685    fn config_validate_accepts_valid_boundary_values() {
3686        let dir = tempfile::tempdir().unwrap();
3687        let path = dir.path().join("config.toml");
3688        std::fs::write(
3689            &path,
3690            "[server.limits]\nmax_id_length = 1\nmax_device_id_length = 1\nmax_api_key_length = 1\n[server.rate_limit]\nrequests_per_minute = 1\n",
3691        )
3692        .unwrap();
3693        let config = Config::load_from(&path, &|_| None).unwrap();
3694        assert_eq!(config.max_id_length, 1);
3695        assert_eq!(config.max_device_id_length, 1);
3696        assert_eq!(config.max_api_key_length, 1);
3697        assert_eq!(config.rate_limit_per_minute, 1);
3698    }
3699
3700    #[test]
3701    fn authorization_scheme_is_matched_case_insensitively() {
3702        for scheme in ["Bearer", "bearer", "BEARER", "BeArEr"] {
3703            let mut request = Request::new(HealthRequest {});
3704            request.metadata_mut().insert(
3705                "authorization",
3706                format!("{scheme} secret-key").parse().unwrap(),
3707            );
3708            assert_eq!(
3709                extract_api_key(&request, ""),
3710                "secret-key",
3711                "scheme {scheme} must be accepted"
3712            );
3713        }
3714    }
3715
3716    #[test]
3717    fn authorization_falls_back_to_the_body_key_for_other_schemes() {
3718        let mut request = Request::new(HealthRequest {});
3719        request
3720            .metadata_mut()
3721            .insert("authorization", "Basic dXNlcjpwYXNz".parse().unwrap());
3722        assert_eq!(extract_api_key(&request, "body-key"), "body-key");
3723
3724        let request = Request::new(HealthRequest {});
3725        assert_eq!(extract_api_key(&request, "body-key"), "body-key");
3726    }
3727
3728    fn snippet(id: &str, updated_at: i64) -> ProtoSnippet {
3729        ProtoSnippet {
3730            id: id.to_string(),
3731            description: format!("description {id}"),
3732            command: format!("echo {id}"),
3733            tags: vec![],
3734            created_at: updated_at,
3735            updated_at,
3736            device_id: "device".to_string(),
3737            deleted: false,
3738            encrypted: false,
3739        }
3740    }
3741
3742    #[tokio::test]
3743    async fn sync_reports_a_database_failure_as_an_internal_error() {
3744        let service = setup_test_service().await;
3745        let api_key = register_test_user(&service).await;
3746
3747        // Fail the second insert only: if the transaction were committed the
3748        // first snippet would survive the failed sync.
3749        sqlx::query(
3750            "CREATE TRIGGER fail_second_insert BEFORE INSERT ON snippets \
3751             WHEN NEW.id = 'boom' BEGIN SELECT RAISE(ABORT, 'injected failure'); END",
3752        )
3753        .execute(service.db.pool())
3754        .await
3755        .unwrap();
3756
3757        let request = Request::new(SyncRequest {
3758            api_key: api_key.clone(),
3759            local_snippets: vec![snippet("ok-1", 100), snippet("boom", 101)],
3760            last_sync_timestamp: 0,
3761            last_sync_sequence: 0,
3762            library_id: String::new(),
3763            limit: 100,
3764            offset: 0,
3765        });
3766
3767        let status = service
3768            .sync(request)
3769            .await
3770            .expect_err("a database failure must not be reported as a successful sync");
3771        assert_eq!(status.code(), tonic::Code::Internal);
3772        assert_eq!(
3773            service.metrics.db_errors_total.get(),
3774            1,
3775            "the database failure must be counted"
3776        );
3777
3778        let stored: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM snippets")
3779            .fetch_one(service.db.pool())
3780            .await
3781            .unwrap();
3782        assert_eq!(
3783            stored.0, 0,
3784            "the failed sync must not leave a partial write behind"
3785        );
3786    }
3787
3788    #[tokio::test]
3789    async fn sync_reports_a_database_failure_without_a_skip_count() {
3790        let service = setup_test_service().await;
3791        let api_key = register_test_user(&service).await;
3792
3793        sqlx::query(
3794            "CREATE TRIGGER fail_every_insert BEFORE INSERT ON snippets \
3795             BEGIN SELECT RAISE(ABORT, 'injected failure'); END",
3796        )
3797        .execute(service.db.pool())
3798        .await
3799        .unwrap();
3800
3801        let request = Request::new(SyncRequest {
3802            api_key: api_key.clone(),
3803            local_snippets: vec![snippet("only", 100)],
3804            last_sync_timestamp: 0,
3805            last_sync_sequence: 0,
3806            library_id: String::new(),
3807            limit: 100,
3808            offset: 0,
3809        });
3810
3811        let status = service
3812            .sync(request)
3813            .await
3814            .expect_err("expected an internal error");
3815        assert_eq!(status.code(), tonic::Code::Internal);
3816        // The failure is not reported as a content-level skip: the response
3817        // carries no snippet list at all.
3818        assert!(status.message().contains("Internal error"));
3819    }
3820
3821    #[tokio::test]
3822    async fn sync_total_and_has_more_are_unchanged_for_a_known_dataset() {
3823        let service = setup_test_service().await;
3824        let api_key = register_test_user(&service).await;
3825
3826        let library_id = service
3827            .create_library(Request::new(CreateLibraryRequest {
3828                api_key: api_key.clone(),
3829                name: "paging".to_string(),
3830            }))
3831            .await
3832            .unwrap()
3833            .into_inner()
3834            .library_id;
3835
3836        let local_snippets: Vec<ProtoSnippet> = (0..5)
3837            .map(|index| snippet(&format!("row-{index}"), 100 + index))
3838            .collect();
3839
3840        // First page: two of five rows, more pages follow.
3841        let first = service
3842            .sync(Request::new(SyncRequest {
3843                api_key: api_key.clone(),
3844                local_snippets,
3845                last_sync_timestamp: 0,
3846                last_sync_sequence: 0,
3847                library_id: library_id.clone(),
3848                limit: 2,
3849                offset: 0,
3850            }))
3851            .await
3852            .unwrap()
3853            .into_inner();
3854        assert_eq!(first.snippets.len(), 2);
3855        assert!(first.has_more);
3856        assert_eq!(first.total_count, 5);
3857
3858        // Last page: the exact total is still reported.
3859        let last = service
3860            .sync(Request::new(SyncRequest {
3861                api_key: api_key.clone(),
3862                local_snippets: vec![],
3863                last_sync_timestamp: 0,
3864                last_sync_sequence: 0,
3865                library_id: library_id.clone(),
3866                limit: 2,
3867                offset: 4,
3868            }))
3869            .await
3870            .unwrap()
3871            .into_inner();
3872        assert_eq!(last.snippets.len(), 1);
3873        assert!(!last.has_more);
3874        assert_eq!(last.total_count, 5);
3875
3876        // A single page covers the whole range.
3877        let whole = service
3878            .sync(Request::new(SyncRequest {
3879                api_key: api_key.clone(),
3880                local_snippets: vec![],
3881                last_sync_timestamp: 0,
3882                last_sync_sequence: 0,
3883                library_id: library_id.clone(),
3884                limit: 100,
3885                offset: 0,
3886            }))
3887            .await
3888            .unwrap()
3889            .into_inner();
3890        assert_eq!(whole.snippets.len(), 5);
3891        assert!(!whole.has_more);
3892        assert_eq!(whole.total_count, 5);
3893
3894        // `GetSnippets` keeps its exact total as well.
3895        let listed = service
3896            .get_snippets(Request::new(GetSnippetsRequest {
3897                api_key,
3898                library_id,
3899                limit: 2,
3900                offset: 0,
3901                since: 0,
3902            }))
3903            .await
3904            .unwrap()
3905            .into_inner();
3906        assert_eq!(listed.snippets.len(), 2);
3907        assert_eq!(listed.total_count, 5);
3908        assert!(listed.has_more);
3909    }
3910}