hygg 0.1.20

Simplifying the way you read
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
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

fn strip_ansi_escapes(input: &str) -> String {
  let mut output = String::with_capacity(input.len());
  let mut chars = input.chars().peekable();
  while let Some(ch) = chars.next() {
    if ch == '\x1b' && chars.peek() == Some(&'[') {
      chars.next();
      for c in chars.by_ref() {
        if c.is_ascii_alphabetic() {
          break;
        }
      }
      continue;
    }
    output.push(ch);
  }
  output
}

fn hygg_command(test_name: &str) -> Command {
  let config_home = std::env::temp_dir()
    .join(format!("hygg-config-{}-{test_name}", std::process::id()));
  let mut command = Command::new(env!("CARGO_BIN_EXE_hygg"));
  command.env("XDG_CONFIG_HOME", config_home);
  command
}

#[cfg(feature = "pdf-ocr-bundled")]
fn write_native_text_pdf(path: &Path, text: &str) {
  let stream = format!("BT\n/F1 18 Tf\n40 90 Td\n({text}) Tj\nET\n");
  let objects = [
    "<< /Type /Catalog /Pages 2 0 R >>".to_string(),
    "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(),
    "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 144] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>".to_string(),
    "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".to_string(),
    format!("<< /Length {} >>\nstream\n{stream}endstream", stream.len()),
  ];

  let mut pdf = String::from("%PDF-1.4\n");
  let mut offsets = vec![0usize];
  for (index, object) in objects.iter().enumerate() {
    offsets.push(pdf.len());
    pdf.push_str(&format!("{} 0 obj\n{object}\nendobj\n", index + 1));
  }

  let xref_offset = pdf.len();
  pdf.push_str("xref\n");
  pdf.push_str(&format!("0 {}\n", objects.len() + 1));
  pdf.push_str("0000000000 65535 f \n");
  for offset in offsets.iter().skip(1) {
    pdf.push_str(&format!("{offset:010} 00000 n \n"));
  }
  pdf.push_str(&format!(
    "trailer\n<< /Root 1 0 R /Size {} >>\nstartxref\n{xref_offset}\n%%EOF\n",
    objects.len() + 1
  ));

  fs::write(path, pdf).expect("failed to write test PDF");
}

#[cfg(feature = "pdf-ocr-bundled")]
fn prepend_fake_ocrmypdf_to_path(dir: &Path) -> PathBuf {
  let fake_bin = dir.join("bin");
  fs::create_dir(&fake_bin).expect("failed to create fake bin directory");
  let fake_ocrmypdf = fake_bin.join("ocrmypdf");
  let marker = dir.join("ocrmypdf-was-invoked");
  fs::write(
    &fake_ocrmypdf,
    format!("#!/bin/sh\ntouch '{}'\nexit 42\n", marker.display()),
  )
  .expect("failed to write fake ocrmypdf");

  #[cfg(unix)]
  {
    use std::os::unix::fs::PermissionsExt;
    fs::set_permissions(&fake_ocrmypdf, fs::Permissions::from_mode(0o755))
      .expect("failed to make fake ocrmypdf executable");
  }

  fake_bin
}

#[test]
fn test_pdf_processing() {
  let test_file = Path::new(env!("CARGO_MANIFEST_DIR"))
    .parent()
    .unwrap()
    .join("test-data/pdf/pdfreference1.7old-1-50.pdf");

  if !test_file.exists() {
    eprintln!("PDF test file not found, skipping test");
    return;
  }

  let output = hygg_command("test_pdf_processing")
    .arg("--col")
    .arg("80")
    .arg(test_file.to_str().unwrap())
    .output()
    .expect("Failed to execute hygg");

  assert!(output.status.success(), "hygg should exit successfully");

  let stdout = String::from_utf8_lossy(&output.stdout);
  assert!(stdout.contains("PDF"), "Output should contain PDF content");
}

