#
# mupdf-portfolio.test --
#

package require TTXN
  # package under test is tclMuPdf 2.0
package require tclMuPDF 2.0

set thisDir [file dirname [file normalize [info script]]]
set samplesDir [file join $thisDir samples]
set TmpResultDir [file join $thisDir tmpResults]
file mkdir $TmpResultDir


Name: portfolio_list_1
Description: {list items in a portfolio}
Setup: { 
    set pdf [mupdf::open [file join $samplesDir Sample-Invoice.pdf]] 
}
Test: {
    $pdf portfolio list
}
Cleanup: {
    $pdf quit
}
Expected: {ZUGFeRD-invoice.xml}

Name: portfolio_list_2
Description: {list 3 items in a portfolio}
Setup: { 
    set pdf [mupdf::open [file join $samplesDir Sample-3attachments.pdf]] 
}
Test: {
    $pdf portfolio list
}
Cleanup: {
    $pdf quit
}
Expected: {hello.txt colors..xlsx Acacia#202288359796.jpg}

Name: portfolio_list_empty
Description: {list items in an empty portfolio}
Setup: { 
    set pdf [mupdf::open [file join $samplesDir Sample-01.pdf]] 
}
Test: {
    $pdf portfolio list
}
Cleanup: {
    $pdf quit
}
Expected: {}

Name: portfolio_extract
Description: {extract first item from a a portfolio}
Setup: { 
    set pdf [mupdf::open [file join $samplesDir Sample-3attachments.pdf]] 
}
Test: {
    set L [$pdf portfolio list]
    set filename [lindex $L 0]
    file delete -force [file join $TmpResultDir $filename]

    $pdf portfolio extract 0 -dir $TmpResultDir
    file exists [file join $TmpResultDir $filename]
}
Cleanup: {
    $pdf quit
}
Expected: 1

Name: portfolio_extract
Description: {extract last item from a portfolio, with a different name}
Setup: { 
    set pdf [mupdf::open [file join $samplesDir Sample-3attachments.pdf]] 
}
Test: {
    set L [$pdf portfolio list]
    set filename "copy of [lindex $L 0]"
    file delete -force [file join $TmpResultDir $filename]

    $pdf portfolio extract 0 -dir $TmpResultDir -as $filename
    file exists [file join $TmpResultDir $filename]
}
Cleanup: {
    $pdf quit
}
Expected: 1


# ---- negative tests --------------------------------------------------------

Name: portfolio_list.NEG_1
Description: {list items in a portfolio -- wrong options}
Setup: { 
    set pdf [mupdf::open [file join $samplesDir Sample-Invoice.pdf]] 
}
Test: {
    $pdf portfolio list noise
}
Cleanup: {
    $pdf quit
}
Expected: error glob "wrong # args*"


Name: portfolio_extract.NEG1
Description: {extract item from an empty portfolio}
Setup: { 
    set pdf [mupdf::open [file join $samplesDir Sample-01.pdf]] 
}
Test: {
    $pdf portfolio extract 0 -dir $TmpResultDir
}
Cleanup: {
    $pdf quit
}
Expected: error glob "Index out of bounds."

Name: portfolio_extract.NEG2
Description: {extract item from a portfolio, bad filename}
Setup: { 
    set pdf [mupdf::open [file join $samplesDir Sample-Invoice.pdf]] 
}
Test: {
    $pdf portfolio extract 0 -dir "non-existing-dir"
}
Cleanup: {
    $pdf quit
}
Expected: error glob "failed *"

Name: portfolio_extract.NEG3
Description: {extract item from a empty portfolio, bad index}
Setup: { 
    set pdf [mupdf::open [file join $samplesDir Sample-01.pdf]] 
}
Test: {
    $pdf portfolio extract xxx -dir "non-existing-dir"
}
Cleanup: {
    $pdf quit
}
Expected: error glob "expected integer *"

# === cleanup ========================================
::tcltest::cleanupTests
