sendword 0.8.7

Simple HTTP webhook to command runner sidecar. Frontend for managing hooks, JSON state for config portability, SQLite for execution history and logs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use serde::Serialize;
use sqlx::SqlitePool;

use crate::error::{DbError, DbResult};
use crate::id;
use crate::timestamp;

// --- Types ---

#[derive(Debug, Clone, PartialEq, Eq, Serialize, sqlx::Type)]
#[sqlx(rename_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum TriggerAttemptStatus {
    Fired,
    AuthFailed,
    ValidationFailed,
    Filtered,
    RateLimited,
    ScheduleSkipped,
    CooldownSkipped,
    ConcurrencyRejected,
    PendingApproval,
}

impl TriggerAttemptStatus {
    /// Parse a status string. Returns `None` for unrecognised values.
    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "fired" => Some(Self::Fired),
            "auth_failed" => Some(Self::AuthFailed),
            "validation_failed" => Some(Self::ValidationFailed),
            "filtered" => Some(Self::Filtered),
            "rate_limited" => Some(Self::RateLimited),
            "schedule_skipped" => Some(Self::ScheduleSkipped),
            "cooldown_skipped" => Some(Self::CooldownSkipped),
            "concurrency_rejected" => Some(Self::ConcurrencyRejected),
            "pending_approval" => Some(Self::PendingApproval),
            _ => None,
        }
    }
}

impl std::fmt::Display for TriggerAttemptStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Fired => "fired",
            Self::AuthFailed => "auth_failed",
            Self::ValidationFailed => "validation_failed",
            Self::Filtered => "filtered",
            Self::RateLimited => "rate_limited",
            Self::ScheduleSkipped => "schedule_skipped",
            Self::CooldownSkipped => "cooldown_skipped",
            Self::ConcurrencyRejected => "concurrency_rejected",
            Self::PendingApproval => "pending_approval",
        };
        f.write_str(s)
    }
}

#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
pub struct TriggerAttempt {
    pub id: String,
    pub hook_slug: String,
    pub attempted_at: String,
    pub source_ip: String,
    #[sqlx(rename = "status")]
    pub status: TriggerAttemptStatus,
    pub reason: String,
    pub execution_id: Option<String>,
}

/// Parameters for inserting a new trigger attempt.
pub struct NewTriggerAttempt<'a> {
    pub hook_slug: &'a str,
    pub source_ip: &'a str,
    pub status: TriggerAttemptStatus,
    pub reason: &'a str,
    pub execution_id: Option<&'a str>,
}

// --- Query functions ---

/// Insert a new trigger attempt. Returns the created record.
pub async fn insert(pool: &SqlitePool, new: &NewTriggerAttempt<'_>) -> DbResult<TriggerAttempt> {
    let id = id::new_id();
    let attempted_at = timestamp::now_utc();
    let status = new.status.to_string();

    sqlx::query(
        "INSERT INTO trigger_attempts (id, hook_slug, attempted_at, source_ip, status, reason, execution_id) \
         VALUES (?, ?, ?, ?, ?, ?, ?)",
    )
    .bind(&id)
    .bind(new.hook_slug)
    .bind(&attempted_at)
    .bind(new.source_ip)
    .bind(&status)
    .bind(new.reason)
    .bind(new.execution_id)
    .execute(pool)
    .await?;

    get_by_id(pool, &id).await
}

/// Fetch a single trigger attempt by primary key.
pub async fn get_by_id(pool: &SqlitePool, id: &str) -> DbResult<TriggerAttempt> {
    sqlx::query_as::<_, TriggerAttempt>(
        "SELECT id, hook_slug, attempted_at, source_ip, status, reason, execution_id \
         FROM trigger_attempts WHERE id = ?",
    )
    .bind(id)
    .fetch_optional(pool)
    .await?
    .ok_or_else(|| DbError::NotFound(format!("trigger_attempt {id}")))
}

/// List trigger attempts for a specific hook, ordered by attempted_at DESC, paginated.
pub async fn list_by_hook(
    pool: &SqlitePool,
    hook_slug: &str,
    limit: i64,
    offset: i64,
) -> DbResult<Vec<TriggerAttempt>> {
    let rows = sqlx::query_as::<_, TriggerAttempt>(
        "SELECT id, hook_slug, attempted_at, source_ip, status, reason, execution_id \
         FROM trigger_attempts WHERE hook_slug = ? \
         ORDER BY attempted_at DESC LIMIT ? OFFSET ?",
    )
    .bind(hook_slug)
    .bind(limit)
    .bind(offset)
    .fetch_all(pool)
    .await?;
    Ok(rows)
}

/// List trigger attempts for a specific hook filtered by status, ordered by attempted_at DESC.
pub async fn list_by_hook_filtered(
    pool: &SqlitePool,
    hook_slug: &str,
    status: &TriggerAttemptStatus,
    limit: i64,
    offset: i64,
) -> DbResult<Vec<TriggerAttempt>> {
    let status_str = status.to_string();
    let rows = sqlx::query_as::<_, TriggerAttempt>(
        "SELECT id, hook_slug, attempted_at, source_ip, status, reason, execution_id \
         FROM trigger_attempts WHERE hook_slug = ? AND status = ? \
         ORDER BY attempted_at DESC LIMIT ? OFFSET ?",
    )
    .bind(hook_slug)
    .bind(&status_str)
    .bind(limit)
    .bind(offset)
    .fetch_all(pool)
    .await?;
    Ok(rows)
}

