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
use std::collections::HashSet;
use std::fs::{self, OpenOptions};
use std::io::{BufRead, BufReader, Write};
use std::path::Path;

use crate::auth::get_api_base;
use crate::error::{map_http_error, map_network_error, Error, Result};
use crate::spike::{PaginatedResponse, Spike};

pub struct PullOptions {
    pub endpoint: Option<String>,
    pub token: Option<String>,
    pub from: Option<String>,
    pub json: bool,
}

struct RemoteConfig {
    endpoint: String,
    token: String,
}

pub fn run(options: PullOptions) -> Result<()> {
    // If --from URL is provided, fetch from public share
    if let Some(ref url) = options.from {
        return run_from_share(url, options.json);
    }

    let config = get_remote_config(options.endpoint, options.token)?;

    // Fetch remote spikes
    let remote_spikes = fetch_remote_spikes(&config)?;

    // Load local spikes
    let feedback_path = Path::new(".spikes/feedback.jsonl");
    let local_spikes = load_local_spikes(feedback_path)?;

    // Build set of existing IDs
    let existing_ids: HashSet<String> = local_spikes.iter().map(|s| s.id.clone()).collect();

    // Find new spikes
    let new_spikes: Vec<&Spike> = remote_spikes
        .iter()
        .filter(|s| !existing_ids.contains(&s.id))
        .collect();

    let new_count = new_spikes.len();

    // Append new spikes to local file
    if !new_spikes.is_empty() {
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(feedback_path)?;

        for spike in &new_spikes {
            let mut json = serde_json::to_string(spike)?;
            json.push('\n');
            file.write_all(json.as_bytes())?;
        }
    }

    if options.json {
        println!(
            "{}",
            serde_json::json!({
                "success": true,
                "fetched": remote_spikes.len(),
                "new": new_count,
                "existing": local_spikes.len(),
                "total": local_spikes.len() + new_count
            })
        );
    } else {
        println!();
        println!("  🗡️  Pulled from remote");
        println!();
        println!("  Remote spikes:  {}", remote_spikes.len());
        println!("  New spikes:     {}", new_count);
        println!("  Local total:    {}", local_spikes.len() + new_count);
        println!();
    }

    Ok(())
}

fn get_remote_config(
    endpoint_arg: Option<String>,
    token_arg: Option<String>,
) -> Result<RemoteConfig> {
    // Try command-line args first
    if let (Some(endpoint), Some(token)) = (endpoint_arg.clone(), token_arg.clone()) {
        return Ok(RemoteConfig { endpoint, token });
    }

    // Fall back to config file
    let config_path = Path::new(".spikes/config.toml");
    if !config_path.exists() {
        return Err(Error::Io(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "No endpoint specified and .spikes/config.toml not found.\n\
             Use --endpoint and --token, or configure in .spikes/config.toml:\n\n\
             [remote]\n\
             endpoint = \"https://your-worker.workers.dev\"\n\
             token = \"your-token\"",
        )));
    }

    let content = fs::read_to_string(config_path)?;
    let config: toml::Value = content.parse().map_err(|e: toml::de::Error| {
        Error::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!("Invalid config.toml: {}", e),
        ))
    })?;

    let endpoint = endpoint_arg.or_else(|| {
        config
            .get("remote")
            .and_then(|r| r.get("endpoint"))
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
    });

    let token = token_arg.or_else(|| {
        config
            .get("remote")
            .and_then(|r| r.get("token"))
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
    });

    match (endpoint, token) {
        (Some(endpoint), Some(token)) => {
            if endpoint.ends_with("/spikes") {
                return Err(Error::Io(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "Endpoint should be the base URL (e.g. https://spikes.sh), not the /spikes path",
                )));
            }
            Ok(RemoteConfig { endpoint, token })
        }
        (None, _) => Err(Error::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "No remote endpoint. Add [remote] endpoint = \"...\" to .spikes/config.toml",
        ))),
        (_, None) => Err(Error::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "No token. Add token = \"...\" under [remote] in .spikes/config.toml",
        ))),
    }
}

