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

import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
  getProactiveToolSuggestions,
  isNetworkReliantCommand,
} from './proactivePermissions.js';
import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs';

vi.mock('node:os');
vi.mock('node:fs', () => ({
  default: {
    promises: {
      access: vi.fn(),
    },
    constants: {
      F_OK: 0,
    },
  },
  promises: {
    access: vi.fn(),
  },
  constants: {
    F_OK: 0,
  },
}));

describe('proactivePermissions', () => {
  const homeDir = '/Users/testuser';

  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(os.homedir).mockReturnValue(homeDir);
    vi.mocked(os.platform).mockReturnValue('darwin');
  });

  describe('isNetworkReliantCommand', () => {
    it('should return true for always-network tools', () => {
      expect(isNetworkReliantCommand('ssh')).toBe(true);
      expect(isNetworkReliantCommand('git')).toBe(true);
      expect(isNetworkReliantCommand('curl')).toBe(true);
    });

    it('should return true for network-heavy node subcommands', () => {
      expect(isNetworkReliantCommand('npm', 'install')).toBe(true);
      expect(isNetworkReliantCommand('yarn', 'add')).toBe(true);
      expect(isNetworkReliantCommand('bun', '')).toBe(true);
    });

    it('should return false for local node subcommands', () => {
      expect(isNetworkReliantCommand('npm', 'test')).toBe(false);
      expect(isNetworkReliantCommand('yarn', 'run')).toBe(false);
    });

    it('should return false for unknown tools', () => {
      expect(isNetworkReliantCommand('ls')).toBe(false);
    });
  });

  describe('getProactiveToolSuggestions', () => {
    it('should return undefined for unknown tools', async () => {
      expect(await getProactiveToolSuggestions('ls')).toBeUndefined();
      expect(await getProactiveToolSuggestions('node')).toBeUndefined();
    });

    it('should return permissions for npm if paths exist', async () => {
      vi.mocked(fs.promises.access).mockImplementation(
        (p: fs.PathLike, _mode?: number) => {
          const pathStr = p.toString();
          if (
            pathStr === path.join(homeDir, '.npm') ||
            pathStr === path.join(homeDir, '.cache') ||
            pathStr === path.join(homeDir, '.npmrc')
          ) {
            return Promise.resolve();
          }
          return Promise.reject(new Error('ENOENT'));
        },
      );

      const permissions = await getProactiveToolSuggestions('npm');
      expect(permissions).toBeDefined();
      expect(permissions?.network).toBe(true);
      // .npmrc should be read-only
      expect(permissions?.fileSystem?.read).toContain(
        path.join(homeDir, '.npmrc'),
      );
      expect(permissions?.fileSystem?.write).not.toContain(
        path.join(homeDir, '.npmrc'),
      );
      // .npm should be read-write
      expect(permissions?.fileSystem?.read).toContain(
        path.join(homeDir, '.npm'),
      );
      expect(permissions?.fileSystem?.write).toContain(
        path.join(homeDir, '.npm'),
      );
      // .cache should be read-write
      expect(permissions?.fileSystem?.write).toContain(
        path.join(homeDir, '.cache'),
      );
      // should NOT contain .ssh or .gitconfig for npm
      expect(permissions?.fileSystem?.read).not.toContain(
        path.join(homeDir, '.ssh'),
      );
    });

    it('should grant network access and suggest primary cache paths even if they do not exist', async () => {
      vi.mocked(fs.promises.access).mockRejectedValue(new Error('ENOENT'));
      const permissions = await getProactiveToolSuggestions('npm');
      expect(permissions).toBeDefined();
      expect(permissions?.network).toBe(true);
      expect(permissions?.fileSystem?.write).toContain(
        path.join(homeDir, '.npm'),
      );
      // .cache is optional and should NOT be included if it doesn't exist
      expect(permissions?.fileSystem?.write).not.toContain(
        path.join(homeDir, '.cache'),
      );
    });

    it('should suggest .ssh and .gitconfig only for git', async () => {
      vi.mocked(fs.promises.access).mockImplementation(
        (p: fs.PathLike, _mode?: number) => {
          const pathStr = p.toString();
          if (
            pathStr === path.join(homeDir, '.ssh') ||
            pathStr === path.join(homeDir, '.gitconfig')
          ) {
            return Promise.resolve();
          }
          return Promise.reject(new Error('ENOENT'));
        },
      );

      const permissions = await getProactiveToolSuggestions('git');
      expect(permissions?.network).toBe(true);
      expect(permissions?.fileSystem?.read).toContain(
        path.join(homeDir, '.ssh'),
      );
      expect(permissions?.fileSystem?.read).toContain(
        path.join(homeDir, '.gitconfig'),
      );
    });

    it('should suggest .ssh but NOT .gitconfig for ssh', async () => {
      vi.mocked(fs.promises.access).mockImplementation(
        (p: fs.PathLike, _mode?: number) => {
          const pathStr = p.toString();
          if (pathStr === path.join(homeDir, '.ssh')) {
            return Promise.resolve();
          }
          return Promise.reject(new Error('ENOENT'));
        },
      );

      const permissions = await getProactiveToolSuggestions('ssh');
      expect(permissions?.network).toBe(true);
      expect(permissions?.fileSystem?.read).toContain(
        path.join(homeDir, '.ssh'),
      );
      expect(permissions?.fileSystem?.read).not.toContain(
        path.join(homeDir, '.gitconfig'),
      );
    });

    it('should handle Windows specific paths', async () => {
      vi.mocked(os.platform).mockReturnValue('win32');
      const appData = 'C:\\Users\\testuser\\AppData\\Roaming';
      vi.stubEnv('AppData', appData);

      vi.mocked(fs.promises.access).mockImplementation(
        (p: fs.PathLike, _mode?: number) => {
          const pathStr = p.toString();
          if (pathStr === path.join(appData, 'npm')) {
            return Promise.resolve();
          }
          return Promise.reject(new Error('ENOENT'));
        },
      );

      const permissions = await getProactiveToolSuggestions('npm.exe');
      expect(permissions).toBeDefined();
      expect(permissions?.fileSystem?.read).toContain(
        path.join(appData, 'npm'),
      );

      vi.unstubAllEnvs();
    });

    it('should include bun, pnpm, and yarn specific paths', async () => {
      vi.mocked(fs.promises.access).mockResolvedValue(undefined);

      const bun = await getProactiveToolSuggestions('bun');
      expect(bun?.fileSystem?.read).toContain(path.join(homeDir, '.bun'));
      expect(bun?.fileSystem?.read).not.toContain(path.join(homeDir, '.yarn'));

      const yarn = await getProactiveToolSuggestions('yarn');
      expect(yarn?.fileSystem?.read).toContain(path.join(homeDir, '.yarn'));
    });
  });
});