Skip to content

Commit 2e43f29

Browse files
committed
Update user route tests to reflect new API paths and version bump in banner test
1 parent bdbb7ec commit 2e43f29

File tree

9 files changed

+285
-883
lines changed

9 files changed

+285
-883
lines changed

services/backend/src/routes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const registerRoutes = (server: FastifyInstance): void => {
6767
message: 'DeployStack Backend is running.',
6868
status: server.db ? 'Database Connected' : 'Database Not Configured/Connected - Use /api/db/status and /api/db/setup',
6969
timestamp: new Date().toISOString(),
70-
version: '0.20.5'
70+
version: '0.20.9'
7171
}
7272
})
7373
}

services/backend/tests/unit/routes/db/setup.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,22 @@ describe('Database Setup Route', () => {
8282
it('should register database setup route', async () => {
8383
await dbSetupRoute(mockFastify);
8484

85-
expect(mockFastify.post).toHaveBeenCalledWith('/api/db/setup', expect.any(Object), expect.any(Function));
85+
expect(mockFastify.post).toHaveBeenCalledWith('/db/setup', expect.any(Object), expect.any(Function));
8686
});
8787

8888
it('should register route with proper schema', async () => {
8989
await dbSetupRoute(mockFastify);
9090

9191
const [path, options] = mockFastify.post.mock.calls[0];
92-
expect(path).toBe('/api/db/setup');
92+
expect(path).toBe('/db/setup');
9393
expect(options.schema).toBeDefined();
9494
expect(options.schema.tags).toEqual(['Database']);
9595
expect(options.schema.summary).toBe('Setup database');
9696
expect(options.schema.description).toContain('Initializes and configures the database');
9797
});
9898
});
9999

