@studious-lms/server 1.2.45 → 1.2.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +45 -0
- package/.env.test.example +37 -0
- package/README.md +34 -7
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/clover.xml +12110 -0
- package/coverage/coverage-final.json +44 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +221 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/server/index.html +116 -0
- package/coverage/server/src/exportType.ts.html +109 -0
- package/coverage/server/src/index.html +161 -0
- package/coverage/server/src/index.ts.html +1702 -0
- package/coverage/server/src/instrument.ts.html +130 -0
- package/coverage/server/src/lib/config/env.ts.html +448 -0
- package/coverage/server/src/lib/config/index.html +116 -0
- package/coverage/server/src/lib/fileUpload.ts.html +1138 -0
- package/coverage/server/src/lib/googleCloudStorage.ts.html +334 -0
- package/coverage/server/src/lib/index.html +206 -0
- package/coverage/server/src/lib/jsonConversion.ts.html +2323 -0
- package/coverage/server/src/lib/jsonStyles.ts.html +193 -0
- package/coverage/server/src/lib/notificationHandler.ts.html +193 -0
- package/coverage/server/src/lib/pusher.ts.html +121 -0
- package/coverage/server/src/lib/thumbnailGenerator.ts.html +592 -0
- package/coverage/server/src/middleware/auth.ts.html +646 -0
- package/coverage/server/src/middleware/index.html +146 -0
- package/coverage/server/src/middleware/logging.ts.html +244 -0
- package/coverage/server/src/middleware/security.ts.html +271 -0
- package/coverage/server/src/routers/_app.ts.html +232 -0
- package/coverage/server/src/routers/agenda.ts.html +319 -0
- package/coverage/server/src/routers/announcement.ts.html +3481 -0
- package/coverage/server/src/routers/assignment.ts.html +7633 -0
- package/coverage/server/src/routers/attendance.ts.html +1030 -0
- package/coverage/server/src/routers/auth.ts.html +1081 -0
- package/coverage/server/src/routers/class.ts.html +3535 -0
- package/coverage/server/src/routers/comment.ts.html +991 -0
- package/coverage/server/src/routers/conversation.ts.html +982 -0
- package/coverage/server/src/routers/event.ts.html +1609 -0
- package/coverage/server/src/routers/file.ts.html +1144 -0
- package/coverage/server/src/routers/folder.ts.html +2797 -0
- package/coverage/server/src/routers/index.html +386 -0
- package/coverage/server/src/routers/labChat.ts.html +3073 -0
- package/coverage/server/src/routers/marketing.ts.html +340 -0
- package/coverage/server/src/routers/message.ts.html +1912 -0
- package/coverage/server/src/routers/notifications.ts.html +364 -0
- package/coverage/server/src/routers/section.ts.html +1120 -0
- package/coverage/server/src/routers/user.ts.html +862 -0
- package/coverage/server/src/routers/worksheet.ts.html +1729 -0
- package/coverage/server/src/trpc.ts.html +397 -0
- package/coverage/server/src/types/index.html +116 -0
- package/coverage/server/src/types/trpc.ts.html +127 -0
- package/coverage/server/src/utils/aiUser.ts.html +280 -0
- package/coverage/server/src/utils/email.ts.html +121 -0
- package/coverage/server/src/utils/generateInviteCode.ts.html +106 -0
- package/coverage/server/src/utils/index.html +206 -0
- package/coverage/server/src/utils/inference.ts.html +709 -0
- package/coverage/server/src/utils/logger.ts.html +664 -0
- package/coverage/server/src/utils/prismaErrorHandler.ts.html +907 -0
- package/coverage/server/src/utils/prismaWrapper.ts.html +355 -0
- package/coverage/server/vitest.config.ts.html +196 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +83 -52
- package/dist/index.js.map +1 -1
- package/dist/instrument.js +15 -8
- package/dist/instrument.js.map +1 -1
- package/dist/lib/config/env.d.ts +169 -0
- package/dist/lib/config/env.d.ts.map +1 -0
- package/dist/lib/config/env.js +115 -0
- package/dist/lib/config/env.js.map +1 -0
- package/dist/lib/fileUpload.d.ts.map +1 -1
- package/dist/lib/fileUpload.js +5 -4
- package/dist/lib/fileUpload.js.map +1 -1
- package/dist/lib/googleCloudStorage.d.ts.map +1 -1
- package/dist/lib/googleCloudStorage.js +7 -8
- package/dist/lib/googleCloudStorage.js.map +1 -1
- package/dist/lib/jsonConversion.d.ts.map +1 -1
- package/dist/lib/jsonConversion.js +14 -16
- package/dist/lib/jsonConversion.js.map +1 -1
- package/dist/lib/notificationHandler.d.ts +2 -2
- package/dist/lib/prisma.d.ts +2 -2
- package/dist/lib/prisma.d.ts.map +1 -1
- package/dist/lib/prisma.js +22 -3
- package/dist/lib/prisma.js.map +1 -1
- package/dist/lib/pusher.d.ts.map +1 -1
- package/dist/lib/pusher.js +8 -7
- package/dist/lib/pusher.js.map +1 -1
- package/dist/middleware/auth.d.ts.map +1 -1
- package/dist/middleware/auth.js +7 -5
- package/dist/middleware/auth.js.map +1 -1
- package/dist/middleware/security.d.ts +5 -0
- package/dist/middleware/security.d.ts.map +1 -0
- package/dist/middleware/security.js +77 -0
- package/dist/middleware/security.js.map +1 -0
- package/dist/routers/_app.d.ts +368 -108
- package/dist/routers/_app.d.ts.map +1 -1
- package/dist/routers/_app.js +4 -2
- package/dist/routers/_app.js.map +1 -1
- package/dist/routers/agenda.d.ts.map +1 -1
- package/dist/routers/agenda.js +12 -9
- package/dist/routers/agenda.js.map +1 -1
- package/dist/routers/announcement.d.ts +8 -0
- package/dist/routers/announcement.d.ts.map +1 -1
- package/dist/routers/announcement.js +6 -4
- package/dist/routers/announcement.js.map +1 -1
- package/dist/routers/assignment.d.ts +17 -4
- package/dist/routers/assignment.d.ts.map +1 -1
- package/dist/routers/assignment.js +51 -19
- package/dist/routers/assignment.js.map +1 -1
- package/dist/routers/attendance.d.ts +1 -0
- package/dist/routers/attendance.d.ts.map +1 -1
- package/dist/routers/attendance.js +4 -4
- package/dist/routers/attendance.js.map +1 -1
- package/dist/routers/auth.d.ts +20 -0
- package/dist/routers/auth.d.ts.map +1 -1
- package/dist/routers/auth.js +132 -15
- package/dist/routers/auth.js.map +1 -1
- package/dist/routers/class.d.ts +10 -0
- package/dist/routers/class.d.ts.map +1 -1
- package/dist/routers/class.js +49 -5
- package/dist/routers/class.js.map +1 -1
- package/dist/routers/comment.d.ts +2 -0
- package/dist/routers/comment.d.ts.map +1 -1
- package/dist/routers/conversation.d.ts +2 -0
- package/dist/routers/conversation.d.ts.map +1 -1
- package/dist/routers/conversation.js +46 -31
- package/dist/routers/conversation.js.map +1 -1
- package/dist/routers/file.d.ts.map +1 -1
- package/dist/routers/file.js +30 -7
- package/dist/routers/file.js.map +1 -1
- package/dist/routers/labChat.d.ts +2 -0
- package/dist/routers/labChat.d.ts.map +1 -1
- package/dist/routers/labChat.js +5 -322
- package/dist/routers/labChat.js.map +1 -1
- package/dist/routers/marketing.d.ts +1 -1
- package/dist/routers/message.d.ts +1 -0
- package/dist/routers/message.d.ts.map +1 -1
- package/dist/routers/message.js +3 -2
- package/dist/routers/message.js.map +1 -1
- package/dist/routers/newtonChat.d.ts +55 -0
- package/dist/routers/newtonChat.d.ts.map +1 -0
- package/dist/routers/newtonChat.js +262 -0
- package/dist/routers/newtonChat.js.map +1 -0
- package/dist/routers/notifications.d.ts +4 -4
- package/dist/routers/section.d.ts +19 -4
- package/dist/routers/section.d.ts.map +1 -1
- package/dist/routers/section.js +26 -8
- package/dist/routers/section.js.map +1 -1
- package/dist/routers/user.d.ts.map +1 -1
- package/dist/routers/user.js +5 -4
- package/dist/routers/user.js.map +1 -1
- package/dist/routers/worksheet.d.ts +44 -41
- package/dist/routers/worksheet.d.ts.map +1 -1
- package/dist/routers/worksheet.js +25 -34
- package/dist/routers/worksheet.js.map +1 -1
- package/dist/seedDatabase.d.ts +1 -1
- package/dist/seedDatabase.js +275 -284
- package/dist/seedDatabase.js.map +1 -1
- package/dist/server/pipelines/aiLabChat.d.ts +21 -0
- package/dist/server/pipelines/aiLabChat.d.ts.map +1 -0
- package/dist/server/pipelines/aiLabChat.js +456 -0
- package/dist/server/pipelines/aiLabChat.js.map +1 -0
- package/dist/server/pipelines/aiNewtonChat.d.ts +30 -0
- package/dist/server/pipelines/aiNewtonChat.d.ts.map +1 -0
- package/dist/server/pipelines/aiNewtonChat.js +280 -0
- package/dist/server/pipelines/aiNewtonChat.js.map +1 -0
- package/dist/server/pipelines/gradeWorksheet.d.ts +15 -0
- package/dist/server/pipelines/gradeWorksheet.d.ts.map +1 -0
- package/dist/server/pipelines/gradeWorksheet.js +139 -0
- package/dist/server/pipelines/gradeWorksheet.js.map +1 -0
- package/dist/trpc.d.ts.map +1 -1
- package/dist/trpc.js +2 -2
- package/dist/trpc.js.map +1 -1
- package/dist/utils/email.d.ts +9 -1
- package/dist/utils/email.d.ts.map +1 -1
- package/dist/utils/email.js +20 -5
- package/dist/utils/email.js.map +1 -1
- package/dist/utils/inference.d.ts +5 -0
- package/dist/utils/inference.d.ts.map +1 -1
- package/dist/utils/inference.js +71 -7
- package/dist/utils/inference.js.map +1 -1
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +3 -3
- package/dist/utils/logger.js.map +1 -1
- package/docker-compose.yml +14 -0
- package/package.json +13 -4
- package/prisma/schema.prisma +34 -5
- package/scripts/test-pre-push.ts +14 -0
- package/src/index.ts +98 -54
- package/src/instrument.ts +13 -6
- package/src/lib/config/env.ts +126 -0
- package/src/lib/fileUpload.ts +3 -2
- package/src/lib/googleCloudStorage.ts +6 -6
- package/src/lib/jsonConversion.ts +12 -14
- package/src/lib/prisma.ts +23 -2
- package/src/lib/pusher.ts +6 -5
- package/src/middleware/auth.ts +5 -3
- package/src/middleware/security.ts +80 -0
- package/src/routers/_app.ts +2 -0
- package/src/routers/agenda.ts +10 -7
- package/src/routers/announcement.ts +4 -2
- package/src/routers/assignment.ts +74 -41
- package/src/routers/attendance.ts +2 -2
- package/src/routers/auth.ts +143 -14
- package/src/routers/class.ts +52 -3
- package/src/routers/conversation.ts +49 -29
- package/src/routers/file.ts +29 -5
- package/src/routers/labChat.ts +3 -367
- package/src/routers/message.ts +1 -1
- package/src/routers/newtonChat.ts +299 -0
- package/src/routers/section.ts +26 -6
- package/src/routers/user.ts +3 -2
- package/src/routers/worksheet.ts +26 -38
- package/src/seedDatabase.ts +290 -283
- package/src/server/pipelines/aiLabChat.ts +507 -0
- package/src/server/pipelines/aiNewtonChat.ts +338 -0
- package/src/server/pipelines/gradeWorksheet.ts +151 -0
- package/src/trpc.ts +2 -0
- package/src/utils/email.ts +30 -3
- package/src/utils/inference.ts +85 -5
- package/src/utils/logger.ts +2 -1
- package/tests/announcement.test.ts +164 -0
- package/tests/assignment.test.ts +296 -0
- package/tests/attendance.test.ts +168 -0
- package/tests/auth.test.ts +33 -10
- package/tests/class.test.ts +34 -9
- package/tests/event.test.ts +228 -0
- package/tests/section.test.ts +216 -0
- package/tests/setup.ts +70 -16
- package/tests/user.test.ts +158 -0
- package/vitest.config.ts +26 -0
- package/API_SPECIFICATION.md +0 -1597
- package/BASE64_REMOVAL_SUMMARY.md +0 -164
- package/CHAT_API_SPEC.md +0 -579
- package/LAB_CHAT_API_SPEC.md +0 -518
- package/dist/routers/school.d.ts +0 -208
- package/dist/routers/school.d.ts.map +0 -1
- package/dist/routers/school.js +0 -483
package/tests/class.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { test, expect, describe, beforeEach } from 'vitest';
|
|
1
|
+
import { test, expect, describe, beforeEach, afterEach } from 'vitest';
|
|
2
2
|
import { appRouter } from '../src/routers/_app';
|
|
3
3
|
import { createTRPCContext } from '../src/trpc';
|
|
4
4
|
import { prisma } from '../src/lib/prisma';
|
|
@@ -7,26 +7,33 @@ import { caller, user1Caller, user2Caller } from './setup';
|
|
|
7
7
|
describe('Class Router', () => {
|
|
8
8
|
let testClass: any;
|
|
9
9
|
let testClass2: any;
|
|
10
|
+
const createdClassIds: string[] = [];
|
|
10
11
|
|
|
11
12
|
beforeEach(async () => {
|
|
12
|
-
// Create test classes for each test
|
|
13
|
+
// Create test classes for each test with unique names to avoid conflicts
|
|
14
|
+
const timestamp = Date.now();
|
|
13
15
|
testClass = await user1Caller.class.create({
|
|
14
|
-
name:
|
|
16
|
+
name: `Test Class 1`,
|
|
15
17
|
subject: 'Mathematics',
|
|
16
18
|
section: '10th Grade',
|
|
17
19
|
});
|
|
18
20
|
|
|
19
21
|
testClass2 = await user2Caller.class.create({
|
|
20
|
-
name:
|
|
22
|
+
name: `Test Class 2`,
|
|
21
23
|
subject: 'Science',
|
|
22
24
|
section: '11th Grade',
|
|
23
25
|
});
|
|
26
|
+
|
|
27
|
+
// Track created classes for cleanup
|
|
28
|
+
if (testClass?.id) createdClassIds.push(testClass.id);
|
|
29
|
+
if (testClass2?.id) createdClassIds.push(testClass2.id);
|
|
24
30
|
});
|
|
25
31
|
|
|
26
32
|
describe('create', () => {
|
|
27
33
|
test('should create a class successfully', async () => {
|
|
28
34
|
expect(testClass).toBeDefined();
|
|
29
|
-
expect(testClass.
|
|
35
|
+
expect(testClass.id).toBeDefined();
|
|
36
|
+
expect(testClass.name).toContain('Test Class 1');
|
|
30
37
|
expect(testClass.subject).toBe('Mathematics');
|
|
31
38
|
expect(testClass.section).toBe('10th Grade');
|
|
32
39
|
});
|
|
@@ -52,10 +59,11 @@ describe('Class Router', () => {
|
|
|
52
59
|
});
|
|
53
60
|
|
|
54
61
|
test('should fail to create class with missing required fields', async () => {
|
|
62
|
+
|
|
63
|
+
// @ts-expect-error - test case
|
|
55
64
|
await expect(user1Caller.class.create({
|
|
56
65
|
name: '',
|
|
57
|
-
|
|
58
|
-
section: '10th Grade',
|
|
66
|
+
color: '#3B82F6',
|
|
59
67
|
})).rejects.toThrow();
|
|
60
68
|
});
|
|
61
69
|
});
|
|
@@ -72,8 +80,9 @@ describe('Class Router', () => {
|
|
|
72
80
|
|
|
73
81
|
test('should include user\'s own classes as teacher', async () => {
|
|
74
82
|
const classes = await user1Caller.class.getAll();
|
|
75
|
-
const userClass = classes.teacherInClass.find((c: any) => c.
|
|
83
|
+
const userClass = classes.teacherInClass.find((c: any) => c.id === testClass.id);
|
|
76
84
|
expect(userClass).toBeDefined();
|
|
85
|
+
expect(userClass?.id).toBe(testClass.id);
|
|
77
86
|
});
|
|
78
87
|
|
|
79
88
|
test('should fail to get classes without authentication', async () => {
|
|
@@ -152,6 +161,12 @@ describe('Class Router', () => {
|
|
|
152
161
|
expect(result).toBeDefined();
|
|
153
162
|
expect(result.deletedClass).toBeDefined();
|
|
154
163
|
expect(result.deletedClass.id).toBe(testClass.id);
|
|
164
|
+
|
|
165
|
+
// Remove from cleanup list since it's already deleted
|
|
166
|
+
const index = createdClassIds.indexOf(testClass.id);
|
|
167
|
+
if (index > -1) {
|
|
168
|
+
createdClassIds.splice(index, 1);
|
|
169
|
+
}
|
|
155
170
|
});
|
|
156
171
|
|
|
157
172
|
test('should fail to delete class user is not teacher of', async () => {
|
|
@@ -212,10 +227,12 @@ describe('Class Router', () => {
|
|
|
212
227
|
test('should remove member successfully', async () => {
|
|
213
228
|
// First add a student
|
|
214
229
|
const user2Profile = await user2Caller.user.getProfile();
|
|
215
|
-
await user1Caller.class.addStudent({
|
|
230
|
+
const addResult = await user1Caller.class.addStudent({
|
|
216
231
|
classId: testClass.id,
|
|
217
232
|
studentId: user2Profile.id,
|
|
218
233
|
});
|
|
234
|
+
expect(addResult).toBeDefined();
|
|
235
|
+
expect(addResult.newStudent).toBeDefined();
|
|
219
236
|
|
|
220
237
|
// Then remove them
|
|
221
238
|
const result = await user1Caller.class.removeMember({
|
|
@@ -240,12 +257,20 @@ describe('Class Router', () => {
|
|
|
240
257
|
test('should join class with class code successfully', async () => {
|
|
241
258
|
// First create an invite code
|
|
242
259
|
const inviteCode = await user1Caller.class.createInviteCode({ classId: testClass.id });
|
|
260
|
+
expect(inviteCode).toBeDefined();
|
|
261
|
+
expect(inviteCode.code).toBeDefined();
|
|
243
262
|
|
|
244
263
|
// Then join with the code
|
|
245
264
|
const result = await user2Caller.class.join({ classCode: inviteCode.code });
|
|
246
265
|
expect(result).toBeDefined();
|
|
247
266
|
expect(result.joinedClass).toBeDefined();
|
|
248
267
|
expect(result.joinedClass.id).toBe(testClass.id);
|
|
268
|
+
|
|
269
|
+
// Clean up: remove student from class
|
|
270
|
+
await user1Caller.class.removeMember({
|
|
271
|
+
classId: testClass.id,
|
|
272
|
+
userId: (await user2Caller.user.getProfile()).id,
|
|
273
|
+
});
|
|
249
274
|
});
|
|
250
275
|
|
|
251
276
|
test('should fail to join with invalid class code', async () => {
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { test, expect, describe, beforeEach } from 'vitest';
|
|
2
|
+
import { user1Caller, user2Caller } from './setup';
|
|
3
|
+
import { createTRPCContext } from '../src/trpc';
|
|
4
|
+
import { appRouter } from '../src/routers/_app';
|
|
5
|
+
import { prisma } from '../src/lib/prisma';
|
|
6
|
+
import { startOfWeek } from 'date-fns';
|
|
7
|
+
|
|
8
|
+
describe('Event Router', () => {
|
|
9
|
+
let testClass: any;
|
|
10
|
+
let testEvent: any;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
// Create a test class for user1
|
|
14
|
+
testClass = await user1Caller.class.create({
|
|
15
|
+
name: 'Test Class for Events',
|
|
16
|
+
subject: 'Mathematics',
|
|
17
|
+
section: '10th Grade',
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// Create a test event
|
|
21
|
+
const startTime = new Date();
|
|
22
|
+
startTime.setHours(10, 0, 0, 0);
|
|
23
|
+
const endTime = new Date(startTime);
|
|
24
|
+
endTime.setHours(11, 0, 0, 0);
|
|
25
|
+
|
|
26
|
+
testEvent = await user1Caller.event.create({
|
|
27
|
+
name: 'Test Event',
|
|
28
|
+
location: 'Room 101',
|
|
29
|
+
remarks: 'Test remarks',
|
|
30
|
+
startTime: startTime.toISOString(),
|
|
31
|
+
endTime: endTime.toISOString(),
|
|
32
|
+
classId: testClass.id,
|
|
33
|
+
color: '#3B82F6',
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('create', () => {
|
|
38
|
+
test('should create event successfully', async () => {
|
|
39
|
+
expect(testEvent).toBeDefined();
|
|
40
|
+
expect(testEvent.event).toBeDefined();
|
|
41
|
+
expect(testEvent.event.name).toBe('Test Event');
|
|
42
|
+
expect(testEvent.event.location).toBe('Room 101');
|
|
43
|
+
expect(testEvent.event.remarks).toBe('Test remarks');
|
|
44
|
+
expect(testEvent.event.classId).toBe(testClass.id);
|
|
45
|
+
expect(testEvent.event.color).toBe('#3B82F6');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('should create event without class', async () => {
|
|
49
|
+
const startTime = new Date();
|
|
50
|
+
startTime.setHours(14, 0, 0, 0);
|
|
51
|
+
const endTime = new Date(startTime);
|
|
52
|
+
endTime.setHours(15, 0, 0, 0);
|
|
53
|
+
|
|
54
|
+
const event = await user1Caller.event.create({
|
|
55
|
+
name: 'Personal Event',
|
|
56
|
+
location: 'Home',
|
|
57
|
+
startTime: startTime.toISOString(),
|
|
58
|
+
endTime: endTime.toISOString(),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
expect(event.event).toBeDefined();
|
|
62
|
+
expect(event.event.name).toBe('Personal Event');
|
|
63
|
+
expect(event.event.classId).toBeNull();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('should fail to create event for class user is not teacher of', async () => {
|
|
67
|
+
const startTime = new Date();
|
|
68
|
+
startTime.setHours(10, 0, 0, 0);
|
|
69
|
+
const endTime = new Date(startTime);
|
|
70
|
+
endTime.setHours(11, 0, 0, 0);
|
|
71
|
+
|
|
72
|
+
await expect(user2Caller.event.create({
|
|
73
|
+
name: 'Unauthorized Event',
|
|
74
|
+
startTime: startTime.toISOString(),
|
|
75
|
+
endTime: endTime.toISOString(),
|
|
76
|
+
classId: testClass.id,
|
|
77
|
+
})).rejects.toThrow();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('should fail without authentication', async () => {
|
|
81
|
+
const invalidCaller = await createTRPCContext({
|
|
82
|
+
req: { headers: {} } as any,
|
|
83
|
+
res: {} as any,
|
|
84
|
+
});
|
|
85
|
+
const router = appRouter.createCaller(invalidCaller);
|
|
86
|
+
|
|
87
|
+
const startTime = new Date();
|
|
88
|
+
await expect(router.event.create({
|
|
89
|
+
name: 'Test',
|
|
90
|
+
startTime: startTime.toISOString(),
|
|
91
|
+
endTime: startTime.toISOString(),
|
|
92
|
+
})).rejects.toThrow();
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('get', () => {
|
|
97
|
+
test('should get event successfully', async () => {
|
|
98
|
+
const event = await user1Caller.event.get({
|
|
99
|
+
id: testEvent.event.id,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
expect(event).toBeDefined();
|
|
103
|
+
expect(event.event.id).toBe(testEvent.event.id);
|
|
104
|
+
expect(event.event.name).toBe('Test Event');
|
|
105
|
+
expect(event.event.class).toBeDefined();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('should fail to get event user does not own', async () => {
|
|
109
|
+
await expect(user2Caller.event.get({
|
|
110
|
+
id: testEvent.event.id,
|
|
111
|
+
})).rejects.toThrow();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('should fail to get non-existent event', async () => {
|
|
115
|
+
await expect(user1Caller.event.get({
|
|
116
|
+
id: 'non-existent-id',
|
|
117
|
+
})).rejects.toThrow();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('update', () => {
|
|
122
|
+
test('should update event successfully', async () => {
|
|
123
|
+
const startTime = new Date();
|
|
124
|
+
startTime.setHours(12, 0, 0, 0);
|
|
125
|
+
const endTime = new Date(startTime);
|
|
126
|
+
endTime.setHours(13, 0, 0, 0);
|
|
127
|
+
|
|
128
|
+
const updated = await user1Caller.event.update({
|
|
129
|
+
id: testEvent.event.id,
|
|
130
|
+
data: {
|
|
131
|
+
name: 'Updated Event Name',
|
|
132
|
+
location: 'Room 202',
|
|
133
|
+
remarks: 'Updated remarks',
|
|
134
|
+
startTime: startTime.toISOString(),
|
|
135
|
+
endTime: endTime.toISOString(),
|
|
136
|
+
color: '#10B981',
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
expect(updated.event.name).toBe('Updated Event Name');
|
|
141
|
+
expect(updated.event.location).toBe('Room 202');
|
|
142
|
+
expect(updated.event.remarks).toBe('Updated remarks');
|
|
143
|
+
expect(updated.event.color).toBe('#10B981');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// @todo: not implemented partial updates
|
|
147
|
+
// test('should update event with partial data', async () => {
|
|
148
|
+
// const updated = await user1Caller.event.update({
|
|
149
|
+
// id: testEvent.event.id,
|
|
150
|
+
// data: {
|
|
151
|
+
// name: 'Partially Updated Event',
|
|
152
|
+
// },
|
|
153
|
+
// });
|
|
154
|
+
|
|
155
|
+
// expect(updated.event.name).toBe('Partially Updated Event');
|
|
156
|
+
// expect(updated.event.location).toBe('Room 101'); // Should remain unchanged
|
|
157
|
+
// });
|
|
158
|
+
|
|
159
|
+
test('should fail to update event user does not own', async () => {
|
|
160
|
+
await expect(user2Caller.event.update({
|
|
161
|
+
id: testEvent.event.id,
|
|
162
|
+
data: {
|
|
163
|
+
name: 'Unauthorized Update',
|
|
164
|
+
location: 'Room 101',
|
|
165
|
+
remarks: 'Updated remarks',
|
|
166
|
+
startTime: testEvent.event.startTime,
|
|
167
|
+
endTime: testEvent.event.endTime,
|
|
168
|
+
color: '#10B981',
|
|
169
|
+
},
|
|
170
|
+
})).rejects.toThrow();
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('delete', () => {
|
|
175
|
+
test('should delete event successfully', async () => {
|
|
176
|
+
const result = await user1Caller.event.delete({
|
|
177
|
+
id: testEvent.event.id,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
expect(result.success).toBe(true);
|
|
181
|
+
|
|
182
|
+
// Verify event is deleted
|
|
183
|
+
await expect(user1Caller.event.get({
|
|
184
|
+
id: testEvent.event.id,
|
|
185
|
+
})).rejects.toThrow();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test('should fail to delete event user does not own', async () => {
|
|
189
|
+
await expect(user2Caller.event.delete({
|
|
190
|
+
id: testEvent.event.id,
|
|
191
|
+
})).rejects.toThrow();
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe('getAll', () => {
|
|
196
|
+
test('should get all events for user', async () => {
|
|
197
|
+
// Create another event
|
|
198
|
+
const startTime = new Date();
|
|
199
|
+
startTime.setHours(15, 0, 0, 0);
|
|
200
|
+
const endTime = new Date(startTime);
|
|
201
|
+
endTime.setHours(16, 0, 0, 0);
|
|
202
|
+
|
|
203
|
+
const secondEvent = await user1Caller.event.create({
|
|
204
|
+
name: 'Second Event',
|
|
205
|
+
startTime: startTime.toISOString(),
|
|
206
|
+
endTime: endTime.toISOString(),
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const events = await user1Caller.agenda.get({
|
|
210
|
+
weekStart: startOfWeek(startTime).toISOString(),
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
expect(events.events).toBeDefined();
|
|
214
|
+
expect(events.events.personal.length).toBeGreaterThanOrEqual(2);
|
|
215
|
+
expect(events.events.personal.some((e) => e.id === secondEvent.event.id)).toBe(true);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('should return empty array for user with no events', async () => {
|
|
219
|
+
const events = await user2Caller.agenda.get({
|
|
220
|
+
weekStart: new Date().toISOString(),
|
|
221
|
+
});
|
|
222
|
+
expect(events.events.personal).toBeDefined();
|
|
223
|
+
expect(Array.isArray(events.events.personal)).toBe(true);
|
|
224
|
+
expect(events.events.personal.length).toBe(0);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { test, expect, describe, beforeEach } from 'vitest';
|
|
2
|
+
import { user1Caller, user2Caller } from './setup';
|
|
3
|
+
import { createTRPCContext } from '../src/trpc';
|
|
4
|
+
import { appRouter } from '../src/routers/_app';
|
|
5
|
+
|
|
6
|
+
describe('Section Router', () => {
|
|
7
|
+
let testClass: any;
|
|
8
|
+
let testSection: any;
|
|
9
|
+
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
// Create a test class
|
|
12
|
+
testClass = await user1Caller.class.create({
|
|
13
|
+
name: 'Test Class for Sections',
|
|
14
|
+
subject: 'Mathematics',
|
|
15
|
+
section: '10th Grade',
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// Create a test section
|
|
19
|
+
testSection = await user1Caller.section.create({
|
|
20
|
+
classId: testClass.id,
|
|
21
|
+
name: 'Test Section',
|
|
22
|
+
color: '#3B82F6',
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('create', () => {
|
|
27
|
+
test('should create section successfully', async () => {
|
|
28
|
+
expect(testSection).toBeDefined();
|
|
29
|
+
expect(testSection.id).toBeDefined();
|
|
30
|
+
expect(testSection.name).toBe('Test Section');
|
|
31
|
+
expect(testSection.color).toBe('#3B82F6');
|
|
32
|
+
expect(testSection.classId).toBe(testClass.id);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('should create section without color', async () => {
|
|
36
|
+
const section = await user1Caller.section.create({
|
|
37
|
+
classId: testClass.id,
|
|
38
|
+
name: 'Section Without Color',
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
expect(section).toBeDefined();
|
|
42
|
+
expect(section.name).toBe('Section Without Color');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('should fail to create section for class user is not teacher of', async () => {
|
|
46
|
+
await expect(user2Caller.section.create({
|
|
47
|
+
classId: testClass.id,
|
|
48
|
+
name: 'Unauthorized Section',
|
|
49
|
+
})).rejects.toThrow();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('should fail without authentication', async () => {
|
|
53
|
+
const invalidCaller = await createTRPCContext({
|
|
54
|
+
req: { headers: {} } as any,
|
|
55
|
+
res: {} as any,
|
|
56
|
+
});
|
|
57
|
+
const router = appRouter.createCaller(invalidCaller);
|
|
58
|
+
|
|
59
|
+
await expect(router.section.create({
|
|
60
|
+
classId: testClass.id,
|
|
61
|
+
name: 'Test Section',
|
|
62
|
+
})).rejects.toThrow();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('update', () => {
|
|
67
|
+
test('should update section successfully', async () => {
|
|
68
|
+
const updated = await user1Caller.section.update({
|
|
69
|
+
id: testSection.id,
|
|
70
|
+
classId: testClass.id,
|
|
71
|
+
name: 'Updated Section Name',
|
|
72
|
+
color: '#10B981',
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
expect(updated.name).toBe('Updated Section Name');
|
|
76
|
+
expect(updated.color).toBe('#10B981');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('should update section with partial data', async () => {
|
|
80
|
+
const updated = await user1Caller.section.update({
|
|
81
|
+
id: testSection.id,
|
|
82
|
+
classId: testClass.id,
|
|
83
|
+
name: 'Partially Updated Section',
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
expect(updated.name).toBe('Partially Updated Section');
|
|
87
|
+
expect(updated.color).toBe('#3B82F6'); // Should remain unchanged
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('should fail to update section user is not teacher of', async () => {
|
|
91
|
+
await expect(user2Caller.section.update({
|
|
92
|
+
id: testSection.id,
|
|
93
|
+
classId: testClass.id,
|
|
94
|
+
name: 'Unauthorized Update',
|
|
95
|
+
})).rejects.toThrow();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe('delete', () => {
|
|
100
|
+
test('should delete section successfully', async () => {
|
|
101
|
+
// Create a new section to delete
|
|
102
|
+
const newSection = await user1Caller.section.create({
|
|
103
|
+
classId: testClass.id,
|
|
104
|
+
name: 'To Be Deleted',
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const result = await user1Caller.section.delete({
|
|
108
|
+
id: newSection.id,
|
|
109
|
+
classId: testClass.id,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
expect(result.id).toBe(newSection.id);
|
|
113
|
+
|
|
114
|
+
// Verify section is deleted by trying to update it
|
|
115
|
+
await expect(user1Caller.section.update({
|
|
116
|
+
id: newSection.id,
|
|
117
|
+
classId: testClass.id,
|
|
118
|
+
name: 'Test',
|
|
119
|
+
})).rejects.toThrow();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('should fail to delete section user is not teacher of', async () => {
|
|
123
|
+
await expect(user2Caller.section.delete({
|
|
124
|
+
id: testSection.id,
|
|
125
|
+
classId: testClass.id,
|
|
126
|
+
})).rejects.toThrow();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('reorder', () => {
|
|
131
|
+
test('should reorder section to start', async () => {
|
|
132
|
+
// Create another section
|
|
133
|
+
const section2 = await user1Caller.section.create({
|
|
134
|
+
classId: testClass.id,
|
|
135
|
+
name: 'Second Section',
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const originalOrder = section2.order;
|
|
139
|
+
const result = await user1Caller.section.reorder({
|
|
140
|
+
classId: testClass.id,
|
|
141
|
+
movedId: section2.id,
|
|
142
|
+
position: 'start',
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
expect(result).toBeDefined();
|
|
146
|
+
expect(result?.order).toBeDefined();
|
|
147
|
+
// When moved to start, order should be less than original
|
|
148
|
+
expect(result?.order).toBeLessThanOrEqual(originalOrder || Infinity);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('should reorder section to end', async () => {
|
|
152
|
+
const section2 = await user1Caller.section.create({
|
|
153
|
+
classId: testClass.id,
|
|
154
|
+
name: 'Third Section',
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const originalOrder = section2.order;
|
|
158
|
+
const result = await user1Caller.section.reorder({
|
|
159
|
+
classId: testClass.id,
|
|
160
|
+
movedId: section2.id,
|
|
161
|
+
position: 'end',
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
expect(result).toBeDefined();
|
|
165
|
+
expect(result?.order).toBeDefined();
|
|
166
|
+
// When moved to end, order should be greater than or equal to original
|
|
167
|
+
expect(result?.order).toBeGreaterThanOrEqual(originalOrder || 0);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('should reorder section before another', async () => {
|
|
171
|
+
const section2 = await user1Caller.section.create({
|
|
172
|
+
classId: testClass.id,
|
|
173
|
+
name: 'Fourth Section',
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const targetOrder = testSection.order;
|
|
177
|
+
const result = await user1Caller.section.reorder({
|
|
178
|
+
classId: testClass.id,
|
|
179
|
+
movedId: section2.id,
|
|
180
|
+
position: 'before',
|
|
181
|
+
targetId: testSection.id,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
expect(result).toBeDefined();
|
|
185
|
+
expect(result?.order).toBeDefined();
|
|
186
|
+
// When moved before target, order should be less than target's order
|
|
187
|
+
expect(result?.order).toBeLessThan(targetOrder || Infinity);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('should fail to reorder section user is not teacher of', async () => {
|
|
191
|
+
// Create a separate class that user2 is not part of
|
|
192
|
+
const otherClass = await user1Caller.class.create({
|
|
193
|
+
name: 'Other Class for Reorder Test',
|
|
194
|
+
subject: 'Science',
|
|
195
|
+
section: '11th Grade',
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const otherSection = await user1Caller.section.create({
|
|
199
|
+
classId: otherClass.id,
|
|
200
|
+
name: 'Other Section',
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// user2 is not a member of otherClass, so they shouldn't be able to reorder
|
|
204
|
+
// Note: The reorder endpoint uses protectedProcedure, not protectedTeacherProcedure
|
|
205
|
+
// It only checks if the section exists, not teacher permissions
|
|
206
|
+
// So if user2 can see the section (e.g., if they're a student), reorder will succeed
|
|
207
|
+
// This test verifies that non-class members can't reorder
|
|
208
|
+
await expect(user2Caller.section.reorder({
|
|
209
|
+
classId: otherClass.id,
|
|
210
|
+
movedId: otherSection.id,
|
|
211
|
+
position: 'start',
|
|
212
|
+
})).rejects.toThrow();
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|