File size: 4,473 Bytes
84aa3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
import { ExtensionManager } from './extension-manager.js';
import { debugLogger } from '@google/gemini-cli-core';
import { createTestMergedSettings } from './settings.js';
import { createExtension } from '../test-utils/createExtension.js';
import { EXTENSIONS_DIRECTORY_NAME } from './extensions/variables.js';

const mockHomedir = vi.hoisted(() => vi.fn(() => '/tmp/mock-home'));

vi.mock('node:os', async (importOriginal) => {
  const actual = await importOriginal<typeof import('node:os')>();
  return {
    ...actual,
    homedir: mockHomedir,
  };
});

// Mock @google/gemini-cli-core
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
  const core = await importOriginal<typeof import('@google/gemini-cli-core')>();
  return {
    ...core,
    homedir: mockHomedir,
    loadAgentsFromDirectory: core.loadAgentsFromDirectory,
    loadSkillsFromDir: core.loadSkillsFromDir,
  };
});

describe('ExtensionManager agents loading', () => {
  let extensionManager: ExtensionManager;
  let tempDir: string;
  let extensionsDir: string;

  beforeEach(() => {
    vi.clearAllMocks();
    vi.stubEnv('ANTIGRAVITY_CLI_ALIAS', '');
    vi.spyOn(debugLogger, 'warn').mockImplementation(() => {});

    tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gemini-test-agents-'));
    mockHomedir.mockReturnValue(tempDir);
    vi.stubEnv('GEMINI_CLI_HOME', tempDir);

    // Create the extensions directory that ExtensionManager expects
    extensionsDir = path.join(tempDir, '.gemini', EXTENSIONS_DIRECTORY_NAME);
    fs.mkdirSync(extensionsDir, { recursive: true });

    extensionManager = new ExtensionManager({
      settings: createTestMergedSettings({
        telemetry: { enabled: false },
      }),
      requestConsent: vi.fn().mockResolvedValue(true),
      requestSetting: vi.fn(),
      workspaceDir: tempDir,
    });
  });

  afterEach(() => {
    try {
      fs.rmSync(tempDir, { recursive: true, force: true });
    } catch {
      // ignore
    }
  });

  it('should load agents from an extension', async () => {
    const sourceDir = path.join(tempDir, 'source-ext-good');
    createExtension({
      extensionsDir: sourceDir,
      name: 'good-agents-ext',
      version: '1.0.0',
      installMetadata: {
        type: 'local',
        source: path.join(sourceDir, 'good-agents-ext'),
      },
    });
    const extensionPath = path.join(sourceDir, 'good-agents-ext');

    const agentsDir = path.join(extensionPath, 'agents');
    fs.mkdirSync(agentsDir, { recursive: true });
    fs.writeFileSync(
      path.join(agentsDir, 'test-agent.md'),
      '---\nname: test-agent\nkind: local\ndescription: test desc\n---\nbody',
    );

    await extensionManager.loadExtensions();

    const extension = await extensionManager.installOrUpdateExtension({
      type: 'local',
      source: extensionPath,
    });

    expect(extension.name).toBe('good-agents-ext');
    expect(extension.agents).toBeDefined();
    expect(extension.agents).toHaveLength(1);
    expect(extension.agents![0].name).toBe('test-agent');
    expect(debugLogger.warn).not.toHaveBeenCalled();
  });

  it('should log errors but continue if an agent fails to load', async () => {
    const sourceDir = path.join(tempDir, 'source-ext-bad');
    createExtension({
      extensionsDir: sourceDir,
      name: 'bad-agents-ext',
      version: '1.0.0',
      installMetadata: {
        type: 'local',
        source: path.join(sourceDir, 'bad-agents-ext'),
      },
    });
    const extensionPath = path.join(sourceDir, 'bad-agents-ext');

    const agentsDir = path.join(extensionPath, 'agents');
    fs.mkdirSync(agentsDir, { recursive: true });
    // Invalid agent (missing description)
    fs.writeFileSync(
      path.join(agentsDir, 'bad-agent.md'),
      '---\nname: bad-agent\nkind: local\n---\nbody',
    );

    await extensionManager.loadExtensions();

    const extension = await extensionManager.installOrUpdateExtension({
      type: 'local',
      source: extensionPath,
    });

    expect(extension.name).toBe('bad-agents-ext');
    expect(extension.agents).toEqual([]);
    expect(debugLogger.warn).toHaveBeenCalledWith(
      expect.stringContaining('Error loading agent from bad-agents-ext'),
    );
  });
});