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

index.test.ts 4.0 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
  1. import * as path from 'path';
  2. import {
  3. closeDb,
  4. DrizzleLogWriter,
  5. getDb,
  6. getDbPath,
  7. getDbSignalPath,
  8. isDbOpen,
  9. } from '../../src/database';
  10. import { getEnvBool } from '../../src/envars';
  11. import logger from '../../src/logger';
  12. import { getConfigDirectoryPath } from '../../src/util/config/manage';
  13. jest.mock('../../src/envars');
  14. jest.mock('../../src/logger');
  15. jest.mock('../../src/util/config/manage');
  16. describe('database', () => {
  17. beforeEach(() => {
  18. jest.clearAllMocks();
  19. closeDb();
  20. jest.mocked(getConfigDirectoryPath).mockReturnValue('/test/config/path');
  21. jest.mocked(getEnvBool).mockImplementation((key) => {
  22. if (key === 'IS_TESTING') {
  23. return true;
  24. }
  25. return false;
  26. });
  27. });
  28. afterEach(() => {
  29. closeDb();
  30. });
  31. describe('getDbPath', () => {
  32. it('should return path in config directory', () => {
  33. const configPath = '/test/config/path';
  34. jest.mocked(getConfigDirectoryPath).mockReturnValue(configPath);
  35. expect(getDbPath()).toBe(path.resolve(configPath, 'promptfoo.db'));
  36. });
  37. });
  38. describe('getDbSignalPath', () => {
  39. it('should return evalLastWritten path in config directory', () => {
  40. const configPath = '/test/config/path';
  41. jest.mocked(getConfigDirectoryPath).mockReturnValue(configPath);
  42. expect(getDbSignalPath()).toBe(path.resolve(configPath, 'evalLastWritten'));
  43. });
  44. });
  45. describe('getDb', () => {
  46. beforeEach(() => {
  47. jest.mocked(getEnvBool).mockImplementation((key) => {
  48. if (key === 'IS_TESTING') {
  49. return true;
  50. }
  51. return false;
  52. });
  53. });
  54. it('should return in-memory database when testing', () => {
  55. const db = getDb();
  56. expect(db).toBeDefined();
  57. });
  58. it('should initialize database with WAL mode', () => {
  59. const db = getDb();
  60. expect(db).toBeDefined();
  61. });
  62. it('should return same instance on subsequent calls', () => {
  63. const db1 = getDb();
  64. const db2 = getDb();
  65. expect(db1).toBe(db2);
  66. });
  67. });
  68. describe('DrizzleLogWriter', () => {
  69. it('should log debug message when database logs enabled', () => {
  70. jest.mocked(getEnvBool).mockImplementation((key) => {
  71. if (key === 'PROMPTFOO_ENABLE_DATABASE_LOGS') {
  72. return true;
  73. }
  74. return false;
  75. });
  76. const writer = new DrizzleLogWriter();
  77. writer.write('test message');
  78. expect(logger.debug).toHaveBeenCalledWith('Drizzle: test message');
  79. });
  80. it('should not log debug message when database logs disabled', () => {
  81. jest.mocked(getEnvBool).mockReturnValue(false);
  82. const writer = new DrizzleLogWriter();
  83. writer.write('test message');
  84. expect(logger.debug).not.toHaveBeenCalled();
  85. });
  86. });
  87. describe('closeDb', () => {
  88. it('should close database connection and reset instances', () => {
  89. const _db = getDb();
  90. expect(isDbOpen()).toBe(true);
  91. closeDb();
  92. expect(isDbOpen()).toBe(false);
  93. const newDb = getDb();
  94. expect(newDb).toBeDefined();
  95. expect(isDbOpen()).toBe(true);
  96. });
  97. it('should handle errors when closing database', () => {
  98. const _db = getDb();
  99. closeDb();
  100. closeDb(); // Second close should be handled gracefully
  101. expect(logger.error).not.toHaveBeenCalled();
  102. });
  103. it('should handle close errors gracefully', () => {
  104. const _db = getDb();
  105. // Force an error by closing twice
  106. closeDb();
  107. closeDb();
  108. expect(logger.error).not.toHaveBeenCalled();
  109. });
  110. });
  111. describe('isDbOpen', () => {
  112. it('should return false when database is not initialized', () => {
  113. closeDb(); // Ensure clean state
  114. expect(isDbOpen()).toBe(false);
  115. });
  116. it('should return true when database is open', () => {
  117. const _db = getDb();
  118. expect(isDbOpen()).toBe(true);
  119. });
  120. it('should return false after closing database', () => {
  121. const _db = getDb();
  122. expect(isDbOpen()).toBe(true);
  123. closeDb();
  124. expect(isDbOpen()).toBe(false);
  125. });
  126. });
  127. });
Tip!

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

Comments

Loading...