spikes 0.3.2

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
//! Tests for the Spikes GitHub Action gate logic
//!
//! These tests verify the check.sh script behavior:
//! - Threshold comparison
//! - ignore-paths filtering
//! - require-resolution mode
//! - Edge cases (missing data, empty files)

mod common;

use assert_cmd::cargo::cargo_bin_cmd;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;

/// Helper to create a test spike JSON
fn make_spike(id: &str, page: &str, rating: Option<&str>, resolved: bool) -> String {
    let rating_json = match rating {
        Some(r) => format!("\"{}\"", r),
        None => "null".to_string(),
    };
    let resolved_json = if resolved {
        r#""resolved": true, "resolvedAt": "2024-01-02T00:00:00Z""#
    } else {
        r#""resolved": false"#
    };

    format!(
        r#"{{"id":"{}","type":"page","projectKey":"test","page":"{}","url":"http://test/{}","reviewer":{{"id":"r1","name":"Test"}},"rating":{},"comments":"Test comment","timestamp":"2024-01-01T00:00:00Z",{}}}"#,
        id, page, page, rating_json, resolved_json
    )
}

/// Helper to set up a test project with spikes
fn setup_test_project() -> TempDir {
    let temp_dir = tempfile::tempdir().unwrap();

    // Create .spikes directory
    fs::create_dir_all(temp_dir.path().join(".spikes")).unwrap();

    // Create config
    fs::write(
        temp_dir.path().join(".spikes/config.toml"),
        "project_key = \"test\"\n",
    )
    .unwrap();

    // Create empty feedback file
    fs::write(temp_dir.path().join(".spikes/feedback.jsonl"), "").unwrap();

    temp_dir
}

/// Helper to write spikes to feedback file
fn write_spikes(dir: &TempDir, spikes: &[String]) {
    let content = spikes.join("\n");
    fs::write(dir.path().join(".spikes/feedback.jsonl"), content).unwrap();
}

/// Get path to the check.sh script
fn check_script_path() -> PathBuf {
    let mut path = std::env::current_dir().unwrap();
    // Navigate from cli/ to action/
    path.pop(); // Remove 'cli'
    path.push("action");
    path.push("check.sh");
    path
}

/// Get path to the spikes binary
fn spikes_binary_path() -> PathBuf {
    // Use cargo_bin_cmd to get the binary path
    cargo_bin_cmd!("spikes")
        .arg("--help")
        .assert()
        .success();

    // Get the path to the binary
    let _output = cargo_bin_cmd!("spikes")
        .arg("version")
        .assert()
        .get_output()
        .to_owned();

    // The binary should be at target/debug/spikes
    let mut path = std::env::current_exe().unwrap();
    path.pop(); // Remove test binary name
    path.pop(); // Remove 'deps'
    path.push("spikes");

    if !path.exists() {
        // Try release build
        path.pop();
        path.push("release");
        path.push("spikes");
    }

    path
}

// ============================================================================
// Test: Action passes when no negative feedback exists
// ============================================================================

