-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathapi.test.ts
More file actions
118 lines (106 loc) · 3.61 KB
/
Copy pathapi.test.ts
File metadata and controls
118 lines (106 loc) · 3.61 KB
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
import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('server-only', () => ({}));
import { findSearchBasedSymbolReferences, findSearchBasedSymbolDefinitions } from './api';
import { search } from '@/features/search';
vi.mock('@/features/search', () => ({
search: vi.fn(),
}));
vi.mock('@/middleware/withAuth', () => ({
withOptionalAuth: (fn: any) => fn(),
}));
vi.mock('@/middleware/sew', () => ({
sew: (fn: any) => fn(),
}));
const MOCK_SEARCH_RESPONSE = {
stats: {
actualMatchCount: 1,
totalMatchCount: 1,
duration: 100,
fileCount: 1,
filesSkipped: 0,
contentBytesLoaded: 100,
indexBytesLoaded: 100,
crashes: 0,
shardFilesConsidered: 1,
filesConsidered: 1,
filesLoaded: 1,
shardsScanned: 1,
shardsSkipped: 0,
shardsSkippedFilter: 0,
ngramMatches: 1,
ngramLookups: 1,
wait: 0,
matchTreeConstruction: 10,
matchTreeSearch: 90,
regexpsConsidered: 0,
flushReason: 'FLUSH_REASON_FINAL_FLUSH',
},
files: [
{
fileName: {
text: 'src/index.ts',
matchRanges: [],
},
repository: 'github.com/owner/repo',
repositoryId: 123,
webUrl: 'https://sourcebot.example.com/browse/github.com/owner/repo/blob/main/src/index.ts',
language: 'TypeScript',
ref: 'abcdef1234567890',
chunks: [
{
content: 'const a = 1;',
matchRanges: [
{
start: { byteOffset: 0, lineNumber: 1, column: 1 },
end: { byteOffset: 12, lineNumber: 1, column: 13 },
}
],
}
],
branches: ['main'],
}
],
repositoryInfo: [],
isSearchExhaustive: true,
};
describe('CodeNav Search-Based APIs', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(search).mockResolvedValue(MOCK_SEARCH_RESPONSE as any);
});
describe('findSearchBasedSymbolReferences', () => {
it('includes the ref (commit SHA) in the returned file results', async () => {
const result = await findSearchBasedSymbolReferences({
symbolName: 'mySymbol',
repoName: 'github.com/owner/repo',
revisionName: 'HEAD',
});
expect(isServiceError(result)).toBe(false);
const response = result as any;
expect(response.files).toHaveLength(1);
expect(response.files[0].ref).toBe('abcdef1234567890');
});
});
describe('findSearchBasedSymbolDefinitions', () => {
it('includes the ref (commit SHA) in the returned file results', async () => {
const result = await findSearchBasedSymbolDefinitions({
symbolName: 'mySymbol',
repoName: 'github.com/owner/repo',
revisionName: 'HEAD',
});
expect(isServiceError(result)).toBe(false);
const response = result as any;
expect(response.files).toHaveLength(1);
expect(response.files[0].ref).toBe('abcdef1234567890');
});
});
});
/**
* Type guard that checks whether a given object is a ServiceError by looking for an `errorCode` property.
*
* @param obj - The value to inspect.
* @returns True if the object looks like a ServiceError.
*/
function isServiceError(obj: any): boolean {
return obj && typeof obj === 'object' && 'errorCode' in obj;
}