'Test using client-side API for commands.'

def testClientAPI(self):

    results = []
    def handle_complete(iq, session):
        for item in session['custom_data']:
            results.append(item)
    def handle_step2(iq, session):
        form = self.xmpp['xep_0004'].makeForm(ftype='submit')
        form.addField(var='bar', value='123')
        session['custom_data'].append('baz')
        session['payload'] = form
        session['next'] = handle_complete
        self.xmpp['xep_0050'].complete_command(session)
    def handle_step1(iq, session):
        form = self.xmpp['xep_0004'].makeForm(ftype='submit')
        form.addField(var='foo', value='42')
        session['custom_data'].append('bar')
        session['payload'] = form
        session['next'] = handle_step2
        self.xmpp['xep_0050'].continue_command(session)
    session = {'custom_data': ['foo'], 'next': handle_step1}
    self.xmpp['xep_0050'].start_command('foo@example.com', 'test_client', session)
    self.send('\n                                        <iq    id="1"    to="foo@example.com"    type="set">\n                                                <command    xmlns="http://jabber.org/protocol/commands"\n                                                                                    node="test_client"\n                                                                                    action="execute"    />\n                                        </iq>\n                                ')
    self.recv('\n                                        <iq    id="1"    from="foo@example.com"    type="result">\n                                                <command    xmlns="http://jabber.org/protocol/commands"\n                                                                                    node="test_client"\n                                                                                    sessionid="_sessionid_"\n                                                                                    status="executing">\n                                                        <x    xmlns="jabber:x:data"    type="form">\n                                                                <field    var="foo"    type="text-single"    />\n                                                        </x>\n                                                </command>\n                                        </iq>\n                                ')
    self.send('\n                                        <iq    id="2"    to="foo@example.com"    type="set">\n                                                <command    xmlns="http://jabber.org/protocol/commands"\n                                                                                    node="test_client"\n                                                                                    sessionid="_sessionid_"\n                                                                                    action="next">\n                                                        <x    xmlns="jabber:x:data"    type="submit">\n                                                                <field    var="foo">\n                                                                        <value>42</value>\n                                                                </field>\n                                                        </x>\n                                                </command>\n                                        </iq>\n                                ')
    self.recv('\n                                        <iq    id="2"    from="foo@example.com"    type="result">\n                                                <command    xmlns="http://jabber.org/protocol/commands"\n                                                                                    node="test_client"\n                                                                                    sessionid="_sessionid_"\n                                                                                    status="executing">\n                                                        <x    xmlns="jabber:x:data"    type="form">\n                                                                <field    var="bar"    type="text-single"    />\n                                                        </x>\n                                                </command>\n                                        </iq>\n                                ')
    self.send('\n                                        <iq    id="3"    to="foo@example.com"    type="set">\n                                                <command    xmlns="http://jabber.org/protocol/commands"\n                                                                                    node="test_client"\n                                                                                    sessionid="_sessionid_"\n                                                                                    action="complete">\n                                                        <x    xmlns="jabber:x:data"    type="submit">\n                                                                <field    var="bar">\n                                                                        <value>123</value>\n                                                                </field>\n                                                        </x>\n                                                </command>\n                                        </iq>\n                                ')
    self.recv('\n                                        <iq    id="3"    from="foo@example.com"    type="result">\n                                                <command    xmlns="http://jabber.org/protocol/commands"\n                                                                                    node="test_client"\n                                                                                    sessionid="_sessionid_"\n                                                                                    status="completed"    />\n                                        </iq>\n                                ')
    time.sleep(0.3)
    self.failUnless((results == ['foo', 'bar', 'baz']), ('Incomplete    command    workflow:    %s' % results))
