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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
use std::fs::read_to_string;
use std::io::Write;
use std::process::{Command, Stdio};
use tempfile::TempDir;

use liwe::model::config::{Configuration, NoteTemplate};

fn setup_iwe_project() -> TempDir {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");
    let temp_path = temp_dir.path();

    Command::new(crate::common::get_iwe_binary_path())
        .arg("init")
        .current_dir(temp_path)
        .output()
        .expect("Failed to initialize IWE");

    temp_dir
}

fn add_template(temp_dir: &TempDir, name: &str, template: NoteTemplate) {
    let config_path = temp_dir.path().join(".iwe").join("config.toml");
    let config_content = read_to_string(&config_path).expect("Read config");
    let mut config: Configuration = toml::from_str(&config_content).expect("Parse config");

    config.templates.insert(name.to_string(), template);

    let updated_config = toml::to_string(&config).expect("Serialize config");
    std::fs::write(&config_path, updated_config).expect("Write config");
}

#[test]
fn test_new_creates_file_with_default_template() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("My Test Note")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(output.status.success(), "Command should succeed");

    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    let created_path = stdout.trim();

    assert!(
        std::path::Path::new(created_path).exists(),
        "Created file should exist"
    );

    let content = read_to_string(created_path).expect("Should read file");
    assert!(
        content.contains("# My Test Note"),
        "Should have title as header"
    );
}

#[test]
fn test_new_with_slug_in_filename() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Hello World Test")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    assert!(
        stdout.contains("hello-world-test.md"),
        "Filename should be slugified"
    );
}

#[test]
fn test_new_with_content_argument() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Note With Content")
        .arg("--content")
        .arg("This is the initial content")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(output.status.success(), "Command should succeed");

    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    let created_path = stdout.trim();
    let content = read_to_string(created_path).expect("Should read file");

    assert!(
        content.contains("# Note With Content"),
        "Should have title as header"
    );
    assert!(
        content.contains("This is the initial content"),
        "Should have content"
    );
}

#[test]
fn test_new_with_stdin_content() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let mut child = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Stdin Note")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .current_dir(temp_path)
        .spawn()
        .expect("Failed to spawn command");

    if let Some(mut stdin) = child.stdin.take() {
        stdin
            .write_all(b"This is piped content")
            .expect("Failed to write stdin");
    }

    let output = child.wait_with_output().expect("Failed to wait");
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    let created_path = stdout.trim();
    let content = read_to_string(created_path).expect("Should read file");

    assert!(
        content.contains("This is piped content"),
        "File should contain stdin content"
    );
}

#[test]
fn test_new_default_appends_suffix_for_existing_file() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let first_output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Duplicate Note")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(first_output.status.success());
    let first_path = String::from_utf8(first_output.stdout).expect("Valid UTF-8");
    assert!(
        first_path.contains("duplicate-note.md"),
        "First file should be duplicate-note.md"
    );

    let second_output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Duplicate Note")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(
        second_output.status.success(),
        "Second creation should succeed with suffix"
    );
    let second_path = String::from_utf8(second_output.stdout).expect("Valid UTF-8");
    assert!(
        second_path.contains("duplicate-note-1.md"),
        "Second file should be duplicate-note-1.md, got: {}",
        second_path
    );
}

#[test]
fn test_new_creates_parent_directories() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    add_template(
        &temp_dir,
        "nested",
        NoteTemplate {
            key_template: "notes/{{slug}}".to_string(),
            document_template: "# {{title}}\n".to_string(),
        },
    );

    let output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Nested Note")
        .arg("--template")
        .arg("nested")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(output.status.success(), "Command should succeed");

    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    let created_path = stdout.trim();
    assert!(
        std::path::Path::new(created_path)
            .components()
            .any(|c| c.as_os_str() == "notes"),
        "Should create nested path"
    );

    assert!(
        std::path::Path::new(created_path).exists(),
        "File should exist in nested directory"
    );
}

#[test]
fn test_new_unknown_template_fails() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Test")
        .arg("--template")
        .arg("nonexistent")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(!output.status.success(), "Should fail for unknown template");
}

#[test]
fn test_new_content_argument_takes_precedence_over_stdin() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let mut child = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Priority Test")
        .arg("--content")
        .arg("Content from argument")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .current_dir(temp_path)
        .spawn()
        .expect("Failed to spawn command");

    if let Some(mut stdin) = child.stdin.take() {
        stdin
            .write_all(b"Content from stdin")
            .expect("Failed to write stdin");
    }

    let output = child.wait_with_output().expect("Failed to wait");
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    let created_path = stdout.trim();
    let content = read_to_string(created_path).expect("Should read file");

    assert!(
        content.contains("Content from argument"),
        "Should use content from argument"
    );
    assert!(
        !content.contains("Content from stdin"),
        "Should not use stdin content"
    );
}

