-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathextensions.test.ts
More file actions
131 lines (114 loc) · 3.86 KB
/
extensions.test.ts
File metadata and controls
131 lines (114 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import path from 'node:path';
import {afterEach, describe, it} from 'node:test';
import sinon from 'sinon';
import type {McpResponse} from '../../src/McpResponse.js';
import {
installExtension,
uninstallExtension,
listExtensions,
reloadExtension,
} from '../../src/tools/extensions.js';
import {withMcpContext} from '../utils.js';
const EXTENSION_PATH = path.join(
import.meta.dirname,
'../../../tests/tools/fixtures/extension',
);
function extractId(response: McpResponse) {
const responseLine = response.responseLines[0];
assert.ok(responseLine, 'Response should not be empty');
const match = responseLine.match(/Extension installed\. Id: (.+)/);
const extensionId = match ? match[1] : null;
assert.ok(extensionId, 'Response should contain a valid key');
return extensionId;
}
describe('extension', () => {
afterEach(() => {
sinon.restore();
});
it('installs and uninstalls an extension and verifies it in chrome://extensions', async () => {
await withMcpContext(async (response, context) => {
// Install the extension
await installExtension.handler(
{params: {path: EXTENSION_PATH}},
response,
context,
);
const extensionId = extractId(response);
const page = context.getSelectedPage();
await page.goto('chrome://extensions');
const element = await page.waitForSelector(
`extensions-manager >>> extensions-item[id="${extensionId}"]`,
);
assert.ok(
element,
`Extension with ID "${extensionId}" should be visible on chrome://extensions`,
);
// Uninstall the extension
await uninstallExtension.handler(
{params: {id: extensionId!}},
response,
context,
);
const uninstallResponseLine = response.responseLines[1];
assert.ok(
uninstallResponseLine.includes('Extension uninstalled'),
'Response should indicate uninstallation',
);
await page.waitForSelector('extensions-manager');
const elementAfterUninstall = await page.$(
`extensions-manager >>> extensions-item[id="${extensionId}"]`,
);
assert.strictEqual(
elementAfterUninstall,
null,
`Extension with ID "${extensionId}" should NOT be visible on chrome://extensions`,
);
});
});
it('lists installed extensions', async () => {
await withMcpContext(async (response, context) => {
const setListExtensionsSpy = sinon.spy(response, 'setListExtensions');
await listExtensions.handler({params: {}}, response, context);
assert.ok(
setListExtensionsSpy.calledOnce,
'setListExtensions should be called',
);
});
});
it('reloads an extension', async () => {
await withMcpContext(async (response, context) => {
await installExtension.handler(
{params: {path: EXTENSION_PATH}},
response,
context,
);
const extensionId = extractId(response);
const installSpy = sinon.spy(context, 'installExtension');
response.resetResponseLineForTesting();
await reloadExtension.handler(
{params: {id: extensionId!}},
response,
context,
);
assert.ok(
installSpy.calledOnceWithExactly(EXTENSION_PATH),
'installExtension should be called with the extension path',
);
const reloadResponseLine = response.responseLines[0];
assert.ok(
reloadResponseLine.includes('Extension reloaded'),
'Response should indicate reload',
);
const list = context.listExtensions();
assert.ok(list.length === 1, 'List should have only one extension');
const reinstalled = list.find(e => e.id === extensionId);
assert.ok(reinstalled, 'Extension should be present after reload');
});
});
});