#[test]
fn test_redirected_pdf_output_includes_inline_ansi_images() {
  let test_file = Path::new(env!("CARGO_MANIFEST_DIR"))
    .parent()
    .unwrap()
    .join("test-data/pdf/progit-1-50.pdf");

  if !test_file.exists() {
    eprintln!("PDF image test file not found, skipping test");
    return;
  }

  let output =
    hygg_command("test_redirected_pdf_output_includes_inline_ansi_images")
      .arg("--col")
      .arg("80")
      .arg(test_file.to_str().unwrap())
      .output()
      .expect("Failed to execute hygg");

  assert!(output.status.success(), "hygg should exit successfully");

  let stdout = String::from_utf8_lossy(&output.stdout);
  assert!(
    stdout.contains("\x1b[38;2"),
    "redirected PDF output should include truecolor ANSI image art"
  );
  assert!(
    stdout.contains('\u{2580}'),
    "redirected PDF output should include half-block image art"
  );
  assert!(
    stdout.contains("Pro Git"),
    "redirected PDF output should preserve extracted text"
  );
  assert!(
    stdout.contains("Figure 1. Local version control diagram"),
    "redirected PDF output should include early Pro Git figure labels"
  );
  for needle in ["$ git status", "$ git config", "Changes to be committed"] {
    assert!(
      !stdout.lines().any(|line| {
        let stripped = strip_ansi_escapes(line);
        stripped.contains('\u{2580}') && stripped.contains(needle)
      }),
      "code/preformatted text should remain plaintext, not ANSI art: {needle}"
    );
  }
  for line in stdout.lines() {
    let visible = strip_ansi_escapes(line).chars().count();
    assert!(
      visible <= 80,
      "redirected PDF line should fit --col=80, got {visible}: {line:?}"
    );
  }
}

#[test]
#[cfg(not(feature = "pdf-ocr-bundled"))]
fn test_ocr_without_bundled_feature_gives_clear_error() {
  let test_file = Path::new(env!("CARGO_MANIFEST_DIR"))
    .parent()
    .unwrap()
    .join("test-data/pdf/ocr-0.pdf");

  if !test_file.exists() {
    eprintln!("OCR PDF test file not found, skipping test");
    return;
  }

  let output =
    hygg_command("test_ocr_without_bundled_feature_gives_clear_error")
      .arg("--ocr=on")
      .arg(test_file.to_str().unwrap())
      .output()
      .expect("Failed to execute hygg");

  assert!(
    !output.status.success(),
    "hygg --ocr=on should fail without bundled OCR"
  );

  let stderr = String::from_utf8_lossy(&output.stderr);
  assert!(
    stderr.contains("--features pdf-ocr-bundled"),
    "expected feature guidance in stderr, got: {stderr}"
  );
}

#[test]
#[cfg(feature = "pdf-ocr-bundled")]
fn test_ocr_with_bundled_feature_does_not_invoke_ocrmypdf() {
  let temp_dir = std::env::temp_dir().join(format!(
    "hygg-bundled-ocr-{}-{}",
    std::process::id(),
    std::thread::current().name().unwrap_or("test")
  ));
  let _ = fs::remove_dir_all(&temp_dir);
  fs::create_dir(&temp_dir).expect("failed to create test temp directory");

  let pdf_path = temp_dir.join("native-text.pdf");
  write_native_text_pdf(&pdf_path, "Bundled OCR smoke text");
  let fake_bin = prepend_fake_ocrmypdf_to_path(&temp_dir);
  let marker = temp_dir.join("ocrmypdf-was-invoked");

  let old_path = std::env::var_os("PATH").unwrap_or_default();
  let mut paths = vec![fake_bin];
  paths.extend(std::env::split_paths(&old_path));
  let path =
    std::env::join_paths(paths).expect("failed to construct test PATH");

  let output =
    hygg_command("test_ocr_with_bundled_feature_does_not_invoke_ocrmypdf")
      .arg("--ocr=on")
      .arg(pdf_path.to_str().unwrap())
      .env("PATH", path)
      .output()
      .expect("Failed to execute hygg");

  let stdout = String::from_utf8_lossy(&output.stdout);
  let stderr = String::from_utf8_lossy(&output.stderr);
  let ocrmypdf_was_invoked = marker.exists();
  let _ = fs::remove_dir_all(&temp_dir);

  assert!(
    output.status.success(),
    "hygg --ocr=on should succeed with bundled OCR; stdout: {stdout}; stderr: {stderr}"
  );
  assert!(
    stdout.contains("Bundled OCR smoke text"),
    "expected bundled OCR path to preserve native text, got: {stdout}"
  );
  assert!(
    !ocrmypdf_was_invoked,
    "hygg --ocr invoked ocrmypdf even though bundled OCR was enabled"
  );
}

