tazuna 0.1.0

TUI tool for managing multiple Claude Code sessions in parallel
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Webhook notification via HTTP POST.
//!
//! Sends hook events to external services (Slack, Discord, etc.)

use serde::Serialize;
use tracing::{debug, error};

use crate::config::WebhookConfig;
use crate::error::NotificationError;
use crate::hooks::HookEvent;

/// Target service type detected from webhook URL
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WebhookTarget {
    /// Slack (Block Kit format)
    Slack,
    /// Discord (Embeds format)
    Discord,
    /// Generic (minimal JSON)
    Generic,
}

impl WebhookTarget {
    /// Detect target from URL pattern
    #[must_use]
    pub fn detect(url: &str) -> Self {
        if url.contains("hooks.slack.com") {
            Self::Slack
        } else if url.contains("discord.com/api/webhooks")
            || url.contains("discordapp.com/api/webhooks")
        {
            Self::Discord
        } else {
            Self::Generic
        }
    }
}

/// Format timestamp as ISO8601 string
fn format_timestamp_iso8601(timestamp: std::time::SystemTime) -> String {
    use chrono::{DateTime, SecondsFormat, Utc};
    DateTime::<Utc>::from(timestamp).to_rfc3339_opts(SecondsFormat::Secs, true)
}

// ─────────────────────────────────────────────────────────────
// Slack Payload (Block Kit)
// ─────────────────────────────────────────────────────────────

/// Slack Block Kit payload
#[derive(Debug, Serialize)]
pub struct SlackPayload {
    text: String,
    blocks: Vec<SlackBlock>,
}

#[derive(Debug, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum SlackBlock {
    Section { text: SlackText },
    Context { elements: Vec<SlackText> },
}

#[derive(Debug, Serialize)]
struct SlackText {
    #[serde(rename = "type")]
    text_type: String,
    text: String,
}

impl SlackPayload {
    /// Build Slack payload from hook event
    #[must_use]
    pub fn from_event(event: &HookEvent, session_name: &str) -> Self {
        let message = event
            .message()
            .unwrap_or_else(|| format!("{:?} event occurred", event.event_type));
        let timestamp = format_timestamp_iso8601(event.timestamp);

        Self {
            text: format!("[tazuna] {session_name}: {message}"),
            blocks: vec![
                SlackBlock::Section {
                    text: SlackText {
                        text_type: "mrkdwn".to_string(),
                        text: format!("*{message}*"),
                    },
                },
                SlackBlock::Context {
                    elements: vec![SlackText {
                        text_type: "mrkdwn".to_string(),
                        text: format!(":bookmark: {session_name} | :clock1: {timestamp}"),
                    }],
                },
            ],
        }
    }
}

// ─────────────────────────────────────────────────────────────
// Discord Payload (Embeds)
// ─────────────────────────────────────────────────────────────

/// Discord Embeds payload
#[derive(Debug, Serialize)]
pub struct DiscordPayload {
    embeds: Vec<DiscordEmbed>,
}

#[derive(Debug, Serialize)]
struct DiscordEmbed {
    title: String,
    description: String,
    color: u32,
    fields: Vec<DiscordField>,
    timestamp: String,
    footer: DiscordFooter,
}

#[derive(Debug, Serialize)]
struct DiscordField {
    name: String,
    value: String,
    inline: bool,
}

#[derive(Debug, Serialize)]
struct DiscordFooter {
    text: String,
}

impl DiscordPayload {
    /// Build Discord payload from hook event
    #[must_use]
    pub fn from_event(event: &HookEvent, session_name: &str) -> Self {
        let message = event
            .message()
            .unwrap_or_else(|| format!("{:?} event occurred", event.event_type));
        let timestamp = format_timestamp_iso8601(event.timestamp);

        Self {
            embeds: vec![DiscordEmbed {
                title: "tazuna".to_string(),
                description: message,
                color: 16_750_848, // Orange
                fields: vec![DiscordField {
                    name: "Session".to_string(),
                    value: session_name.to_string(),
                    inline: true,
                }],
                timestamp,
                footer: DiscordFooter {
                    text: "tazuna".to_string(),
                },
            }],
        }
    }
}

// ─────────────────────────────────────────────────────────────
// Generic Payload (backward compatible)
// ─────────────────────────────────────────────────────────────

/// Generic minimal payload
#[derive(Debug, Serialize)]
pub struct GenericPayload {
    text: String,
    session: String,
    timestamp: String,
}

