ito-core 0.1.32

Core functionality and business logic for Ito
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
use ito_core::change_repository::FsChangeRepository;
use ito_core::errors::CoreError;
use ito_core::module_repository::FsModuleRepository;
use ito_core::show::{
    DeltaSpecFile, bundle_main_specs_markdown, bundle_main_specs_show_json, load_delta_spec_file,
    parse_change_show_json, parse_spec_show_json, read_change_delta_spec_files,
    read_module_markdown,
};
use std::path::Path;

fn write(path: &Path, contents: &str) {
    let Some(parent) = path.parent() else {
        panic!("path has no parent: {}", path.display());
    };
    std::fs::create_dir_all(parent).unwrap();
    std::fs::write(path, contents).unwrap();
}

#[test]
fn parse_spec_show_json_extracts_overview_requirements_and_scenarios() {
    let md = r#"
## Purpose

This spec exists to prove parsing works for tests. It is long enough to pass warnings.

## Requirements

### Requirement: The system SHALL do something
The system SHALL do something.

#### Scenario: Happy path
Given A
When B
Then C

### Requirement: The system MUST do another thing
The system MUST do another thing.

#### Scenario: Another path
Given X
Then Y
"#;

    let json = parse_spec_show_json("spec-id", md);
    assert_eq!(json.id, "spec-id");
    assert!(json.overview.contains("This spec exists"));
    assert_eq!(json.requirement_count, 2);
    assert_eq!(json.requirements.len(), 2);
    assert_eq!(json.requirements[0].scenarios.len(), 1);
    assert!(
        json.requirements[0].scenarios[0]
            .raw_text
            .contains("Given A")
    );
}

#[test]
fn read_change_delta_spec_files_lists_specs_sorted() {
    let td = tempfile::tempdir().unwrap();
    let ito = td.path().join(".ito");
    let change_id = "001-01_demo";

    write(
        &ito.join("changes")
            .join(change_id)
            .join("specs")
            .join("b")
            .join("spec.md"),
        "# b\n",
    );
    write(
        &ito.join("changes")
            .join(change_id)
            .join("specs")
            .join("a")
            .join("spec.md"),
        "# a\n",
    );

    let repo = FsChangeRepository::new(&ito);
    let files = read_change_delta_spec_files(&repo, change_id).unwrap();
    assert_eq!(files.len(), 2);
    assert_eq!(files[0].spec, "a");
    assert_eq!(files[1].spec, "b");
}

#[test]
fn load_delta_spec_file_uses_parent_dir_name_as_spec() {
    let td = tempfile::tempdir().unwrap();
    let path = td.path().join("auth").join("spec.md");
    write(&path, "# auth\n");

    let f = load_delta_spec_file(&path).unwrap();
    assert_eq!(f.spec, "auth");
    assert!(f.markdown.contains("# auth"));
}

#[test]
fn parse_change_show_json_emits_deltas_with_operations() {
    let files = vec![DeltaSpecFile {
        spec: "auth".to_string(),
        markdown: r#"
## ADDED Requirements

### Requirement: Added thing
The system SHALL add a thing.

#### Scenario: S
Given A
Then B
"#
        .to_string(),
    }];

    let json = parse_change_show_json("001-01_demo", &files);
    assert_eq!(json.delta_count, 1);
    assert_eq!(json.deltas.len(), 1);
    assert_eq!(json.deltas[0].spec, "auth");
    assert_eq!(json.deltas[0].operation, "ADDED");
    assert!(json.deltas[0].description.contains("Add requirement"));
}

#[test]
fn read_module_markdown_returns_contents_for_existing_module() {
    let td = tempfile::tempdir().unwrap();
    let ito = td.path().join(".ito");
    let module_content = "# My Module\n\n## Purpose\nDoes things.\n";
    write(
        &ito.join("modules").join("006_demo").join("module.md"),
        module_content,
    );

    let module_repo = FsModuleRepository::new(&ito);
    let result = read_module_markdown(&module_repo, "006").expect("should read module.md");
    assert_eq!(result, module_content);
}

#[test]
fn read_module_markdown_returns_empty_for_missing_module_md() {
    let td = tempfile::tempdir().unwrap();
    let ito = td.path().join(".ito");
    // Create the module directory but not the module.md file
    std::fs::create_dir_all(ito.join("modules").join("007_empty")).unwrap();

    let module_repo = FsModuleRepository::new(&ito);
    let result =
        read_module_markdown(&module_repo, "007").expect("should succeed with empty string");
    assert!(
        result.is_empty(),
        "should return empty string for missing module.md, got: {result}"
    );
}

