<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\tests;

use seekquarry\yioop\library\JavascriptUnitTest;

/**
 * Tests the letting go of the channels a conversation listens on. A
 * browser leaving a page may suspend it rather than take it down, and a
 * socket still open at that moment is reported as closed by suspension,
 * so the channels have to be let go while the page is still there.
 *
 * @author Chris Pollett
 */
class MessagesJavascriptTest extends JavascriptUnitTest
{
    /**
     * Checks that letting the channels go closes both a socket and a
     * stream and forgets them, and that doing it when nothing is open
     * passes quietly.
     *
     * @return string html and Javascript that checks the letting go in a
     *      browser or, from the command line, under node
     */
    public function channelsAreLetGoTestCase()
    {
        ob_start();
        ?>
        <div id="messagesChannelTest"></div>
        <script src="../scripts/messages.js" ></script>
        <script>
        var unit_test_results = [];
        var socket_closed = false;
        var stream_closed = false;
        message_socket = { close: function () {
            socket_closed = true;
        } };
        event_source = { close: function () {
            stream_closed = true;
        } };
        closeMessageChannels();
        unit_test_results.push({name: "an open socket is closed",
            pass: socket_closed});
        unit_test_results.push({name: "and the socket is forgotten",
            pass: message_socket === null});
        unit_test_results.push({name: "an open stream is closed",
            pass: stream_closed});
        unit_test_results.push({name: "and the stream is forgotten",
            pass: event_source === null});
        var quiet = true;
        try {
            closeMessageChannels();
        } catch (problem) {
            quiet = false;
        }
        unit_test_results.push(
            {name: "letting go again when nothing is open passes quietly",
            pass: quiet});
        </script>
        <?php
        $out_data = ob_get_contents();
        ob_end_clean();
        return $out_data;
    }
}
