scud-cli 1.22.0

Fast, simple task master for AI-driven development
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# SCUD Testing Guide

This document explains how to run and write tests for the SCUD Rust CLI.

## Table of Contents

1. [Running Tests]#running-tests
2. [Test Structure]#test-structure
3. [Writing New Tests]#writing-new-tests
4. [Test Coverage]#test-coverage
5. [Continuous Integration]#continuous-integration

---

## Running Tests

### Run All Tests

```bash
cd scud-cli
cargo test
```

### Run Specific Test Module

```bash
# Task model tests only
cargo test --lib models::task::tests

# Epic model tests only
cargo test --lib models::epic::tests

# All model tests
cargo test --lib models
```

### Run Specific Test

```bash
cargo test --lib test_circular_dependency_self_reference
```

### Run Tests with Output

```bash
# Show println! output from passing tests
cargo test -- --nocapture

# Show output only from failing tests (default)
cargo test
```

### Run Tests in Release Mode

```bash
cargo test --release
```

---

## Test Structure

### Unit Tests

Unit tests are located in the same file as the code they test, in a `#[cfg(test)]` module:

```rust
// src/models/task.rs

impl Task {
    pub fn claim(&mut self, assignee: &str) -> Result<(), String> {
        // implementation
    }
}

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

    #[test]
    fn test_task_claim_success() {
        let mut task = Task::new("TASK-1".to_string(), "Test".to_string(), "Desc".to_string());
        let result = task.claim("alice");
        assert!(result.is_ok());
    }
}
```

### Integration Tests

Integration tests are in the `tests/` directory:

```
scud-cli/
├── src/
├── tests/
│   ├── integration_test.rs      # Command integration tests
│   ├── error_handling_test.rs   # Error scenario tests
│   └── fixtures/                 # Test data
│       ├── sample_prd.md
│       └── complex_epic.json
```

---

## Writing New Tests

### Test Naming Convention

- Test functions start with `test_`
- Use descriptive names: `test_task_claim_already_locked_by_different_user`
- Group related tests with common prefix: `test_circular_dependency_*`

### Assertion Macros

```rust
// Equality
assert_eq!(actual, expected);
assert_ne!(actual, not_expected);

// Boolean
assert!(condition);
assert!(!condition);

// Result types
assert!(result.is_ok());
assert!(result.is_err());

// Option types
assert!(option.is_some());
assert!(option.is_none());

// String contains
assert!(string.contains("substring"));
```

### Testing Async Code

```rust
#[tokio::test]
async fn test_async_function() {
    let result = some_async_function().await;
    assert!(result.is_ok());
}
```

### Using Test Fixtures

```rust
use std::fs;

#[test]
fn test_parse_prd() {
    let prd_content = fs::read_to_string("tests/fixtures/sample_prd.md").unwrap();
    // Test PRD parsing
}
```

### Testing Error Cases

```rust
#[test]
fn test_invalid_input_returns_error() {
    let result = function_that_should_fail("invalid input");

    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), "Expected error message");
}
```

### Testing Panics

```rust
#[test]
#[should_panic(expected = "task ID cannot be empty")]
fn test_empty_task_id_panics() {
    Task::new("".to_string(), "Title".to_string(), "Desc".to_string());
}
```

---

## Test Coverage

### Measuring Coverage

Install `cargo-tarpaulin`:

```bash
cargo install cargo-tarpaulin
```

Generate coverage report:

```bash
cd scud-cli
cargo tarpaulin --out Html --output-dir ../coverage
```

View report:

```bash
open ../coverage/index.html
```

### Coverage Goals

- **Unit tests**: 80%+ coverage for core logic
- **Integration tests**: All commands tested end-to-end
- **Error handling**: 90%+ coverage for error paths

### Current Coverage

Run to see current coverage:

```bash
cd scud-cli
cargo tarpaulin
```

Example output:
```
|| Tested/Total Lines:
|| src/models/task.rs: 95.2% (120/126)
|| src/models/epic.rs: 92.5% (74/80)
||
|| Total: 87.3% (1234/1413)
```

---

## Continuous Integration

### GitHub Actions

Tests run automatically on:
- Every push to `master` or `main`
- Every pull request

Workflows:
- **`.github/workflows/test.yml`** - Runs tests on Ubuntu and macOS
- **`.github/workflows/coverage.yml`** - Generates coverage reports

### CI Commands

The CI runs:

```bash
# Lint
cargo clippy --all-targets --all-features -- -D warnings

# Format check
cargo fmt -- --check

# Tests
cargo test --all-features

# Build
cargo build --release
```

### Local CI Simulation

Run the same checks locally before pushing:

```bash
./scripts/ci-check.sh
```

Or manually:

