<?php

use App\Domains\Analytics\Jobs\RecordClickJob;
use App\Domains\Analytics\Models\Click;
use App\Domains\Link\Models\Link;

it('records a click to the database', function () {
    $link = Link::factory()->create();

    RecordClickJob::dispatchSync($link->id, '1.2.3.4', 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0)', []);

    $this->assertDatabaseCount('clicks', 1);
    $this->assertDatabaseHas('clicks', [
        'link_id' => $link->id,
        'device' => 'mobile',
    ]);
});

it('hashes IP address before storage', function () {
    $link = Link::factory()->create();

    RecordClickJob::dispatchSync($link->id, '1.2.3.4', 'test-agent', []);

    $click = Click::first();
    expect($click->ip_hash)->not->toBe('1.2.3.4')
        ->and(strlen($click->ip_hash))->toBe(64);
});
