ferro-cli 0.2.24

CLI for scaffolding Ferro web applications
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
use console::style;
use std::fs;
use std::path::Path;

// ---------------------------------------------------------------------------
// Template generators
// ---------------------------------------------------------------------------

fn whatsapp_mod_template() -> String {
    r#"pub mod listeners;
pub mod webhook;

use ferro::WhatsApp;

/// Initialize WhatsApp. Call from bootstrap.rs.
pub fn init() {
    let config = ferro::WhatsAppConfig::from_env(Box::new(|phone| {
        // TODO: Replace with your owner phone number check.
        // Phone numbers arrive in E.164 format without '+' (e.g. "393401234567").
        phone == std::env::var("OWNER_PHONE").as_deref().unwrap_or("")
    }))
    .expect("WhatsApp configuration missing. Set WHATSAPP_APP_SECRET, WHATSAPP_ACCESS_TOKEN, WHATSAPP_PHONE_NUMBER_ID, and WHATSAPP_VERIFY_TOKEN.");
    WhatsApp::init(config);
}
"#
    .to_string()
}

fn whatsapp_webhook_template() -> String {
    r#"use ferro::{handler, HttpResponse, Request, Response, WhatsApp};
use ferro::{verify_whatsapp_webhook, ProcessWhatsAppWebhook, queue_dispatch};

/// GET /whatsapp/webhook — Meta challenge verification endpoint.
///
/// Meta sends a GET request to verify the webhook endpoint before enabling it.
/// Must respond with hub.challenge as plain text.
#[handler]
pub async fn whatsapp_webhook_verify(req: Request) -> Response {
    let mode = req.query("hub.mode").unwrap_or_default();
    let token = req.query("hub.verify_token").unwrap_or_default();
    let challenge = req.query("hub.challenge").unwrap_or_default();

    if mode == "subscribe" && token == WhatsApp::config().verify_token {
        Ok(HttpResponse::text(challenge))
    } else {
        Err(HttpResponse::text("Forbidden").status(403))
    }
}

/// POST /whatsapp/webhook — Inbound message and status update handler.
///
/// Verifies HMAC-SHA256 signature, acknowledges immediately, then queues
/// ProcessWhatsAppWebhook job for async processing.
#[handler]
pub async fn whatsapp_webhook(req: Request) -> Response {
    let sig = req
        .header("x-hub-signature-256")
        .ok_or_else(|| HttpResponse::text("Missing x-hub-signature-256").status(400))?;
    let body = req
        .body_string()
        .await
        .map_err(|_| HttpResponse::text("Failed to read body").status(400))?;

    verify_whatsapp_webhook(body.as_bytes(), &sig, &WhatsApp::config().app_secret)
        .map_err(|_| HttpResponse::text("Invalid signature").status(400))?;

    let job = ProcessWhatsAppWebhook {
        payload_json: body,
    };
    queue_dispatch(job)
        .await
        .map_err(|e| HttpResponse::text(format!("Queue error: {e}")).status(500))?;

    Ok(HttpResponse::json(serde_json::json!({"received": true})))
}
"#
    .to_string()
}

fn whatsapp_listeners_template() -> String {
    r#"use ferro::{async_trait, EventError, Listener};
use ferro::{WhatsAppTextReceived, WhatsAppStatusUpdate};

pub struct HandleInboundMessage;

#[async_trait]
impl Listener<WhatsAppTextReceived> for HandleInboundMessage {
    async fn handle(&self, event: &WhatsAppTextReceived) -> Result<(), EventError> {
        // TODO: Handle inbound text message from sender.
        // event.sender_identity — Owner or Customer with phone number
        // event.text — the message text body
        // event.wamid — WhatsApp message ID for dedup/correlation
        println!("WhatsApp message received: {}", event.wamid);
        Ok(())
    }
}

pub struct HandleDeliveryStatus;

#[async_trait]
impl Listener<WhatsAppStatusUpdate> for HandleDeliveryStatus {
    async fn handle(&self, event: &WhatsAppStatusUpdate) -> Result<(), EventError> {
        // TODO: Handle delivery status update (Sent/Delivered/Read/Failed).
        // event.wamid — correlates with SendResult.wamid from WhatsApp::send()
        // event.status — DeliveryStatus enum variant
        println!("WhatsApp status update: {:?} for {}", event.status, event.wamid);
        Ok(())
    }
}
"#
    .to_string()
}

// ---------------------------------------------------------------------------
// File generation helpers
// ---------------------------------------------------------------------------

/// Write a file only if it does not already exist.
/// Returns true if the file was created, false if skipped.
fn write_if_not_exists(path: &Path, content: &str, label: &str) -> bool {
    if path.exists() {
        println!(
            "{} {} already exists, skipping",
            style("Skip:").yellow().bold(),
            label
        );
        return false;
    }
    if let Err(e) = fs::write(path, content) {
        eprintln!(
            "{} Failed to write {}: {}",
            style("Error:").red().bold(),
            label,
            e
        );
        return false;
    }
    println!("{} {}", style("Created:").green().bold(), label);
    true
}