```bash
cd scud-cli

# 1. Lint
cargo clippy --all-targets --all-features -- -D warnings

# 2. Format
cargo fmt --check

# 3. Test
cargo test

# 4. Build
cargo build --release
```

---

## Test Categories

### Unit Tests (37 tests)

**Task Model (24 tests):**
- Task creation and defaults
- Status transitions
- Task assignment and claiming
- Lock management and stale detection
- Dependency resolution
- Circular dependency detection
- Serialization

**Epic Model (13 tests):**
- Epic creation and task management
- Statistics calculation
- Next task finder (dependency-aware)
- Task expansion detection
- Serialization

### Integration Tests (Planned)

**Command Tests:**
- `init` - Directory structure creation
- `parse-prd` - PRD to tasks (with mock LLM)
- `list` - Task filtering
- `next` - Dependency-aware next task
- `set-status` - Status transitions
- `claim/release` - Task locking
- `stats` - Statistics calculation

**Error Handling Tests:**
- Invalid task IDs
- Missing files
- Malformed JSON
- Concurrent access conflicts

---

## Debugging Tests

### Run Single Test with Output

```bash
cargo test test_name -- --nocapture
```

### Show All Test Names

```bash
cargo test -- --list
```

### Run Tests Matching Pattern

```bash
# All circular dependency tests
cargo test circular_dependency

# All task claim tests
cargo test task_claim
```

### Ignore Slow Tests

```rust
#[test]
#[ignore]
fn test_slow_operation() {
    // This test is skipped by default
}
```

Run ignored tests:

```bash
cargo test -- --ignored

# Run all tests including ignored
cargo test -- --include-ignored
```

---

## Best Practices

### 1. Test One Thing Per Test

❌ Bad:
```rust
#[test]
fn test_task() {
    let task = Task::new(...);
    assert_eq!(task.status, TaskStatus::Pending);
    task.claim("alice").unwrap();
    assert!(task.is_locked());
    task.set_status(TaskStatus::Done);
    // Testing too many things
}
```

✅ Good:
```rust
#[test]
fn test_task_creation_defaults_to_pending() {
    let task = Task::new(...);
    assert_eq!(task.status, TaskStatus::Pending);
}

#[test]
fn test_task_claim_locks_task() {
    let mut task = Task::new(...);
    task.claim("alice").unwrap();
    assert!(task.is_locked());
}
```

### 2. Use Descriptive Test Names

The test name should describe what it's testing:

```rust
#[test]
fn test_task_claim_already_locked_by_different_user() {
    // Test name explains the scenario
}
```

### 3. Arrange-Act-Assert Pattern

```rust
#[test]
fn test_example() {
    // Arrange: Set up test data
    let mut task = Task::new("TASK-1".to_string(), "Test".to_string(), "Desc".to_string());
    task.claim("alice").unwrap();

    // Act: Perform the action
    let result = task.claim("bob");

    // Assert: Verify the outcome
    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), "Task is locked by alice");
}
```

### 4. Test Edge Cases

Always test:
- Empty inputs
- Null/None values
- Maximum values
- Invalid inputs
- Concurrent access
- Missing dependencies

### 5. Keep Tests Fast

- Avoid `sleep()` in tests
- Use mocks for external dependencies
- Use in-memory data instead of files when possible

---

## Common Issues

### Tests Pass Locally But Fail in CI

Check for:
- Platform-specific code (Unix vs Windows)
- Hardcoded paths
- Race conditions
- Time zone dependencies

### Flaky Tests

If a test sometimes passes and sometimes fails:
- Look for timing issues
- Check for proper cleanup
- Verify no shared state between tests
- Use `cargo test -- --test-threads=1` to run serially

### Slow Test Suite

- Run tests in parallel (default)
- Use `--release` for heavy tests
- Mock expensive operations (API calls, file I/O)
- Use `#[ignore]` for very slow tests

---

## Resources

- [Rust Book - Testing]https://doc.rust-lang.org/book/ch11-00-testing.html
- [Rust By Example - Testing]https://doc.rust-lang.org/rust-by-example/testing.html
- [cargo-tarpaulin]https://github.com/xd009642/tarpaulin
- [mockall crate]https://docs.rs/mockall/latest/mockall/

---

## Summary

Quick reference:

```bash
# Run all tests
cargo test

# Run with output
cargo test -- --nocapture

# Run specific test
cargo test test_name

# Check coverage
cargo tarpaulin

# Lint
cargo clippy -- -D warnings

# Format
cargo fmt

# Format check
cargo fmt -- --check
```

**Current Test Stats:**
- ✅ 37 unit tests
- ✅ 100% passing
-~87% estimated coverage
- ✅ CI/CD configured
- ⏳ Integration tests (planned)