oxidize-pdf-cli 1.1.6

Command-line interface for oxidizePdf
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
//! Integration tests for the oxidize-pdf CLI
//!
//! Tests command-line interface functionality including:
//! - Command parsing and validation
//! - PDF creation and manipulation
//! - Error handling and edge cases
//! - File I/O operations

use anyhow::Result;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::{tempdir, TempDir};

/// Test helper to get the CLI binary path
fn get_cli_path() -> PathBuf {
    let mut path = std::env::current_exe().unwrap();
    path.pop(); // Remove test binary name
    if path.ends_with("deps") {
        path.pop(); // Remove "deps" directory
    }
    path.push("oxidizepdf");
    #[cfg(windows)]
    path.set_extension("exe");
    path
}

/// Test helper to create a temporary directory
fn setup_temp_dir() -> TempDir {
    tempdir().expect("Failed to create temp directory")
}

/// Test helper to run CLI command and return output
fn run_cli_command(args: &[&str]) -> Result<std::process::Output> {
    let output = Command::new(get_cli_path()).args(args).output()?;
    Ok(output)
}

/// Test helper to check if PDF file exists and has content
fn assert_pdf_exists_and_valid(path: &Path) {
    assert!(path.exists(), "PDF file should exist: {}", path.display());
    let metadata = fs::metadata(path).expect("Failed to read file metadata");
    assert!(
        metadata.len() > 100,
        "PDF file should have content (> 100 bytes)"
    );

    // Basic PDF header check
    let content = fs::read(path).expect("Failed to read PDF file");
    assert!(
        content.starts_with(b"%PDF-"),
        "File should start with PDF header"
    );
}

#[test]
fn test_cli_create_command() {
    let temp_dir = setup_temp_dir();
    let output_path = temp_dir.path().join("test_create.pdf");

    let output = run_cli_command(&[
        "create",
        "-o",
        output_path.to_str().unwrap(),
        "-t",
        "Hello, World!",
    ])
    .expect("CLI command should succeed");

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

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("PDF created successfully"),
        "Should show success message"
    );
}

#[test]
fn test_cli_create_command_with_multiline_text() {
    let temp_dir = setup_temp_dir();
    let output_path = temp_dir.path().join("test_multiline.pdf");

    let output = run_cli_command(&[
        "create",
        "-o",
        output_path.to_str().unwrap(),
        "-t",
        "Line 1\nLine 2\nLine 3",
    ])
    .expect("CLI command should succeed");

    assert!(output.status.success(), "Command should succeed");
    assert_pdf_exists_and_valid(&output_path);
}

#[test]
fn test_cli_demo_command() {
    let temp_dir = setup_temp_dir();
    let output_path = temp_dir.path().join("test_demo.pdf");

    let output = run_cli_command(&["demo", "-o", output_path.to_str().unwrap()])
        .expect("CLI command should succeed");

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

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("Demo PDF created successfully"),
        "Should show success message"
    );
}

#[test]
fn test_cli_demo_command_default_output() {
    let temp_dir = setup_temp_dir();
    let current_dir = std::env::current_dir().unwrap();

    // Change to temp directory to avoid polluting the workspace
    std::env::set_current_dir(&temp_dir).unwrap();

    let output = run_cli_command(&["demo"]).expect("CLI command should succeed");

    // Restore original directory
    std::env::set_current_dir(current_dir).unwrap();

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

    let default_path = temp_dir.path().join("demo.pdf");
    assert_pdf_exists_and_valid(&default_path);
}

#[test]
fn test_cli_merge_command_not_implemented() {
    let temp_dir = setup_temp_dir();
    let output_path = temp_dir.path().join("merged.pdf");
    let input1 = temp_dir.path().join("input1.pdf");
    let input2 = temp_dir.path().join("input2.pdf");

    let output = run_cli_command(&[
        "merge",
        input1.to_str().unwrap(),
        input2.to_str().unwrap(),
        "-o",
        output_path.to_str().unwrap(),
    ])
    .expect("CLI command should run");

    // Command should succeed but print not implemented message
    assert!(output.status.success(), "Command should succeed");

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("PDF merge functionality coming"),
        "Should show not implemented message"
    );
}

