<?xml version="1.0" encoding="utf-8"?>
<t:test xmlns:t="http://xproc.org/ns/testsuite/3.0"
        xml:base="/polyglot-006.xml"
        name="polyglot-006.xml"
        expected="pass"
        features="polyglot">
   <t:info>
      <t:title>polyglot-006</t:title>
      <t:revision-history>
         <t:revision>
            <t:date>2025-01-03</t:date>
            <t:author>
               <t:name>Norm Tovey-Walsh</t:name>
            </t:author>
            <t:description xmlns="http://www.w3.org/1999/xhtml">
               <p>Created test.</p>
            </t:description>
         </t:revision>
      </t:revision-history>
   </t:info>
   <t:description xmlns="http://www.w3.org/1999/xhtml">
      <p>Tests that the cx:javascript polyglot step returns a map.</p>
   </t:description>
   <t:pipeline>
      <p:declare-step xmlns:cx="http://xmlcalabash.com/ns/extensions"
                      xmlns:err="http://www.w3.org/ns/xproc-error"
                      xmlns:p="http://www.w3.org/ns/xproc"
                      version="3.0">
         <p:import href="https://xmlcalabash.com/ext/library/polyglot.xpl"/>
         <p:output port="result"/>
         <cx:javascript variables="map{'x': 7, 'y': 9, 'now': current-dateTime()}">
            <p:with-input>
               <doc>Test</doc>
            </p:with-input>
            <p:with-option name="args" select="('a', 'b', 'c')"/>
            <p:with-input port="program">
               <p:inline content-type="text/plain" expand-text="false">
// Unclear if/how to access process.argv from GraalVM
let result = {
  "x": x,
  "y": y,
  "product": x * y,
  "time": now
}
result
          </p:inline>
            </p:with-input>
         </cx:javascript>
         <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>
            <s:rule context="/">
               <s:assert test="fn:map">The result isn’t a map.</s:assert>
            </s:rule>
         </s:pattern>
         <s:pattern>
            <s:rule context="/fn:map">
               <s:assert test="fn:number[@key='product']">No product reported.</s:assert>
               <s:assert test="fn:number[@key='product'] = '63'">The product is incorrect.</s:assert>
            </s:rule>
         </s:pattern>
      </s:schema>
   </t:schematron>
   <t:result name="polyglot-006.xml">
      <map xmlns="http://www.w3.org/2005/xpath-functions">
         <string key="time">2026-07-21T06:16:27.020862749Z</string>
         <number key="product">63</number>
         <number key="x">7</number>
         <number key="y">9</number>
      </map>
   </t:result>
</t:test>
