|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { |
| 4 | + ActivityBar, |
| 5 | + CustomTreeSection, |
| 6 | + EditorView, |
| 7 | + SideBarView, |
| 8 | + TreeItem, |
| 9 | + ViewControl, |
| 10 | + VSBrowser, |
| 11 | + Workbench |
| 12 | +} from 'vscode-extension-tester'; |
| 13 | +import { expect } from 'chai'; |
| 14 | + |
| 15 | +const fixtureRoot = path.resolve(__dirname, '../../../test-resources/multi-root'); |
| 16 | +const repoAPath = path.join(fixtureRoot, 'Repo-A'); |
| 17 | +const repoAConfigPath = path.join(repoAPath, '.vscode', 'virtualTab.json'); |
| 18 | +const repoAOriginal = [{ id: 'repo-a-existing', name: 'Repo A Existing', files: [] }]; |
| 19 | +const regressionFileRelativePath = 'src/remove-selected-file-regression.ts'; |
| 20 | +const regressionFileAbsolutePath = path.join(repoAPath, regressionFileRelativePath); |
| 21 | + |
| 22 | +function writeConfig(configPath: string, groups: object[]): void { |
| 23 | + fs.writeFileSync(configPath, `${JSON.stringify(groups, null, 2)}\n`); |
| 24 | +} |
| 25 | + |
| 26 | +function readConfig(configPath: string): Array<{ id?: string; name?: string; files?: string[] }> { |
| 27 | + return JSON.parse(fs.readFileSync(configPath, 'utf8')) as Array<{ id?: string; name?: string; files?: string[] }>; |
| 28 | +} |
| 29 | + |
| 30 | +async function dismissOnboardingOverlay(): Promise<void> { |
| 31 | + const driver = VSBrowser.instance.driver; |
| 32 | + await driver.wait(async () => { |
| 33 | + return await driver.executeScript(` |
| 34 | + const styleId = 'virtual-tabs-e2e-hide-onboarding-remove'; |
| 35 | + if (!document.getElementById(styleId)) { |
| 36 | + const style = document.createElement('style'); |
| 37 | + style.id = styleId; |
| 38 | + style.textContent = '.onboarding-a-overlay { display: none !important; }'; |
| 39 | + document.head.appendChild(style); |
| 40 | + } |
| 41 | + for (const el of document.querySelectorAll('.onboarding-a-overlay, [aria-label="Welcome to Visual Studio Code"][role="dialog"]')) { |
| 42 | + el.remove(); |
| 43 | + } |
| 44 | + return document.querySelectorAll('.onboarding-a-overlay.visible').length === 0; |
| 45 | + `) as boolean; |
| 46 | + }, 5_000, 'Onboarding overlay did not disappear'); |
| 47 | +} |
| 48 | + |
| 49 | +async function getVisibleTreeLabels(): Promise<string[]> { |
| 50 | + const driver = VSBrowser.instance.driver; |
| 51 | + return await driver.executeScript(` |
| 52 | + return Array.from(document.querySelectorAll('.monaco-list-row')) |
| 53 | + .map(row => row.textContent ? row.textContent.trim().replace(/\\s+/g, ' ') : '') |
| 54 | + .filter(Boolean); |
| 55 | + `) as string[]; |
| 56 | +} |
| 57 | + |
| 58 | +async function waitForTreeLabel(label: string, timeoutMs = 15_000): Promise<void> { |
| 59 | + await VSBrowser.instance.driver.wait(async () => { |
| 60 | + const labels = await getVisibleTreeLabels(); |
| 61 | + return labels.some(t => t.includes(label)); |
| 62 | + }, timeoutMs, `Tree item "${label}" not found`); |
| 63 | +} |
| 64 | + |
| 65 | +async function waitForTreeLabelAbsent(label: string, timeoutMs = 15_000): Promise<void> { |
| 66 | + await VSBrowser.instance.driver.wait(async () => { |
| 67 | + const labels = await getVisibleTreeLabels(); |
| 68 | + return !labels.some(t => t.includes(label)); |
| 69 | + }, timeoutMs, `Tree item "${label}" is still visible`); |
| 70 | +} |
| 71 | + |
| 72 | +async function openVirtualTabsView(): Promise<SideBarView> { |
| 73 | + await dismissOnboardingOverlay(); |
| 74 | + const activityBar = new ActivityBar(); |
| 75 | + const viewControl = (await activityBar.getViewControl('Virtual Tabs')) as ViewControl; |
| 76 | + expect(viewControl, 'Virtual Tabs icon not found in Activity Bar').to.not.be.undefined; |
| 77 | + let sidebar: SideBarView; |
| 78 | + try { |
| 79 | + sidebar = await viewControl.openView() as SideBarView; |
| 80 | + } catch { |
| 81 | + await dismissOnboardingOverlay(); |
| 82 | + await viewControl.getDriver().executeScript('arguments[0].click()', viewControl); |
| 83 | + sidebar = await new SideBarView().wait(); |
| 84 | + } |
| 85 | + |
| 86 | + await VSBrowser.instance.driver.wait(async () => { |
| 87 | + const labels = await getVisibleTreeLabels(); |
| 88 | + return labels.length > 0; |
| 89 | + }, 30_000, 'Virtual Tabs extension did not activate within 30 s'); |
| 90 | + |
| 91 | + return sidebar; |
| 92 | +} |
| 93 | + |
| 94 | +async function getVirtualTabsSection(sidebar: SideBarView): Promise<CustomTreeSection> { |
| 95 | + const content = sidebar.getContent(); |
| 96 | + return await content.getSection<CustomTreeSection>( |
| 97 | + section => section.getTitle().then(title => title.toLowerCase().includes('virtual tabs')), |
| 98 | + CustomTreeSection |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +async function findTreeItem(section: CustomTreeSection, label: string, timeoutMs: number = 10_000): Promise<TreeItem> { |
| 103 | + const driver = VSBrowser.instance.driver; |
| 104 | + await driver.wait(async () => (await section.findItem(label)) !== undefined, timeoutMs, `Tree item "${label}" not found`); |
| 105 | + return await section.findItem(label) as TreeItem; |
| 106 | +} |
| 107 | + |
| 108 | +async function clickRefresh(sidebar: SideBarView): Promise<void> { |
| 109 | + const driver = VSBrowser.instance.driver; |
| 110 | + await driver.wait(async () => { |
| 111 | + try { |
| 112 | + const actions = await sidebar.getTitlePart().getActions(); |
| 113 | + for (const action of actions) { |
| 114 | + const title = await action.getTitle(); |
| 115 | + if (/refresh/i.test(title)) { |
| 116 | + await action.click(); |
| 117 | + return true; |
| 118 | + } |
| 119 | + } |
| 120 | + return false; |
| 121 | + } catch { |
| 122 | + return false; |
| 123 | + } |
| 124 | + }, 10_000, 'Refresh button not found'); |
| 125 | +} |
| 126 | + |
| 127 | +describe('Virtual Tabs - Remove selected files from group', function () { |
| 128 | + this.timeout(90_000); |
| 129 | + |
| 130 | + before(async function () { |
| 131 | + await VSBrowser.instance.waitForWorkbench(); |
| 132 | + await dismissOnboardingOverlay(); |
| 133 | + }); |
| 134 | + |
| 135 | + after(async function () { |
| 136 | + await new EditorView().closeAllEditors(); |
| 137 | + if (fs.existsSync(regressionFileAbsolutePath)) { |
| 138 | + fs.unlinkSync(regressionFileAbsolutePath); |
| 139 | + } |
| 140 | + writeConfig(repoAConfigPath, repoAOriginal); |
| 141 | + }); |
| 142 | + |
| 143 | + it('removes a file from a group when config stores relative file paths after reload', async function () { |
| 144 | + fs.mkdirSync(path.dirname(regressionFileAbsolutePath), { recursive: true }); |
| 145 | + fs.writeFileSync(regressionFileAbsolutePath, 'export const removeSelectedFileRegression = true;\n'); |
| 146 | + |
| 147 | + writeConfig(repoAConfigPath, [ |
| 148 | + { |
| 149 | + id: 'remove-selected-regression', |
| 150 | + name: 'Remove Selected Regression', |
| 151 | + files: [regressionFileRelativePath] |
| 152 | + } |
| 153 | + ]); |
| 154 | + |
| 155 | + const sidebar = await openVirtualTabsView(); |
| 156 | + await clickRefresh(sidebar); |
| 157 | + const section = await getVirtualTabsSection(sidebar); |
| 158 | + const groupItem = await findTreeItem(section, 'Remove Selected Regression'); |
| 159 | + await groupItem.expand(); |
| 160 | + await waitForTreeLabel('remove-selected-file-regression.ts'); |
| 161 | + |
| 162 | + const fileItem = await findTreeItem(section, 'remove-selected-file-regression.ts'); |
| 163 | + await fileItem.select(); |
| 164 | + |
| 165 | + await new Workbench().executeCommand('Remove Selected Files from Group'); |
| 166 | + |
| 167 | + await VSBrowser.instance.driver.wait(() => { |
| 168 | + const groups = readConfig(repoAConfigPath); |
| 169 | + const target = groups.find(group => group.id === 'remove-selected-regression'); |
| 170 | + return !!target && Array.isArray(target.files) && target.files.length === 0; |
| 171 | + }, 15_000, 'Selected file was not removed from virtualTab.json'); |
| 172 | + |
| 173 | + await waitForTreeLabelAbsent('remove-selected-file-regression.ts'); |
| 174 | + }); |
| 175 | +}); |
0 commit comments