#[test]
fn test_cli_split_command_not_implemented() {
    let temp_dir = setup_temp_dir();
    let input_path = temp_dir.path().join("input.pdf");

    let output =
        run_cli_command(&["split", input_path.to_str().unwrap()]).expect("CLI command should run");

    // Command should succeed but print not implemented message
    assert!(output.status.success(), "Command should succeed");

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("PDF split functionality coming"),
        "Should show not implemented message"
    );
}

#[test]
fn test_cli_info_command_with_nonexistent_file() {
    let temp_dir = setup_temp_dir();
    let nonexistent_path = temp_dir.path().join("nonexistent.pdf");

    let output = run_cli_command(&["info", nonexistent_path.to_str().unwrap()])
        .expect("CLI command should run");

    assert!(
        !output.status.success(),
        "Command should fail for nonexistent file"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("Error"), "Should show error message");
}

#[test]
fn test_cli_info_command_with_created_pdf() {
    let temp_dir = setup_temp_dir();
    let pdf_path = temp_dir.path().join("test_info.pdf");

    // First create a PDF
    let create_output = run_cli_command(&[
        "create",
        "-o",
        pdf_path.to_str().unwrap(),
        "-t",
        "Test PDF for info command",
    ])
    .expect("Create command should succeed");
    assert!(create_output.status.success());

    // Then get info about it
    let info_output =
        run_cli_command(&["info", pdf_path.to_str().unwrap()]).expect("Info command should run");

    if info_output.status.success() {
        let stdout = String::from_utf8_lossy(&info_output.stdout);
        assert!(
            stdout.contains("PDF Information"),
            "Should show PDF information"
        );
        assert!(stdout.contains("PDF Version"), "Should show PDF version");
    } else {
        // Info command might fail if parser is not fully implemented
        let stderr = String::from_utf8_lossy(&info_output.stderr);
        assert!(
            stderr.contains("PDF parser is currently in early development"),
            "Should explain parser limitations"
        );
    }
}

#[test]
fn test_cli_info_command_detailed() {
    let temp_dir = setup_temp_dir();
    let pdf_path = temp_dir.path().join("test_detailed.pdf");

    // Create a demo PDF (more complex than simple create)
    let create_output = run_cli_command(&["demo", "-o", pdf_path.to_str().unwrap()])
        .expect("Demo command should succeed");
    assert!(create_output.status.success());

    // Get detailed info
    let info_output = run_cli_command(&["info", pdf_path.to_str().unwrap(), "--detailed"])
        .expect("Info command should run");

    if info_output.status.success() {
        let stdout = String::from_utf8_lossy(&info_output.stdout);
        assert!(
            stdout.contains("Detailed Information"),
            "Should show detailed section"
        );
    }
    // If it fails, it's due to parser limitations (acceptable for now)
}

#[test]
fn test_cli_rotate_command_invalid_angle() {
    let temp_dir = setup_temp_dir();
    let input_path = temp_dir.path().join("input.pdf");
    let output_path = temp_dir.path().join("rotated.pdf");

    let output = run_cli_command(&[
        "rotate",
        input_path.to_str().unwrap(),
        "-o",
        output_path.to_str().unwrap(),
        "-a",
        "45", // Invalid angle
    ])
    .expect("CLI command should run");

    assert!(
        !output.status.success(),
        "Command should fail for invalid angle"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Valid angles are 90, 180, 270"),
        "Should show valid angles"
    );
}

#[test]
fn test_cli_rotate_command_invalid_page_range() {
    let temp_dir = setup_temp_dir();
    let input_path = temp_dir.path().join("input.pdf");
    let output_path = temp_dir.path().join("rotated.pdf");

    let output = run_cli_command(&[
        "rotate",
        input_path.to_str().unwrap(),
        "-o",
        output_path.to_str().unwrap(),
        "-p",
        "invalid-range",
    ])
    .expect("CLI command should run");

    assert!(
        !output.status.success(),
        "Command should fail for invalid page range"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Error parsing page range"),
        "Should show page range error"
    );
}

