-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathactions.test.ts
More file actions
70 lines (56 loc) · 1.88 KB
/
Copy pathactions.test.ts
File metadata and controls
70 lines (56 loc) · 1.88 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
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
const mockLoadIndex = vi.fn()
const mockQuery = vi.fn()
vi.mock('@moss-dev/moss-web', () => ({
MossClient: vi.fn().mockImplementation(() => ({
loadIndex: mockLoadIndex,
query: mockQuery,
})),
}))
describe('searchMoss', () => {
const originalEnv = { ...process.env }
beforeEach(() => {
vi.resetModules()
mockLoadIndex.mockReset()
mockQuery.mockReset()
process.env.MOSS_PROJECT_ID = 'pid'
process.env.MOSS_PROJECT_KEY = 'pkey'
process.env.MOSS_INDEX_NAME = 'idx'
})
afterEach(() => {
process.env = { ...originalEnv }
})
it('returns success with docs and timeTaken', async () => {
mockLoadIndex.mockResolvedValue(undefined)
mockQuery.mockResolvedValue({
docs: [
{ id: '1', text: 'hello', score: 0.9, metadata: { title: 'Hello' } },
],
timeTakenMs: 42,
})
const { searchMoss } = await import('./actions')
const result = await searchMoss('test')
expect(result).toEqual({
success: true,
docs: [{ id: '1', text: 'hello', score: 0.9, metadata: { title: 'Hello' } }],
timeTaken: 42,
})
})
it('throws when env vars are missing', async () => {
delete process.env.MOSS_PROJECT_ID
delete process.env.MOSS_PROJECT_KEY
delete process.env.MOSS_INDEX_NAME
const { searchMoss } = await import('./actions')
await expect(searchMoss('q')).rejects.toThrow(
'Missing Moss credentials in environment variables.'
)
})
it('returns error on API failure', async () => {
mockLoadIndex.mockRejectedValue(new Error('network down'))
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const { searchMoss } = await import('./actions')
const result = await searchMoss('q')
expect(result).toEqual({ success: false, error: 'network down' })
errorSpy.mockRestore()
})
})