#include <QTemporaryDir>
#include <QtTest>

#include "config-loader.h"

using namespace UkuiQuick;

class config_loader_test : public QObject
{
    Q_OBJECT

private slots:
    void fileNameReflectsIdAndType();
    void fullFileNameReflectsDomainAndApplication();
};

void config_loader_test::fileNameReflectsIdAndType()
{
    QCOMPARE(ConfigLoader::getConfigFileName(QString(), ConfigLoader::Json), QString());
    QCOMPARE(ConfigLoader::getConfigFileName(QStringLiteral("panel"), ConfigLoader::Json), QStringLiteral("panel.json"));
    QCOMPARE(ConfigLoader::getConfigFileName(QStringLiteral("panel"), ConfigLoader::Ini), QStringLiteral("panel.conf"));
}

void config_loader_test::fullFileNameReflectsDomainAndApplication()
{
    QTemporaryDir homeDir;
    QVERIFY(homeDir.isValid());
    const QByteArray oldHome = qgetenv("HOME");
    qputenv("HOME", QFile::encodeName(homeDir.path()));

    const QString globalPath = homeDir.path() + QStringLiteral("/.config/org.ukui/_ukui-config-global/");
    const QString localPath = homeDir.path() + QStringLiteral("/.config/org.ukui/");

    QCOMPARE(ConfigLoader::globalConfigPath(), globalPath);
    QCOMPARE(ConfigLoader::localConfigPath(), localPath);
    QCOMPARE(ConfigLoader::getFullFileName(QString(), ConfigLoader::Json, ConfigLoader::Global), QString());
    QCOMPARE(ConfigLoader::getFullFileName(QStringLiteral("panel"), ConfigLoader::Json, ConfigLoader::Global),
             globalPath + QStringLiteral("panel.json"));
    QCOMPARE(ConfigLoader::getFullFileName(QStringLiteral("panel"), ConfigLoader::Ini, ConfigLoader::Local, QStringLiteral("demo")),
             localPath + QStringLiteral("demo/panel.conf"));
    QCOMPARE(ConfigLoader::getFullFileName(QStringLiteral("panel"), ConfigLoader::Json, ConfigLoader::Local), QString());

    qputenv("HOME", oldHome);
}

QTEST_GUILESS_MAIN(config_loader_test)

#include "test-config-loader.moc"