#[test]
fn test_cli_extract_text_nonexistent_file() {
    let temp_dir = setup_temp_dir();
    let nonexistent_path = temp_dir.path().join("nonexistent.pdf");

    let output = run_cli_command(&["extract-text", nonexistent_path.to_str().unwrap()])
        .expect("CLI command should run");

    assert!(
        !output.status.success(),
        "Command should fail for nonexistent file"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Failed to open PDF"),
        "Should show file error"
    );
}

#[test]
fn test_cli_extract_text_with_created_pdf() {
    let temp_dir = setup_temp_dir();
    let pdf_path = temp_dir.path().join("test_extract.pdf");
    let text_content = "This is test content for text extraction";

    // Create a PDF with known text
    let create_output = run_cli_command(&[
        "create",
        "-o",
        pdf_path.to_str().unwrap(),
        "-t",
        text_content,
    ])
    .expect("Create command should succeed");
    assert!(create_output.status.success());

    // Extract text from it
    let extract_output = run_cli_command(&["extract-text", pdf_path.to_str().unwrap()])
        .expect("Extract command should run");

    if extract_output.status.success() {
        let stdout = String::from_utf8_lossy(&extract_output.stdout);
        // Text extraction might not be perfect, but should contain some content
        assert!(!stdout.trim().is_empty(), "Should extract some text");
    }
    // If it fails, it's due to text extraction limitations (acceptable for now)
}

#[test]
fn test_cli_extract_text_with_output_file() {
    let temp_dir = setup_temp_dir();
    let pdf_path = temp_dir.path().join("test_extract_file.pdf");
    let output_path = temp_dir.path().join("extracted.txt");
    let text_content = "Content for file extraction test";

    // Create a PDF
    let create_output = run_cli_command(&[
        "create",
        "-o",
        pdf_path.to_str().unwrap(),
        "-t",
        text_content,
    ])
    .expect("Create command should succeed");
    assert!(create_output.status.success());

    // Extract text to file
    let extract_output = run_cli_command(&[
        "extract-text",
        pdf_path.to_str().unwrap(),
        "-o",
        output_path.to_str().unwrap(),
    ])
    .expect("Extract command should run");

    if extract_output.status.success() {
        assert!(output_path.exists(), "Output text file should be created");
        let stdout = String::from_utf8_lossy(&extract_output.stdout);
        assert!(
            stdout.contains("Text extracted to"),
            "Should show success message"
        );
    }
    // If it fails, it's due to text extraction limitations (acceptable for now)
}

#[test]
fn test_cli_help_command() {
    let output = run_cli_command(&["--help"]).expect("Help command should work");

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

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("oxidizepdf"), "Should show program name");
    assert!(
        stdout.contains("Commands"),
        "Should show available commands"
    );
    assert!(stdout.contains("create"), "Should list create command");
    assert!(stdout.contains("demo"), "Should list demo command");
    assert!(stdout.contains("info"), "Should list info command");
}

#[test]
fn test_cli_version_command() {
    let output = run_cli_command(&["--version"]).expect("Version command should work");

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

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("oxidizepdf"), "Should show program name");
    assert!(stdout.contains("1.1"), "Should show version number");
}

#[test]
fn test_cli_invalid_command() {
    let output = run_cli_command(&["invalid-command"]).expect("Command should run");

    assert!(!output.status.success(), "Invalid command should fail");

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("error") || stderr.contains("unrecognized"),
        "Should show error for invalid command"
    );
}

#[test]
fn test_cli_missing_required_arguments() {
    // Test create command without required arguments
    let output = run_cli_command(&["create"]).expect("Command should run");

    assert!(
        !output.status.success(),
        "Command should fail without required args"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("required") || stderr.contains("missing"),
        "Should show missing argument error"
    );
}