#[test]
fn test_epub_processing() {
  let test_file = Path::new(env!("CARGO_MANIFEST_DIR"))
    .parent()
    .unwrap()
    .join("test-data/epub/test-standard.epub");

  if !test_file.exists() {
    eprintln!("EPUB test file not found, skipping test");
    return;
  }

  // Test using cli-epub-to-text directly to verify EPUB extraction works
  let epub_bin = Path::new(env!("CARGO_MANIFEST_DIR"))
    .parent()
    .unwrap()
    .join("target")
    .join("debug")
    .join("cli-epub-to-text");

  let epub_output = Command::new(&epub_bin)
    .arg(test_file.to_str().unwrap())
    .output()
    .expect("Failed to execute cli-epub-to-text");

  assert!(epub_output.status.success(), "cli-epub-to-text should succeed");

  let stdout = String::from_utf8_lossy(&epub_output.stdout);
  assert!(
    stdout.contains("Hygg Test EPUB"),
    "Output should contain EPUB title"
  );
  assert!(stdout.contains("Chapter"), "Output should contain chapter content");

  // For the full hygg test, we'll spawn it and kill it after a short time
  // This verifies it doesn't panic on startup
  let mut child = hygg_command("test_epub_processing")
    .arg("--col")
    .arg("80")
    .arg(test_file.to_str().unwrap())
    .stdout(Stdio::null())
    .stderr(Stdio::piped())
    .spawn()
    .expect("Failed to spawn hygg");

  // Give it time to start and potentially panic
  std::thread::sleep(std::time::Duration::from_millis(500));

  // Kill the process
  let _ = child.kill();

  // Check if it panicked (would have exited with error)
  match child.try_wait() {
    Ok(Some(status)) if !status.success() => {
      let stderr = child.wait_with_output().unwrap().stderr;
      let stderr_str = String::from_utf8_lossy(&stderr);
      if stderr_str.contains("panic") {
        panic!("hygg panicked: {}", stderr_str);
      }
    }
    _ => {
      // Still running, that's good - no panic
    }
  }
}

#[test]
fn test_odt_processing_with_pandoc() {
  // Check if pandoc is available
  let pandoc_check = Command::new("pandoc").arg("--version").output();

  if pandoc_check.is_err() || !pandoc_check.unwrap().status.success() {
    eprintln!("pandoc not installed, skipping ODT test");
    return;
  }

  let test_file = Path::new(env!("CARGO_MANIFEST_DIR"))
    .parent()
    .unwrap()
    .join("test-data/odf/test.odt");

  if !test_file.exists() {
    eprintln!("ODT test file not found, skipping test");
    return;
  }

  let output = hygg_command("test_odt_processing_with_pandoc")
    .arg("--col")
    .arg("80")
    .arg(test_file.to_str().unwrap())
    .output()
    .expect("Failed to execute hygg");

  assert!(
    output.status.success(),
    "hygg should process ODT successfully with pandoc"
  );
}

#[test]
fn test_docx_processing_with_pandoc() {
  // Check if pandoc is available
  let pandoc_check = Command::new("pandoc").arg("--version").output();

  if pandoc_check.is_err() || !pandoc_check.unwrap().status.success() {
    eprintln!("pandoc not installed, skipping DOCX test");
    return;
  }

  let test_file = Path::new(env!("CARGO_MANIFEST_DIR"))
    .parent()
    .unwrap()
    .join("test-data/docx/test-standard.docx");

  if !test_file.exists() {
    eprintln!("DOCX test file not found, skipping test");
    return;
  }

  // Test pandoc conversion directly
  let pandoc_output = Command::new("pandoc")
    .arg(test_file.to_str().unwrap())
    .arg("-t")
    .arg("plain")
    .output()
    .expect("Failed to execute pandoc");

  assert!(
    pandoc_output.status.success(),
    "pandoc should convert DOCX successfully"
  );

  let text = String::from_utf8_lossy(&pandoc_output.stdout);
  assert!(text.contains("Hygg Test DOCX"), "Should contain document title");
  assert!(text.contains("Unicode"), "Should contain Unicode section");

  // Test full hygg processing (spawn and kill due to TUI)
  let mut child = hygg_command("test_docx_processing_with_pandoc")
    .arg("--col")
    .arg("80")
    .arg(test_file.to_str().unwrap())
    .stdout(Stdio::null())
    .stderr(Stdio::piped())
    .spawn()
    .expect("Failed to spawn hygg");

  std::thread::sleep(std::time::Duration::from_millis(500));
  let _ = child.kill();

  // Check if it panicked
  match child.try_wait() {
    Ok(Some(status)) if !status.success() => {
      let stderr = child.wait_with_output().unwrap().stderr;
      let stderr_str = String::from_utf8_lossy(&stderr);
      if stderr_str.contains("panic") {
        panic!("hygg panicked on DOCX: {}", stderr_str);
      }
    }
    _ => {
      // Still running, no panic
    }
  }
}

