<?php

use Beebmx\KirbySign\Signed;
use Carbon\Carbon;

describe('basic', function () {
    beforeEach(function () {
        App(options: ['beebmx.kirby-sign.key' => 'test-key']);
    });

    test('a url contains signature parameter', function () {
        expect((new Signed)->url('/'))
            ->toContain('signature');
    });

    test('a url can be relative', function () {
        expect((new Signed)->url('/docs', absolute: false))
            ->not->toContain('example.com')
            ->toStartWith('/docs');
    });
});

describe('query', function () {
    beforeEach(function () {
        App(options: ['beebmx.kirby-sign.key' => 'test-key']);
    });

    it('can have query parameters', function () {
        expect((new Signed)->url('/', ['foo' => 'bar', 'baz' => 'qux']))
            ->toContain('foo=bar', 'baz=qux');
    });

    it('sort query parameters', function () {
        expect((new Signed)->url('/', ['foo' => 'bar', 'baz' => 'qux'], absolute: false))
            ->toStartWith('/?baz=qux&foo=bar');
    });

    it('can have expires parameter', function () {
        expect((new Signed)->url('/', expiration: Carbon::now()->addHour()))
            ->toContain('expires');
    });
});

describe('params', function () {
    beforeEach(function () {
        App(options: ['beebmx.kirby-sign' => ['key' => 'test-key', 'format' => 'params']]);
    });

    it('can have query parameters', function () {
        expect((new Signed)->url('/', ['foo' => 'bar', 'baz' => 'qux']))
            ->toContain('foo:bar', 'baz:qux');
    });

    it('sort query parameters', function () {
        expect((new Signed)->url('/', ['foo' => 'bar', 'baz' => 'qux']))
            ->toStartWith('http://example.com/baz:qux/foo:bar');
    });

    it('can have expires parameter', function () {
        expect((new Signed)->url('/', expiration: Carbon::now()->addHour()))
            ->toContain('expires');
    });
});

describe('options', function () {
    it('can define a different signature', function () {
        App(options: ['beebmx.kirby-sign' => ['key' => 'test-key', 'sign' => 'kirby']]);

        expect((new Signed)->url('/'))
            ->not->toContain('signature')
            ->toContain('kirby');
    });

    it('can set params insted of query', function () {
        App(options: ['beebmx.kirby-sign' => ['key' => 'test-key', 'format' => 'params']]);

        expect((new Signed)->url('/', ['foo' => 'bar', 'baz' => 'qux']))
            ->toContain('foo:bar', 'baz:qux');
    });
});