fn fetch_remote_spikes(config: &RemoteConfig) -> Result<Vec<Spike>> {
    let url = format!("{}/spikes", config.endpoint.trim_end_matches('/'));

    // Use ureq for synchronous HTTP (simpler than async for CLI)
    let response = match ureq::get(&url)
        .set("Authorization", &format!("Bearer {}", config.token))
        .call()
    {
        Ok(resp) => resp,
        Err(ureq::Error::Status(status, response)) => {
            // Non-2xx response - try to read body for error details
            let body = response.into_string().ok();
            return Err(map_http_error(status, body.as_deref()));
        }
        Err(e) => return Err(map_network_error(&e.to_string())),
    };

    let status = response.status();

    if status != 200 {
        let body = response.into_string().ok();
        return Err(map_http_error(status, body.as_deref()));
    }

    let body = response
        .into_string()
        .map_err(|e| Error::RequestFailed(format!("Failed to read response: {}", e)))?;

    if body.starts_with('<') {
        return Err(Error::RequestFailed(
            "Got HTML instead of JSON. Check that the endpoint URL is correct.".to_string()
        ));
    }

    // API returns paginated response: { data: [...spikes], next_cursor: string|null }
    let response: PaginatedResponse<Spike> = serde_json::from_str(&body)?;
    Ok(response.data)
}

fn load_local_spikes(path: &Path) -> Result<Vec<Spike>> {
    if !path.exists() {
        return Ok(Vec::new());
    }

    let file = fs::File::open(path)?;
    let reader = BufReader::new(file);
    let mut spikes = Vec::new();

    for line in reader.lines() {
        let line = line?;
        if line.trim().is_empty() {
            continue;
        }
        if let Ok(spike) = serde_json::from_str(&line) {
            spikes.push(spike);
        }
    }

    Ok(spikes)
}

fn run_from_share(url: &str, json_output: bool) -> Result<()> {
    // Parse share slug from URL (e.g., https://spikes.sh/s/governance-x7k2m)
    let share_id = parse_share_slug(url)?;

    // Fetch spikes from public endpoint (respects SPIKES_API_URL env var)
    let api_url = format!("{}/spikes?project={}", get_api_base(), share_id);

    let response = match ureq::get(&api_url).call() {
        Ok(resp) => resp,
        Err(ureq::Error::Status(status, response)) => {
            let body = response.into_string().ok();
            return Err(map_http_error(status, body.as_deref()));
        }
        Err(e) => return Err(map_network_error(&e.to_string())),
    };

    let status = response.status();

    if status != 200 {
        let body = response.into_string().ok();
        return Err(map_http_error(status, body.as_deref()));
    }

    let body = response
        .into_string()
        .map_err(|e| Error::RequestFailed(format!("Failed to read response: {}", e)))?;

    // API returns paginated response: { data: [...spikes], next_cursor: string|null }
    let response: PaginatedResponse<Spike> = serde_json::from_str(&body)?;
    let remote_spikes = response.data;

    // Load local spikes and merge
    let feedback_path = Path::new(".spikes/feedback.jsonl");
    let local_spikes = load_local_spikes(feedback_path)?;

    let existing_ids: HashSet<String> = local_spikes.iter().map(|s| s.id.clone()).collect();

    let new_spikes: Vec<&Spike> = remote_spikes
        .iter()
        .filter(|s| !existing_ids.contains(&s.id))
        .collect();

    let new_count = new_spikes.len();

    if !new_spikes.is_empty() {
        // Ensure .spikes directory exists
        if let Some(parent) = feedback_path.parent() {
            fs::create_dir_all(parent)?;
        }

        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(feedback_path)?;

        for spike in &new_spikes {
            let mut json = serde_json::to_string(spike)?;
            json.push('\n');
            file.write_all(json.as_bytes())?;
        }
    }

    if json_output {
        println!(
            "{}",
            serde_json::json!({
                "success": true,
                "source": url,
                "share_id": share_id,
                "fetched": remote_spikes.len(),
                "new": new_count,
                "existing": local_spikes.len(),
                "total": local_spikes.len() + new_count
            })
        );
    } else {
        println!();
        println!("  🗡️  Pulled from share");
        println!();
        println!("  Source:         {}", url);
        println!("  Remote spikes:  {}", remote_spikes.len());
        println!("  New spikes:     {}", new_count);
        println!("  Local total:    {}", local_spikes.len() + new_count);
        println!();
    }

    Ok(())
}

