spikes 0.4.0

Drop-in feedback collection for static HTML mockups
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;

use crate::error::{Error, Result};

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    #[serde(default)]
    pub project: ProjectConfig,
    #[serde(default)]
    pub widget: WidgetConfig,
    #[serde(default)]
    pub remote: RemoteConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ProjectConfig {
    /// Project key for grouping spikes
    pub key: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WidgetConfig {
    /// Widget theme: "dark" or "light"
    #[serde(default = "default_theme")]
    pub theme: String,
    /// Collect email from reviewers
    #[serde(default)]
    pub collect_email: bool,
    /// Button position: "bottom-right", "bottom-left", "top-right", "top-left"
    #[serde(default = "default_position")]
    pub position: String,
    /// Accent color (hex)
    #[serde(default = "default_color")]
    pub color: String,
}

impl Default for WidgetConfig {
    fn default() -> Self {
        Self {
            theme: default_theme(),
            collect_email: false,
            position: default_position(),
            color: default_color(),
        }
    }
}

fn default_theme() -> String {
    "dark".to_string()
}

fn default_position() -> String {
    "bottom-right".to_string()
}

fn default_color() -> String {
    "#e74c3c".to_string()
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RemoteConfig {
    /// Remote endpoint URL
    pub endpoint: Option<String>,
    /// Auth token for remote
    pub token: Option<String>,
    /// Use hosted spikes.sh backend
    #[serde(default)]
    pub hosted: bool,
}

impl Config {
    /// Load config from .spikes/config.toml, or return defaults
    pub fn load() -> Result<Self> {
        Self::load_from(Path::new(".spikes/config.toml"))
    }

    /// Load config from a specific path
    pub fn load_from(path: &Path) -> Result<Self> {
        if !path.exists() {
            return Ok(Self::default());
        }

        let content = fs::read_to_string(path)?;
        let config: Config = toml::from_str(&content).map_err(|e| {
            Error::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Invalid config.toml: {}", e),
            ))
        })?;

        Ok(config)
    }

    /// Save config to .spikes/config.toml
    pub fn save(&self) -> Result<()> {
        self.save_to(Path::new(".spikes/config.toml"))
    }

    /// Save config to a specific path
    pub fn save_to(&self, path: &Path) -> Result<()> {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }

        let content = toml::to_string_pretty(self).map_err(|e| {
            Error::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Failed to serialize config: {}", e),
            ))
        })?;

        fs::write(path, content)?;
        Ok(())
    }

    /// Get effective endpoint (remote.endpoint or hosted fallback)
    ///
    /// Priority:
    /// 1. If remote.endpoint is explicitly set, use it (explicit wins over hosted)
    /// 2. If remote.hosted is true, use the canonical hosted URL https://spikes.sh
    /// 3. Otherwise, return None
    pub fn effective_endpoint(&self) -> Option<String> {
        // Explicit endpoint takes precedence over hosted flag
        if let Some(ref endpoint) = self.remote.endpoint {
            return Some(endpoint.clone());
        }

        if self.remote.hosted {
            Some("https://spikes.sh".to_string())
        } else {
            None
        }
    }

    /// Get effective project key (from config or current directory name)
    pub fn effective_project_key(&self) -> String {
        self.project.key.clone().unwrap_or_else(|| {
            std::env::current_dir()
                .ok()
                .and_then(|p| p.file_name().map(|s| s.to_string_lossy().to_string()))
                .unwrap_or_else(|| "default".to_string())
        })
    }

    /// Generate widget script tag attributes from config
    pub fn widget_attributes(&self) -> String {
        self.widget_attributes_with_endpoint_override(None)
    }

    /// Generate widget script tag attributes with optional endpoint override
    ///
    /// This preserves all config-derived attributes (theme, position, color, collect_email)
    /// while allowing the endpoint to be overridden (e.g., by CLI --endpoint flag).
    pub fn widget_attributes_with_endpoint_override(&self, endpoint_override: Option<&str>) -> String {
        let mut attrs = vec![
            format!("data-project=\"{}\"", self.effective_project_key()),
            format!("data-theme=\"{}\"", self.widget.theme),
            format!("data-position=\"{}\"", self.widget.position),
            format!("data-color=\"{}\"", self.widget.color),
        ];

        if self.widget.collect_email {
            attrs.push("data-collect-email=\"true\"".to_string());
        }

        // Use endpoint override if provided, otherwise fall back to config
        let config_endpoint = self.effective_endpoint();
        let endpoint_to_use = endpoint_override.or(config_endpoint.as_deref());

        if let Some(endpoint) = endpoint_to_use {
            // For CLI --endpoint flag, use the value verbatim (no /spikes suffix, no token handling)
            // For config-derived endpoints, normalize and append /spikes path
            let full_endpoint = if endpoint_override.is_some() {
                // CLI flag value: use verbatim (VAL-INJECT-002)
                endpoint.to_string()
            } else {
                // Config-derived endpoint: normalize and append /spikes
                // Normalize endpoint: strip trailing "/spikes" if present to avoid double paths
                // This handles legacy configs where users may have manually edited endpoint to include /spikes
                let base_endpoint = endpoint
                    .trim_end_matches('/')
                    .trim_end_matches("/spikes");

                if let Some(token) = &self.remote.token {
                    format!("{}/spikes?token={}", base_endpoint, token)
                } else {
                    format!("{}/spikes", base_endpoint)
                }
            };
            attrs.push(format!("data-endpoint=\"{}\"", full_endpoint));
        }

        attrs.join(" ")
    }
}

