/*
 *                                      Copyright 2025 WebPros International, LLC
 *                                                           All rights reserved.
 * copyright@cpanel.net                                         http://cpanel.net
 * This code is subject to the cPanel license. Unauthorized copying is prohibited.
 */

import { test, expect } from "@fixtures/plugins/sitejet/sitejet_banner";
import { CommonWhmapi } from "@classes/plugins/common/common";
import { SJConfig } from "@classes/plugins/sitejet/config";

/**
 * Fast API tests for Sitejet Beta enable/disable functionality
 * These tests verify that the WHMAPI calls correctly control beta feature access
 */

let commonWhmapi: CommonWhmapi;

test.beforeAll(async ({}) => {
    commonWhmapi = new CommonWhmapi();
});

test.afterAll("Restore original server state", async ({ rootSFTP }) => {
    // Reset to WebPros rules
    await commonWhmapi.resetSitejetBeta();

    // Clean up, in case the function under test failed
    await rootSFTP.delete(SJConfig.BETA_DISABLE_FLAG, true);
    await rootSFTP.delete(SJConfig.BETA_ENABLE_FLAG, true);
});

test.describe("Sitejet Beta API Control", () => {
    test.beforeEach("Clean up feature flags", async ({}) => {
        await commonWhmapi.resetSitejetBeta();
    });

    test.describe(
        "Company in beta group",
        { tag: SJConfig.getBetaTag() },
        () => {
            test("Beta features enabled by default for beta company", async ({
                rootSFTP,
            }) => {
                // Verify no override flags exist
                await expect(
                    rootSFTP.exists(SJConfig.BETA_DISABLE_FLAG)
                ).resolves.toBe(false);
                await expect(
                    rootSFTP.exists(SJConfig.BETA_ENABLE_FLAG)
                ).resolves.toBe(false);
            });

            test("API can disable beta features for beta company", async ({
                rootSFTP,
            }) => {
                // Disable beta features via API
                await commonWhmapi.disableSitejetBeta();

                // Verify disable flag is not created, because company is in beta group
                expect(
                    await rootSFTP.exists(SJConfig.BETA_DISABLE_FLAG)
                ).not.toBe(true);

                // Verify enable flag is not created
                expect(
                    await rootSFTP.exists(SJConfig.BETA_ENABLE_FLAG)
                ).not.toBe(true);
            });

            test("API can re-enable beta features after disable", async ({
                rootSFTP,
            }) => {
                // First disable
                await commonWhmapi.disableSitejetBeta();
                expect(
                    await rootSFTP.exists(SJConfig.BETA_ENABLE_FLAG)
                ).not.toBe(true);

                // SFTP.exists kept giving me "-" even tho the file was there. Using stat as alternative
                expect(
                    (await rootSFTP.stat(SJConfig.BETA_DISABLE_FLAG)).isFile
                ).toBe(true);

                // Then enable via API
                await commonWhmapi.enableSitejetBeta();

                // Verify enable flag is created and disable flag is removed
                expect(
                    await rootSFTP.exists(SJConfig.BETA_ENABLE_FLAG)
                ).toBeTruthy();
                // Falsey() didn't work.
                expect(
                    await rootSFTP.exists(SJConfig.BETA_DISABLE_FLAG)
                ).not.toBe(true);
            });

            test("API reset removes local overrides", async ({ rootSFTP }) => {
                // First disable to create an override
                await commonWhmapi.disableSitejetBeta();
                expect(
                    (await rootSFTP.stat(SJConfig.BETA_DISABLE_FLAG)).isFile
                ).toBe(true);

                // Reset to WebPros rules
                await commonWhmapi.resetSitejetBeta();

                // Verify both flags are removed
                expect(
                    await rootSFTP.exists(SJConfig.BETA_DISABLE_FLAG)
                ).not.toBe(true);
                expect(
                    await rootSFTP.exists(SJConfig.BETA_ENABLE_FLAG)
                ).not.toBe(true);
            });
        }
    );

    test.describe(
        "Company not in beta group",
        { tag: SJConfig.getNonBetaTag() },
        () => {
            test("Beta features disabled by default for non-beta company", async ({
                rootSFTP,
            }) => {
                // Verify no override flags exist
                expect(
                    await rootSFTP.exists(SJConfig.BETA_DISABLE_FLAG)
                ).not.toBe(true);
                expect(
                    await rootSFTP.exists(SJConfig.BETA_ENABLE_FLAG)
                ).not.toBe(true);
            });

            test("API can enable beta features for non-beta company", async ({
                rootSFTP,
            }) => {
                // Enable beta features via API
                await commonWhmapi.enableSitejetBeta();

                // Verify enable flag is created
                expect(
                    (await rootSFTP.stat(SJConfig.BETA_ENABLE_FLAG)).isFile
                ).toBe(true);
            });

            test("API reset removes local overrides for non-beta company", async ({
                rootSFTP,
            }) => {
                // First enable to create an override
                await commonWhmapi.enableSitejetBeta();
                expect(
                    (await rootSFTP.stat(SJConfig.BETA_ENABLE_FLAG)).isFile
                ).toBe(true);

                // Reset to WebPros rules
                await commonWhmapi.resetSitejetBeta();

                // Verify both flags are removed
                expect(
                    await rootSFTP.exists(SJConfig.BETA_DISABLE_FLAG)
                ).not.toBe(true);
                expect(
                    await rootSFTP.exists(SJConfig.BETA_ENABLE_FLAG)
                ).not.toBe(true);
            });
        }
    );
});