#[test]
fn test_gate_passes_with_positive_only() {
    let temp_dir = setup_test_project();

    // Add only positive spikes
    write_spikes(
        &temp_dir,
        &[
            make_spike("s1", "/index.html", Some("love"), false),
            make_spike("s2", "/about.html", Some("like"), false),
        ],
    );

    // Run check.sh
    let spikes_bin = spikes_binary_path();
    let output = Command::new(check_script_path())
        .args(["0", "", "false"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should pass (exit 0)
    assert!(
        output.status.success(),
        "Should pass with positive only. stdout: {}, stderr: {}",
        stdout,
        stderr
    );
    assert!(stdout.contains("CLEAN SLATE") || stdout.contains("No blocking"));
}

// ============================================================================
// Test: Action fails when negative feedback exceeds threshold
// ============================================================================

#[test]
fn test_gate_fails_with_negative_spikes() {
    let temp_dir = setup_test_project();

    // Add negative spikes
    write_spikes(
        &temp_dir,
        &[
            make_spike("s1", "/index.html", Some("no"), false),
            make_spike("s2", "/about.html", Some("meh"), false),
        ],
    );

    // Run check.sh with threshold 0
    let spikes_bin = spikes_binary_path();
    let output = Command::new(check_script_path())
        .args(["0", "", "false"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should fail (exit 1)
    assert!(
        !output.status.success(),
        "Should fail with negative spikes. stdout: {}, stderr: {}",
        stdout,
        stderr
    );
    assert!(stdout.contains("BLOCKING FEEDBACK") || stderr.contains("VIBES ARE OFF"));
}

// ============================================================================
// Test: Action passes when count is within threshold
// ============================================================================

#[test]
fn test_gate_passes_within_threshold() {
    let temp_dir = setup_test_project();

    // Add 2 negative spikes
    write_spikes(
        &temp_dir,
        &[
            make_spike("s1", "/index.html", Some("no"), false),
            make_spike("s2", "/about.html", Some("meh"), false),
        ],
    );

    // Run check.sh with threshold 2 (allow 2 blocking)
    let spikes_bin = spikes_binary_path();
    let output = Command::new(check_script_path())
        .args(["2", "", "false"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should pass (exit 0) - 2 blocking <= threshold of 2
    assert!(
        output.status.success(),
        "Should pass within threshold. stdout: {}",
        stdout
    );
    assert!(stdout.contains("PASSED") || stdout.contains("acceptable"));
}

// ============================================================================
// Test: ignore-paths filters out matching pages
// ============================================================================

#[test]
fn test_gate_ignores_matching_paths() {
    let temp_dir = setup_test_project();

    // Add negative spikes on different pages
    write_spikes(
        &temp_dir,
        &[
            make_spike("s1", "/docs/index.html", Some("no"), false),
            make_spike("s2", "/about.html", Some("no"), false),
        ],
    );

    // Run check.sh with ignore-paths for /docs/**
    let spikes_bin = spikes_binary_path();
    let ignore_paths = "/docs/**";
    let output = Command::new(check_script_path())
        .args(["0", ignore_paths, "false"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should fail - /about.html spike is not ignored
    assert!(
        !output.status.success(),
        "Should fail - /about.html not ignored. stdout: {}",
        stdout
    );
}

#[test]
fn test_gate_ignores_all_negative_with_wildcard() {
    let temp_dir = setup_test_project();

    // Add negative spikes on various pages
    write_spikes(
        &temp_dir,
        &[
            make_spike("s1", "/docs/index.html", Some("no"), false),
            make_spike("s2", "/docs/api.html", Some("meh"), false),
        ],
    );

    // Run check.sh with ignore-paths for /docs/**
    let spikes_bin = spikes_binary_path();
    let ignore_paths = "/docs/**";
    let output = Command::new(check_script_path())
        .args(["0", ignore_paths, "false"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should pass - all spikes ignored
    assert!(
        output.status.success(),
        "Should pass - all spikes ignored. stdout: {}",
        stdout
    );
}

// ============================================================================
// Test: require-resolution mode fails on unresolved positive spikes
// ============================================================================

#[test]
fn test_require_resolution_fails_on_unresolved_positive() {
    let temp_dir = setup_test_project();

    // Add unresolved positive spikes (love, like)
    write_spikes(
        &temp_dir,
        &[
            make_spike("s1", "/index.html", Some("love"), false),
            make_spike("s2", "/about.html", Some("like"), false),
        ],
    );

    // Run check.sh with require-resolution=true
    let spikes_bin = spikes_binary_path();
    let output = Command::new(check_script_path())
        .args(["0", "", "true"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should fail - unresolved spikes are blocking in require-resolution mode
    assert!(
        !output.status.success(),
        "Should fail with unresolved positive spikes. stdout: {}",
        stdout
    );
}

#[test]
fn test_require_resolution_passes_with_resolved() {
    let temp_dir = setup_test_project();

    // Add resolved spikes
    write_spikes(
        &temp_dir,
        &[
            make_spike("s1", "/index.html", Some("no"), true),
            make_spike("s2", "/about.html", Some("meh"), true),
        ],
    );

    // Run check.sh with require-resolution=true
    let spikes_bin = spikes_binary_path();
    let output = Command::new(check_script_path())
        .args(["0", "", "true"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should pass - all spikes are resolved
    assert!(
        output.status.success(),
        "Should pass with all resolved. stdout: {}",
        stdout
    );
}

// ============================================================================
// Test: Missing .spikes/ directory passes with warning
// ============================================================================

#[test]
fn test_gate_passes_without_spikes_dir() {
    let temp_dir = tempfile::tempdir().unwrap();

    // No .spikes/ directory created

    // Run check.sh
    let spikes_bin = spikes_binary_path();
    let output = Command::new(check_script_path())
        .args(["0", "", "false"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should pass (exit 0) with warning
    assert!(
        output.status.success(),
        "Should pass without .spikes/ dir. stdout: {}, stderr: {}",
        stdout,
        stderr
    );
    assert!(
        stdout.contains("Clean slate") || stderr.contains("Clean slate") || stdout.contains("warning"),
        "Should show warning"
    );
}

// ============================================================================
// Test: Empty feedback file passes
// ============================================================================

#[test]
fn test_gate_passes_with_empty_feedback() {
    let temp_dir = setup_test_project();

    // Keep feedback.jsonl empty

    // Run check.sh
    let spikes_bin = spikes_binary_path();
    let output = Command::new(check_script_path())
        .args(["0", "", "false"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should pass (exit 0) with warning
    assert!(
        output.status.success(),
        "Should pass with empty feedback. stdout: {}",
        stdout
    );
}

// ============================================================================
// Test: Resolved negative spikes are not blocking
// ============================================================================

#[test]
fn test_resolved_negative_not_blocking() {
    let temp_dir = setup_test_project();

    // Add resolved negative spikes
    write_spikes(
        &temp_dir,
        &[
            make_spike("s1", "/index.html", Some("no"), true),
            make_spike("s2", "/about.html", Some("meh"), true),
        ],
    );

    // Run check.sh with threshold 0
    let spikes_bin = spikes_binary_path();
    let output = Command::new(check_script_path())
        .args(["0", "", "false"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should pass - resolved spikes are not blocking
    assert!(
        output.status.success(),
        "Should pass with resolved negative. stdout: {}",
        stdout
    );
}

// ============================================================================
// Test: Mixed resolved/unresolved only counts unresolved
// ============================================================================

#[test]
fn test_mixed_resolved_unresolved() {
    let temp_dir = setup_test_project();

    // Add mix of resolved and unresolved negative
    write_spikes(
        &temp_dir,
        &[
            make_spike("s1", "/index.html", Some("no"), false), // blocking
            make_spike("s2", "/about.html", Some("meh"), true), // resolved
            make_spike("s3", "/contact.html", Some("no"), false), // blocking
        ],
    );

    // Run check.sh with threshold 1
    let spikes_bin = spikes_binary_path();
    let output = Command::new(check_script_path())
        .args(["1", "", "false"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should fail - 2 unresolved > threshold 1
    assert!(
        !output.status.success(),
        "Should fail with 2 unresolved. stdout: {}",
        stdout
    );
}

// ============================================================================
// Test: No rating is not blocking
// ============================================================================

#[test]
fn test_no_rating_not_blocking() {
    let temp_dir = setup_test_project();

    // Add spike with no rating
    write_spikes(&temp_dir, &[make_spike("s1", "/index.html", None, false)]);

    // Run check.sh with threshold 0
    let spikes_bin = spikes_binary_path();
    let output = Command::new(check_script_path())
        .args(["0", "", "false"])
        .env("SPIKES_BIN", &spikes_bin)
        .current_dir(temp_dir.path())
        .output()
        .expect("Failed to run check.sh");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should pass - no rating is not blocking
    assert!(
        output.status.success(),
        "Should pass with no rating. stdout: {}",
        stdout
    );
}