<?xml version="1.0" encoding="utf-8"?>
<t:test xmlns:t="http://xproc.org/ns/testsuite/3.0"
        xml:base="nw-send-mail-002.xml"
        name="nw-send-mail-002.xml"
        expected="pass"
        features="send-mail">
   <t:info>
      <t:title>p:send-mail-002</t:title>
      <t:revision-history>
         <t:revision>
            <t:date>2025-05-23</t:date>
            <t:author>
               <t:name>Norm Tovey-Walsh</t:name>
            </t:author>
            <t:description xmlns="http://www.w3.org/1999/xhtml">
               <p>Added static options for SMTP host and ports.</p>
            </t:description>
         </t:revision>
         <t:revision>
            <t:date>2025-04-21</t:date>
            <t:author>
               <t:name>Norm Tovey-Walsh</t:name>
            </t:author>
            <t:description xmlns="http://www.w3.org/1999/xhtml">
               <p>Add dependencies for multithreading and a delay so sendri has time to process the message.</p>
            </t:description>
         </t:revision>
         <t:revision>
            <t:date>2024-10-12</t:date>
            <t:author>
               <t:name>Norm Tovey-Walsh</t:name>
            </t:author>
            <t:description xmlns="http://www.w3.org/1999/xhtml">
               <p>Avoid content-type error if clearing the log fails.</p>
            </t:description>
         </t:revision>
         <t:revision>
            <t:date>2024-05-30</t:date>
            <t:author>
               <t:name>Norm Tovey-Walsh</t:name>
            </t:author>
            <t:description xmlns="http://www.w3.org/1999/xhtml">
               <p>Test for the p:send-mail step.</p>
            </t:description>
         </t:revision>
      </t:revision-history>
   </t:info>
   <t:description xmlns="http://www.w3.org/1999/xhtml">
      <p>Test p:send-mail sending HTML mail. </p>
   </t:description>
   <t:pipeline>
      <p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0">
         <p:output port="result"/>
         <p:option name="SHOST" select="'localhost'" static="true"/>
         <p:option name="SMTPPORT" select="1025" static="true"/>
         <p:option name="APIPORT" select="1080" static="true"/>
         <!-- Clear the Sendria log -->
         <p:http-request href="http://{$SHOST}:{$APIPORT}/api/messages/" method="delete">
            <p:with-input>
               <p:empty/>
            </p:with-input>
         </p:http-request>
         <!-- Make sure that succeeded -->
         <p:if test=".?code != 'OK'" name="check-delete">
            <p:error code="Q{{http://example.com/}}irrelevant">
               <p:with-input>
                  <p:empty/>
               </p:with-input>
            </p:error>
         </p:if>
         <p:send-mail parameters="map{'host': $SHOST, 'port': $SMTPPORT}"
                      auth="map{'username':'username','password':'password'}"
                      depends="check-delete">
            <p:with-input>
               <p:inline>
                  <emx:Message xmlns:emx="URN:ietf:params:email-xml:" xmlns:rfc822="URN:ietf:params:rfc822:">
                     <rfc822:from>
                        <emx:Address>
                           <emx:adrs>mailto:user@example.com</emx:adrs>
                           <emx:name>Example User</emx:name>
                        </emx:Address>
                     </rfc822:from>
                     <rfc822:to>
                        <emx:Address>
                           <emx:adrs>mailto:anotheruser@example.com</emx:adrs>
                           <emx:name>Another User</emx:name>
                        </emx:Address>
                     </rfc822:to>
                     <rfc822:subject>HTML Email</rfc822:subject>
                     <emx:content type="text/plain">
                        <html xmlns="http://www.w3.org/1999/xhtml">
                           <head>
                              <title>Hello, World</title>
                           </head>
                           <body>
                              <h1>Hello, World!</h1>
                              <p>This is an email message encoded in HTML. This is <em>evil</em>. Don’t do this.</p>
                           </body>
                        </html>
                     </emx:content>
                  </emx:Message>
               </p:inline>
            </p:with-input>
         </p:send-mail>
         <p:sleep duration="1"/>
         <!-- Check the Sendria log -->
         <p:http-request href="http://{$SHOST}:{$APIPORT}/api/messages/" method="get"/>
         <p:http-request href="http://{$SHOST}:{$APIPORT}/api/messages/" method="get"/>
         <p:http-request href="http://{$SHOST}:{$APIPORT}/api/messages/" method="get"/>
         <p:cast-content-type content-type="application/xml"/>
      </p:declare-step>
   </t:pipeline>
   <t:schematron>
      <s:schema xmlns:s="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
         <s:ns prefix="fn" uri="http://www.w3.org/2005/xpath-functions"/>
         <s:pattern name="Root must be map">
            <s:rule context="/fn:map">
               <s:assert test="fn:string[@key='code'] = 'OK'">Code must be 'OK'</s:assert>
               <s:assert test="fn:array[@key='data']/fn:map/fn:string[@key='subject']                             = 'HTML Email'">The subject must be correct</s:assert>
               <s:assert test="fn:array[@key='data']/fn:map/fn:string[@key='type']                             = 'text/html'">The content type must be HTML</s:assert>
            </s:rule>
         </s:pattern>
      </s:schema>
   </t:schematron>
   <t:result name="nw-send-mail-002.xml">
      <map xmlns="http://www.w3.org/2005/xpath-functions">
         <string key="code">OK</string>
         <array key="data">
            <map>
               <array key="recipients_envelope">
                  <string>anotheruser@example.com</string>
               </array>
               <string key="sender_envelope">user@example.com</string>
               <string key="subject">HTML Email</string>
               <string key="created_at">2026-05-10T09:18:49</string>
               <array key="recipients_message_cc"/>
               <string key="source">Date: Sun, 10 May 2026 09:18:49 +0000 (UTC)
From: Example User &lt;user@example.com&gt;
To: Another User &lt;anotheruser@example.com&gt;
Message-ID: &lt;2013654259.1.1778404729870@f3203ea5bf11&gt;
Subject: HTML Email
MIME-Version: 1.0
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-Peer: ('172.22.0.6', 42782)
X-MailFrom: user@example.com
X-RcptTo: anotheruser@example.com


          &lt;html xmlns=3D"http://www.w3.org/1999/xhtml" xmlns:emx=3D"URN:iet=
f:params:email-xml:" xmlns:rfc822=3D"URN:ietf:params:rfc822:"&gt;
            &lt;head&gt;
              &lt;title&gt;Hello, World&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
              &lt;h1&gt;Hello, World!&lt;/h1&gt;
              &lt;p&gt;This is an email message encoded in HTML. This is &lt;em&gt;evil=
&lt;/em&gt;. Don=E2=80=99t do this.&lt;/p&gt;
            &lt;/body&gt;
          &lt;/html&gt;
       =20
</string>
               <string key="type">text/html</string>
               <array key="recipients_message_to">
                  <string>Another User &lt;anotheruser@example.com&gt;</string>
               </array>
               <number key="size">831</number>
               <string key="sender_message">Example User &lt;user@example.com&gt;</string>
               <array key="recipients_message_bcc"/>
               <string key="peer">172.22.0.6:42782</string>
               <number key="id">1</number>
            </map>
         </array>
         <map key="meta">
            <number key="pages_total">1</number>
         </map>
      </map>
   </t:result>
</t:test>
