File size: 6,046 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as os from 'node:os';
import { FolderTrustDiscoveryService } from './FolderTrustDiscoveryService.js';
import { GEMINI_DIR } from '../utils/paths.js';

describe('FolderTrustDiscoveryService', () => {
  let tempDir: string;

  beforeEach(async () => {
    tempDir = await fs.mkdtemp(
      path.join(os.tmpdir(), 'gemini-discovery-test-'),
    );
  });

  afterEach(async () => {
    vi.restoreAllMocks();
    await fs.rm(tempDir, { recursive: true, force: true });
  });

  it('should discover commands, skills, mcps, and hooks', async () => {
    const geminiDir = path.join(tempDir, GEMINI_DIR);
    await fs.mkdir(geminiDir, { recursive: true });

    // Mock commands
    const commandsDir = path.join(geminiDir, 'commands');
    await fs.mkdir(commandsDir);
    await fs.writeFile(
      path.join(commandsDir, 'test-cmd.toml'),
      'prompt = "test"',
    );

    // Mock skills
    const skillsDir = path.join(geminiDir, 'skills');
    await fs.mkdir(path.join(skillsDir, 'test-skill'), { recursive: true });
    await fs.writeFile(path.join(skillsDir, 'test-skill', 'SKILL.md'), 'body');

    // Mock agents
    const agentsDir = path.join(geminiDir, 'agents');
    await fs.mkdir(agentsDir);
    await fs.writeFile(path.join(agentsDir, 'test-agent.md'), 'body');

    // Mock settings (MCPs, Hooks, and general settings)
    const settings = {
      mcpServers: {
        'test-mcp': { command: 'node', args: ['test.js'] },
      },
      hooks: {
        BeforeTool: [{ command: 'test-hook' }],
      },
      general: { vimMode: true },
      ui: { theme: 'Dark' },
    };
    await fs.writeFile(
      path.join(geminiDir, 'settings.json'),
      JSON.stringify(settings),
    );

    const results = await FolderTrustDiscoveryService.discover(tempDir);

    expect(results.commands).toContain('test-cmd');
    expect(results.skills).toContain('test-skill');
    expect(results.agents).toContain('test-agent');
    expect(results.mcps).toContain('test-mcp');
    expect(results.hooks).toContain('test-hook');
    expect(results.settings).toContain('general');
    expect(results.settings).toContain('ui');
    expect(results.settings).not.toContain('mcpServers');
    expect(results.settings).not.toContain('hooks');
  });

  it('should flag security warnings for sensitive settings', async () => {
    const geminiDir = path.join(tempDir, GEMINI_DIR);
    await fs.mkdir(geminiDir, { recursive: true });

    const settings = {
      tools: {
        allowed: ['git'],
        sandbox: false,
      },
      security: {
        folderTrust: {
          enabled: false,
        },
      },
    };
    await fs.writeFile(
      path.join(geminiDir, 'settings.json'),
      JSON.stringify(settings),
    );

    const results = await FolderTrustDiscoveryService.discover(tempDir);

    expect(results.securityWarnings).toContain(
      'This project auto-approves certain tools (tools.allowed).',
    );
    expect(results.securityWarnings).toContain(
      'This project attempts to disable folder trust (security.folderTrust.enabled).',
    );
    expect(results.securityWarnings).toContain(
      'This project disables the security sandbox (tools.sandbox).',
    );
  });

  it('should handle missing .gemini directory', async () => {
    const results = await FolderTrustDiscoveryService.discover(tempDir);
    expect(results.commands).toHaveLength(0);
    expect(results.skills).toHaveLength(0);
    expect(results.mcps).toHaveLength(0);
    expect(results.hooks).toHaveLength(0);
    expect(results.settings).toHaveLength(0);
  });

  it('should handle malformed settings.json', async () => {
    const geminiDir = path.join(tempDir, GEMINI_DIR);
    await fs.mkdir(geminiDir, { recursive: true });
    await fs.writeFile(path.join(geminiDir, 'settings.json'), 'invalid json');

    const results = await FolderTrustDiscoveryService.discover(tempDir);
    expect(results.discoveryErrors[0]).toContain(
      'Failed to discover settings: Unexpected token',
    );
  });

  it('should handle null settings.json', async () => {
    const geminiDir = path.join(tempDir, GEMINI_DIR);
    await fs.mkdir(geminiDir, { recursive: true });
    await fs.writeFile(path.join(geminiDir, 'settings.json'), 'null');

    const results = await FolderTrustDiscoveryService.discover(tempDir);
    expect(results.discoveryErrors).toHaveLength(0);
    expect(results.settings).toHaveLength(0);
  });

  it('should handle array settings.json', async () => {
    const geminiDir = path.join(tempDir, GEMINI_DIR);
    await fs.mkdir(geminiDir, { recursive: true });
    await fs.writeFile(path.join(geminiDir, 'settings.json'), '[]');

    const results = await FolderTrustDiscoveryService.discover(tempDir);
    expect(results.discoveryErrors).toHaveLength(0);
    expect(results.settings).toHaveLength(0);
  });

  it('should handle string settings.json', async () => {
    const geminiDir = path.join(tempDir, GEMINI_DIR);
    await fs.mkdir(geminiDir, { recursive: true });
    await fs.writeFile(path.join(geminiDir, 'settings.json'), '"string"');

    const results = await FolderTrustDiscoveryService.discover(tempDir);
    expect(results.discoveryErrors).toHaveLength(0);
    expect(results.settings).toHaveLength(0);
  });

  it('should flag security warning for custom agents', async () => {
    const geminiDir = path.join(tempDir, GEMINI_DIR);
    await fs.mkdir(geminiDir, { recursive: true });

    const agentsDir = path.join(geminiDir, 'agents');
    await fs.mkdir(agentsDir);
    await fs.writeFile(path.join(agentsDir, 'test-agent.md'), 'body');

    const results = await FolderTrustDiscoveryService.discover(tempDir);

    expect(results.agents).toContain('test-agent');
    expect(results.securityWarnings).toContain(
      'This project contains custom agents.',
    );
  });
});