#[test]
fn test_txt_processing() {
  use std::process::Stdio;
  use std::time::{Duration, Instant};

  let test_file = Path::new(env!("CARGO_MANIFEST_DIR"))
    .parent()
    .unwrap()
    .join("test-data/sample.txt");

  if !test_file.exists() {
    eprintln!("TXT test file not found at: {:?}, skipping test", test_file);
    return;
  }

  // Spawn the process with piped stdout/stderr to ensure non-interactive mode
  let mut child = hygg_command("test_txt_processing")
    .arg("--col")
    .arg("80")
    .arg(test_file.to_str().unwrap())
    .stdin(Stdio::null())
    .stdout(Stdio::null())
    .stderr(Stdio::null())
    .spawn()
    .expect("Failed to spawn hygg");

  // Poll for exit. macOS adds substantial overhead when cargo test directly
  // spawns a recently-built debug binary, so the deadline has to comfortably
  // exceed that pre-main load time, not the 100 ms it used to be.
  let deadline = Instant::now() + Duration::from_secs(5);
  loop {
    match child.try_wait() {
      Ok(Some(status)) => {
        assert!(status.code().is_some(), "hygg should exit cleanly");
        return;
      }
      Ok(None) => {
        if Instant::now() >= deadline {
          let _ = child.kill();
          panic!("hygg should have exited when stdout is not a TTY");
        }
        std::thread::sleep(Duration::from_millis(20));
      }
      Err(e) => panic!("Failed to check hygg status: {e}"),
    }
  }
}

#[test]
fn test_file_type_detection() {
  // Test that file extensions are properly detected
  let test_cases = vec![
    ("test.epub", "EPUB"),
    ("test.pdf", "PDF"),
    ("test.txt", "TXT"),
    ("test.odt", "ODT"),
    ("test.docx", "DOCX"),
  ];

  for (filename, _expected_type) in test_cases {
    println!("Testing file type detection for: {}", filename);
    // This is a placeholder - actual implementation would need to
    // expose the file type detection logic or test it indirectly
  }
}

#[test]
fn test_stdin_processing() {
  use std::io::Write;
  use std::process::Stdio;
  use std::time::{Duration, Instant};

  // Test that hygg can accept stdin input
  let mut child = hygg_command("test_stdin_processing")
    .arg("--col")
    .arg("40")
    .stdin(Stdio::piped())
    .stdout(Stdio::null())
    .stderr(Stdio::null())
    .spawn()
    .expect("Failed to spawn hygg");

  let mut stdin = child.stdin.take().expect("Failed to get stdin");
  stdin
    .write_all(
      b"This is a test of stdin processing.\nIt should be properly justified.\n",
    )
    .expect("Failed to write to stdin");
  stdin.flush().expect("Failed to flush stdin");
  // Properly drop the owned stdin to close it
  drop(stdin);

  // Poll for exit. macOS adds substantial overhead when cargo test directly
  // spawns a recently-built debug binary, so the deadline has to comfortably
  // exceed that pre-main load time, not the 100 ms it used to be.
  let deadline = Instant::now() + Duration::from_secs(5);
  loop {
    match child.try_wait() {
      Ok(Some(status)) => {
        assert!(status.code().is_some(), "hygg should exit cleanly");
        return;
      }
      Ok(None) => {
        if Instant::now() >= deadline {
          let _ = child.kill();
          panic!("hygg should have exited when stdout is not a TTY");
        }
        std::thread::sleep(Duration::from_millis(20));
      }
      Err(e) => panic!("Failed to check hygg status: {e}"),
    }
  }
}