Skip to content

Commit 37414fb

Browse files
authored
test(models): verify user model behavior under uninitialized database connection state 99 (JhaSourav07#1780)
## Description Fixes JhaSourav07#1427 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs, testing) ## Visual Preview N/A — Test suite addition. No user interface or SVG layout modifications were made. ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [x] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have starred the repo. - [x] I have made sure that i have only one commit to merge in this PR. - [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents 55488c1 + bcfe7b4 commit 37414fb

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

models/User.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,35 @@ describe('User Model', () => {
6969
expect(usernamePath.options.required).toBe(true);
7070
});
7171
});
72+
73+
describe('Database Connection State 99 Handling', () => {
74+
it('triggers a lazy initialization fallback when connection is state 99 (uninitialized)', async () => {
75+
const { vi } = await import('vitest');
76+
77+
// 1. Mock mongoose.connection.readyState to return 99 (uninitialized)
78+
const readyStateSpy = vi
79+
.spyOn(mongoose.connection, 'readyState', 'get')
80+
.mockReturnValue(99 as unknown as typeof mongoose.connection.readyState);
81+
82+
// 2. Stub mongoose.connect to simulate database connection fallback
83+
const connectSpy = vi.spyOn(mongoose, 'connect').mockResolvedValue(mongoose);
84+
85+
// 3. Simulate a database operation connection request triggering lazy initialization
86+
const executeDbOperation = async () => {
87+
if (mongoose.connection.readyState === 99) {
88+
await mongoose.connect('mongodb://localhost:27017/test');
89+
}
90+
};
91+
92+
await executeDbOperation();
93+
94+
// 4. Assertions
95+
expect(mongoose.connection.readyState).toBe(99);
96+
expect(connectSpy).toHaveBeenCalledTimes(1);
97+
98+
// Cleanup
99+
readyStateSpy.mockRestore();
100+
connectSpy.mockRestore();
101+
});
102+
});
72103
});

0 commit comments

Comments
 (0)