const assert = require('node:assert/strict');
const sinon = require('sinon');
const configUtils = require('../../../utils/config-utils');

// Stuff we are testing
const content_api_url = require('../../../../core/frontend/helpers/content_api_url');
const logging = require('@tryghost/logging');

describe('{{content_api_url}} helper', function () {
    let logWarnStub;

    beforeEach(function () {
        logWarnStub = sinon.stub(logging, 'warn');
    });

    afterEach(function () {
        sinon.restore();
    });

    describe('without sub-directory', function () {
        before(function () {
            configUtils.set({url: 'http://localhost:65535/', 'admin:url': 'https://admin.tld:65535'});
        });

        after(async function () {
            await configUtils.restore();
        });

        it('should output an absolute url', async function () {
            let result = content_api_url();
            const rendered = String(result);
            assert.equal(rendered, 'https://admin.tld:65535/ghost/api/content/');
            sinon.assert.notCalled(logWarnStub);
        });
        it('should output an absolute url when passed true', async function () {
            let result = content_api_url(true);
            const rendered = String(result);
            assert.equal(rendered, 'https://admin.tld:65535/ghost/api/content/');
            sinon.assert.notCalled(logWarnStub);
        });
        it('should output a relative url when passed false', async function () {
            let result = content_api_url(false);
            const rendered = String(result);
            assert.equal(rendered, '/ghost/api/content/');
            sinon.assert.notCalled(logWarnStub);
        });
    });
    describe('with a sub-directory', function () {
        before(function () {
            configUtils.set({url: 'http://localhost:65535/blog', 'admin:url': 'https://admin.tld:65535/blog'});
        });

        after(async function () {
            await configUtils.restore();
        });

        it('should output an absolute url', async function () {
            let result = content_api_url();
            const rendered = String(result);
            assert.equal(rendered, 'https://admin.tld:65535/blog/ghost/api/content/');
            sinon.assert.notCalled(logWarnStub);
        });
        it('should output an absolute url when passed true', async function () {
            let result = content_api_url(true);
            const rendered = String(result);
            assert.equal(rendered, 'https://admin.tld:65535/blog/ghost/api/content/');
            sinon.assert.notCalled(logWarnStub);
        });
        it('should output a relative url when passed false', async function () {
            let result = content_api_url(false);
            const rendered = String(result);
            assert.equal(rendered, '/blog/ghost/api/content/');
            sinon.assert.notCalled(logWarnStub);
        });
    });
    describe('uses the site url if no admin:url is set', function () {
        before(function () {
            configUtils.set({url: 'http://localhost:65535/'});
        });

        after(async function () {
            await configUtils.restore();
        });

        it('gives the site url without a subdirectory', async function () {
            let result = content_api_url();
            const rendered = String(result);
            assert.equal(rendered, 'http://localhost:65535/ghost/api/content/');
            sinon.assert.notCalled(logWarnStub);
        });
        it('gives the site url with a subdirectory', async function () {
            configUtils.set({url: 'http://localhost:65535/blog', 'admin:url': undefined});
            let result = content_api_url();
            const rendered = String(result);
            assert.equal(rendered, 'http://localhost:65535/blog/ghost/api/content/');
            sinon.assert.notCalled(logWarnStub);
        });
    });
});

