<?php

it('uses default broker url but requires explicit shared secret configuration', function (): void {
    $source = file_get_contents(app_path('classes/edge_broker_client.php'));

    expect($source)->not->toBeFalse();
    expect($source)->toContain("private const DEFAULT_BROKER_URL = 'http://edge-broker:4300';");
    expect($source)->toContain('class edge_broker_transport_exception extends Exception');
    expect($source)->toContain('class edge_broker_http_exception extends Exception');
    expect($source)->toContain("getenv('EDGE_BROKER_SHARED_SECRET')");
    expect($source)->toContain("getenv('EDGE_INTERNAL_SECRET')");
    expect($source)->not->toContain('DEFAULT_SHARED_SECRET');
});


it('fails closed when no broker shared secret is configured', function (): void {
    $previousBrokerSecret = getenv('EDGE_BROKER_SHARED_SECRET');
    $previousInternalSecret = getenv('EDGE_INTERNAL_SECRET');
    putenv('EDGE_BROKER_SHARED_SECRET');
    putenv('EDGE_INTERNAL_SECRET');

    try {
        $client = new \classes\edge_broker_client('http://127.0.0.1:9', null, 1);

        expect(fn() => $client->dispatchCommand(1, 'SET_RELAY_STATE', ['on' => true]))
            ->toThrow(Exception::class, 'Edge broker shared secret is not configured');
    } finally {
        $previousBrokerSecret === false
            ? putenv('EDGE_BROKER_SHARED_SECRET')
            : putenv('EDGE_BROKER_SHARED_SECRET=' . $previousBrokerSecret);
        $previousInternalSecret === false
            ? putenv('EDGE_INTERNAL_SECRET')
            : putenv('EDGE_INTERNAL_SECRET=' . $previousInternalSecret);
    }
});
