<?php

app_require('routes/moduleN8nRoute.php');

use routes\moduleN8nRoute;

function n8n_route_invoke_private(moduleN8nRoute $route, string $method, array $args = []): mixed
{
    $reflection = new ReflectionClass($route);
    $target = $reflection->getMethod($method);

    return $target->invokeArgs($route, $args);
}

it('normalizes webhook methods and falls back to POST for unsupported verbs', function (): void {
    $_SERVER['REQUEST_URI'] = '/modules/n8n/webhooks/trigger';
    $route = new moduleN8nRoute();

    expect(n8n_route_invoke_private($route, 'normalizeHttpMethod', ['patch']))->toBe('PATCH');
    expect(n8n_route_invoke_private($route, 'normalizeHttpMethod', [' delete ']))->toBe('DELETE');
    expect(n8n_route_invoke_private($route, 'normalizeHttpMethod', ['trace']))->toBe('POST');
});

it('filters request parameters down to the allowed n8n query keys', function (): void {
    $_SERVER['REQUEST_URI'] = '/modules/n8n/workflows';
    $route = new moduleN8nRoute();

    $filtered = n8n_route_invoke_private($route, 'filterRequestParameters', [[
        'active' => 'true',
        'limit' => '25',
        'cursor' => '',
        'projectId' => 'abc123',
        'ignored' => 'value',
    ], [
        'active',
        'limit',
        'projectId',
    ]]);

    expect($filtered)->toBe([
        'active' => 'true',
        'limit' => '25',
        'projectId' => 'abc123',
    ]);
});
