|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import assert from 'node:assert'; |
8 | | -import {describe, it} from 'node:test'; |
| 8 | +import fs from 'node:fs'; |
| 9 | +import os from 'node:os'; |
| 10 | +import path from 'node:path'; |
| 11 | +import { describe, it, after } from 'node:test'; |
9 | 12 |
|
10 | 13 | import {installExtension} from '../../src/tools/extensions.js'; |
11 | 14 | import {withMcpContext} from '../utils.js'; |
12 | 15 |
|
13 | | -const EXTENSION_PATH = '/usr/local/google/home/nharshunova/test/extensions'; |
14 | | -const EXTENSION_ID = 'emhhlofcjnaambdnpppkpbcimdeaccnn'; |
15 | | - |
16 | 16 | describe('extension', () => { |
| 17 | + let tmpDir: string; |
| 18 | + |
17 | 19 | it('installs an extension and verifies it is listed in chrome://extensions', async () => { |
| 20 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'extension-test-')); |
| 21 | + fs.writeFileSync( |
| 22 | + path.join(tmpDir, 'manifest.json'), |
| 23 | + JSON.stringify({ |
| 24 | + manifest_version: 3, |
| 25 | + name: 'Test Extension', |
| 26 | + version: '1.0', |
| 27 | + action: { |
| 28 | + default_popup: 'popup.html', |
| 29 | + }, |
| 30 | + }), |
| 31 | + ); |
| 32 | + fs.writeFileSync( |
| 33 | + path.join(tmpDir, 'popup.html'), |
| 34 | + '<!DOCTYPE html><html><body><h1>Test Popup</h1></body></html>', |
| 35 | + ); |
| 36 | + |
18 | 37 | await withMcpContext(async (response, context) => { |
19 | | - // 1. Install the extension |
20 | 38 | await installExtension.handler( |
21 | | - {params: {path: EXTENSION_PATH}}, |
| 39 | + { params: { path: tmpDir } }, |
22 | 40 | response, |
23 | 41 | context, |
24 | 42 | ); |
25 | 43 |
|
26 | | - // 2. Verify response |
27 | | - assert.ok( |
28 | | - response.responseLines[0]?.includes(EXTENSION_ID), |
29 | | - `Response should include extension ID ${EXTENSION_ID}`, |
30 | | - ); |
| 44 | + const responseLine = response.responseLines[0]; |
| 45 | + assert.ok(responseLine, 'Response should not be empty'); |
| 46 | + const match = responseLine.match(/Extension installed\. Id: (.+)/); |
| 47 | + const extensionId = match ? match[1] : null; |
| 48 | + assert.ok(extensionId, 'Response should contain a valid key'); |
31 | 49 |
|
32 | | - // 3. Verify extension is accessible by navigating to its popup |
33 | 50 | const page = context.getSelectedPage(); |
34 | | - await page.goto(`chrome-extension://${EXTENSION_ID}/popup.html`); |
35 | | - const popupContent = await page.content(); |
36 | | - assert.ok( |
37 | | - popupContent.includes('Popup Action'), |
38 | | - 'Extension popup should be accessible', |
39 | | - ); |
40 | | - |
41 | | - // 4. Verify extension presence in chrome://extensions UI |
42 | | - // 4. Verify extension presence in chrome://extensions UI |
43 | 51 | await page.goto('chrome://extensions'); |
44 | 52 |
|
45 | | - // Wait for usage to ensure page is loaded |
46 | | - await new Promise(r => setTimeout(r, 2000)); |
47 | | - |
48 | | - const EXTENSION_NAME = 'Simple Popup Action'; |
49 | | - const found = await page.evaluate(extName => { |
50 | | - function deepSearch(root: Element | ShadowRoot): boolean { |
51 | | - if (root.textContent?.includes(extName)) return true; |
52 | | - if ('shadowRoot' in root && root.shadowRoot) { |
53 | | - if (deepSearch(root.shadowRoot)) return true; |
54 | | - } |
55 | | - for (const child of Array.from(root.children)) { |
56 | | - if (deepSearch(child)) return true; |
57 | | - } |
58 | | - return false; |
59 | | - } |
60 | | - return deepSearch(document.body); |
61 | | - }, EXTENSION_NAME); |
62 | | - |
| 53 | + const element = await page.waitForSelector( |
| 54 | + `extensions-manager >>> extensions-item[id="${extensionId}"]`, |
| 55 | + ); |
63 | 56 | assert.ok( |
64 | | - found, |
65 | | - `Extension "${EXTENSION_NAME}" should be visible on chrome://extensions`, |
| 57 | + element, |
| 58 | + `Extension with ID "${extensionId}" should be visible on chrome://extensions`, |
66 | 59 | ); |
67 | 60 | }); |
68 | 61 | }); |
| 62 | + |
| 63 | + after(() => { |
| 64 | + if (tmpDir) { |
| 65 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 66 | + } |
| 67 | + }); |
69 | 68 | }); |
0 commit comments