From fcf95cd32be8629685b870f6a4e4ce462d716ad7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=EC=A1=B0=EB=8C=80=ED=9D=AC?= <joedaehui@ajou.ac.kr>
Date: Wed, 4 Dec 2024 18:46:41 +0900
Subject: [PATCH] =?UTF-8?q?test:=20=EB=B3=80=EA=B2=BD=EB=90=9C=20=EC=8A=A4?=
 =?UTF-8?q?=EC=BC=80=EC=A4=84=20=EB=A1=9C=EC=A7=81=20=ED=85=8C=EC=8A=A4?=
 =?UTF-8?q?=ED=8A=B8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 services/modifyScheduleResponse.test.js | 187 ++++++++++++++++++++++++
 1 file changed, 187 insertions(+)
 create mode 100644 services/modifyScheduleResponse.test.js

diff --git a/services/modifyScheduleResponse.test.js b/services/modifyScheduleResponse.test.js
new file mode 100644
index 0000000..64ab78c
--- /dev/null
+++ b/services/modifyScheduleResponse.test.js
@@ -0,0 +1,187 @@
+// test/scheduleService.test.js
+const sequelize = require('../config/sequelize');
+const { Schedule, User, Meeting, MeetingParticipant, FcmToken } = require('../models');
+const ScheduleService = require('../services/scheduleService');
+const MeetingService = require('../services/meetingService');
+const ChatRooms = require('../schemas/chatRooms');
+
+describe('Schedule Service and Meeting Integration Tests', () => {
+    beforeAll(async () => {
+        await sequelize.sync({ force: true });
+    });
+
+    beforeEach(async () => {
+        await MeetingParticipant.destroy({ where: {} });
+        await Meeting.destroy({ where: {} });
+        await Schedule.destroy({ where: {} });
+        await User.destroy({ where: {} });
+        await FcmToken.destroy({ where: {} });
+
+        // 더미 사용자 생성
+        await User.bulkCreate([
+            { id: 1, name: 'Alice', email: 'alice@example.com' },
+            { id: 2, name: 'Bob', email: 'bob@example.com' },
+            { id: 3, name: 'Charlie', email: 'charlie@example.com' }
+        ]);
+
+        // ChatRooms Mock 설정
+        jest.spyOn(ChatRooms.prototype, 'save').mockResolvedValue(undefined);
+        jest.spyOn(ChatRooms, 'findOne').mockResolvedValue({
+            participants: [],
+            isOnline: new Map(),
+            lastReadAt: new Map(),
+            lastReadLogId: new Map(),
+            save: jest.fn().mockResolvedValue(true)
+        });
+    });
+
+    afterAll(async () => {
+        await sequelize.close();
+    });
+
+    describe('Schedule Service Tests', () => {
+        test('should create schedule with time_indices', async () => {
+            const scheduleData = {
+                userId: 1,
+                title: 'Test Schedule',
+                is_fixed: true,
+                time_indices: [36, 37, 38]
+            };
+
+            const schedule = await ScheduleService.createSchedules(scheduleData);
+
+            expect(schedule).toBeDefined();
+            expect(schedule.title).toBe('Test Schedule');
+            expect(schedule.time_indices).toEqual([36, 37, 38]);
+
+            const dbSchedules = await Schedule.findAll({
+                where: { user_id: 1, title: 'Test Schedule' }
+            });
+            expect(dbSchedules.length).toBe(3);
+        });
+
+        test('should update schedule with new time_indices', async () => {
+            await ScheduleService.createSchedules({
+                userId: 1,
+                title: 'Original Schedule',
+                is_fixed: true,
+                time_indices: [36, 37, 38]
+            });
+
+            const updateData = {
+                originalTitle: 'Original Schedule',
+                title: 'Updated Schedule',
+                is_fixed: true,
+                time_indices: [36, 37, 38, 39]
+            };
+
+            const updatedSchedule = await ScheduleService.updateSchedules(1, updateData);
+
+            expect(updatedSchedule.title).toBe('Updated Schedule');
+            expect(updatedSchedule.time_indices).toEqual([36, 37, 38, 39]);
+        });
+
+        test('should delete schedule by title', async () => {
+            await ScheduleService.createSchedules({
+                userId: 1,
+                title: 'Schedule to Delete',
+                is_fixed: true,
+                time_indices: [40, 41, 42]
+            });
+
+            const result = await ScheduleService.deleteSchedules(1, 'Schedule to Delete');
+            expect(result.deletedCount).toBe(3);
+
+            const remainingSchedules = await Schedule.findAll({
+                where: { user_id: 1, title: 'Schedule to Delete' }
+            });
+            expect(remainingSchedules.length).toBe(0);
+        });
+    });
+
+    describe('Meeting Integration Tests', () => {
+        beforeEach(() => {
+            jest.spyOn(User, 'findOne').mockResolvedValue({
+                id: 1,
+                name: 'Alice',
+                email: 'alice@example.com',
+                fcmTokenList: []
+            });
+        });
+
+        test('should create meeting with correct schedules', async () => {
+            const meetingData = {
+                title: 'Test Meeting',
+                time_idx_start: 50,
+                time_idx_end: 52,
+                created_by: 1,
+                type: 'OPEN',
+                max_num: 5
+            };
+
+            const meeting = await MeetingService.createMeeting(meetingData);
+
+            const creatorSchedules = await Schedule.findAll({
+                where: {
+                    user_id: 1,
+                    title: `번개 모임: ${meetingData.title}`
+                }
+            });
+
+            expect(creatorSchedules.length).toBe(3);
+            expect(creatorSchedules.map(s => s.time_idx).sort()).toEqual([50, 51, 52]);
+        });
+
+        test('should create correct schedules when joining meeting', async () => {
+            const meetingData = {
+                title: 'Join Test Meeting',
+                time_idx_start: 60,
+                time_idx_end: 62,
+                created_by: 1,
+                type: 'OPEN',
+                max_num: 5,
+                time_idx_deadline: 59 
+            };
+
+            const meeting = await MeetingService.createMeeting(meetingData);
+            jest.spyOn(MeetingService, 'getCurrentTimeIdx').mockReturnValue(58);
+
+            await MeetingService.joinMeeting(meeting.meeting_id, 2);
+
+            const participantSchedules = await Schedule.findAll({
+                where: {
+                    user_id: 2,
+                    title: `번개 모임: ${meetingData.title}`
+                }
+            });
+
+            expect(participantSchedules.length).toBe(3);
+            expect(participantSchedules.map(s => s.time_idx).sort()).toEqual([60, 61, 62]);
+        });
+
+        test('should handle schedule conflicts correctly', async () => {
+            await ScheduleService.createSchedules({
+                userId: 2,
+                title: 'Existing Schedule',
+                is_fixed: true,
+                time_indices: [70, 71]
+            });
+
+            const meetingData = {
+                title: 'Conflict Test Meeting',
+                time_idx_start: 70,
+                time_idx_end: 72,
+                created_by: 1,
+                type: 'OPEN',
+                max_num: 5,
+                time_idx_deadline: 69 
+            };
+
+            const meeting = await MeetingService.createMeeting(meetingData);
+
+            await expect(
+                MeetingService.joinMeeting(meeting.meeting_id, 2)
+            ).rejects.toThrow('스케줄이 겹칩니다');
+        });
+    });
+});
\ No newline at end of file
-- 
GitLab