fn ensure_dir(path: &Path) -> bool {
    if path.exists() {
        return true;
    }
    if let Err(e) = fs::create_dir_all(path) {
        eprintln!(
            "{} Failed to create directory {}: {}",
            style("Error:").red().bold(),
            path.display(),
            e
        );
        return false;
    }
    println!(
        "{} Created directory {}",
        style("Created:").green().bold(),
        path.display()
    );
    true
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Execute `ferro make:whatsapp`.
///
/// Generates the WhatsApp integration scaffold into the current Ferro project.
pub fn execute(project_root: &Path) {
    println!("Scaffolding WhatsApp integration...\n");

    let whatsapp_dir = project_root.join("src/whatsapp");

    if !ensure_dir(&whatsapp_dir) {
        std::process::exit(1);
    }

    // mod.rs
    write_if_not_exists(
        &whatsapp_dir.join("mod.rs"),
        &whatsapp_mod_template(),
        "src/whatsapp/mod.rs",
    );

    // webhook.rs
    write_if_not_exists(
        &whatsapp_dir.join("webhook.rs"),
        &whatsapp_webhook_template(),
        "src/whatsapp/webhook.rs",
    );

    // listeners.rs
    write_if_not_exists(
        &whatsapp_dir.join("listeners.rs"),
        &whatsapp_listeners_template(),
        "src/whatsapp/listeners.rs",
    );

    // Print env hints
    println!("\n{}", style("Add to your .env file:").bold());
    println!("  WHATSAPP_APP_SECRET=<from Meta Developer Dashboard -> App Settings -> Basic -> App Secret>");
    println!("  WHATSAPP_ACCESS_TOKEN=<from Meta Developer Dashboard -> WhatsApp -> API Setup -> Permanent Token>");
    println!("  WHATSAPP_PHONE_NUMBER_ID=<from Meta Developer Dashboard -> WhatsApp -> API Setup -> Phone Number ID>");
    println!("  WHATSAPP_VERIFY_TOKEN=<a secret string you choose for webhook verification>");

    // Print next steps
    print_next_steps();
}

fn print_next_steps() {
    println!("\n{}", style("Next steps:").bold());
    println!(
        "\n  {} Call WhatsApp::init() from your bootstrap.rs:",
        style("1.").dim()
    );
    println!("     {}", style("crate::whatsapp::init();").cyan());

    println!(
        "\n  {} Register webhook routes in src/routes.rs:",
        style("2.").dim()
    );
    println!(
        "     {}",
        style("use crate::whatsapp::webhook::{whatsapp_webhook, whatsapp_webhook_verify};").cyan()
    );
    println!(
        "     {}",
        style("get!(\"/whatsapp/webhook\", whatsapp_webhook_verify)").cyan()
    );
    println!(
        "     {}",
        style("post!(\"/whatsapp/webhook\", whatsapp_webhook)").cyan()
    );

    println!(
        "\n  {} Register event listeners in bootstrap.rs:",
        style("3.").dim()
    );
    println!(
        "     {}",
        style("use crate::whatsapp::listeners::{HandleInboundMessage, HandleDeliveryStatus};")
            .cyan()
    );
    println!(
        "     {}",
        style("ferro::register_listener::<WhatsAppTextReceived, HandleInboundMessage>();").cyan()
    );
    println!(
        "     {}",
        style("ferro::register_listener::<WhatsAppStatusUpdate, HandleDeliveryStatus>();").cyan()
    );

    println!(
        "\n  {} Configure the webhook URL in Meta Developer Dashboard:",
        style("4.").dim()
    );
    println!(
        "     {}",
        style("Meta Developer Dashboard -> Your App -> WhatsApp -> Configuration -> Webhook URL")
            .dim()
    );
    println!(
        "     {}",
        style("Set Callback URL to: https://yourdomain.com/whatsapp/webhook").dim()
    );
    println!(
        "     {}",
        style("Set Verify Token to the value of WHATSAPP_VERIFY_TOKEN").dim()
    );
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

/// Generate the scaffold files in a temp directory for testing.
#[cfg(test)]
pub fn generate_in_dir(base_dir: &Path) {
    let whatsapp_dir = base_dir.join("src/whatsapp");
    fs::create_dir_all(&whatsapp_dir).unwrap();

    fs::write(whatsapp_dir.join("mod.rs"), whatsapp_mod_template()).unwrap();
    fs::write(whatsapp_dir.join("webhook.rs"), whatsapp_webhook_template()).unwrap();
    fs::write(
        whatsapp_dir.join("listeners.rs"),
        whatsapp_listeners_template(),
    )
    .unwrap();
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn read_file(path: &Path) -> String {
        fs::read_to_string(path).unwrap_or_else(|e| panic!("Failed to read {path:?}: {e}"))
    }

    // --- mod.rs template tests ---

    #[test]
    fn test_mod_template_has_correct_imports() {
        let tmpl = whatsapp_mod_template();
        assert!(tmpl.contains("pub mod listeners;"));
        assert!(tmpl.contains("pub mod webhook;"));
        assert!(tmpl.contains("use ferro::WhatsApp;"));
        assert!(tmpl.contains("pub fn init()"));
        assert!(tmpl.contains("ferro::WhatsAppConfig::from_env("));
        assert!(tmpl.contains("WhatsApp::init(config);"));
    }

    #[test]
    fn test_mod_template_has_is_owner_closure() {
        let tmpl = whatsapp_mod_template();
        // is_owner is a closure passed to from_env
        assert!(tmpl.contains("Box::new(|phone|"));
    }

    // --- webhook.rs template tests ---

    #[test]
    fn test_webhook_template_uses_queue_dispatch() {
        let tmpl = whatsapp_webhook_template();
        assert!(tmpl.contains("queue_dispatch(job)"));
        assert!(!tmpl.contains("dispatch_event"));
        assert!(tmpl.contains("verify_whatsapp_webhook("));
        assert!(tmpl.contains("x-hub-signature-256"));
        assert!(tmpl.contains(r#"{"received": true}"#));
    }

    #[test]
    fn test_webhook_template_has_verify_handler() {
        let tmpl = whatsapp_webhook_template();
        assert!(tmpl.contains("whatsapp_webhook_verify"));
        assert!(tmpl.contains("hub.mode"));
        assert!(tmpl.contains("hub.verify_token"));
        assert!(tmpl.contains("hub.challenge"));
        assert!(tmpl.contains("HttpResponse::text(challenge)"));
    }

    #[test]
    fn test_webhook_template_uses_ferro_imports() {
        let tmpl = whatsapp_webhook_template();
        assert!(tmpl.contains("use ferro::{"));
        assert!(tmpl.contains("ProcessWhatsAppWebhook"));
    }

    // --- listeners.rs template tests ---

    #[test]
    fn test_listeners_template_has_both_event_types() {
        let tmpl = whatsapp_listeners_template();
        assert!(tmpl.contains("WhatsAppTextReceived"));
        assert!(tmpl.contains("WhatsAppStatusUpdate"));
        assert!(tmpl.contains("impl Listener<WhatsAppTextReceived> for HandleInboundMessage"));
        assert!(tmpl.contains("impl Listener<WhatsAppStatusUpdate> for HandleDeliveryStatus"));
        assert!(tmpl.contains("async fn handle("));
        assert!(tmpl.contains("use ferro::{async_trait, EventError, Listener};"));
    }

    // --- file generation tests ---

    #[test]
    fn test_generates_three_required_files() {
        let tmp = TempDir::new().unwrap();
        generate_in_dir(tmp.path());

        let whatsapp_dir = tmp.path().join("src/whatsapp");
        assert!(
            whatsapp_dir.exists(),
            "src/whatsapp directory should be created"
        );
        assert!(
            whatsapp_dir.join("mod.rs").exists(),
            "mod.rs should be created"
        );
        assert!(
            whatsapp_dir.join("webhook.rs").exists(),
            "webhook.rs should be created"
        );
        assert!(
            whatsapp_dir.join("listeners.rs").exists(),
            "listeners.rs should be created"
        );
    }

    #[test]
    fn test_generated_files_have_correct_content() {
        let tmp = TempDir::new().unwrap();
        generate_in_dir(tmp.path());

        let whatsapp_dir = tmp.path().join("src/whatsapp");

        let mod_content = read_file(&whatsapp_dir.join("mod.rs"));
        assert!(mod_content.contains("use ferro::WhatsApp;"));

        let webhook_content = read_file(&whatsapp_dir.join("webhook.rs"));
        assert!(webhook_content.contains("queue_dispatch"));
        assert!(webhook_content.contains("verify_whatsapp_webhook"));

        let listeners_content = read_file(&whatsapp_dir.join("listeners.rs"));
        assert!(listeners_content.contains("WhatsAppTextReceived"));
        assert!(listeners_content.contains("WhatsAppStatusUpdate"));
    }

    #[test]
    fn test_does_not_overwrite_existing_files() {
        let tmp = TempDir::new().unwrap();

        // Test write_if_not_exists directly
        let out_path = tmp.path().join("test_file.txt");
        write_if_not_exists(&out_path, "original content", "test_file.txt");
        assert_eq!(fs::read_to_string(&out_path).unwrap(), "original content");

        // Second write should be skipped
        write_if_not_exists(&out_path, "overwritten content", "test_file.txt");
        assert_eq!(
            fs::read_to_string(&out_path).unwrap(),
            "original content",
            "write_if_not_exists must not overwrite existing files"
        );
    }

    #[test]
    fn test_generated_webhook_uses_queue_not_events() {
        let tmp = TempDir::new().unwrap();
        generate_in_dir(tmp.path());

        let webhook_path = tmp.path().join("src/whatsapp/webhook.rs");
        let content = read_file(&webhook_path);

        assert!(
            content.contains("queue_dispatch"),
            "webhook.rs must use queue_dispatch"
        );
        assert!(
            !content.contains("dispatch_event"),
            "webhook.rs must NOT use dispatch_event"
        );
    }
}