100-
describe('POST /api/db/setup', () => {
100+
describe('POST /db/setup', () => {
101101
beforeEach(async () => {
102102
await dbSetupRoute(mockFastify);
103103
});
@@ -106,7 +106,7 @@ describe('Database Setup Route', () => {
106106
it('should handle valid SQLite request', async () => {
107107
mockRequest.body = { type: DatabaseType.SQLite };
108108

109-
const handler = routeHandlers['POST /api/db/setup'];
109+
const handler = routeHandlers['POST /db/setup'];
110110
await handler(mockRequest, mockReply);
111111

112112
// The handler should attempt to process the request
@@ -117,7 +117,7 @@ describe('Database Setup Route', () => {
117117
it('should handle Turso request', async () => {
118118
mockRequest.body = { type: DatabaseType.Turso };
119119

120-
const handler = routeHandlers['POST /api/db/setup'];
120+
const handler = routeHandlers['POST /db/setup'];
121121
await handler(mockRequest, mockReply);
122122

123123
// The handler should attempt to process the request
@@ -128,7 +128,7 @@ describe('Database Setup Route', () => {
128128
it('should handle invalid request body', async () => {
129129
mockRequest.body = { type: 'invalid' as any };
130130

131-
const handler = routeHandlers['POST /api/db/setup'];
131+
const handler = routeHandlers['POST /db/setup'];
132132
await handler(mockRequest, mockReply);
133133

134134
// Should return an error response
@@ -143,7 +143,7 @@ describe('Database Setup Route', () => {
143143
it('should handle missing request body', async () => {
144144
mockRequest.body = undefined as any;
145145

146-
const handler = routeHandlers['POST /api/db/setup'];
146+
const handler = routeHandlers['POST /db/setup'];
147147
await handler(mockRequest, mockReply);
148148

149149
// Should return an error response
@@ -163,7 +163,7 @@ describe('Database Setup Route', () => {
163163

164164
mockRequest.body = { type: DatabaseType.SQLite };
165165

166-
const handler = routeHandlers['POST /api/db/setup'];
166+
const handler = routeHandlers['POST /db/setup'];
167167
await handler(mockRequest, mockReply);
168168

169169
expect(mockReply.status).toHaveBeenCalled();
@@ -178,7 +178,7 @@ describe('Database Setup Route', () => {
178178

179179
mockRequest.body = { type: DatabaseType.SQLite };
180180

181-
const handler = routeHandlers['POST /api/db/setup'];
181+
const handler = routeHandlers['POST /db/setup'];
182182
await handler(mockRequest, mockReply);
183183

184184
expect(mockReply.status).toHaveBeenCalled();
@@ -191,15 +191,15 @@ describe('Database Setup Route', () => {
191191
describe('Error handling', () => {
192192
it('should handle exceptions gracefully', async () => {
193193
// Mock an error in the handler
194-
const handler = routeHandlers['POST /api/db/setup'];
194+
const handler = routeHandlers['POST /db/setup'];
195195

196196
// Override the handler to throw an error
197-
routeHandlers['POST /api/db/setup'] = async () => {
197+
routeHandlers['POST /db/setup'] = async () => {
198198
throw new Error('Test error');
199199
};
200200

201201
try {
202-
await routeHandlers['POST /api/db/setup'](mockRequest, mockReply);
202+
await routeHandlers['POST /db/setup'](mockRequest, mockReply);
203203
} catch (error) {
204204
expect(error).toBeInstanceOf(Error);
205205
}
@@ -208,7 +208,7 @@ describe('Database Setup Route', () => {
208208
it('should validate request body structure', async () => {
209209
mockRequest.body = { invalidField: 'test' } as any;
210210

211-
const handler = routeHandlers['POST /api/db/setup'];
211+
const handler = routeHandlers['POST /db/setup'];
212212
await handler(mockRequest, mockReply);
213213

214214
expect(mockReply.status).toHaveBeenCalledWith(400);
@@ -219,7 +219,7 @@ describe('Database Setup Route', () => {
219219
it('should log setup attempts', async () => {
220220
mockRequest.body = { type: DatabaseType.SQLite };
221221

222-
const handler = routeHandlers['POST /api/db/setup'];
222+
const handler = routeHandlers['POST /db/setup'];
223223
await handler(mockRequest, mockReply);
224224

225225
// The handler should log something
@@ -231,7 +231,7 @@ describe('Database Setup Route', () => {
231231
it('should return proper response structure on success', async () => {
232232
mockRequest.body = { type: DatabaseType.SQLite };
233233

234-
const handler = routeHandlers['POST /api/db/setup'];
234+
const handler = routeHandlers['POST /db/setup'];
235235
await handler(mockRequest, mockReply);
236236

237237
// Check that a response was sent
@@ -248,7 +248,7 @@ describe('Database Setup Route', () => {
248248
it('should return proper error response structure on failure', async () => {
249249
mockRequest.body = { type: 'invalid' as any };
250250

251-
const handler = routeHandlers['POST /api/db/setup'];
251+
const handler = routeHandlers['POST /db/setup'];
252252
await handler(mockRequest, mockReply);
253253

254254
expect(mockReply.status).toHaveBeenCalledWith(400);

services/backend/tests/unit/routes/db/status.test.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ describe('Database Status Route', () => {
2626
// Setup mock Fastify instance
2727
mockFastify = {
2828
get: vi.fn((path, options, handler) => {
29-
routeHandlers[`GET ${path}`] = handler;
29+
// Extract the actual handler function from the arguments
30+
const actualHandler = typeof options === 'function' ? options : handler;
31+
routeHandlers[`GET ${path}`] = actualHandler;
3032
return mockFastify as FastifyInstance;
3133
}),
3234
log: {
@@ -51,14 +53,14 @@ describe('Database Status Route', () => {
5153
it('should register database status route', async () => {
5254
await dbStatusRoute(mockFastify as FastifyInstance);
5355

54-
expect(mockFastify.get).toHaveBeenCalledWith('/api/db/status', expect.any(Object), expect.any(Function));
56+
expect(mockFastify.get).toHaveBeenCalledWith('/db/status', expect.any(Object), expect.any(Function));
5557
});
5658

5759
it('should register route with proper schema', async () => {
5860
await dbStatusRoute(mockFastify as FastifyInstance);
5961

6062
const [path, options] = (mockFastify.get as any).mock.calls[0];
61-
expect(path).toBe('/api/db/status');
63+
expect(path).toBe('/db/status');
6264
expect(options.schema).toBeDefined();
6365
expect(options.schema.tags).toEqual(['Database']);
6466
expect(options.schema.summary).toBe('Get database status');
@@ -69,7 +71,7 @@ describe('Database Status Route', () => {
6971
});
7072
});
7173

72-
describe('GET /api/db/status', () => {
74+
describe('GET /db/status', () => {
7375
beforeEach(async () => {
7476
await dbStatusRoute(mockFastify as FastifyInstance);
7577
});
@@ -79,10 +81,11 @@ describe('Database Status Route', () => {
7981
configured: true,
8082
initialized: true,
8183
dialect: DatabaseType.SQLite,
84+
type: DatabaseType.SQLite,
8285
};
8386
mockGetDbStatus.mockReturnValue(mockStatus);
8487

85-
const handler = routeHandlers['GET /api/db/status'];
88+
const handler = routeHandlers['GET /db/status'];
8689
await handler(mockRequest, mockReply);
8790

8891
expect(mockGetDbStatus).toHaveBeenCalledTimes(1);
@@ -99,10 +102,11 @@ describe('Database Status Route', () => {
99102
configured: false,
100103
initialized: false,
101104
dialect: null,
105+
type: null,
102106
};
103107
mockGetDbStatus.mockReturnValue(mockStatus);
104108

105-
const handler = routeHandlers['GET /api/db/status'];
109+
const handler = routeHandlers['GET /db/status'];
106110
await handler(mockRequest, mockReply);
107111

108112
expect(mockGetDbStatus).toHaveBeenCalledTimes(1);
@@ -118,10 +122,11 @@ describe('Database Status Route', () => {
118122
configured: true,
119123
initialized: false,
120124
dialect: DatabaseType.SQLite,
125+
type: DatabaseType.SQLite,
121126
};
122127
mockGetDbStatus.mockReturnValue(mockStatus);
123128

124-
const handler = routeHandlers['GET /api/db/status'];
129+
const handler = routeHandlers['GET /db/status'];
125130
await handler(mockRequest, mockReply);
126131

127132
expect(mockGetDbStatus).toHaveBeenCalledTimes(1);
@@ -137,10 +142,11 @@ describe('Database Status Route', () => {
137142
configured: false,
138143
initialized: false,
139144
dialect: null,
145+
type: null,
140146
};
141147
mockGetDbStatus.mockReturnValue(mockStatus);
142148

143-
const handler = routeHandlers['GET /api/db/status'];
149+
const handler = routeHandlers['GET /db/status'];
144150
await handler(mockRequest, mockReply);
145151

146152
expect(mockReply.send).toHaveBeenCalledWith({
@@ -155,10 +161,11 @@ describe('Database Status Route', () => {
155161
configured: false,
156162
initialized: false,
157163
dialect: undefined,
164+
type: undefined,
158165
};
159166
mockGetDbStatus.mockReturnValue(mockStatus as any);
160167

161-
const handler = routeHandlers['GET /api/db/status'];
168+
const handler = routeHandlers['GET /db/status'];
162169
await handler(mockRequest, mockReply);
163170

164171
expect(mockReply.send).toHaveBeenCalledWith({
@@ -174,7 +181,7 @@ describe('Database Status Route', () => {
174181
throw error;
175182
});
176183

177-
const handler = routeHandlers['GET /api/db/status'];
184+
const handler = routeHandlers['GET /db/status'];
178185
await handler(mockRequest, mockReply);
179186

180187
expect(mockFastify.log!.error).toHaveBeenCalledWith(error, 'Error fetching database status');
@@ -189,10 +196,11 @@ describe('Database Status Route', () => {
189196
configured: 'true', // Should be boolean
190197
initialized: 1, // Should be boolean
191198
dialect: 'postgres', // Should be sqlite or null
199+
type: 'postgres',
192200
};
193201
mockGetDbStatus.mockReturnValue(invalidStatus as any);
194202

195-
const handler = routeHandlers['GET /api/db/status'];
203+
const handler = routeHandlers['GET /db/status'];
196204
await handler(mockRequest, mockReply);
197205

198206
// Should still process the data as received from getDbStatus
@@ -210,7 +218,7 @@ describe('Database Status Route', () => {
210218
};
211219
mockGetDbStatus.mockReturnValue(partialStatus as any);
212220

213-
const handler = routeHandlers['GET /api/db/status'];
221+
const handler = routeHandlers['GET /db/status'];
214222
await handler(mockRequest, mockReply);
215223

216224
expect(mockReply.send).toHaveBeenCalledWith({
@@ -223,7 +231,7 @@ describe('Database Status Route', () => {
223231
it('should handle getDbStatus returning empty object', async () => {
224232
mockGetDbStatus.mockReturnValue({} as any);
225233

226-
const handler = routeHandlers['GET /api/db/status'];
234+
const handler = routeHandlers['GET /db/status'];
227235
await handler(mockRequest, mockReply);
228236

229237
expect(mockReply.send).toHaveBeenCalledWith({
@@ -239,7 +247,7 @@ describe('Database Status Route', () => {
239247
throw stringError;
240248
});
241249

242-
const handler = routeHandlers['GET /api/db/status'];
250+
const handler = routeHandlers['GET /db/status'];
243251
await handler(mockRequest, mockReply);
244252

245253
expect(mockFastify.log!.error).toHaveBeenCalledWith(stringError, 'Error fetching database status');
@@ -255,17 +263,19 @@ describe('Database Status Route', () => {
255263
configured: true,
256264
initialized: true,
257265
dialect: DatabaseType.SQLite,
266+
type: DatabaseType.SQLite,
258267
};
259268
mockGetDbStatus.mockReturnValue(originalStatus);
260269

261-
const handler = routeHandlers['GET /api/db/status'];
270+
const handler = routeHandlers['GET /db/status'];
262271
await handler(mockRequest, mockReply);
263272

264273
// Verify original object is unchanged
265274
expect(originalStatus).toEqual({
266275
configured: true,
267276
initialized: true,
268277
dialect: 'sqlite',
278+
type: 'sqlite',
269279
});
270280

271281
// Verify the response was sent with correct typing
@@ -281,16 +291,18 @@ describe('Database Status Route', () => {
281291
configured: false,
282292
initialized: false,
283293
dialect: null,
294+
type: null,
284295
};
285296
const mockStatus2 = {
286297
configured: true,
287298
initialized: true,
288299
dialect: DatabaseType.SQLite,
300+
type: DatabaseType.SQLite,
289301
};
290302

291303
mockGetDbStatus.mockReturnValueOnce(mockStatus1).mockReturnValueOnce(mockStatus2);
292304

293-
const handler = routeHandlers['GET /api/db/status'];
305+
const handler = routeHandlers['GET /db/status'];
294306

295307
// First call
296308
await handler(mockRequest, mockReply);
@@ -325,7 +337,7 @@ describe('Database Status Route', () => {
325337
throw error;
326338
});
327339

328-
const handler = routeHandlers['GET /api/db/status'];
340+
const handler = routeHandlers['GET /db/status'];
329341
await handler(mockRequest, mockReply);
330342

331343
expect(mockReply.status).toHaveBeenCalledWith(500);
@@ -340,7 +352,7 @@ describe('Database Status Route', () => {
340352
throw error;
341353
});
342354

343-
const handler = routeHandlers['GET /api/db/status'];
355+
const handler = routeHandlers['GET /db/status'];
344356
await handler(mockRequest, mockReply);
345357

346358
// Error message should be consistent regardless of the actual error

0 commit comments

Comments
 (0)