File size: 7,636 Bytes
a5d1c1b
 
 
 
 
61b1da1
a5d1c1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61b1da1
 
a5d1c1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61b1da1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5d1c1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { app } from '../src/app.js';
import { githubService } from '../src/services/github-service.js';
import { treeCache } from '../src/services/tree-cache.js';

describe('Ever-Brain API Routes', () => {
  beforeEach(() => {
    treeCache.clear();
    vi.restoreAllMocks();
  });

  describe('GET /health', () => {
    it('returns healthy operational status', async () => {
      const res = await app.request('/health');
      expect(res.status).toBe(200);

      const json = await res.json();
      expect(json.status).toBe('ok');
      expect(json.version).toBe('1.0.0');
      expect(typeof json.uptimeSeconds).toBe('number');
      expect(json.timestamp).toBeDefined();
    });
  });

  describe('GET /repos/:owner/:repo', () => {
    it('returns repository metadata and default branch', async () => {
      vi.spyOn(githubService, 'getRepoMetadata').mockResolvedValue({
        owner: 'acme',
        repo: 'project',
        defaultBranch: 'main',
        description: 'Test repository',
      });

      const res = await app.request('/repos/acme/project');
      expect(res.status).toBe(200);

      const json = await res.json();
      expect(json.owner).toBe('acme');
      expect(json.repo).toBe('project');
      expect(json.defaultBranch).toBe('main');
      expect(json.description).toBe('Test repository');
    });
  });

  describe('GET /repos/:owner/:repo/files', () => {
    it('returns discovered project files', async () => {
      vi.spyOn(githubService, 'getRepoMetadata').mockResolvedValue({
        owner: 'acme',
        repo: 'project',
        defaultBranch: 'main',
      });

      vi.spyOn(githubService, 'getGitTree').mockResolvedValue([
        { path: 'README.md', mode: '100644', type: 'blob', sha: '1', size: 100, url: '' },
        { path: 'knowledge/arch.md', mode: '100644', type: 'blob', sha: '2', size: 200, url: '' },
      ]);

      const res = await app.request('/repos/acme/project/files');
      expect(res.status).toBe(200);

      const json = await res.json();
      expect(json.repository).toBe('acme/project');
      expect(json.branch).toBe('main');
      expect(json.totalFiles).toBe(2);
      expect(json.files[0].path).toBe('README.md');
      expect(json.files[0].mimeType).toBe('text/markdown');
    });

    it('supports /projects alias for backward compatibility', async () => {
      vi.spyOn(githubService, 'getRepoMetadata').mockResolvedValue({
        owner: 'acme',
        repo: 'project',
        defaultBranch: 'main',
      });

      vi.spyOn(githubService, 'getGitTree').mockResolvedValue([
        { path: 'README.md', mode: '100644', type: 'blob', sha: '1', size: 100, url: '' },
      ]);

      const res = await app.request('/projects/acme/project/files');
      expect(res.status).toBe(200);

      const json = await res.json();
      expect(json.repository).toBe('acme/project');
      expect(json.totalFiles).toBe(1);
    });
  });

  describe('GET /repos/:owner/:repo/file/*', () => {
    it('streams raw file content with appropriate MIME and headers', async () => {
      vi.spyOn(githubService, 'getRepoMetadata').mockResolvedValue({
        owner: 'acme',
        repo: 'project',
        defaultBranch: 'main',
      });

      vi.spyOn(githubService, 'getGitTree').mockResolvedValue([
        { path: 'README.md', mode: '100644', type: 'blob', sha: '1', size: 100, url: '' },
      ]);

      vi.spyOn(githubService, 'getRawFileContent').mockResolvedValue({
        status: 200,
        content: '# Test Project\nHello World',
        sizeBytes: 26,
        durationMs: 15,
      });

      const res = await app.request('/repos/acme/project/file/README.md');
      expect(res.status).toBe(200);
      expect(res.headers.get('Content-Type')).toContain('text/markdown');
      expect(res.headers.get('X-Ever-Brain-Path')).toBe('README.md');
      expect(res.headers.get('X-Ever-Brain-Branch')).toBe('main');

      const text = await res.text();
      expect(text).toBe('# Test Project\nHello World');
    });

    it('returns 404 when file does not exist', async () => {
      vi.spyOn(githubService, 'getRepoMetadata').mockResolvedValue({
        owner: 'acme',
        repo: 'project',
        defaultBranch: 'main',
      });

      vi.spyOn(githubService, 'getGitTree').mockResolvedValue([
        { path: 'README.md', mode: '100644', type: 'blob', sha: '1', size: 100, url: '' },
      ]);

      vi.spyOn(githubService, 'getRawFileContent').mockResolvedValue({
        status: 404,
        sizeBytes: 0,
        durationMs: 10,
      });

      const res = await app.request('/repos/acme/project/file/missing.md');
      expect(res.status).toBe(404);

      const json = await res.json();
      expect(json.code).toBe('FILE_NOT_FOUND');
    });

    it('returns 200 OK and empty text when file is empty', async () => {
      vi.spyOn(githubService, 'getRepoMetadata').mockResolvedValue({
        owner: 'acme',
        repo: 'project',
        defaultBranch: 'main',
      });

      vi.spyOn(githubService, 'getGitTree').mockResolvedValue([
        { path: 'empty.txt', mode: '100644', type: 'blob', sha: 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', size: 0, url: '' },
      ]);

      vi.spyOn(githubService, 'getRawFileContent').mockResolvedValue({
        status: 200,
        content: '',
        sizeBytes: 0,
        durationMs: 5,
      });

      const res = await app.request('/repos/acme/project/file/empty.txt');
      expect(res.status).toBe(200);
      expect(res.headers.get('Content-Type')).toContain('text/plain');
      expect(res.headers.get('X-Ever-Brain-Path')).toBe('empty.txt');

      const text = await res.text();
      expect(text).toBe('');
    });
  });

  describe('GET /repos/:owner/:repo/search', () => {
    it('returns search results matching query string', async () => {
      vi.spyOn(githubService, 'getRepoMetadata').mockResolvedValue({
        owner: 'acme',
        repo: 'project',
        defaultBranch: 'main',
      });

      vi.spyOn(githubService, 'getGitTree').mockResolvedValue([
        { path: 'README.md', mode: '100644', type: 'blob', sha: '1', size: 100, url: '' },
        { path: 'knowledge/auth.md', mode: '100644', type: 'blob', sha: '2', size: 200, url: '' },
      ]);

      vi.spyOn(githubService, 'getRawFileContent').mockImplementation(
        async (_owner, _repo, _branch, filePath) => {
          if (filePath === 'knowledge/auth.md') {
            return {
              status: 200,
              content: '# Authentication\nWe use JWT authentication tokens here.',
              sizeBytes: 55,
              durationMs: 10,
            };
          }
          return {
            status: 200,
            content: '# Project\nGeneral info.',
            sizeBytes: 25,
            durationMs: 10,
          };
        }
      );

      const res = await app.request('/repos/acme/project/search?q=authentication');
      expect(res.status).toBe(200);

      const json = await res.json();
      expect(json.query).toBe('authentication');
      expect(json.totalMatches).toBe(2);
      expect(json.results.length).toBe(1);
      expect(json.results[0].path).toBe('knowledge/auth.md');
      expect(json.results[0].matches).toBe(2);
      expect(json.results[0].lines).toEqual([1, 2]);
    });
  });

  describe('404 Not Found Handler', () => {
    it('returns structured 404 for unknown endpoints', async () => {
      const res = await app.request('/unknown-endpoint');
      expect(res.status).toBe(404);

      const json = await res.json();
      expect(json.code).toBe('NOT_FOUND');
    });
  });
});