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 5.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
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
  1. import { Command } from 'commander';
  2. import { createAndDisplayShareableUrl, shareCommand } from '../../src/commands/share';
  3. import * as envars from '../../src/envars';
  4. import logger from '../../src/logger';
  5. import { createShareableUrl } from '../../src/share';
  6. jest.mock('../../src/share');
  7. jest.mock('../../src/logger');
  8. jest.mock('../../src/telemetry', () => ({
  9. record: jest.fn(),
  10. send: jest.fn(),
  11. }));
  12. jest.mock('../../src/envars');
  13. jest.mock('readline');
  14. jest.mock('../../src/models/eval');
  15. describe('Share Command', () => {
  16. describe('createAndDisplayShareableUrl', () => {
  17. beforeEach(() => {
  18. jest.clearAllMocks();
  19. });
  20. it('should return a URL and log it when successful', async () => {
  21. const mockEval = { id: 'test-eval-id' } as any;
  22. const mockUrl = 'https://app.promptfoo.dev/eval/test-eval-id';
  23. jest.mocked(createShareableUrl).mockResolvedValue(mockUrl);
  24. const result = await createAndDisplayShareableUrl(mockEval, false);
  25. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, false);
  26. expect(logger.info).toHaveBeenCalledWith(expect.stringContaining(mockUrl));
  27. expect(result).toBe(mockUrl);
  28. });
  29. it('should pass showAuth parameter correctly', async () => {
  30. const mockEval = { id: 'test-eval-id' } as any;
  31. const mockUrl = 'https://app.promptfoo.dev/eval/test-eval-id';
  32. jest.mocked(createShareableUrl).mockResolvedValue(mockUrl);
  33. await createAndDisplayShareableUrl(mockEval, true);
  34. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, true);
  35. });
  36. it('should return null when createShareableUrl returns null', async () => {
  37. const mockEval = { id: 'test-eval-id' } as any;
  38. jest.mocked(createShareableUrl).mockResolvedValue(null);
  39. const result = await createAndDisplayShareableUrl(mockEval, false);
  40. expect(createShareableUrl).toHaveBeenCalledWith(mockEval, false);
  41. expect(result).toBeNull();
  42. expect(logger.error).toHaveBeenCalledWith('Failed to create shareable URL');
  43. expect(logger.info).not.toHaveBeenCalled();
  44. });
  45. });
  46. describe('shareCommand', () => {
  47. let program: Command;
  48. beforeEach(() => {
  49. jest.clearAllMocks();
  50. program = new Command();
  51. shareCommand(program);
  52. });
  53. it('should register share command with correct options', () => {
  54. const cmd = program.commands.find((c) => c.name() === 'share');
  55. expect(cmd).toBeDefined();
  56. expect(cmd?.description()).toContain('Create a shareable URL');
  57. const options = cmd?.options;
  58. expect(options?.find((o) => o.short === '-y')).toBeDefined();
  59. expect(options?.find((o) => o.long === '--show-auth')).toBeDefined();
  60. expect(options?.find((o) => o.long === '--env-path')).toBeDefined();
  61. });
  62. // Test just the hostname determination logic directly
  63. it('should use app.promptfoo.dev by default if no environment variables are set', () => {
  64. // Mock environment variables to return undefined
  65. jest.mocked(envars.getEnvString).mockImplementation(() => '');
  66. const baseUrl =
  67. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  68. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  69. const hostname = baseUrl ? new URL(baseUrl).hostname : 'app.promptfoo.dev';
  70. expect(hostname).toBe('app.promptfoo.dev');
  71. });
  72. it('should use PROMPTFOO_SHARING_APP_BASE_URL for hostname when set', () => {
  73. // Mock PROMPTFOO_SHARING_APP_BASE_URL
  74. jest.mocked(envars.getEnvString).mockImplementation((key) => {
  75. if (key === 'PROMPTFOO_SHARING_APP_BASE_URL') {
  76. return 'https://custom-domain.com';
  77. }
  78. return '';
  79. });
  80. const baseUrl =
  81. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  82. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  83. const hostname = baseUrl ? new URL(baseUrl).hostname : 'app.promptfoo.dev';
  84. expect(hostname).toBe('custom-domain.com');
  85. });
  86. it('should use PROMPTFOO_REMOTE_APP_BASE_URL for hostname when PROMPTFOO_SHARING_APP_BASE_URL is not set', () => {
  87. // Mock PROMPTFOO_REMOTE_APP_BASE_URL
  88. jest.mocked(envars.getEnvString).mockImplementation((key) => {
  89. if (key === 'PROMPTFOO_REMOTE_APP_BASE_URL') {
  90. return 'https://self-hosted-domain.com';
  91. }
  92. return '';
  93. });
  94. const baseUrl =
  95. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  96. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  97. const hostname = baseUrl ? new URL(baseUrl).hostname : 'app.promptfoo.dev';
  98. expect(hostname).toBe('self-hosted-domain.com');
  99. });
  100. it('should prioritize PROMPTFOO_SHARING_APP_BASE_URL over PROMPTFOO_REMOTE_APP_BASE_URL', () => {
  101. // Mock both environment variables
  102. jest.mocked(envars.getEnvString).mockImplementation((key) => {
  103. if (key === 'PROMPTFOO_SHARING_APP_BASE_URL') {
  104. return 'https://sharing-domain.com';
  105. }
  106. if (key === 'PROMPTFOO_REMOTE_APP_BASE_URL') {
  107. return 'https://remote-domain.com';
  108. }
  109. return '';
  110. });
  111. const baseUrl =
  112. envars.getEnvString('PROMPTFOO_SHARING_APP_BASE_URL') ||
  113. envars.getEnvString('PROMPTFOO_REMOTE_APP_BASE_URL');
  114. const hostname = baseUrl ? new URL(baseUrl).hostname : 'app.promptfoo.dev';
  115. expect(hostname).toBe('sharing-domain.com');
  116. });
  117. });
  118. });
Tip!

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

Comments

Loading...