/*
 *                                      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/base";

const uuidRegex = /^\w{8}(?:-\w{4}){3}-\w{12}$/;

test("Plugins::get_uuid creates a UUID but does not replace", async ({
    testCpanelAccount,
    uapiResponseTemplate,
    rootSSH,
    rootSFTP,
}) => {
    const pluginsUUIDFile =
        (await testCpanelAccount.getHomeDirectory()) + "/.cpanel/plugin_uuid";

    expect(
        await rootSSH.fileExists(pluginsUUIDFile),
        "The plugin_uuid file does NOT exist by default"
    ).toBeFalsy();

    let result = await testCpanelAccount.uapi.call("Plugins", "get_uuid");
    expect(result.raw, "Plugins::get_uuid returns a uuid and success").toEqual(
        uapiResponseTemplate({
            data: { uuid: expect.stringMatching(uuidRegex) },
        })
    );
    expect(
        await rootSSH.fileExists(pluginsUUIDFile),
        "The plugin_uuid file was created"
    ).toBeTruthy();
    const firstUUID = (await rootSFTP.get(pluginsUUIDFile)).toString();
    expect(
        firstUUID,
        "The uuid in returned by get_uuid matches the plugins_uuid file"
    ).toBe(result.data.uuid);

    result = await testCpanelAccount.uapi.call("Plugins", "get_uuid");
    expect(
        result.raw,
        "Plugins::get_uuid returns the same uuid when called a second time"
    ).toEqual(
        uapiResponseTemplate({
            data: { uuid: expect.stringMatching(firstUUID) },
        })
    );
    expect(
        (await rootSFTP.get(pluginsUUIDFile)).toString(),
        "The uuid returned by get_uuid still matches the plugins_uuid file"
    ).toBe(result.data.uuid);
});
