/*
 * -------------------------------------------------------------------------
 * This file is part of the MindStudio project.
 * Copyright (c) 2025 Huawei Technologies Co.,Ltd.
 *
 * MindStudio is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *          http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * -------------------------------------------------------------------------
 */
import { expect, test as baseTest, WebSocket } from '@playwright/test';
import { FrameworkPage, SourcePage } from '@/page-object';
import { clearAllData, importData, setupWebSocketListener } from '@/utils';
import { FilePath } from '@/utils/constants';
import { SelectHelpers } from '@/components';

interface TestFixtures {
    sourcePage: SourcePage;
    ws: Promise<WebSocket>;
}

const test = baseTest.extend<TestFixtures>({
    sourcePage: async ({ page }, use) => {
        const sourcePage = new SourcePage(page);
        await use(sourcePage);
    },
    ws: async ({ page }, use) => {
        const ws = setupWebSocketListener(page);
        await use(ws);
    },
});

const imgMap = {
    loadDataSuccess: 'loadDataSuccess.png',
    coreSelectChangeSuccess: 'coreSelectChangeSuccess.png',
    codeTableInteractInsructionTableSuccess: 'CodeTable_interact_InstructionTable.png',
    insructionTableInteractCodeTableSuccess: 'InstructionTable_interact_CodeTable.png',
    switchChineseLanguageSuccess: 'Chinese_Language.png',
};
const inputMap = {
    secondFileName: '/home/wangyunkai/code/samples/operator/ascendc/0_introduction/13_matmulleakyrelu_kernellaunch/MatmulLeakyReluInvocationAsync/matmul_leakyrelu_custom.cpp',
};
const resMap = {
    secondFileContens:'#include "kernel_operator.h"',
};

test.describe('Source', () => {
    test.beforeEach(async ({ page, sourcePage, ws }, testInfo) => {
        await page.goto('/');
        const filePath = FilePath.OP_SIMULATOR_BIN;
        await importData(page, filePath);
        await sourcePage.goto();
        await page.mouse.move(0, 0);
        const coreValue = sourcePage.sourceFrame.getByText('core0');
        await expect(coreValue).toBeVisible();
    });

    test.afterEach(async ({ page, ws }) => {
        await clearAllData(page, ws);
    });

    // 源码数据导入成功
    // 预期:界面正确
    test('test_source_loadDataSuccess', async ({ page, sourcePage }) => {
        const { mainContent } = sourcePage;
        await page.mouse.move(0,0);
        await expect(mainContent).toHaveScreenshot(imgMap.loadDataSuccess);
    });

    // 筛选条件变化,修改Core选项
    // 预期:
    // 1、左侧代码表 Instructions Executed、Cycles列变化
    // 2、右侧指令表 Instructions Executed、Cycles列变化
    test('test_source_coreSelectChange', async ({ page, sourcePage }) => {
        const { sourceFrame, coreSelector, mainContent } = sourcePage;
        const coreSelect = new SelectHelpers(page, coreSelector, sourceFrame);
        await coreSelect.open();
        await coreSelect.selectOption('core0.veccore1');
        await expect(mainContent).toHaveScreenshot(imgMap.coreSelectChangeSuccess);
    });

    // 筛选条件变化,修改Source选项
    // 预期:代码表的源代码Source变化
    test('test_source_sourceSelectChange', async ({ page, sourcePage }) => {
        await sourcePage.goto();
        const { sourceFrame, sourceSelector } = sourcePage;
        const coreSelect = new SelectHelpers(page, sourceSelector, sourceFrame);
        await coreSelect.open();
        await coreSelect.selectOption(inputMap.secondFileName);
        const codeTable = sourceFrame.locator('#CodeTable');
        await expect(codeTable).toContainText(resMap.secondFileContens);
    });

    // 点击代码行,联动指令表
    // 点击左侧代码行(有Instructions Executed、Cycles数据的)
    // 预期:
    // 1、代码行高亮
    // 2、右侧指令表高亮相关指令行,并滚动到高亮行
    test('test_source_CodeTable_interact_InstructionTable', async ({ sourcePage }) => {
        const { mainContent } = sourcePage;
        const codeAttrTable = mainContent.locator('#CodeAttrTable');
        // 代码表中有指令数据的行
        const td = codeAttrTable.locator('td', { hasText: '16' }).first();
        await td.click();
        await expect(codeAttrTable.locator('tr.selected').first()).toBeVisible();
        await sourcePage.mouseOut();
        await expect(mainContent).toHaveScreenshot(imgMap.codeTableInteractInsructionTableSuccess);
    });

    // 点击指令表,联动代码行
    // 点击右侧指令表(不是每个都有关联代码行)
    // 预期:
    // 1、左侧代码表高亮关联代码行(只有一行)
    // 2、此行代码关联的其它指令行高亮
    test('test_source_InstructionTable_interact_CodeTable', async ({ sourcePage }) => {
        const { mainContent } = sourcePage;
        const instructionTable = mainContent.locator('#Instructions');
        // 代码表中有关联代码的指令行
        const tr = instructionTable.locator('tr', { hasText: '1' }).first();
        await tr.click();
        await expect(instructionTable.locator('tr.selected').first()).toBeVisible();
        await sourcePage.mouseOut();
        await expect(mainContent).toHaveScreenshot(imgMap.insructionTableInteractCodeTableSuccess);
    });

    // 切换语言,查看中文界面
    test('test_source_Chinese_Language', async ({ page, sourcePage }) => {
        const { mainContent } = sourcePage;
        const frameworkPage = new FrameworkPage(page);
        await frameworkPage.switchLanguageBtn.click();
        await sourcePage.mouseOut();
        await expect(mainContent).toHaveScreenshot(imgMap.switchChineseLanguageSuccess);
        await frameworkPage.switchLanguageBtn.click();
    });
});