File size: 12,091 Bytes
f0634fb | 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | import { execFileSync } from 'node:child_process';
import { createServer } from 'node:http';
import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { PluginManager } from '#/app/plugin/manager';
describe('PluginManager', () => {
let home: string;
let root: string;
beforeEach(async () => {
home = await mkdtemp(join(tmpdir(), 'plugin-manager-home-'));
root = await mkdtemp(join(tmpdir(), 'plugin-manager-root-'));
await mkdir(join(home, 'plugins'), { recursive: true });
await mkdir(join(root, 'commands'), { recursive: true });
await writeFile(join(root, 'commands', 'deploy.md'), '---\ndescription: Deploy\n---\n\nBody', 'utf8');
await writeFile(
join(root, 'kimi.plugin.json'),
JSON.stringify({
name: 'demo',
commands: ['./commands'],
hooks: [{ event: 'Stop', command: 'echo stop' }],
}),
'utf8',
);
await writeFile(
join(home, 'plugins', 'installed.json'),
JSON.stringify({
version: 1,
plugins: [
{
id: 'demo',
root,
source: 'local-path',
enabled: true,
installedAt: '2026-01-01T00:00:00.000Z',
},
],
}),
'utf8',
);
});
afterEach(async () => {
vi.unstubAllGlobals();
await rm(home, { recursive: true, force: true });
await rm(root, { recursive: true, force: true });
});
it('loads installed plugins and exposes summaries, hooks, and commands', async () => {
const manager = new PluginManager({ kimiHomeDir: home });
await manager.load();
expect(manager.summaries()).toEqual([
expect.objectContaining({
id: 'demo',
state: 'ok',
commandCount: 1,
hookCount: 1,
}),
]);
expect(manager.enabledHooks()).toEqual([
{
event: 'Stop',
command: 'echo stop',
cwd: root,
env: { KIMI_CODE_HOME: home, KIMI_PLUGIN_ROOT: root },
},
]);
await expect(manager.enabledCommands()).resolves.toEqual([
expect.objectContaining({ pluginId: 'demo', name: 'deploy', description: 'Deploy' }),
]);
});
it('installs a local-path plugin into the managed root', async () => {
const sourceRoot = await mkdtemp(join(tmpdir(), 'plugin-install-source-'));
try {
await writeFile(join(sourceRoot, 'kimi.plugin.json'), JSON.stringify({ name: 'other' }), 'utf8');
const manager = new PluginManager({ kimiHomeDir: home });
const record = await manager.install(sourceRoot);
expect(record.id).toBe('other');
expect(record.root).toContain(join(home, 'plugins', 'managed', 'other'));
expect(manager.get('other')?.manifest?.name).toBe('other');
} finally {
await rm(sourceRoot, { recursive: true, force: true });
}
});
it('installs a zip-url plugin', async () => {
const sourceRoot = await mkdtemp(join(tmpdir(), 'plugin-zip-source-'));
const zipPath = join(tmpdir(), `plugin-${Date.now()}.zip`);
const server = createServer((_req, res) => {
void readFile(zipPath).then((data) => res.end(data));
});
try {
await writeFile(join(sourceRoot, 'kimi.plugin.json'), JSON.stringify({ name: 'zip-plugin' }), 'utf8');
execFileSync('zip', ['-qr', zipPath, '.'], { cwd: sourceRoot });
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
const address = server.address();
if (address === null || typeof address === 'string') throw new Error('bad server address');
const manager = new PluginManager({ kimiHomeDir: home });
const record = await manager.install(`http://127.0.0.1:${address.port}/plugin.zip`);
expect(record.id).toBe('zip-plugin');
expect(record.source).toBe('zip-url');
expect(manager.get('zip-plugin')?.manifest?.name).toBe('zip-plugin');
} finally {
await new Promise<void>((resolve, reject) => server.close((err) => (err === undefined ? resolve() : reject(err))));
await rm(sourceRoot, { recursive: true, force: true });
await rm(zipPath, { force: true });
}
});
it('installs a github plugin through codeload', async () => {
const sourceRoot = await mkdtemp(join(tmpdir(), 'plugin-github-source-'));
const zipPath = join(tmpdir(), `plugin-github-${Date.now()}.zip`);
try {
await writeFile(join(sourceRoot, 'kimi.plugin.json'), JSON.stringify({ name: 'github-plugin' }), 'utf8');
execFileSync('zip', ['-qr', zipPath, '.'], { cwd: sourceRoot });
const zip = await readFile(zipPath);
const fetchMock = vi.fn(async (input: Parameters<typeof fetch>[0]) => {
const url =
typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
if (url.endsWith('/commits/v1.atom')) {
return new Response(
'<entry><id>tag:github.com,2008:Grit::Commit/1111111111111111111111111111111111111111</id></entry>',
);
}
return new Response(zip);
});
vi.stubGlobal('fetch', fetchMock as typeof fetch);
const manager = new PluginManager({ kimiHomeDir: home });
const record = await manager.install('https://github.com/owner/repo/tree/v1');
expect(record.id).toBe('github-plugin');
expect(record.source).toBe('github');
expect(record.github).toEqual({
owner: 'owner',
repo: 'repo',
ref: { kind: 'branch', value: 'v1' },
installedSha: '1111111111111111111111111111111111111111',
});
expect(fetchMock).toHaveBeenCalledWith(
'https://codeload.github.com/owner/repo/zip/1111111111111111111111111111111111111111',
expect.objectContaining({ signal: expect.any(AbortSignal) }),
);
const stored = JSON.parse(
await readFile(join(home, 'plugins', 'installed.json'), 'utf8'),
) as { plugins: Array<{ id: string; github?: { installedSha?: string } }> };
expect(stored.plugins.find((plugin) => plugin.id === 'github-plugin')?.github?.installedSha)
.toBe('1111111111111111111111111111111111111111');
expect(manager.get('github-plugin')?.manifest?.name).toBe('github-plugin');
} finally {
await rm(sourceRoot, { recursive: true, force: true });
await rm(zipPath, { force: true });
}
});
it('checks github plugin updates against latest release', async () => {
await writeFile(
join(home, 'plugins', 'installed.json'),
JSON.stringify({
version: 1,
plugins: [
{
id: 'demo',
root,
source: 'github',
enabled: true,
installedAt: '2026-01-01T00:00:00.000Z',
originalSource: 'https://github.com/owner/repo',
github: { owner: 'owner', repo: 'repo', ref: { kind: 'branch', value: 'v1' } },
},
],
}),
'utf8',
);
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
status: 302,
ok: false,
headers: new Headers({ location: 'https://github.com/owner/repo/releases/tag/v2' }),
}),
);
const manager = new PluginManager({ kimiHomeDir: home });
await manager.load();
await expect(manager.checkUpdates()).resolves.toEqual([
{
id: 'demo',
source: 'github',
current: { kind: 'branch', value: 'v1' },
latest: { kind: 'tag', value: 'v2' },
displayVersion: 'v2',
updateAvailable: true,
},
]);
});
it('reports a pinned branch update only when its commit advances', async () => {
await writeFile(
join(home, 'plugins', 'installed.json'),
JSON.stringify({
version: 1,
plugins: [
{
id: 'demo',
root,
source: 'github',
enabled: true,
installedAt: '2026-01-01T00:00:00.000Z',
originalSource: 'https://github.com/owner/repo/tree/main',
github: {
owner: 'owner',
repo: 'repo',
ref: { kind: 'branch', value: 'main' },
installedSha: '1111111111111111111111111111111111111111',
},
},
],
}),
'utf8',
);
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValueOnce(
new Response(
'<entry><id>tag:github.com,2008:Grit::Commit/1111111111111111111111111111111111111111</id></entry>',
),
)
.mockResolvedValueOnce(
new Response(
'<entry><id>tag:github.com,2008:Grit::Commit/2222222222222222222222222222222222222222</id></entry>',
),
),
);
const manager = new PluginManager({ kimiHomeDir: home });
await manager.load();
await expect(manager.checkUpdates()).resolves.toEqual([
expect.objectContaining({ id: 'demo', updateAvailable: false }),
]);
await expect(manager.checkUpdates()).resolves.toEqual([
expect.objectContaining({
id: 'demo',
current: { kind: 'branch', value: 'main' },
latest: { kind: 'branch', value: 'main' },
updateAvailable: true,
}),
]);
});
it('treats legacy commit metadata without originalSource as pinned', async () => {
const sha = '1111111111111111111111111111111111111111';
await writeFile(
join(home, 'plugins', 'installed.json'),
JSON.stringify({
version: 1,
plugins: [
{
id: 'demo',
root,
source: 'github',
enabled: true,
installedAt: '2026-01-01T00:00:00.000Z',
github: { owner: 'owner', repo: 'repo', ref: { kind: 'sha', value: sha } },
},
],
}),
'utf8',
);
const fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);
const manager = new PluginManager({ kimiHomeDir: home });
await manager.load();
await expect(manager.checkUpdates()).resolves.toEqual([
expect.objectContaining({
id: 'demo',
latest: { kind: 'sha', value: sha },
updateAvailable: false,
}),
]);
expect(fetchMock).not.toHaveBeenCalled();
});
it('keeps successful update results when another repository lookup fails', async () => {
await writeFile(
join(home, 'plugins', 'installed.json'),
JSON.stringify({
version: 1,
plugins: ['good', 'offline'].map((id) => ({
id,
root,
source: 'github',
enabled: true,
installedAt: '2026-01-01T00:00:00.000Z',
github: {
owner: 'owner',
repo: id,
ref: { kind: 'tag', value: 'v1' },
},
})),
}),
'utf8',
);
vi.stubGlobal(
'fetch',
vi.fn(async (input: Parameters<typeof fetch>[0]) => {
const url =
typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
if (url.includes('/offline/')) throw new Error('network offline');
return new Response(null, {
status: 302,
headers: { location: 'https://github.com/owner/good/releases/tag/v2' },
});
}) as typeof fetch,
);
const manager = new PluginManager({ kimiHomeDir: home });
await manager.load();
await expect(manager.checkUpdates()).resolves.toEqual([
expect.objectContaining({ id: 'good', updateAvailable: true }),
]);
});
it('persists enabled state changes', async () => {
const manager = new PluginManager({ kimiHomeDir: home });
await manager.load();
await manager.setEnabled('demo', false);
expect(manager.get('demo')?.enabled).toBe(false);
const stored = JSON.parse(await readFile(join(home, 'plugins', 'installed.json'), 'utf8')) as {
plugins: Array<{ id: string; enabled: boolean }>;
};
expect(stored.plugins).toEqual([expect.objectContaining({ id: 'demo', enabled: false })]);
});
});
|