<?php

namespace Tests\Feature;

use Tests\TestCase;

/**
 * A LinkedIn place that is not already in the list is searched in that same list.
 * Places already ticked stay selected.
 */
class LinkedInLocationListSearchTest extends TestCase
{
    public function test_a_missing_place_is_searched_without_dropping_the_ones_already_ticked(): void
    {
        $command = 'node '.escapeshellarg(base_path('tests/js/linkedin-location-search.cjs'))
            .' '.escapeshellarg(public_path('assets/js/chat.js')).' 2>&1';
        $output = shell_exec($command);
        $result = json_decode((string) $output, true);

        $this->assertIsArray($result, (string) $output);
        $this->assertArrayNotHasKey('error', $result, (string) $output);
        $this->assertSame('California, United States', $result['local']['labels'][0]);
        $this->assertSame('Los Angeles County, California, United States', $result['local']['labels'][1]);
        $this->assertFalse($result['local']['settled']);
        $this->assertSame(0, $result['beforeSearch']['sends']);
        $this->assertSame([], $result['beforeSearch']['searches']);
        $this->assertSame('Nothing in this list. Searching LinkedIn…', $result['beforeSearch']['empty']);
        $this->assertSame(0, $result['searched']['sends']);
        $this->assertSame(['new york'], $result['searched']['searches']);
        $this->assertSame(1, $result['searched']['cards']);
        $this->assertFalse($result['searched']['settled']);
        $this->assertSame('', $result['searched']['filter']);
        $this->assertSame('California, United States', $result['searched']['labels'][0]);
        $this->assertSame(['California, United States'], $result['searched']['picked']);
        $this->assertContains('New York, United States', $result['searched']['labels']);
        $this->assertContains('California, United States', $result['searched']['labels']);
        $this->assertSame(0, $result['button']['sends']);
        $this->assertSame(['york'], $result['button']['searches']);
        $this->assertSame(1, $result['button']['cards']);
        $this->assertSame(['California, United States'], $result['button']['picked']);
        $this->assertSame(
            ['California, United States', 'Los Angeles, California, United States'],
            $result['topped']['labels'],
        );
        $this->assertSame(
            ['California, United States', 'Los Angeles, California, United States'],
            $result['topped']['picked'],
        );
        $this->assertSame(['Los Angeles, California, United States'], $result['excluded']['picked']);
        $this->assertSame('Los Angeles, California, United States', $result['excluded']['labels'][0]);
    }
}