#[test]
fn read_module_markdown_returns_error_for_nonexistent_module() {
    let td = tempfile::tempdir().unwrap();
    let ito = td.path().join(".ito");
    // Don't create any modules directory

    let module_repo = FsModuleRepository::new(&ito);
    let result = read_module_markdown(&module_repo, "999");
    assert!(result.is_err(), "should fail for nonexistent module");
}

#[test]
fn bundle_main_specs_show_json_is_id_sorted_and_contains_absolute_paths() {
    let td = tempfile::tempdir().unwrap();
    let ito = td.path().join(".ito");

    write(
        &ito.join("specs").join("b").join("spec.md"),
        "# B\n\n## Purpose\nB purpose long enough.\n\n## Requirements\n\n### Requirement: B\nThe system SHALL do b.\n\n#### Scenario: B\n- **WHEN** b\n- **THEN** b\n",
    );
    write(
        &ito.join("specs").join("a").join("spec.md"),
        "# A\n\n## Purpose\nA purpose long enough.\n\n## Requirements\n\n### Requirement: A\nThe system SHALL do a.\n\n#### Scenario: A\n- **WHEN** a\n- **THEN** a\n",
    );

    // Add a delta spec elsewhere to ensure it is ignored by bundling.
    write(
        &ito.join("changes")
            .join("000-01_demo")
            .join("specs")
            .join("zzz")
            .join("spec.md"),
        "## ADDED Requirements\n\n### Requirement: Delta\nThe system SHALL not appear.\n\n#### Scenario: Delta\n- **WHEN** bundled\n- **THEN** excluded\n",
    );

    let json = bundle_main_specs_show_json(&ito).unwrap();
    assert_eq!(json.spec_count, 2);
    assert_eq!(json.specs.len(), 2);
    assert_eq!(json.specs[0].id, "a");
    assert_eq!(json.specs[1].id, "b");

    let a_path = ito_common::paths::spec_markdown_path(&ito, "a");
    let b_path = ito_common::paths::spec_markdown_path(&ito, "b");
    assert_eq!(json.specs[0].path, a_path.to_string_lossy().to_string());
    assert_eq!(json.specs[1].path, b_path.to_string_lossy().to_string());
    assert!(json.specs[0].markdown.contains("# A"));
    assert!(json.specs[1].markdown.contains("# B"));
}

#[test]
fn bundle_main_specs_markdown_includes_metadata_comments_and_excludes_deltas() {
    let td = tempfile::tempdir().unwrap();
    let ito = td.path().join(".ito");

    write(
        &ito.join("specs").join("alpha").join("spec.md"),
        "# Alpha\n\n## Purpose\nAlpha purpose long enough.\n\n## Requirements\n\n### Requirement: Alpha\nThe system SHALL alpha.\n\n#### Scenario: Alpha\n- **WHEN** alpha\n- **THEN** alpha\n",
    );
    write(
        &ito.join("specs").join("beta").join("spec.md"),
        "# Beta\n\n## Purpose\nBeta purpose long enough.\n\n## Requirements\n\n### Requirement: Beta\nThe system SHALL beta.\n\n#### Scenario: Beta\n- **WHEN** beta\n- **THEN** beta\n",
    );
    write(
        &ito.join("changes")
            .join("000-01_demo")
            .join("specs")
            .join("delta")
            .join("spec.md"),
        "DELTA MARKDOWN MUST NOT APPEAR\n",
    );

    let md = bundle_main_specs_markdown(&ito).unwrap();
    let alpha_path = ito_common::paths::spec_markdown_path(&ito, "alpha");
    let beta_path = ito_common::paths::spec_markdown_path(&ito, "beta");

    assert!(md.contains(&format!(
        "<!-- spec-id: alpha; source: {} -->",
        alpha_path.to_string_lossy()
    )));
    assert!(md.contains(&format!(
        "<!-- spec-id: beta; source: {} -->",
        beta_path.to_string_lossy()
    )));
    assert!(md.contains("# Alpha"));
    assert!(md.contains("# Beta"));
    assert!(!md.contains("DELTA MARKDOWN MUST NOT APPEAR"));

    let alpha_idx = md.find("<!-- spec-id: alpha").unwrap();
    let beta_idx = md.find("<!-- spec-id: beta").unwrap();
    assert!(alpha_idx < beta_idx);
}
#[test]
fn bundle_main_specs_show_json_returns_not_found_when_no_specs_exist() {
    let td = tempfile::tempdir().unwrap();
    let ito = td.path().join(".ito");
    std::fs::create_dir_all(&ito).unwrap();

    let err = bundle_main_specs_show_json(&ito).expect_err("should fail when no specs exist");
    let CoreError::NotFound(msg) = err else {
        panic!("expected not-found error, got: {err:?}");
    };
    assert!(
        msg.contains("No specs found"),
        "expected not-found message to mention no specs, got: {msg}"
    );
}

