import { createTestingPinia } from "@pinia/testing";
import { getLocalVue } from "@tests/vitest/helpers";
import { shallowMount } from "@vue/test-utils";
import { setActivePinia } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";

import { setMockConfig } from "@/composables/__mocks__/config";

import Login from "./Login.vue";

const localVue = getLocalVue(true);

vi.mock("@/app/index", () => ({
    getGalaxyInstance: vi.fn(() => ({ session_csrf_token: "session_csrf_token" })),
}));

vi.mock("@/composables/config");

beforeEach(() => {
    setMockConfig({
        allow_local_account_creation: true,
        enable_oidc: true,
        mailing_join_addr: "mailing_join_addr",
        prefer_oidc_login: true,
        registration_warning_message: "registration_warning_message",
        server_mail_configured: true,
        show_welcome_with_login: true,
        terms_url: "terms_url",
        welcome_url: "welcome_url",
    });
});

const mockRouter = (query: object) => ({
    currentRoute: {
        query,
    },
});

function shallowMountLogin(routerQuery: object = {}) {
    const pinia = createTestingPinia({ createSpy: vi.fn });
    setActivePinia(pinia);

    return shallowMount(Login as object, {
        localVue,
        pinia,
        mocks: {
            $router: mockRouter(routerQuery),
        },
    });
}

describe("Login", () => {
    it("login index attribute matching", async () => {
        const wrapper = shallowMountLogin({
            redirect: "redirect_url",
        });

        const attributes = wrapper.find("#login-index").attributes();

        expect(attributes.allowusercreation).toBe("true");
        expect(attributes.enableoidc).toBe("true");
        expect(attributes.redirect).toBe("redirect_url");
        expect(attributes.registrationwarningmessage).toBe("registration_warning_message");
        expect(attributes.sessioncsrftoken).toBe("session_csrf_token");
        expect(attributes.showwelcomewithlogin).toBe("true");
        expect(attributes.termsurl).toBe("terms_url");
        expect(attributes.welcomeurl).toBe("welcome_url");
    });

    it("change password attribute matching", async () => {
        const wrapper = shallowMountLogin({
            token: "test_token",
            status: "test_status",
            message: "test_message",
            expired_user: "test_user",
        });

        const attributes = wrapper.find("#change-password").attributes();
        expect(attributes.token).toBe("test_token");
        expect(attributes.expireduser).toBe("test_user");
        expect(attributes.messagetext).toBe("test_message");
        expect(attributes.messagevariant).toBe("test_status");
    });
});