pub async fn count_by_hook(pool: &SqlitePool, hook_slug: &str) -> DbResult<i64> {
    let row: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM trigger_attempts WHERE hook_slug = ?")
        .bind(hook_slug)
        .fetch_one(pool)
        .await?;
    Ok(row.0)
}

pub async fn count_by_hook_filtered(
    pool: &SqlitePool,
    hook_slug: &str,
    status: Option<&TriggerAttemptStatus>,
) -> DbResult<i64> {
    match status {
        None => count_by_hook(pool, hook_slug).await,
        Some(s) => {
            let status_str = s.to_string();
            let row: (i64,) = sqlx::query_as(
                "SELECT COUNT(*) FROM trigger_attempts WHERE hook_slug = ? AND status = ?",
            )
            .bind(hook_slug)
            .bind(&status_str)
            .fetch_one(pool)
            .await?;
            Ok(row.0)
        }
    }
}

/// List recent trigger attempts across all hooks, ordered by attempted_at DESC.
pub async fn list_recent(pool: &SqlitePool, limit: i64) -> DbResult<Vec<TriggerAttempt>> {
    let rows = sqlx::query_as::<_, TriggerAttempt>(
        "SELECT id, hook_slug, attempted_at, source_ip, status, reason, execution_id \
         FROM trigger_attempts ORDER BY attempted_at DESC LIMIT ?",
    )
    .bind(limit)
    .fetch_all(pool)
    .await?;
    Ok(rows)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::Db;

    async fn test_pool() -> SqlitePool {
        let db = Db::new_in_memory().await.expect("in-memory db");
        db.migrate().await.expect("migration");
        db.pool().clone()
    }

    fn make_attempt(status: TriggerAttemptStatus) -> NewTriggerAttempt<'static> {
        NewTriggerAttempt {
            hook_slug: "deploy-app",
            source_ip: "127.0.0.1",
            status,
            reason: "test reason",
            execution_id: None,
        }
    }

    // --- Display round-trip: verify each variant produces the exact DB-safe string ---

    #[test]
    fn status_display_matches_db_check_constraint() {
        let cases = [
            (TriggerAttemptStatus::Fired, "fired"),
            (TriggerAttemptStatus::AuthFailed, "auth_failed"),
            (TriggerAttemptStatus::ValidationFailed, "validation_failed"),
            (TriggerAttemptStatus::Filtered, "filtered"),
            (TriggerAttemptStatus::RateLimited, "rate_limited"),
            (TriggerAttemptStatus::ScheduleSkipped, "schedule_skipped"),
            (TriggerAttemptStatus::CooldownSkipped, "cooldown_skipped"),
            (
                TriggerAttemptStatus::ConcurrencyRejected,
                "concurrency_rejected",
            ),
            (TriggerAttemptStatus::PendingApproval, "pending_approval"),
        ];
        for (variant, expected) in cases {
            assert_eq!(variant.to_string(), expected);
        }
    }

    // --- insert + get_by_id round-trip for every status variant ---

    #[tokio::test]
    async fn insert_and_get_by_id_round_trip_all_statuses() {
        let pool = test_pool().await;

        let all_statuses = [
            TriggerAttemptStatus::Fired,
            TriggerAttemptStatus::AuthFailed,
            TriggerAttemptStatus::ValidationFailed,
            TriggerAttemptStatus::Filtered,
            TriggerAttemptStatus::RateLimited,
            TriggerAttemptStatus::ScheduleSkipped,
            TriggerAttemptStatus::CooldownSkipped,
            TriggerAttemptStatus::ConcurrencyRejected,
            TriggerAttemptStatus::PendingApproval,
        ];

        for status in all_statuses {
            let expected_status = status.clone();
            let attempt = insert(&pool, &make_attempt(status)).await.unwrap();

            assert_eq!(attempt.status, expected_status);
            assert_eq!(attempt.hook_slug, "deploy-app");
            assert_eq!(attempt.source_ip, "127.0.0.1");
            assert_eq!(attempt.reason, "test reason");
            assert!(attempt.execution_id.is_none());
            assert!(!attempt.id.is_empty());
            assert!(!attempt.attempted_at.is_empty());

            // Fetch back from DB and verify the status survived the round-trip
            let fetched = get_by_id(&pool, &attempt.id).await.unwrap();
            assert_eq!(fetched.id, attempt.id);
            assert_eq!(fetched.status, expected_status);
        }
    }

    // --- get_by_id error case ---

    #[tokio::test]
    async fn get_by_id_returns_not_found_for_missing_id() {
        let pool = test_pool().await;
        let result = get_by_id(&pool, "nonexistent").await;
        assert!(matches!(result, Err(DbError::NotFound(_))));
    }

    // --- Nullable execution_id ---

    #[tokio::test]
    async fn insert_with_execution_id_persists_it() {
        let pool = test_pool().await;

        // Create an execution first so the FK is satisfied
        use crate::models::execution::{self, NewExecution};
        let exec = execution::create(
            &pool,
            &NewExecution {
                id: None,
                hook_slug: "deploy-app",
                log_path: "data/logs/test",
                trigger_source: "127.0.0.1",
                request_payload: "{}",
                retry_of: None,
                status: None,
            },
        )
        .await
        .unwrap();

        let attempt = insert(
            &pool,
            &NewTriggerAttempt {
                hook_slug: "deploy-app",
                source_ip: "10.0.0.1",
                status: TriggerAttemptStatus::Fired,
                reason: "matched",
                execution_id: Some(&exec.id),
            },
        )
        .await
        .unwrap();

        assert_eq!(attempt.execution_id.as_deref(), Some(exec.id.as_str()));

        let fetched = get_by_id(&pool, &attempt.id).await.unwrap();
        assert_eq!(fetched.execution_id.as_deref(), Some(exec.id.as_str()));
    }

    #[tokio::test]
    async fn insert_without_execution_id_stores_null() {
        let pool = test_pool().await;
        let attempt = insert(&pool, &make_attempt(TriggerAttemptStatus::AuthFailed))
            .await
            .unwrap();
        assert!(attempt.execution_id.is_none());
    }

    // --- list_by_hook: ordering and pagination ---

    #[tokio::test]
    async fn list_by_hook_returns_descending_order() {
        let pool = test_pool().await;

        for _ in 0..3 {
            insert(&pool, &make_attempt(TriggerAttemptStatus::Fired))
                .await
                .unwrap();
        }

        let list = list_by_hook(&pool, "deploy-app", 10, 0).await.unwrap();
        assert_eq!(list.len(), 3);

        for pair in list.windows(2) {
            assert!(pair[0].attempted_at >= pair[1].attempted_at);
        }
    }

    #[tokio::test]
    async fn list_by_hook_respects_limit_and_offset() {
        let pool = test_pool().await;

        for _ in 0..5 {
            insert(&pool, &make_attempt(TriggerAttemptStatus::Filtered))
                .await
                .unwrap();
        }

        let page1 = list_by_hook(&pool, "deploy-app", 2, 0).await.unwrap();
        assert_eq!(page1.len(), 2);

        let page2 = list_by_hook(&pool, "deploy-app", 2, 2).await.unwrap();
        assert_eq!(page2.len(), 2);

        let page3 = list_by_hook(&pool, "deploy-app", 2, 4).await.unwrap();
        assert_eq!(page3.len(), 1);

        // Pages should not overlap
        assert_ne!(page1[0].id, page2[0].id);
        assert_ne!(page2[0].id, page3[0].id);
    }

    #[tokio::test]
    async fn list_by_hook_filters_by_slug() {
        let pool = test_pool().await;

        insert(&pool, &make_attempt(TriggerAttemptStatus::Fired))
            .await
            .unwrap();
        insert(
            &pool,
            &NewTriggerAttempt {
                hook_slug: "other-hook",
                source_ip: "10.0.0.1",
                status: TriggerAttemptStatus::RateLimited,
                reason: "too fast",
                execution_id: None,
            },
        )
        .await
        .unwrap();

        let list = list_by_hook(&pool, "deploy-app", 10, 0).await.unwrap();
        assert_eq!(list.len(), 1);
        assert_eq!(list[0].hook_slug, "deploy-app");
    }

    // --- list_recent: ordering, limit, cross-hook ---

    #[tokio::test]
    async fn list_recent_returns_across_all_hooks() {
        let pool = test_pool().await;

        insert(&pool, &make_attempt(TriggerAttemptStatus::Fired))
            .await
            .unwrap();
        insert(
            &pool,
            &NewTriggerAttempt {
                hook_slug: "other-hook",
                source_ip: "10.0.0.1",
                status: TriggerAttemptStatus::AuthFailed,
                reason: "bad token",
                execution_id: None,
            },
        )
        .await
        .unwrap();

        let list = list_recent(&pool, 10).await.unwrap();
        assert_eq!(list.len(), 2);
    }

    #[tokio::test]
    async fn list_recent_respects_limit() {
        let pool = test_pool().await;

        for _ in 0..5 {
            insert(&pool, &make_attempt(TriggerAttemptStatus::Fired))
                .await
                .unwrap();
        }

        let list = list_recent(&pool, 3).await.unwrap();
        assert_eq!(list.len(), 3);
    }

    #[tokio::test]
    async fn list_recent_returns_descending_order() {
        let pool = test_pool().await;

        for _ in 0..3 {
            insert(&pool, &make_attempt(TriggerAttemptStatus::Fired))
                .await
                .unwrap();
        }

        let list = list_recent(&pool, 10).await.unwrap();
        for pair in list.windows(2) {
            assert!(pair[0].attempted_at >= pair[1].attempted_at);
        }
    }
}