#[test]
fn test_new_if_exists_suffix_appends_numbers() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let first_output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Suffix Test")
        .arg("--if-exists")
        .arg("suffix")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(first_output.status.success());
    let first_path = String::from_utf8(first_output.stdout).expect("Valid UTF-8");
    assert!(
        first_path.contains("suffix-test.md"),
        "First file should be suffix-test.md"
    );

    let second_output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Suffix Test")
        .arg("--if-exists")
        .arg("suffix")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(
        second_output.status.success(),
        "Second creation should succeed"
    );
    let second_path = String::from_utf8(second_output.stdout).expect("Valid UTF-8");
    assert!(
        second_path.contains("suffix-test-1.md"),
        "Second file should be suffix-test-1.md, got: {}",
        second_path
    );

    let third_output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Suffix Test")
        .arg("--if-exists")
        .arg("suffix")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(
        third_output.status.success(),
        "Third creation should succeed"
    );
    let third_path = String::from_utf8(third_output.stdout).expect("Valid UTF-8");
    assert!(
        third_path.contains("suffix-test-2.md"),
        "Third file should be suffix-test-2.md, got: {}",
        third_path
    );
}

#[test]
fn test_new_if_exists_override_replaces_file() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let first_output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Override Test")
        .arg("--content")
        .arg("First content")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(first_output.status.success());
    let first_path = String::from_utf8(first_output.stdout).expect("Valid UTF-8");
    let first_path = first_path.trim();
    let first_content = read_to_string(first_path).expect("Read first file");
    assert!(
        first_content.contains("First content"),
        "First file should have first content"
    );

    let second_output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Override Test")
        .arg("--content")
        .arg("Second content")
        .arg("--if-exists")
        .arg("override")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(second_output.status.success(), "Override should succeed");
    let second_path = String::from_utf8(second_output.stdout).expect("Valid UTF-8");
    assert!(
        second_path.contains("override-test.md"),
        "Should use same filename"
    );

    let overwritten_content = read_to_string(first_path).expect("Read overwritten file");
    assert!(
        overwritten_content.contains("Second content"),
        "File should have second content after override"
    );
    assert!(
        !overwritten_content.contains("First content"),
        "File should not have first content after override"
    );
}

#[test]
fn test_new_if_exists_skip_does_nothing() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let first_output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Skip Test")
        .arg("--content")
        .arg("Original content")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(first_output.status.success());
    let first_path = String::from_utf8(first_output.stdout).expect("Valid UTF-8");
    let first_path = first_path.trim();

    let second_output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Skip Test")
        .arg("--content")
        .arg("New content")
        .arg("--if-exists")
        .arg("skip")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(second_output.status.success(), "Skip should succeed");
    let second_stdout = String::from_utf8(second_output.stdout).expect("Valid UTF-8");
    assert!(
        second_stdout.trim().is_empty(),
        "Skip should print nothing, got: {}",
        second_stdout
    );

    let content = read_to_string(first_path).expect("Read file");
    assert!(
        content.contains("Original content"),
        "File should still have original content"
    );
    assert!(
        !content.contains("New content"),
        "File should not have new content"
    );
}

#[test]
fn test_new_with_german_locale_formats_date() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let config_path = temp_dir.path().join(".iwe").join("config.toml");
    let config_content = read_to_string(&config_path).expect("Read config");
    let mut config: Configuration = toml::from_str(&config_content).expect("Parse config");
    config.library.locale = Some("de_DE".to_string());
    config.markdown.locale = Some("de_DE".to_string());
    config.markdown.date_format = Some("%A, %d. %B %Y".to_string());
    let updated_config = toml::to_string(&config).expect("Serialize config");
    std::fs::write(&config_path, updated_config).expect("Write config");

    add_template(
        &temp_dir,
        "today",
        NoteTemplate {
            key_template: "journal/{{today}}".to_string(),
            document_template: "# {{today}}\n\n{{content}}".to_string(),
        },
    );

    let output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("Test")
        .arg("--template")
        .arg("today")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(output.status.success(), "Command should succeed");

    let stdout = String::from_utf8(output.stdout).expect("Valid UTF-8");
    let created_path = stdout.trim();
    let content = read_to_string(created_path).expect("Should read file");

    let german_days = [
        "Montag",
        "Dienstag",
        "Mittwoch",
        "Donnerstag",
        "Freitag",
        "Samstag",
        "Sonntag",
    ];
    let has_german_day = german_days.iter().any(|day| content.contains(day));
    assert!(
        has_german_day,
        "Content should contain a German day name. Got: {}",
        content
    );
}

#[test]
fn test_new_long_title_error() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let long_title = "a".repeat(255);

    let output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg(&long_title)
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(
        !output.status.success(),
        "Should fail for excessively long title"
    );

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.contains("too long"),
        "Should report filename too long: {}",
        stderr
    );
}

#[test]
fn test_new_empty_title_error() {
    let temp_dir = setup_iwe_project();
    let temp_path = temp_dir.path();

    let output = Command::new(crate::common::get_iwe_binary_path())
        .arg("new")
        .arg("")
        .current_dir(temp_path)
        .output()
        .expect("Failed to execute iwe new");

    assert!(!output.status.success(), "Should fail for empty title");

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.contains("empty"),
        "Should report empty key: {}",
        stderr
    );
}