# basic.test --
#
#   Tests for paragraph, append, br, tab, and basic run formatting.

package require tcltest 2.5
namespace import ::tcltest::*
package require ooxml::docx

# ──────────────────────────────────────────────────────────────────
#  Paragraph creation
# ──────────────────────────────────────────────────────────────────

test basic-1.1 {single paragraph} -body {
    withDoc doc {
        $doc paragraph "Hello"
        countNodes $doc {//w:p}
    }
} -result 1

test basic-1.2 {multiple paragraphs} -body {
    withDoc doc {
        $doc paragraph "One"
        $doc paragraph "Two"
        $doc paragraph "Three"
        countNodes $doc {//w:p}
    }
} -result 3

test basic-1.3 {paragraph text content} -body {
    withDoc doc {
        $doc paragraph "Hello, World!"
        xpath $doc {string(//w:p[1]//w:t)}
    }
} -result {Hello, World!}

test basic-1.4 {empty paragraph} -body {
    withDoc doc {
        $doc paragraph ""
        countNodes $doc {//w:p}
    }
} -result 1

# ──────────────────────────────────────────────────────────────────
#  Append to paragraph
# ──────────────────────────────────────────────────────────────────

test basic-2.1 {append creates additional run} -body {
    withDoc doc {
        $doc paragraph "Start "
        $doc append "end"
        countNodes $doc {//w:p[1]/w:r}
    }
} -result 2

test basic-2.2 {append text content} -body {
    withDoc doc {
        $doc paragraph "A"
        $doc append "B"
        $doc append "C"
        xpath $doc {string(//w:p[1]/w:r[3]//w:t)}
    }
} -result C

# ──────────────────────────────────────────────────────────────────
#  Character formatting
# ──────────────────────────────────────────────────────────────────

test basic-3.1 {bold} -body {
    withDoc doc {
        $doc paragraph "Bold" -bold on
        xpath $doc {string(//w:r/w:rPr/w:b/@w:val)}
    }
} -result 1

test basic-3.2 {italic} -body {
    withDoc doc {
        $doc paragraph "Italic" -italic on
        xpath $doc {string(//w:r/w:rPr/w:i/@w:val)}
    }
} -result 1

test basic-3.3 {color} -body {
    withDoc doc {
        $doc paragraph "Red" -color FF0000
        xpath $doc {string(//w:r/w:rPr/w:color/@w:val)}
    }
} -result FF0000

test basic-3.4 {fontsize in half-points} -body {
    withDoc doc {
        $doc paragraph "Big" -fontsize 28
        xpath $doc {string(//w:r/w:rPr/w:sz/@w:val)}
    }
} -result 28

test basic-3.5 {underline single} -body {
    withDoc doc {
        $doc paragraph "U" -underline single
        xpath $doc {string(//w:r/w:rPr/w:u/@w:val)}
    }
} -result single

test basic-3.6 {strike} -body {
    withDoc doc {
        $doc paragraph "S" -strike on
        xpath $doc {string(//w:r/w:rPr/w:strike/@w:val)}
    }
} -result 1

test basic-3.7 {multiple formatting options} -body {
    withDoc doc {
        $doc paragraph "Combo" -bold on -italic on -color 00FF00
        set b [xpath $doc {string(//w:r/w:rPr/w:b/@w:val)}]
        set i [xpath $doc {string(//w:r/w:rPr/w:i/@w:val)}]
        set c [xpath $doc {string(//w:r/w:rPr/w:color/@w:val)}]
        list $b $i $c
    }
} -result {1 1 00FF00}

# ──────────────────────────────────────────────────────────────────
#  Paragraph options
# ──────────────────────────────────────────────────────────────────

test basic-4.1 {alignment} -body {
    withDoc doc {
        $doc paragraph "Centered" -align center
        xpath $doc {string(//w:p/w:pPr/w:jc/@w:val)}
    }
} -result center

test basic-4.2 {spacing} -body {
    withDoc doc {
        $doc paragraph "Spaced" -spacing {before 240 after 120}
        set b [xpath $doc {string(//w:p/w:pPr/w:spacing/@w:before)}]
        set a [xpath $doc {string(//w:p/w:pPr/w:spacing/@w:after)}]
        list $b $a
    }
} -result {240 120}

test basic-4.3 {paragraph background} -body {
    withDoc doc {
        $doc paragraph "Yellow" -background FFFF00
        set val [xpath $doc {string(//w:p/w:pPr/w:shd/@w:val)}]
        set fill [xpath $doc {string(//w:p/w:pPr/w:shd/@w:fill)}]
        list $val $fill
    }
} -result {clear FFFF00}

test basic-4.4 {paragraph shading} -body {
    withDoc doc {
        $doc paragraph "Shaded" -shading {type clear color auto fill CCCCCC}
        xpath $doc {string(//w:p/w:pPr/w:shd/@w:fill)}
    }
} -result CCCCCC

# ──────────────────────────────────────────────────────────────────
#  Break and tab
# ──────────────────────────────────────────────────────────────────

test basic-5.1 {br inserts break} -body {
    withDoc doc {
        $doc paragraph "Before"
        $doc br
        countNodes $doc {//w:br}
    }
} -result 1

test basic-5.2 {br with count} -body {
    withDoc doc {
        $doc paragraph "Before"
        $doc br 3
        countNodes $doc {//w:br}
    }
} -result 3

test basic-5.3 {tab inserts tab} -body {
    withDoc doc {
        $doc paragraph "Before"
        $doc tab
        countNodes $doc {//w:tab}
    }
} -result 1

# ──────────────────────────────────────────────────────────────────
#  Write and basic package structure
# ──────────────────────────────────────────────────────────────────

test basic-6.1 {write produces file} -body {
    withDoc doc {
        $doc paragraph "Test"
        set f [writedoc $doc]
        file exists $f
    }
} -result 1

test basic-6.2 {write produces valid zip} -body {
    withDoc doc {
        $doc paragraph "Test"
        set f [writedoc $doc]
        set parts [ziplist $f]
        expr {
            "word/document.xml" in $parts &&
            {[Content_Types].xml} in $parts &&
            "_rels/.rels" in $parts &&
            "word/_rels/document.xml.rels" in $parts
        }
    }
} -result 1

test basic-6.3 {write sectPr is last child of body} -body {
    withDoc doc {
        $doc paragraph "Test"
        set f [writedoc $doc]
        set xml [zipread $f word/document.xml]
        set ddoc [xmlparse $xml]
        $ddoc selectNodesNamespaces {w http://schemas.openxmlformats.org/wordprocessingml/2006/main}
        set root [$ddoc documentElement]
        set body [$root selectNodes w:body]
        set last [$body lastChild]
        set result [$last localName]
        $ddoc delete
        set result
    }
} -result sectPr

cleanupTests
