Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

share.test.ts 4.3 KB

You have to be logged in to leave a comment. Sign In
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
  1. import { getUserEmail } from '../src/globalConfig/accounts';
  2. import { cloudConfig } from '../src/globalConfig/cloud';
  3. import logger from '../src/logger';
  4. import type Eval from '../src/models/eval';
  5. import { stripAuthFromUrl, createShareableUrl } from '../src/share';
  6. jest.mock('../src/logger');
  7. jest.mock('../src/globalConfig/cloud');
  8. jest.mock('../src/fetch', () => ({
  9. fetchWithProxy: jest.fn().mockResolvedValue({
  10. ok: true,
  11. json: jest.fn().mockResolvedValue({ id: 'mock-eval-id' }),
  12. }),
  13. }));
  14. jest.mock('../src/globalConfig/accounts', () => ({
  15. getUserEmail: jest.fn(),
  16. setUserEmail: jest.fn(),
  17. }));
  18. describe('stripAuthFromUrl', () => {
  19. it('removes username and password from URL', () => {
  20. const input = 'https://user:pass@example.com/path?query=value#hash';
  21. const expected = 'https://example.com/path?query=value#hash';
  22. expect(stripAuthFromUrl(input)).toBe(expected);
  23. });
  24. it('handles URLs without auth info', () => {
  25. const input = 'https://example.com/path?query=value#hash';
  26. expect(stripAuthFromUrl(input)).toBe(input);
  27. });
  28. it('handles URLs with only username', () => {
  29. const input = 'https://user@example.com/path';
  30. const expected = 'https://example.com/path';
  31. expect(stripAuthFromUrl(input)).toBe(expected);
  32. });
  33. it('handles URLs with special characters in auth', () => {
  34. const input = 'https://user%40:p@ss@example.com/path';
  35. const expected = 'https://example.com/path';
  36. expect(stripAuthFromUrl(input)).toBe(expected);
  37. });
  38. it('returns original string for invalid URLs', () => {
  39. const input = 'not a valid url';
  40. expect(stripAuthFromUrl(input)).toBe(input);
  41. });
  42. it('handles URLs with IP addresses', () => {
  43. const input = 'http://user:pass@192.168.1.1:8080/path';
  44. const expected = 'http://192.168.1.1:8080/path';
  45. expect(stripAuthFromUrl(input)).toBe(expected);
  46. });
  47. });
  48. describe('createShareableUrl', () => {
  49. beforeEach(() => {
  50. jest.clearAllMocks();
  51. });
  52. it('creates correct URL for cloud config and updates author', async () => {
  53. jest.mocked(cloudConfig.isEnabled).mockReturnValue(true);
  54. jest.mocked(cloudConfig.getAppUrl).mockReturnValue('https://app.example.com');
  55. jest.mocked(cloudConfig.getApiHost).mockReturnValue('https://api.example.com');
  56. jest.mocked(cloudConfig.getApiKey).mockReturnValue('mock-api-key');
  57. jest.mocked(getUserEmail).mockReturnValue('logged-in@example.com');
  58. const mockEval: Partial<Eval> = {
  59. config: {},
  60. author: 'original@example.com',
  61. useOldResults: jest.fn().mockReturnValue(false),
  62. loadResults: jest.fn().mockResolvedValue(undefined),
  63. save: jest.fn().mockResolvedValue(undefined),
  64. };
  65. const result = await createShareableUrl(mockEval as Eval);
  66. expect(result).toBe('https://app.example.com/eval/mock-eval-id');
  67. expect(mockEval.author).toBe('logged-in@example.com');
  68. expect(mockEval.save).toHaveBeenCalledWith();
  69. });
  70. it('throws error if cloud config enabled but no user email', async () => {
  71. jest.mocked(cloudConfig.isEnabled).mockReturnValue(true);
  72. jest.mocked(getUserEmail).mockReturnValue(null);
  73. const mockEval: Partial<Eval> = {
  74. config: {},
  75. useOldResults: jest.fn().mockReturnValue(false),
  76. loadResults: jest.fn().mockResolvedValue(undefined),
  77. };
  78. await expect(createShareableUrl(mockEval as Eval)).rejects.toThrow('User email is not set');
  79. });
  80. it('logs warning when changing author in cloud mode', async () => {
  81. jest.mocked(cloudConfig.isEnabled).mockReturnValue(true);
  82. jest.mocked(cloudConfig.getAppUrl).mockReturnValue('https://app.example.com');
  83. jest.mocked(cloudConfig.getApiHost).mockReturnValue('https://api.example.com');
  84. jest.mocked(cloudConfig.getApiKey).mockReturnValue('mock-api-key');
  85. jest.mocked(getUserEmail).mockReturnValue('new@example.com');
  86. const mockEval: Partial<Eval> = {
  87. config: {},
  88. author: 'original@example.com',
  89. useOldResults: jest.fn().mockReturnValue(false),
  90. loadResults: jest.fn().mockResolvedValue(undefined),
  91. save: jest.fn().mockResolvedValue(undefined),
  92. };
  93. await createShareableUrl(mockEval as Eval);
  94. expect(mockEval.author).toBe('new@example.com');
  95. expect(jest.mocked(logger.warn)).toHaveBeenCalledWith(
  96. 'Warning: Changing eval author from original@example.com to logged-in user new@example.com',
  97. );
  98. });
  99. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...