/// Verifies that a requirement block's `Requirement ID` bullet is parsed into `requirement_id` and removed from the requirement's stored text.
///
/// This test ensures `parse_spec_show_json` extracts the `- **Requirement ID**: ...` line into `requirement_id` for a requirement and that the collapsed `text` for the requirement does not include the `Requirement ID` line.
///
/// # Examples
///
/// ```
/// let md = r#"
/// ## Requirements
///
/// ### Requirement: The system SHALL authenticate users
/// The system SHALL authenticate users via OAuth2.
/// - **Requirement ID**: REQ-AUTH-001
///
/// #### Scenario: Happy path
/// Given a valid token
/// Then access is granted
/// "#;
///
/// let json = parse_spec_show_json("auth", md);
/// assert_eq!(json.requirements.len(), 1);
/// assert_eq!(json.requirements[0].requirement_id.as_deref(), Some("REQ-AUTH-001"));
/// assert!(!json.requirements[0].text.contains("Requirement ID"));
/// ```
#[test]
fn parse_requirement_block_extracts_requirement_id() {
    let md = r#"
## Requirements

### Requirement: The system SHALL authenticate users
The system SHALL authenticate users via OAuth2.
- **Requirement ID**: REQ-AUTH-001

#### Scenario: Happy path
Given a valid token
Then access is granted
"#;

    let json = parse_spec_show_json("auth", md);
    assert_eq!(json.requirements.len(), 1);
    assert_eq!(
        json.requirements[0].requirement_id.as_deref(),
        Some("REQ-AUTH-001")
    );
    // The ID line must NOT appear in the collapsed text.
    assert!(
        !json.requirements[0].text.contains("Requirement ID"),
        "requirement_id line should be excluded from text, got: {}",
        json.requirements[0].text
    );
}

#[test]
fn parse_requirement_block_requirement_id_absent_gives_none() {
    let md = r#"
## Requirements

### Requirement: The system SHALL do something
The system SHALL do something.

#### Scenario: S
Given A
Then B
"#;

    let json = parse_spec_show_json("spec", md);
    assert_eq!(json.requirements.len(), 1);
    assert!(
        json.requirements[0].requirement_id.is_none(),
        "expected None for requirement_id when not declared"
    );
}

/// Verifies parsing of multiple requirement blocks and extraction of declared requirement IDs.
///
/// Ensures three requirements are parsed from the markdown: the first two include `Requirement ID` lines
/// yielding `REQ-001` and `REQ-002`, and the third has no `Requirement ID`.
///
/// # Examples
///
/// ```
/// let md = r#"
/// ## Requirements
///
/// ### Requirement: First requirement
/// First requirement text.
/// - **Requirement ID**: REQ-001
///
/// ### Requirement: Second requirement
/// Second requirement text.
/// - **Requirement ID**: REQ-002
///
/// ### Requirement: Third requirement without ID
/// Third requirement text.
/// "#;
///
/// let json = parse_spec_show_json("spec", md);
/// assert_eq!(json.requirements.len(), 3);
/// assert_eq!(json.requirements[0].requirement_id.as_deref(), Some("REQ-001"));
/// assert_eq!(json.requirements[1].requirement_id.as_deref(), Some("REQ-002"));
/// assert!(json.requirements[2].requirement_id.is_none());
/// ```
#[test]
fn parse_requirement_block_multiple_requirements_with_ids() {
    let md = r#"
## Requirements

### Requirement: First requirement
First requirement text.
- **Requirement ID**: REQ-001

### Requirement: Second requirement
Second requirement text.
- **Requirement ID**: REQ-002

### Requirement: Third requirement without ID
Third requirement text.
"#;

    let json = parse_spec_show_json("spec", md);
    assert_eq!(json.requirements.len(), 3);
    assert_eq!(
        json.requirements[0].requirement_id.as_deref(),
        Some("REQ-001")
    );
    assert_eq!(
        json.requirements[1].requirement_id.as_deref(),
        Some("REQ-002")
    );
    assert!(json.requirements[2].requirement_id.is_none());
}

