<?xml version="1.0" encoding="UTF-8"?>
<t:test expected="pass" features="p:file-copy p:directory-list"
        xmlns:t="http://xproc.org/ns/testsuite/3.0">
   <t:info>
      <t:title>p:file-copy 023 (AB)</t:title>
      <t:revision-history>
         <t:revision>
            <t:date>2022-08-06</t:date>
            <t:author>
               <t:name>Achim Berndzen</t:name>
            </t:author>
            <t:description xmlns="http://www.w3.org/1999/xhtml">
               <p>Initial commit</p>
            </t:description>
         </t:revision>       
      </t:revision-history>
   </t:info>
   <t:description xmlns="http://www.w3.org/1999/xhtml">
      <p>Tests p:file-copy</p>
   </t:description>
 
   <t:file-environment>
      <t:file path="source/file1.txt" >source</t:file>
      <t:file path="target/source/file1.txt" >target</t:file>
   </t:file-environment>
   
   <t:pipeline>
      <p:declare-step version="3.0"
                      xmlns:p="http://www.w3.org/ns/xproc"
                      xmlns:fn="http://www.w3.org/2005/xpath-functions"
                      xmlns:c="http://www.w3.org/ns/xproc-step">
         <p:output port="result"/>
         <p:file-copy href="../testfolder/source" target="../testfolder/target" overwrite="true" name="copy"/>
         
         <p:directory-list path="../testfolder" max-depth="unbounded" depends="copy" name="listing"/>
         
         <p:wrap-sequence wrapper="content" name="content">
            <p:with-input href="{fn:base-uri(//c:directory[@name='target']/c:directory[@name='source']/c:file[@name='file1.txt']/@name)}" />
         </p:wrap-sequence>
         
         <p:insert match="//c:directory[@name='target']/c:directory[@name='source']/c:file[@name='file1.txt']" position="first-child">
            <p:with-input port="source" pipe="@listing"  />
            <p:with-input port="insertion" pipe="@content" />
         </p:insert>
      </p:declare-step>
   </t:pipeline>
   
   <t:schematron>
      <s:schema queryBinding="xslt2"
         xmlns:s="http://purl.oclc.org/dsdl/schematron"
         xmlns="http://www.w3.org/1999/xhtml">
         <s:ns uri="http://www.w3.org/ns/xproc-step" prefix="c"/>
         <s:ns uri="http://www.w3.org/2001/XMLSchema" prefix="ns" />
         <s:pattern>
            <s:rule context="/">
               <s:assert test="c:directory">Root element is not c:directory.</s:assert>
               <s:assert test="c:directory/@name='testfolder'">Attribute 'name' of root is not 'testfolder'.</s:assert>
               <s:assert test="c:directory/c:directory/@name='target'">There is no entry for folder 'target'.</s:assert>
               <s:assert test="//c:directory[@name='target']/c:directory/@name='source'">There is no entry for 'source' in 'target'.</s:assert>
               <s:assert test="//c:directory[@name='target']/c:directory[@name='source']/c:file/@name='file1.txt'">There is no entry for target/source/file1.txt</s:assert>
               <s:assert test="//c:directory[@name='target']/c:directory[@name='source']/c:file[@name='file1.txt']/content/text()='source'">Content of target/source/file1.txt is not 'source'.</s:assert>
            </s:rule>
         </s:pattern>
      </s:schema>
   </t:schematron>
</t:test>