/*
 * Copyright (C) 2026 UBports Foundation.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../../src/vendors/NextcloudVendor.h"
#include "../../src/vendors/Vendor.h"

#include <gtest/gtest.h>

using namespace std;
using namespace lomiri::storage::provider;

TEST(NextcloudVendor, for_context)
{
    auto nextcloud = Vendor::for_context(NextcloudVendor::test_context());
    EXPECT_NE(nullptr, dynamic_cast<NextcloudVendor*>(nextcloud.get()));
}

TEST(NextcloudVendor, is_valid_context)
{
    EXPECT_TRUE(NextcloudVendor::is_valid_context(
        NextcloudVendor::test_context()));
    EXPECT_TRUE(NextcloudVendor::is_valid_context(
        NextcloudVendor::test_context("https://cloud.example.com")));

    EXPECT_FALSE(NextcloudVendor::is_valid_context(Context()));
    EXPECT_FALSE(NextcloudVendor::is_valid_context(
        NextcloudVendor::test_context("")));
    EXPECT_FALSE(NextcloudVendor::is_valid_context(
        NextcloudVendor::test_context("/tmp/lsf-rclone")));

    EXPECT_FALSE(NextcloudVendor::is_valid_context(
        NextcloudVendor::test_context("https://cloud.example.com", "", "secret")));
    EXPECT_FALSE(NextcloudVendor::is_valid_context(
        NextcloudVendor::test_context("https://cloud.example.com", "worf", "")));
}

TEST(NextcloudVendor, files_url)
{
    EXPECT_EQ("https://cloud.example.com/remote.php/dav/files/mogh/",
              NextcloudVendor::files_url("https://cloud.example.com", "mogh"));

    EXPECT_EQ("https://cloud.example.com/remote.php/dav/files/martok/",
              NextcloudVendor::files_url("https://cloud.example.com/", "martok"));
    EXPECT_EQ("https://cloud.example.com/remote.php/dav/files/kang/",
              NextcloudVendor::files_url("https://cloud.example.com///", "kang"));

    EXPECT_EQ("https://example.com/nextcloud/remote.php/dav/files/kor/",
              NextcloudVendor::files_url("https://example.com/nextcloud", "kor"));

    EXPECT_EQ("https://example.com/remote.php/dav/files/kara%20trothir/",
              NextcloudVendor::files_url("https://example.com", "kara trothir"));
    EXPECT_EQ("https://example.com/remote.php/dav/files/k%27ehleyr/",
              NextcloudVendor::files_url("https://example.com", "k'ehleyr"));
}
