summaryrefslogtreecommitdiff
path: root/packages/core/src/services/fileDiscoveryService.test.ts
blob: 2ef83bfa66f0e4515c61e33154dc63f21b2c7a61 (plain)
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import type { Mocked } from 'vitest';
import { FileDiscoveryService } from './fileDiscoveryService.js';
import { GitIgnoreParser } from '../utils/gitIgnoreParser.js';

// Mock the GitIgnoreParser
vi.mock('../utils/gitIgnoreParser.js');

// Mock gitUtils module
vi.mock('../utils/gitUtils.js', () => ({
  isGitRepository: vi.fn(() => true),
  findGitRoot: vi.fn(() => '/test/project'),
}));

describe('FileDiscoveryService', () => {
  let service: FileDiscoveryService;
  let mockGitIgnoreParser: Mocked<GitIgnoreParser>;
  const mockProjectRoot = '/test/project';

  beforeEach(() => {
    service = new FileDiscoveryService(mockProjectRoot);

    mockGitIgnoreParser = {
      initialize: vi.fn(),
      isIgnored: vi.fn(),
      getIgnoredPatterns: vi.fn(() => ['.git/**', 'node_modules/**']),
      parseGitIgnoreContent: vi.fn(),
    } as unknown as Mocked<GitIgnoreParser>;

    vi.mocked(GitIgnoreParser).mockImplementation(() => mockGitIgnoreParser);
    vi.clearAllMocks();
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  describe('initialization', () => {
    it('should initialize git ignore parser by default', async () => {
      await service.initialize();

      expect(GitIgnoreParser).toHaveBeenCalledWith(mockProjectRoot);
      expect(mockGitIgnoreParser.initialize).toHaveBeenCalled();
    });

    it('should not initialize git ignore parser when respectGitIgnore is false', async () => {
      await service.initialize({ respectGitIgnore: false });

      expect(GitIgnoreParser).not.toHaveBeenCalled();
      expect(mockGitIgnoreParser.initialize).not.toHaveBeenCalled();
    });

    it('should handle initialization errors gracefully', async () => {
      mockGitIgnoreParser.initialize.mockRejectedValue(
        new Error('Init failed'),
      );

      await expect(service.initialize()).rejects.toThrow('Init failed');
    });
  });

  describe('filterFiles', () => {
    beforeEach(async () => {
      mockGitIgnoreParser.isIgnored.mockImplementation(
        (path: string) =>
          path.includes('node_modules') || path.includes('.git'),
      );
      await service.initialize();
    });

    it('should filter out git-ignored files by default', () => {
      const files = [
        'src/index.ts',
        'node_modules/package/index.js',
        'README.md',
        '.git/config',
        'dist/bundle.js',
      ];

      const filtered = service.filterFiles(files);

      expect(filtered).toEqual(['src/index.ts', 'README.md', 'dist/bundle.js']);
    });

    it('should not filter files when respectGitIgnore is false', () => {
      const files = [
        'src/index.ts',
        'node_modules/package/index.js',
        '.git/config',
      ];

      const filtered = service.filterFiles(files, { respectGitIgnore: false });

      expect(filtered).toEqual(files);
    });

    it('should handle empty file list', () => {
      const filtered = service.filterFiles([]);
      expect(filtered).toEqual([]);
    });
  });

  describe('shouldIgnoreFile', () => {
    beforeEach(async () => {
      mockGitIgnoreParser.isIgnored.mockImplementation((path: string) =>
        path.includes('node_modules'),
      );
      await service.initialize();
    });

    it('should return true for git-ignored files', () => {
      expect(service.shouldIgnoreFile('node_modules/package/index.js')).toBe(
        true,
      );
    });

    it('should return false for non-ignored files', () => {
      expect(service.shouldIgnoreFile('src/index.ts')).toBe(false);
    });

    it('should return false when respectGitIgnore is false', () => {
      expect(
        service.shouldIgnoreFile('node_modules/package/index.js', {
          respectGitIgnore: false,
        }),
      ).toBe(false);
    });

    it('should return false when git ignore parser is not initialized', async () => {
      const uninitializedService = new FileDiscoveryService(mockProjectRoot);
      expect(
        uninitializedService.shouldIgnoreFile('node_modules/package/index.js'),
      ).toBe(false);
    });
  });

  describe('getIgnoreInfo', () => {
    beforeEach(async () => {
      await service.initialize();
    });

    it('should return git ignored patterns', () => {
      const info = service.getIgnoreInfo();

      expect(info.gitIgnored).toEqual(['.git/**', 'node_modules/**']);
    });

    it('should return empty arrays when git ignore parser is not initialized', async () => {
      const uninitializedService = new FileDiscoveryService(mockProjectRoot);
      const info = uninitializedService.getIgnoreInfo();

      expect(info.gitIgnored).toEqual([]);
    });

    it('should handle git ignore parser returning null patterns', async () => {
      mockGitIgnoreParser.getIgnoredPatterns.mockReturnValue([] as string[]);

      const info = service.getIgnoreInfo();

      expect(info.gitIgnored).toEqual([]);
    });
  });

  describe('isGitRepository', () => {
    it('should return true when isGitRepo is explicitly set to true in options', () => {
      const result = service.isGitRepository({ isGitRepo: true });
      expect(result).toBe(true);
    });

    it('should return false when isGitRepo is explicitly set to false in options', () => {
      const result = service.isGitRepository({ isGitRepo: false });
      expect(result).toBe(false);
    });

    it('should use git utility function when isGitRepo is not specified', () => {
      const result = service.isGitRepository();
      expect(result).toBe(true); // mocked to return true
    });

    it('should use git utility function when options are undefined', () => {
      const result = service.isGitRepository(undefined);
      expect(result).toBe(true); // mocked to return true
    });
  });

  describe('initialization with isGitRepo config', () => {
    it('should initialize git ignore parser when isGitRepo is true in options', async () => {
      await service.initialize({ isGitRepo: true });

      expect(GitIgnoreParser).toHaveBeenCalledWith(mockProjectRoot);
      expect(mockGitIgnoreParser.initialize).toHaveBeenCalled();
    });

    it('should not initialize git ignore parser when isGitRepo is false in options', async () => {
      await service.initialize({ isGitRepo: false });

      expect(GitIgnoreParser).not.toHaveBeenCalled();
      expect(mockGitIgnoreParser.initialize).not.toHaveBeenCalled();
    });

    it('should initialize git ignore parser when isGitRepo is not specified but respectGitIgnore is true', async () => {
      await service.initialize({ respectGitIgnore: true });

      expect(GitIgnoreParser).toHaveBeenCalledWith(mockProjectRoot);
      expect(mockGitIgnoreParser.initialize).toHaveBeenCalled();
    });
  });

  describe('shouldIgnoreFile with isGitRepo config', () => {
    it('should respect isGitRepo option when checking if file should be ignored', async () => {
      mockGitIgnoreParser.isIgnored.mockImplementation((path: string) =>
        path.includes('node_modules'),
      );
      await service.initialize({ isGitRepo: true });

      expect(
        service.shouldIgnoreFile('node_modules/package/index.js', {
          isGitRepo: true,
        }),
      ).toBe(true);
      expect(
        service.shouldIgnoreFile('node_modules/package/index.js', {
          isGitRepo: false,
        }),
      ).toBe(false);
    });
  });

  describe('edge cases', () => {
    it('should handle relative project root paths', () => {
      const relativeService = new FileDiscoveryService('./relative/path');
      expect(relativeService).toBeInstanceOf(FileDiscoveryService);
    });

    it('should handle undefined options', async () => {
      await service.initialize(undefined);
      expect(GitIgnoreParser).toHaveBeenCalled();
    });

    it('should handle filterFiles with undefined options', async () => {
      await service.initialize();
      const files = ['src/index.ts'];
      const filtered = service.filterFiles(files, undefined);
      expect(filtered).toEqual(files);
    });
  });
});