impl GenericPayload {
    /// Build generic payload from hook event
    #[must_use]
    pub fn from_event(event: &HookEvent, session_name: &str) -> Self {
        let message = event
            .message()
            .unwrap_or_else(|| format!("{:?} event occurred", event.event_type));
        let timestamp = format_timestamp_iso8601(event.timestamp);

        Self {
            text: format!("[tazuna] {session_name}: {message}"),
            session: session_name.to_string(),
            timestamp,
        }
    }
}

/// Webhook notification sender
pub struct WebhookNotifier {
    client: reqwest::Client,
    url: String,
    target: WebhookTarget,
}

impl WebhookNotifier {
    /// Create new webhook notifier
    #[must_use]
    pub fn new(config: &WebhookConfig) -> Self {
        Self {
            client: reqwest::Client::new(),
            target: WebhookTarget::detect(&config.url),
            url: config.url.clone(),
        }
    }

    /// Create with custom client (for testing)
    #[cfg(test)]
    #[must_use]
    pub(crate) fn with_client(client: reqwest::Client, url: String) -> Self {
        let target = WebhookTarget::detect(&url);
        Self {
            client,
            url,
            target,
        }
    }

    /// Send webhook notification
    pub async fn send(
        &self,
        event: &HookEvent,
        session_name: &str,
    ) -> Result<(), NotificationError> {
        debug!(
            "Sending webhook to {} (target: {:?})",
            self.url, self.target
        );

        let response = match self.target {
            WebhookTarget::Slack => {
                let payload = SlackPayload::from_event(event, session_name);
                self.client.post(&self.url).json(&payload).send().await
            }
            WebhookTarget::Discord => {
                let payload = DiscordPayload::from_event(event, session_name);
                self.client.post(&self.url).json(&payload).send().await
            }
            WebhookTarget::Generic => {
                let payload = GenericPayload::from_event(event, session_name);
                self.client.post(&self.url).json(&payload).send().await
            }
        }
        .map_err(NotificationError::WebhookFailed)?;

        if !response.status().is_success() {
            error!("Webhook failed with status: {}", response.status());
        }

        Ok(())
    }

