<?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\configs as C;
use seekquarry\yioop\library\av_processing\SoundTrack;
use seekquarry\yioop\library\UnitTest;

/**
 * SoundTrackTest checks that SoundTrack finds the sound in a recording
 * a browser wrote.
 *
 * A browser writes a recording in parts as it records, rather than
 * writing a table of where its pieces are at the end. Such a file leaves
 * that table empty and puts the length of every piece in a heading
 * before each part. The test data here was recorded by Chromium's own
 * recorder, so it has the shape a real message carries rather than one
 * this test made up.
 *
 * @author Chris Pollett
 */
class SoundTrackTest extends UnitTest
{
    /**
     * $where stores the path this test writes its recording to, so each
     * case can read it and the file can be deleted afterward.
     * @var string
     */
    public $where;
    /**
     * PIECES_IN_TEST_DATA is how many pieces of compressed sound the
     * recording in the test data holds.
     */
    const PIECES_IN_TEST_DATA = 17;
    /**
     * setUp writes the test recording out of the base64 copy that
     * travels with the tests, since a test file of sound cannot be kept
     * as bytes in the repository.
     */
    public function setUp()
    {
        $this->where = C\WORK_DIRECTORY . "/temp/inparts" . getmypid() .
            ".mp4";
        $held = file_get_contents(C\PARENT_DIR .
            "/tests/test_files/recording_written_in_parts.txt");
        file_put_contents($this->where, base64_decode($held));
        @chmod($this->where, 0777);
    }
    /**
     * tearDown deletes the recording this test wrote.
     */
    public function tearDown()
    {
        if (file_exists($this->where)) {
            unlink($this->where);
        }
    }
    /**
     * recordingWrittenInPartsGivesItsPiecesTestCase checks that the
     * pieces of a recording written in parts are found. Before the
     * reader looked at the headings of those parts it found nothing at
     * all in such a file, and a message made from one held no sound.
     */
    public function recordingWrittenInPartsGivesItsPiecesTestCase()
    {
        $found = SoundTrack::fromFile($this->where, "mp4");
        $this->assertEqual("opus", $found["codec"],
            "the recording holds sound compressed the way a browser " .
            "compresses a voice");
        $this->assertEqual(self::PIECES_IN_TEST_DATA,
            count($found["pieces"]),
            "every piece of the recording is found");
    }
    /**
     * everyPieceHoldsSomethingTestCase checks that no piece comes back
     * empty. A piece read at the wrong place in the file comes back
     * empty or short, which a count of pieces alone would not show.
     */
    public function everyPieceHoldsSomethingTestCase()
    {
        $found = SoundTrack::fromFile($this->where, "mp4");
        $smallest = 0;
        foreach ($found["pieces"] as $one) {
            if ($smallest == 0 || strlen($one) < $smallest) {
                $smallest = strlen($one);
            }
        }
        $this->assertTrue($smallest > 0,
            "the shortest piece holds something, and it holds " .
            $smallest . " bytes");
    }
}
