forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshare.test.ts
67 lines (56 loc) · 2.34 KB
/
share.test.ts
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
import { cloudConfig } from '../src/globalConfig/cloud';
import type Eval from '../src/models/eval';
import { stripAuthFromUrl, createShareableUrl } from '../src/share';
jest.mock('../src/logger');
jest.mock('../src/globalConfig/cloud');
jest.mock('../src/fetch', () => ({
fetchWithProxy: jest.fn().mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue({ id: 'mock-eval-id' }),
}),
}));
describe('stripAuthFromUrl', () => {
it('removes username and password from URL', () => {
const input = 'https://user:pass@example.com/path?query=value#hash';
const expected = 'https://example.com/path?query=value#hash';
expect(stripAuthFromUrl(input)).toBe(expected);
});
it('handles URLs without auth info', () => {
const input = 'https://example.com/path?query=value#hash';
expect(stripAuthFromUrl(input)).toBe(input);
});
it('handles URLs with only username', () => {
const input = 'https://user@example.com/path';
const expected = 'https://example.com/path';
expect(stripAuthFromUrl(input)).toBe(expected);
});
it('handles URLs with special characters in auth', () => {
const input = 'https://user%40:p@ss@example.com/path';
const expected = 'https://example.com/path';
expect(stripAuthFromUrl(input)).toBe(expected);
});
it('returns original string for invalid URLs', () => {
const input = 'not a valid url';
expect(stripAuthFromUrl(input)).toBe(input);
});
it('handles URLs with IP addresses', () => {
const input = 'http://user:pass@192.168.1.1:8080/path';
const expected = 'http://192.168.1.1:8080/path';
expect(stripAuthFromUrl(input)).toBe(expected);
});
});
describe('createShareableUrl', () => {
it('creates correct URL for cloud config', async () => {
jest.mocked(cloudConfig.isEnabled).mockReturnValue(true);
jest.mocked(cloudConfig.getAppUrl).mockReturnValue('https://app.example.com');
jest.mocked(cloudConfig.getApiHost).mockReturnValue('https://api.example.com');
jest.mocked(cloudConfig.getApiKey).mockReturnValue('mock-api-key');
const mockEval: Partial<Eval> = {
config: {},
useOldResults: jest.fn().mockReturnValue(false),
loadResults: jest.fn().mockResolvedValue(undefined),
};
const result = await createShareableUrl(mockEval as Eval);
expect(result).toBe('https://app.example.com/eval/mock-eval-id');
});
});