|
| 1 | +import { |
| 2 | + mkdirSync, |
| 3 | + mkdtempSync, |
| 4 | + realpathSync, |
| 5 | + rmSync, |
| 6 | + writeFileSync, |
| 7 | +} from 'node:fs' |
| 8 | +import { tmpdir } from 'node:os' |
| 9 | +import { join } from 'node:path' |
| 10 | + |
| 11 | +import type { Page } from '@playwright/test' |
| 12 | + |
| 13 | +import type { Settings } from '@/shared/settings' |
| 14 | + |
| 15 | +import { test, expect } from '../fixtures/electron-app' |
| 16 | +import { readSettingsFile, writeSettingsFile } from '../helpers/settings-file' |
| 17 | + |
| 18 | +const SEARCH_COUNT_SKILLS = [ |
| 19 | + { name: 'alpha-count-e2e', source: 'laststance/skills' }, |
| 20 | + { name: 'beta-count-e2e', source: 'laststance/skills' }, |
| 21 | + { name: 'gamma-count-e2e', source: 'pbakaus/impeccable' }, |
| 22 | +] |
| 23 | + |
| 24 | +type SearchCountDisplaySetting = Settings['installedSearchCountDisplay'] |
| 25 | +type IsolatedHomeUse = (home: string) => Promise<void> |
| 26 | + |
| 27 | +/** |
| 28 | + * Write one source skill folder that the real scanner will count after app launch. |
| 29 | + * @param home - Isolated E2E HOME used by the Electron fixture. |
| 30 | + * @param skillName - Folder name and SKILL.md title for the staged skill. |
| 31 | + * @returns void after the source skill exists on disk. |
| 32 | + * @example |
| 33 | + * stageSourceSkill('/tmp/home', 'alpha-count-e2e') |
| 34 | + */ |
| 35 | +function stageSourceSkill(home: string, skillName: string): void { |
| 36 | + const sourcePath = join(home, '.agents', 'skills', skillName) |
| 37 | + mkdirSync(sourcePath, { recursive: true }) |
| 38 | + writeFileSync( |
| 39 | + join(sourcePath, 'SKILL.md'), |
| 40 | + `---\nname: ${skillName}\ndescription: ${skillName} description\n---\n# ${skillName}\n`, |
| 41 | + 'utf8', |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Write the skills CLI lockfile so the real scanner exposes deterministic repo facets. |
| 47 | + * @param home - Isolated E2E HOME used by the Electron fixture. |
| 48 | + * @returns void after `.skill-lock.json` maps each staged skill to its repo. |
| 49 | + * @example |
| 50 | + * stageSkillLock('/tmp/home') |
| 51 | + */ |
| 52 | +function stageSkillLock(home: string): void { |
| 53 | + const lockPath = join(home, '.agents', '.skill-lock.json') |
| 54 | + const skills = Object.fromEntries( |
| 55 | + SEARCH_COUNT_SKILLS.map((skill) => [ |
| 56 | + skill.name, |
| 57 | + { |
| 58 | + source: skill.source, |
| 59 | + sourceType: 'github', |
| 60 | + sourceUrl: `https://github.com/${skill.source}.git`, |
| 61 | + }, |
| 62 | + ]), |
| 63 | + ) |
| 64 | + |
| 65 | + writeFileSync(lockPath, JSON.stringify({ skills }, null, 2), 'utf8') |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Stage the complete Installed-count HOME before Electron starts scanning. |
| 70 | + * @param home - Isolated E2E HOME used by the Electron fixture. |
| 71 | + * @returns void after source skills and repo metadata are on disk. |
| 72 | + * @example |
| 73 | + * stageInstalledCountHome('/tmp/home') |
| 74 | + */ |
| 75 | +function stageInstalledCountHome(home: string): void { |
| 76 | + mkdirSync(join(home, '.agents', 'skills'), { recursive: true }) |
| 77 | + for (const skill of SEARCH_COUNT_SKILLS) { |
| 78 | + stageSourceSkill(home, skill.name) |
| 79 | + } |
| 80 | + stageSkillLock(home) |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Provide an isolated HOME with only the three Installed-count skills staged. |
| 85 | + * @param use - Playwright fixture continuation that launches Electron after setup. |
| 86 | + * @param display - Optional persisted count placement to write before launch. |
| 87 | + * @returns Promise that resolves after the fixture HOME is cleaned up. |
| 88 | + * @example |
| 89 | + * await useInstalledCountHome(use, 'inline') |
| 90 | + */ |
| 91 | +async function useInstalledCountHome( |
| 92 | + use: IsolatedHomeUse, |
| 93 | + display?: SearchCountDisplaySetting, |
| 94 | +): Promise<void> { |
| 95 | + const home = realpathSync.native( |
| 96 | + mkdtempSync(join(tmpdir(), 'skills-desktop-e2e-search-count-')), |
| 97 | + ) |
| 98 | + try { |
| 99 | + stageInstalledCountHome(home) |
| 100 | + if (display) { |
| 101 | + writeSettingsFile(home, { installedSearchCountDisplay: display }) |
| 102 | + } |
| 103 | + await use(home) |
| 104 | + } finally { |
| 105 | + rmSync(home, { recursive: true, force: true }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Dispatch Installed search and repo filters in one renderer transaction. |
| 111 | + * @param page - Electron renderer page under test. |
| 112 | + * @param filters - Search query and selected repository ids to apply. |
| 113 | + * @returns Promise that resolves once Redux has the requested filter state. |
| 114 | + * @example |
| 115 | + * await applyInstalledFilters(appWindow, { query: 'missing', sources: [] }) |
| 116 | + */ |
| 117 | +async function applyInstalledFilters( |
| 118 | + page: Page, |
| 119 | + filters: { query: string; sources: string[] }, |
| 120 | +): Promise<void> { |
| 121 | + await page.evaluate(({ query, sources }) => { |
| 122 | + const store = window.__store__ |
| 123 | + if (!store) throw new Error('window.__store__ is not exposed') |
| 124 | + store.dispatch({ |
| 125 | + type: 'ui/setSearchQuery', |
| 126 | + payload: query, |
| 127 | + }) |
| 128 | + store.dispatch({ |
| 129 | + type: 'ui/setSelectedSources', |
| 130 | + payload: sources, |
| 131 | + }) |
| 132 | + }, filters) |
| 133 | +} |
| 134 | + |
| 135 | +const installedCountTest = test.extend<{ isolatedHome: string }>({ |
| 136 | + // eslint-disable-next-line no-empty-pattern |
| 137 | + isolatedHome: async ({}, use) => { |
| 138 | + await useInstalledCountHome(use) |
| 139 | + }, |
| 140 | +}) |
| 141 | + |
| 142 | +const inlineInstalledCountTest = test.extend<{ isolatedHome: string }>({ |
| 143 | + // eslint-disable-next-line no-empty-pattern |
| 144 | + isolatedHome: async ({}, use) => { |
| 145 | + await useInstalledCountHome(use, 'inline') |
| 146 | + }, |
| 147 | +}) |
| 148 | + |
| 149 | +installedCountTest( |
| 150 | + 'Installed tab badge tracks the current visible count and Marketplace stays count-free', |
| 151 | + async ({ appWindow }) => { |
| 152 | + // Arrange / Assert |
| 153 | + await expect( |
| 154 | + appWindow.getByRole('tab', { |
| 155 | + name: /^Installed, 3 skills visible$/, |
| 156 | + }), |
| 157 | + ).toBeVisible() |
| 158 | + await expect( |
| 159 | + appWindow.getByRole('tab', { name: /^Marketplace$/ }), |
| 160 | + ).toBeVisible() |
| 161 | + |
| 162 | + // Act |
| 163 | + await applyInstalledFilters(appWindow, { |
| 164 | + query: 'alpha-count', |
| 165 | + sources: [], |
| 166 | + }) |
| 167 | + |
| 168 | + // Assert |
| 169 | + await expect( |
| 170 | + appWindow.getByRole('tab', { |
| 171 | + name: /^Installed, 1 skill visible$/, |
| 172 | + }), |
| 173 | + ).toBeVisible() |
| 174 | + |
| 175 | + // Act |
| 176 | + await applyInstalledFilters(appWindow, { |
| 177 | + query: '', |
| 178 | + sources: ['pbakaus/impeccable'], |
| 179 | + }) |
| 180 | + |
| 181 | + // Assert |
| 182 | + await expect( |
| 183 | + appWindow.getByRole('tab', { |
| 184 | + name: /^Installed, 1 skill visible$/, |
| 185 | + }), |
| 186 | + ).toBeVisible() |
| 187 | + |
| 188 | + // Act |
| 189 | + await applyInstalledFilters(appWindow, { |
| 190 | + query: 'missing-count-e2e', |
| 191 | + sources: [], |
| 192 | + }) |
| 193 | + |
| 194 | + // Assert |
| 195 | + await expect( |
| 196 | + appWindow.getByRole('tab', { |
| 197 | + name: /^Installed, 0 skills visible$/, |
| 198 | + }), |
| 199 | + ).toBeVisible() |
| 200 | + await expect( |
| 201 | + appWindow.getByRole('tab', { name: /^Marketplace$/ }), |
| 202 | + ).toBeVisible() |
| 203 | + }, |
| 204 | +) |
| 205 | + |
| 206 | +inlineInstalledCountTest( |
| 207 | + 'persisted inline mode moves the count into the toolbar and removes the tab badge', |
| 208 | + async ({ appWindow, isolatedHome }) => { |
| 209 | + // Arrange / Assert |
| 210 | + await appWindow.waitForFunction(() => { |
| 211 | + const store = window.__store__ |
| 212 | + if (!store) return false |
| 213 | + const state = store.getState() as { |
| 214 | + settings?: { installedSearchCountDisplay?: string } |
| 215 | + } |
| 216 | + return state.settings?.installedSearchCountDisplay === 'inline' |
| 217 | + }) |
| 218 | + await expect( |
| 219 | + appWindow.getByRole('tab', { name: /^Installed$/ }), |
| 220 | + ).toBeVisible() |
| 221 | + await expect( |
| 222 | + appWindow.getByRole('tab', { |
| 223 | + name: /^Installed, 3 skills visible$/, |
| 224 | + }), |
| 225 | + ).toHaveCount(0) |
| 226 | + await expect( |
| 227 | + appWindow |
| 228 | + .locator('[aria-live="polite"]') |
| 229 | + .filter({ hasText: /^3 skills$/ }), |
| 230 | + ).toBeVisible() |
| 231 | + |
| 232 | + const persisted = readSettingsFile(isolatedHome) as { |
| 233 | + installedSearchCountDisplay?: string |
| 234 | + } | null |
| 235 | + expect(persisted?.installedSearchCountDisplay).toBe('inline') |
| 236 | + }, |
| 237 | +) |
0 commit comments