/// Ensure .spikes directory exists, creating with defaults if needed
pub fn ensure_initialized() -> Result<bool> {
    let spikes_dir = Path::new(".spikes");
    
    if spikes_dir.exists() {
        return Ok(false); // Already existed
    }

    fs::create_dir_all(spikes_dir)?;
    
    let config = Config::default();
    config.save()?;
    
    fs::write(spikes_dir.join("feedback.jsonl"), "")?;
    
    Ok(true) // Newly created
}

#[allow(dead_code)]
pub const DEFAULT_CONFIG_TEMPLATE: &str = "# Spikes configuration
# https://spikes.sh

[project]
# Project key for grouping spikes (default: directory name)
# key = \"my-project\"

[widget]
# Widget appearance
theme = \"dark\"           # \"dark\" or \"light\"
position = \"bottom-right\" # \"bottom-right\", \"bottom-left\", \"top-right\", \"top-left\"
color = \"#e74c3c\"        # Accent color (hex)
collect_email = false    # Ask reviewers for email (builds prospect list)

[remote]
# Cloud sync configuration
# endpoint = \"https://your-worker.workers.dev\"
# token = \"your-token-here\"
# hosted = false  # Use spikes.sh managed backend instead of self-hosted
";

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

    #[test]
    fn test_default_config() {
        let config = Config::default();

        assert!(config.project.key.is_none());
        assert_eq!(config.widget.theme, "dark");
        assert_eq!(config.widget.position, "bottom-right");
        assert_eq!(config.widget.color, "#e74c3c");
        assert!(!config.widget.collect_email);
        assert!(config.remote.endpoint.is_none());
        assert!(config.remote.token.is_none());
        assert!(!config.remote.hosted);
    }

    #[test]
    fn test_load_missing_config() {
        let temp_dir = TempDir::new().unwrap();
        let missing_path = temp_dir.path().join("nonexistent/config.toml");

        let config = Config::load_from(&missing_path).unwrap();

        // Should return defaults for missing file
        assert!(config.project.key.is_none());
    }

    #[test]
    fn test_load_valid_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        let content = concat!(
            "[project]\n",
            "key = \"my-awesome-project\"\n",
            "\n",
            "[widget]\n",
            "theme = \"light\"\n",
            "position = \"top-left\"\n",
            "color = \"#3498db\"\n",
            "collect_email = true\n",
            "\n",
            "[remote]\n",
            "endpoint = \"https://api.example.com\"\n",
            "token = \"secret-token\"\n",
            "hosted = true\n",
        );
        std::fs::write(&config_path, content).unwrap();

        let config = Config::load_from(&config_path).unwrap();

        assert_eq!(config.project.key, Some("my-awesome-project".to_string()));
        assert_eq!(config.widget.theme, "light");
        assert_eq!(config.widget.position, "top-left");
        assert_eq!(config.widget.color, "#3498db");
        assert!(config.widget.collect_email);
        assert_eq!(config.remote.endpoint, Some("https://api.example.com".to_string()));
        assert_eq!(config.remote.token, Some("secret-token".to_string()));
        assert!(config.remote.hosted);
    }

    #[test]
    fn test_load_invalid_toml() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        std::fs::write(&config_path, "this is not valid toml [[[[").unwrap();

        let result = Config::load_from(&config_path);
        assert!(result.is_err());
    }

    #[test]
    fn test_save_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        let mut config = Config::default();
        config.project.key = Some("saved-project".to_string());
        config.widget.theme = "light".to_string();

        config.save_to(&config_path).unwrap();

        let content = std::fs::read_to_string(&config_path).unwrap();
        assert!(content.contains("saved-project"));
        assert!(content.contains("light"));
    }

    #[test]
    fn test_save_creates_parent_dirs() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("nested/dir/config.toml");

        let config = Config::default();
        config.save_to(&config_path).unwrap();

        assert!(config_path.exists());
    }

    #[test]
    fn test_effective_endpoint_hosted() {
        // Replaced by test_effective_endpoint_hosted_returns_canonical_url
        // Keeping for backward compatibility check - now expects canonical endpoint
        let mut config = Config::default();
        config.remote.hosted = true;

        assert_eq!(config.effective_endpoint(), Some("https://spikes.sh".to_string()));
    }

    #[test]
    fn test_effective_endpoint_custom() {
        let mut config = Config::default();
        config.remote.endpoint = Some("https://custom.example.com".to_string());

        assert_eq!(config.effective_endpoint(), Some("https://custom.example.com".to_string()));
    }

    #[test]
    fn test_effective_endpoint_none() {
        let config = Config::default();
        assert!(config.remote.endpoint.is_none());
        assert!(!config.remote.hosted);
        // effective_endpoint returns None when neither hosted nor custom endpoint
        assert!(config.effective_endpoint().is_none());
    }

    #[test]
    fn test_effective_project_key_from_config() {
        let mut config = Config::default();
        config.project.key = Some("configured-key".to_string());

        assert_eq!(config.effective_project_key(), "configured-key");
    }

    #[test]
    fn test_widget_attributes_basic() {
        let config = Config::default();
        let attrs = config.widget_attributes();

        assert!(attrs.contains("data-project"));
        assert!(attrs.contains("data-theme=\"dark\""));
        assert!(attrs.contains("data-position=\"bottom-right\""));
        assert!(attrs.contains("data-color=\"#e74c3c\""));
    }

    #[test]
    fn test_widget_attributes_with_collect_email() {
        let mut config = Config::default();
        config.widget.collect_email = true;

        let attrs = config.widget_attributes();
        assert!(attrs.contains("data-collect-email=\"true\""));
    }

    #[test]
    fn test_widget_attributes_with_hosted() {
        let mut config = Config::default();
        config.remote.hosted = true;

        let attrs = config.widget_attributes();
        assert!(attrs.contains("data-endpoint"));
        assert!(attrs.contains("spikes.sh"));
    }

    #[test]
    fn test_widget_attributes_with_custom_endpoint() {
        let mut config = Config::default();
        config.remote.endpoint = Some("https://api.custom.com".to_string());

        let attrs = config.widget_attributes();
        assert!(attrs.contains("data-endpoint=\"https://api.custom.com/spikes\""));
    }

    #[test]
    fn test_widget_attributes_with_token() {
        let mut config = Config::default();
        config.remote.endpoint = Some("https://api.custom.com".to_string());
        config.remote.token = Some("my-token".to_string());

        let attrs = config.widget_attributes();
        assert!(attrs.contains("token=my-token"));
    }

    #[test]
    fn test_effective_endpoint_hosted_returns_canonical_url() {
        // VAL-CONFIG-001: When hosted=true, effective_endpoint() returns https://spikes.sh
        let mut config = Config::default();
        config.remote.hosted = true;

        assert_eq!(config.effective_endpoint(), Some("https://spikes.sh".to_string()));
    }

    #[test]
    fn test_widget_attributes_hosted_produces_correct_spikes_url() {
        // VAL-CONFIG-002: widget_attributes() includes data-endpoint="https://spikes.sh/spikes" when hosted=true
        let mut config = Config::default();
        config.remote.hosted = true;

        let attrs = config.widget_attributes();
        assert!(attrs.contains("data-endpoint=\"https://spikes.sh/spikes\""));
        // Ensure no double /spikes
        assert!(!attrs.contains("/spikes/spikes"));
    }

    #[test]
    fn test_explicit_endpoint_wins_over_hosted() {
        // VAL-CONFIG-003: Explicit endpoint wins when both hosted=true and endpoint are set
        let mut config = Config::default();
        config.remote.hosted = true;
        config.remote.endpoint = Some("https://my.worker.dev".to_string());

        // When both are set, the explicit endpoint should win
        assert_eq!(config.effective_endpoint(), Some("https://my.worker.dev".to_string()));
    }

    #[test]
    fn test_widget_attributes_explicit_endpoint_with_hosted() {
        // VAL-CROSS-005: Config with [remote] endpoint="https://my.worker.dev" and hosted=true
        // resolves to the explicit endpoint
        let mut config = Config::default();
        config.remote.hosted = true;
        config.remote.endpoint = Some("https://my.worker.dev".to_string());

        let attrs = config.widget_attributes();
        assert!(attrs.contains("data-endpoint=\"https://my.worker.dev/spikes\""));
        // Should NOT contain spikes.sh since explicit endpoint wins
        assert!(!attrs.contains("spikes.sh"));
    }

    #[test]
    fn test_legacy_config_with_trailing_spikes_not_doubled() {
        // VAL-CONFIG-004: Legacy config with endpoint ending in "/spikes" does NOT produce "/spikes/spikes"
        let mut config = Config::default();
        config.remote.endpoint = Some("https://spikes.sh/spikes".to_string());

        let attrs = config.widget_attributes();
        // Should strip trailing /spikes before appending
        assert!(attrs.contains("data-endpoint=\"https://spikes.sh/spikes\""));
        // Should NOT have double /spikes
        assert!(!attrs.contains("/spikes/spikes"));
    }

    #[test]
    fn test_legacy_config_with_path_prefix_preserved() {
        // Test that endpoints with path prefixes work correctly
        let mut config = Config::default();
        config.remote.endpoint = Some("https://my.worker.dev/api".to_string());

        let attrs = config.widget_attributes();
        assert!(attrs.contains("data-endpoint=\"https://my.worker.dev/api/spikes\""));
    }

    #[test]
    fn test_config_without_remote_section_loads_cleanly() {
        // VAL-CONFIG-024: Configs with no [remote] at all still load cleanly
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        let content = r#"
[project]
key = "no-remote-project"

[widget]
theme = "light"
"#;
        std::fs::write(&config_path, content).unwrap();

        let config = Config::load_from(&config_path).unwrap();

        // Should use defaults for missing remote section
        assert!(!config.remote.hosted);
        assert!(config.remote.endpoint.is_none());
        assert!(config.remote.token.is_none());
    }

    #[test]
    fn test_partial_config() {
        // Test that partial configs use defaults for missing fields
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        let content = r#"
[project]
key = "partial-project"
"#;
        std::fs::write(&config_path, content).unwrap();

        let config = Config::load_from(&config_path).unwrap();

        assert_eq!(config.project.key, Some("partial-project".to_string()));
        // Widget should use defaults
        assert_eq!(config.widget.theme, "dark");
        assert_eq!(config.widget.position, "bottom-right");
    }

    #[test]
    fn test_widget_attributes_with_endpoint_override_preserves_config_attrs() {
        // Regression test: --endpoint flag should preserve theme, position, color, collect_email
        let mut config = Config::default();
        config.project.key = Some("test-project".to_string());
        config.widget.theme = "light".to_string();
        config.widget.position = "top-right".to_string();
        config.widget.color = "#3498db".to_string();
        config.widget.collect_email = true;

        let attrs = config.widget_attributes_with_endpoint_override(Some("https://custom.example.com/api"));

        // All config attributes should be preserved
        assert!(attrs.contains("data-project=\"test-project\""));
        assert!(attrs.contains("data-theme=\"light\""));
        assert!(attrs.contains("data-position=\"top-right\""));
        assert!(attrs.contains("data-color=\"#3498db\""));
        assert!(attrs.contains("data-collect-email=\"true\""));

        // Flag endpoint should be used verbatim
        assert!(attrs.contains("data-endpoint=\"https://custom.example.com/api\""));
    }

    #[test]
    fn test_widget_attributes_with_endpoint_override_no_override_uses_config() {
        // When override is None, should behave like widget_attributes()
        let mut config = Config::default();
        config.remote.hosted = true;
        config.widget.theme = "light".to_string();

        let attrs_without_override = config.widget_attributes_with_endpoint_override(None);
        let attrs_normal = config.widget_attributes();

        assert_eq!(attrs_without_override, attrs_normal);
        assert!(attrs_without_override.contains("data-endpoint=\"https://spikes.sh/spikes\""));
    }

    #[test]
    fn test_widget_attributes_override_preserves_config_endpoint_with_token() {
        // When using override, config's token should NOT be appended (flag is verbatim)
        let mut config = Config::default();
        config.remote.endpoint = Some("https://api.config.com".to_string());
        config.remote.token = Some("config-token".to_string());
        config.widget.theme = "light".to_string();

        // Without override: token should be included
        let attrs_no_override = config.widget_attributes_with_endpoint_override(None);
        assert!(attrs_no_override.contains("token=config-token"));

        // With override: verbatim, no token from config
        let attrs_with_override = config.widget_attributes_with_endpoint_override(Some("https://custom.com/spikes"));
        assert!(attrs_with_override.contains("data-endpoint=\"https://custom.com/spikes\""));
        assert!(!attrs_with_override.contains("token="));
    }
}