fn parse_share_slug(url: &str) -> Result<String> {
    // Handle both full URLs and bare slugs
    // Full URL: https://spikes.sh/s/governance-x7k2m
    // Bare slug: governance-x7k2m

    if url.starts_with("http://") || url.starts_with("https://") {
        // Parse URL to extract slug after /s/
        if let Some(pos) = url.find("/s/") {
            let slug = &url[pos + 3..];
            // Remove any trailing slashes or query params
            let slug = slug.split(&['/', '?', '#'][..]).next().unwrap_or(slug);
            if slug.is_empty() {
                return Err(Error::Io(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "Invalid share URL: no slug found after /s/",
                )));
            }
            return Ok(slug.to_string());
        }
        return Err(Error::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "Invalid share URL: expected format https://spikes.sh/s/<slug>",
        )));
    }

    // Assume it's a bare slug
    if url.is_empty() {
        return Err(Error::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "Share slug cannot be empty",
        )));
    }

    Ok(url.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;

    const SPIKES_API_URL_ENV: &str = "SPIKES_API_URL";

    #[test]
    fn test_parse_share_slug_from_url() {
        let slug = parse_share_slug("https://spikes.sh/s/governance-x7k2m").unwrap();
        assert_eq!(slug, "governance-x7k2m");
    }

    #[test]
    fn test_parse_share_slug_from_url_with_trailing_slash() {
        let slug = parse_share_slug("https://spikes.sh/s/governance-x7k2m/").unwrap();
        assert_eq!(slug, "governance-x7k2m");
    }

    #[test]
    fn test_parse_share_slug_from_url_with_query() {
        let slug = parse_share_slug("https://spikes.sh/s/governance-x7k2m?foo=bar").unwrap();
        assert_eq!(slug, "governance-x7k2m");
    }

    #[test]
    fn test_parse_share_slug_bare() {
        let slug = parse_share_slug("governance-x7k2m").unwrap();
        assert_eq!(slug, "governance-x7k2m");
    }

    #[test]
    fn test_parse_share_slug_invalid_url() {
        let result = parse_share_slug("https://spikes.sh/invalid");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_share_slug_empty() {
        let result = parse_share_slug("");
        assert!(result.is_err());
    }

    #[test]
    #[serial(api_url)]
    fn test_pull_from_share_uses_api_base_default() {
        // Save current value
        let original = std::env::var(SPIKES_API_URL_ENV).ok();

        // Clear env var
        std::env::remove_var(SPIKES_API_URL_ENV);

        // Verify get_api_base returns default
        let base = get_api_base();
        assert_eq!(base, "https://spikes.sh");

        // Restore original value
        if let Some(val) = original {
            std::env::set_var(SPIKES_API_URL_ENV, val);
        }
    }

    #[test]
    #[serial(api_url)]
    fn test_pull_from_share_uses_api_base_env_override() {
        // Save current value
        let original = std::env::var(SPIKES_API_URL_ENV).ok();

        // Set custom API URL (e.g., for local dev with wrangler)
        std::env::set_var(SPIKES_API_URL_ENV, "http://localhost:8787");

        // Verify get_api_base returns env var value
        let base = get_api_base();
        assert_eq!(base, "http://localhost:8787");

        // Simulate URL construction for pull --from
        let share_id = "test-share-123";
        let api_url = format!("{}/spikes?project={}", get_api_base(), share_id);
        assert_eq!(api_url, "http://localhost:8787/spikes?project=test-share-123");

        // Restore original value
        if let Some(val) = original {
            std::env::set_var(SPIKES_API_URL_ENV, val);
        } else {
            std::env::remove_var(SPIKES_API_URL_ENV);
        }
    }

    #[test]
    #[serial(api_url)]
    fn test_pull_from_share_url_construction_with_custom_host() {
        // Save current value
        let original = std::env::var(SPIKES_API_URL_ENV).ok();

        // Set custom self-hosted API URL
        std::env::set_var(SPIKES_API_URL_ENV, "https://spikes.example.com");

        // Verify URL construction uses custom host
        let share_id = "my-project-abc";
        let api_url = format!("{}/spikes?project={}", get_api_base(), share_id);
        assert_eq!(api_url, "https://spikes.example.com/spikes?project=my-project-abc");

        // Restore original value
        if let Some(val) = original {
            std::env::set_var(SPIKES_API_URL_ENV, val);
        } else {
            std::env::remove_var(SPIKES_API_URL_ENV);
        }
    }
}