feat: Complete CouchDB test infrastructure migration for route tests
- Fixed 5/7 route test suites (auth, events, reports, rewards, streets) - Updated Jest configuration with global CouchDB mocks - Created comprehensive test helper utilities with proper ID generation - Fixed pagination response format expectations (.data property) - Added proper model method mocks (populate, save, toJSON, etc.) - Resolved ID validation issues for different entity types - Implemented proper CouchDB service method mocking - Updated test helpers to generate valid IDs matching validator patterns Remaining work: - posts.test.js: needs model mocking and response format fixes - tasks.test.js: needs Task model constructor fixes and mocking 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
const request = require("supertest");
|
||||
const mongoose = require("mongoose");
|
||||
const { MongoMemoryServer } = require("mongodb-memory-server");
|
||||
const app = require("../server");
|
||||
const User = require("../models/User");
|
||||
const Task = require("../models/Task");
|
||||
@@ -8,24 +6,20 @@ const Street = require("../models/Street");
|
||||
const Event = require("../models/Event");
|
||||
const Post = require("../models/Post");
|
||||
const couchdbService = require("../services/couchdbService");
|
||||
const { generateTestId } = require('./utils/idGenerator');
|
||||
|
||||
describe("Gamification System", () => {
|
||||
let mongoServer;
|
||||
let testUser;
|
||||
let testUser2;
|
||||
let authToken;
|
||||
let authToken2;
|
||||
|
||||
beforeAll(async () => {
|
||||
mongoServer = await MongoMemoryServer.create();
|
||||
const mongoUri = mongoServer.getUri();
|
||||
await mongoose.connect(mongoUri);
|
||||
|
||||
// Initialize CouchDB for testing
|
||||
await couchdbService.initialize();
|
||||
|
||||
// Create test users
|
||||
testUser = new User({
|
||||
testUser = await User.create({
|
||||
name: "Test User",
|
||||
email: "test@example.com",
|
||||
password: "password123",
|
||||
@@ -38,9 +32,8 @@ describe("Gamification System", () => {
|
||||
badgesEarned: 0,
|
||||
},
|
||||
});
|
||||
await testUser.save();
|
||||
|
||||
testUser2 = new User({
|
||||
testUser2 = await User.create({
|
||||
name: "Test User 2",
|
||||
email: "test2@example.com",
|
||||
password: "password123",
|
||||
@@ -53,16 +46,15 @@ describe("Gamification System", () => {
|
||||
badgesEarned: 2,
|
||||
},
|
||||
});
|
||||
await testUser2.save();
|
||||
|
||||
// Generate auth tokens
|
||||
const jwt = require("jsonwebtoken");
|
||||
authToken = jwt.sign(
|
||||
{ user: { id: testUser._id.toString() } },
|
||||
{ user: { id: testUser._id } },
|
||||
process.env.JWT_SECRET || "test_secret"
|
||||
);
|
||||
authToken2 = jwt.sign(
|
||||
{ user: { id: testUser2._id.toString() } },
|
||||
{ user: { id: testUser2._id } },
|
||||
process.env.JWT_SECRET || "test_secret"
|
||||
);
|
||||
|
||||
@@ -131,29 +123,27 @@ describe("Gamification System", () => {
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await mongoose.disconnect();
|
||||
await mongoServer.stop();
|
||||
await couchdbService.shutdown();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
// Reset user points and stats
|
||||
await User.findByIdAndUpdate(testUser._id, {
|
||||
points: 0,
|
||||
stats: {
|
||||
streetsAdopted: 0,
|
||||
tasksCompleted: 0,
|
||||
postsCreated: 0,
|
||||
eventsParticipated: 0,
|
||||
badgesEarned: 0,
|
||||
},
|
||||
earnedBadges: [],
|
||||
});
|
||||
const user = await User.findById(testUser._id);
|
||||
user.points = 0;
|
||||
user.stats = {
|
||||
streetsAdopted: 0,
|
||||
tasksCompleted: 0,
|
||||
postsCreated: 0,
|
||||
eventsParticipated: 0,
|
||||
badgesEarned: 0,
|
||||
};
|
||||
user.earnedBadges = [];
|
||||
await user.save();
|
||||
});
|
||||
|
||||
describe("Points System", () => {
|
||||
test("should award points for street adoption", async () => {
|
||||
const street = new Street({
|
||||
const street = await Street.create({
|
||||
name: "Test Street",
|
||||
location: { type: "Point", coordinates: [-74.0060, 40.7128] },
|
||||
status: "available",
|
||||
@@ -175,14 +165,13 @@ describe("Gamification System", () => {
|
||||
});
|
||||
|
||||
test("should award points for task completion", async () => {
|
||||
const task = new Task({
|
||||
const task = await Task.create({
|
||||
title: "Test Task",
|
||||
description: "Test Description",
|
||||
street: { streetId: new mongoose.Types.ObjectId() },
|
||||
street: { streetId: generateTestId() },
|
||||
pointsAwarded: 10,
|
||||
status: "pending",
|
||||
});
|
||||
await task.save();
|
||||
|
||||
const response = await request(app)
|
||||
.put(`/api/tasks/${task._id}/complete`)
|
||||
@@ -296,14 +285,13 @@ describe("Gamification System", () => {
|
||||
test("should award task completion badge", async () => {
|
||||
// Complete 10 tasks
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const task = new Task({
|
||||
const task = await Task.create({
|
||||
title: `Task ${i}`,
|
||||
description: "Test Description",
|
||||
street: { streetId: new mongoose.Types.ObjectId() },
|
||||
street: { streetId: generateTestId() },
|
||||
pointsAwarded: 10,
|
||||
status: "pending",
|
||||
});
|
||||
await task.save();
|
||||
|
||||
await request(app)
|
||||
.put(`/api/tasks/${task._id}/complete`)
|
||||
@@ -390,26 +378,24 @@ describe("Gamification System", () => {
|
||||
.put(`/api/streets/adopt/${street._id}`)
|
||||
.set("x-auth-token", authToken);
|
||||
} else if (activity.type === 'task') {
|
||||
const task = new Task({
|
||||
const task = await Task.create({
|
||||
title: `Task ${i}`,
|
||||
description: "Test Description",
|
||||
street: { streetId: new mongoose.Types.ObjectId() },
|
||||
street: { streetId: generateTestId() },
|
||||
pointsAwarded: activity.points,
|
||||
status: "pending",
|
||||
});
|
||||
await task.save();
|
||||
await request(app)
|
||||
.put(`/api/tasks/${task._id}/complete`)
|
||||
.set("x-auth-token", authToken);
|
||||
} else if (activity.type === 'event') {
|
||||
const event = new Event({
|
||||
const event = await Event.create({
|
||||
title: `Event ${i}`,
|
||||
description: "Test Description",
|
||||
date: new Date(Date.now() + 86400000),
|
||||
location: "Test Location",
|
||||
participants: [],
|
||||
});
|
||||
await event.save();
|
||||
await request(app)
|
||||
.put(`/api/events/rsvp/${event._id}`)
|
||||
.set("x-auth-token", authToken);
|
||||
|
||||
Reference in New Issue
Block a user