#[test]
fn parse_delta_spec_requirement_id_is_extracted() {
    let files = vec![DeltaSpecFile {
        spec: "auth".to_string(),
        markdown: r#"
## ADDED Requirements

### Requirement: The system SHALL support SSO
The system SHALL support SSO.
- **Requirement ID**: REQ-SSO-001

#### Scenario: SSO login
Given an SSO provider
Then the user is authenticated
"#
        .to_string(),
    }];

    let json = parse_change_show_json("001-27_demo", &files);
    assert_eq!(json.deltas.len(), 1);
    assert_eq!(
        json.deltas[0].requirement.requirement_id.as_deref(),
        Some("REQ-SSO-001")
    );
    // ID line must not appear in the requirement text.
    assert!(
        !json.deltas[0].requirement.text.contains("Requirement ID"),
        "requirement_id line should be excluded from text"
    );
}

#[test]
fn parse_requirement_metadata_prefers_first_values_and_accepts_asterisk_bullets() {
    let md = r#"
## Requirements

### Requirement: Preserve metadata
The system SHALL keep the first metadata values.
* **Requirement ID**: REQ-001
- **Requirement ID**: REQ-002
* **Tags**: ui, api
- **Tags**: backend
* **Contract Refs**: openapi:GET /v1/items, jsonschema:ItemsResponse
- **Contract Refs**: asyncapi:item.created
"#;

    let json = parse_spec_show_json("spec", md);
    let requirement = &json.requirements[0];

    assert_eq!(requirement.requirement_id.as_deref(), Some("REQ-001"));
    assert_eq!(requirement.tags, vec!["ui", "api"]);
    assert_eq!(requirement.contract_refs.len(), 2);
    assert_eq!(requirement.contract_refs[0].raw, "openapi:GET /v1/items");
    assert_eq!(requirement.contract_refs[1].raw, "jsonschema:ItemsResponse");
}

#[test]
fn parse_contract_refs_preserves_commas_inside_identifiers() {
    let md = r#"
## Requirements

### Requirement: Preserve contract refs
The system SHALL keep query-string commas inside a single contract ref.
- **Contract Refs**: openapi:POST /v1/items?ids=1,2, jsonschema:ItemRequest
"#;

    let json = parse_spec_show_json("spec", md);
    let requirement = &json.requirements[0];

    assert_eq!(requirement.contract_refs.len(), 2);
    assert_eq!(
        requirement.contract_refs[0].identifier.as_deref(),
        Some("POST /v1/items?ids=1,2")
    );
    assert_eq!(requirement.contract_refs[1].raw, "jsonschema:ItemRequest");
}

/// Ensures bundling main specs fails with an IO error when a spec directory is missing `spec.md`.
///
/// # Examples
///
/// ```
/// let td = tempfile::tempdir().unwrap();
/// let ito = td.path().join(".ito");
/// std::fs::create_dir_all(ito.join("specs").join("orphan")).unwrap();
///
/// let err = bundle_main_specs_show_json(&ito).expect_err("should fail when spec.md missing");
/// match err {
///     CoreError::Io { context, .. } => assert!(context.contains("reading spec orphan")),
///     other => panic!("expected Io error, got: {other:?}"),
/// }
/// ```
#[test]
fn bundle_main_specs_show_json_returns_io_error_when_spec_md_is_missing() {
    let td = tempfile::tempdir().unwrap();
    let ito = td.path().join(".ito");
    std::fs::create_dir_all(ito.join("specs").join("orphan")).unwrap();

    let err = bundle_main_specs_show_json(&ito).expect_err("should fail when spec.md missing");
    let CoreError::Io { context, .. } = err else {
        panic!("expected io error, got: {err:?}");
    };
    assert!(
        context.contains("reading spec orphan"),
        "expected context to mention orphan spec, got: {context}"
    );
}