    /// Get the configured URL
    #[cfg(test)]
    #[must_use]
    pub(crate) fn url(&self) -> &str {
        &self.url
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::session::SessionId;
    use rstest::rstest;
    use uuid::Uuid;

    fn test_session_id() -> SessionId {
        SessionId::from(Uuid::new_v4())
    }

    fn make_event(hook_name: &str, msg_or_tool: &str) -> HookEvent {
        let payload = if hook_name == "Notification" {
            serde_json::json!({"hook_event_name": hook_name, "message": msg_or_tool})
        } else {
            serde_json::json!({"hook_event_name": hook_name, "tool_name": msg_or_tool})
        };
        HookEvent::from_payload(test_session_id(), payload).expect("create event")
    }

    #[test]
    fn url_accessor() {
        let config = WebhookConfig {
            enabled: true,
            url: "https://example.com".to_string(),
        };
        assert_eq!(WebhookNotifier::new(&config).url(), "https://example.com");
    }

    #[test]
    fn with_client_constructor() {
        let notifier =
            WebhookNotifier::with_client(reqwest::Client::new(), "http://test".to_string());
        assert_eq!(notifier.url(), "http://test");
    }

    #[rstest]
    #[case(200)]
    #[case(500)]
    #[tokio::test]
    async fn send_returns_ok(#[case] status: u16) {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let mock_server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/webhook"))
            .respond_with(ResponseTemplate::new(status))
            .mount(&mock_server)
            .await;

        let notifier = WebhookNotifier::with_client(
            reqwest::Client::new(),
            format!("{}/webhook", mock_server.uri()),
        );
        assert!(
            notifier
                .send(&make_event("Notification", "Test"), "s")
                .await
                .is_ok()
        );
    }

    // WebhookTarget detection tests
    #[rstest]
    #[case("https://hooks.slack.com/services/T00/B00/xxx", WebhookTarget::Slack)]
    #[case("https://hooks.slack.com/workflows/T00/A00/xxx", WebhookTarget::Slack)]
    #[case("https://discord.com/api/webhooks/123/abc", WebhookTarget::Discord)]
    #[case("https://discordapp.com/api/webhooks/123/abc", WebhookTarget::Discord)]
    #[case("https://example.com/webhook", WebhookTarget::Generic)]
    #[case("https://my-server.com/slack-webhook", WebhookTarget::Generic)]
    fn detect_webhook_target(#[case] url: &str, #[case] expected: WebhookTarget) {
        assert_eq!(WebhookTarget::detect(url), expected);
    }

    // SlackPayload structure tests
    #[test]
    fn slack_payload_has_required_structure() {
        let event = make_event("Notification", "Permission needed");
        let payload = SlackPayload::from_event(&event, "test-session");
        let json = serde_json::to_value(&payload).expect("serialize");

        // Must have text field (required by Slack)
        assert!(json.get("text").is_some());
        assert!(json["text"].as_str().unwrap().contains("test-session"));

        // Must have blocks array
        assert!(json.get("blocks").is_some());
        assert!(json["blocks"].is_array());

        // First block: section with message
        let blocks = json["blocks"].as_array().unwrap();
        assert!(blocks.len() >= 2);
        assert_eq!(blocks[0]["type"], "section");
        assert!(
            blocks[0]["text"]["text"]
                .as_str()
                .unwrap()
                .contains("Permission needed")
        );

        // Second block: context with session/timestamp
        assert_eq!(blocks[1]["type"], "context");
    }

    // DiscordPayload structure tests
    #[test]
    fn discord_payload_has_required_structure() {
        let event = make_event("Notification", "Action required");
        let payload = DiscordPayload::from_event(&event, "my-session");
        let json = serde_json::to_value(&payload).expect("serialize");

        // Must have embeds array
        assert!(json.get("embeds").is_some());
        let embeds = json["embeds"].as_array().unwrap();
        assert_eq!(embeds.len(), 1);

        let embed = &embeds[0];
        // Title
        assert_eq!(embed["title"], "tazuna");
        // Description contains message
        assert!(
            embed["description"]
                .as_str()
                .unwrap()
                .contains("Action required")
        );
        // Color (orange)
        assert_eq!(embed["color"], 16_750_848);
        // Fields with session
        assert!(embed["fields"].is_array());
        // Timestamp in ISO8601
        assert!(embed.get("timestamp").is_some());
        // Footer
        assert_eq!(embed["footer"]["text"], "tazuna");
    }

    // GenericPayload structure tests (backward compat)
    #[test]
    fn generic_payload_backward_compat() {
        let event = make_event("Notification", "Test message");
        let payload = GenericPayload::from_event(&event, "sess-1");
        let json = serde_json::to_value(&payload).expect("serialize");

        // text field with format: [tazuna] session: message
        assert!(
            json["text"]
                .as_str()
                .unwrap()
                .starts_with("[tazuna] sess-1:")
        );
        assert!(json["text"].as_str().unwrap().contains("Test message"));

        // session field
        assert_eq!(json["session"], "sess-1");

        // timestamp field
        assert!(json.get("timestamp").is_some());
    }

    // Wiremock integration tests per target
    #[tokio::test]
    async fn send_slack_payload_has_blocks() {
        use wiremock::matchers::{body_partial_json, method};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let mock_server = MockServer::start().await;

        // Validate request body contains "blocks"
        Mock::given(method("POST"))
            .and(body_partial_json(serde_json::json!({
                "blocks": [{"type": "section"}]
            })))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&mock_server)
            .await;

        // URL with "hooks.slack.com" triggers Slack payload
        let notifier = WebhookNotifier::with_client(
            reqwest::Client::new(),
            format!("{}?host=hooks.slack.com", mock_server.uri()),
        );

        notifier
            .send(&make_event("Notification", "Test"), "test-session")
            .await
            .expect("send ok");
    }

    #[tokio::test]
    async fn send_discord_payload_has_embeds() {
        use wiremock::matchers::{body_partial_json, method};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let mock_server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(body_partial_json(serde_json::json!({
                "embeds": [{"title": "tazuna"}]
            })))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&mock_server)
            .await;

        // URL with "discord.com/api/webhooks" triggers Discord payload
        let notifier = WebhookNotifier::with_client(
            reqwest::Client::new(),
            format!("{}?host=discord.com/api/webhooks", mock_server.uri()),
        );

        notifier
            .send(&make_event("Notification", "Test"), "test-session")
            .await
            .expect("send ok");
    }

    #[tokio::test]
    async fn send_generic_payload_minimal() {
        use wiremock::matchers::{body_partial_json, method};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let mock_server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(body_partial_json(serde_json::json!({
                "session": "test-session"
            })))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&mock_server)
            .await;

        // Generic URL (no slack/discord pattern)
        let notifier = WebhookNotifier::with_client(reqwest::Client::new(), mock_server.uri());

        notifier
            .send(&make_event("Notification", "Test"), "test-session")
            .await
            .expect("send ok");
    }
}