|
| 1 | +import { faker } from "@faker-js/faker"; |
| 2 | +import { zodToMongoSchema } from "@scripts/common/zod-to-mongo-schema"; |
| 3 | +import Migration from "@scripts/migrations/2025.10.03T01.19.59.calendar-schema"; |
| 4 | +import { gcalCalendarList } from "@core/__mocks__/v1/calendarlist/gcal.calendarlist"; |
| 5 | +import { |
| 6 | + CompassCalendarSchema, |
| 7 | + GoogleCalendarMetadataSchema, |
| 8 | +} from "@core/types/calendar.types"; |
| 9 | +import { |
| 10 | + cleanupCollections, |
| 11 | + cleanupTestDb, |
| 12 | + setupTestDb, |
| 13 | +} from "@backend/__tests__/helpers/mock.db.setup"; |
| 14 | +import { Collections } from "@backend/common/constants/collections"; |
| 15 | +import mongoService from "@backend/common/services/mongo.service"; |
| 16 | + |
| 17 | +describe("2025.10.03T01.19.59.calendar-schema", () => { |
| 18 | + const migration = new Migration(); |
| 19 | + const collectionName = Collections.CALENDAR; |
| 20 | + |
| 21 | + beforeAll(setupTestDb); |
| 22 | + beforeEach(cleanupCollections); |
| 23 | + afterEach(() => mongoService.calendar.drop()); |
| 24 | + afterAll(cleanupTestDb); |
| 25 | + |
| 26 | + async function validateUpMigration() { |
| 27 | + const indexes = await mongoService.calendar.indexes(); |
| 28 | + const collectionInfo = await mongoService.calendar.options(); |
| 29 | + const $jsonSchema = zodToMongoSchema(CompassCalendarSchema); |
| 30 | + |
| 31 | + expect(collectionInfo["validationLevel"]).toBe("strict"); |
| 32 | + expect(collectionInfo["validator"]).toBeDefined(); |
| 33 | + expect(collectionInfo["validator"]).toHaveProperty("$jsonSchema"); |
| 34 | + expect(collectionInfo["validator"]["$jsonSchema"]).toEqual($jsonSchema); |
| 35 | + |
| 36 | + expect(indexes).toEqual( |
| 37 | + expect.arrayContaining([ |
| 38 | + expect.objectContaining({ name: "_id_", key: { _id: 1 } }), |
| 39 | + expect.objectContaining({ |
| 40 | + name: "calendar_user_primary_unique", |
| 41 | + unique: true, |
| 42 | + key: { user: 1, primary: 1 }, |
| 43 | + }), |
| 44 | + expect.objectContaining({ |
| 45 | + name: "calendar_user_metadata__id_metadata__provider_unique", |
| 46 | + unique: true, |
| 47 | + key: { user: 1, "metadata.id": 1, "metadata.provider": 1 }, |
| 48 | + }), |
| 49 | + expect.objectContaining({ |
| 50 | + name: "calendar_user_selected_index", |
| 51 | + key: { user: 1, selected: 1 }, |
| 52 | + }), |
| 53 | + ]), |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + it("should create collection with schema and indexes if not exists", async () => { |
| 58 | + const existsBefore = await mongoService.collectionExists(collectionName); |
| 59 | + |
| 60 | + expect(existsBefore).toBe(false); |
| 61 | + |
| 62 | + await migration.up(); |
| 63 | + |
| 64 | + const existsAfter = await mongoService.collectionExists(collectionName); |
| 65 | + |
| 66 | + expect(existsAfter).toBe(true); |
| 67 | + |
| 68 | + await validateUpMigration(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should update collection with schema and indexes if exists", async () => { |
| 72 | + await mongoService.db.createCollection(collectionName); |
| 73 | + |
| 74 | + const existsBefore = await mongoService.collectionExists(collectionName); |
| 75 | + |
| 76 | + expect(existsBefore).toBe(true); |
| 77 | + |
| 78 | + await migration.up(); |
| 79 | + |
| 80 | + const existsAfter = await mongoService.collectionExists(collectionName); |
| 81 | + |
| 82 | + expect(existsAfter).toBe(true); |
| 83 | + |
| 84 | + await validateUpMigration(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should remove schema and indexes but not collection on down", async () => { |
| 88 | + await migration.up(); |
| 89 | + |
| 90 | + const existsBefore = await mongoService.collectionExists(collectionName); |
| 91 | + |
| 92 | + expect(existsBefore).toBe(true); |
| 93 | + |
| 94 | + await validateUpMigration(); |
| 95 | + |
| 96 | + await migration.down(); |
| 97 | + |
| 98 | + const existsAfter = await mongoService.collectionExists(collectionName); |
| 99 | + |
| 100 | + expect(existsAfter).toBe(true); |
| 101 | + |
| 102 | + const indexes = await mongoService.calendar.indexes(); |
| 103 | + const collectionInfo = await mongoService.calendar.options(); |
| 104 | + |
| 105 | + expect(indexes).toHaveLength(1); |
| 106 | + expect(indexes).toEqual([ |
| 107 | + expect.objectContaining({ name: "_id_", key: { _id: 1 } }), |
| 108 | + ]); |
| 109 | + |
| 110 | + expect(collectionInfo["validationLevel"]).toBe("off"); |
| 111 | + expect(collectionInfo["validationAction"]).toBe("error"); |
| 112 | + expect(collectionInfo).not.toHaveProperty("validator"); |
| 113 | + }); |
| 114 | + |
| 115 | + describe("mongo $jsonSchema validation", () => { |
| 116 | + const maxIndex = gcalCalendarList.items!.length - 1; |
| 117 | + |
| 118 | + function generateCompassCalendar() { |
| 119 | + const calendarIndex = faker.number.int({ min: 0, max: maxIndex }); |
| 120 | + const gCalendarEntry = gcalCalendarList.items![calendarIndex]!; |
| 121 | + const gCalendar = GoogleCalendarMetadataSchema.parse(gCalendarEntry); |
| 122 | + |
| 123 | + return CompassCalendarSchema.parse({ |
| 124 | + _id: faker.database.mongodbObjectId(), |
| 125 | + user: faker.database.mongodbObjectId(), |
| 126 | + backgroundColor: gCalendarEntry.backgroundColor!, |
| 127 | + color: gCalendarEntry.foregroundColor!, |
| 128 | + primary: gCalendarEntry.primary!, |
| 129 | + selected: gCalendarEntry.selected!, |
| 130 | + timezone: gCalendarEntry.timeZone!, |
| 131 | + createdAt: new Date(), |
| 132 | + metadata: gCalendar, |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + beforeEach(migration.up.bind(migration)); |
| 137 | + |
| 138 | + it("should accept valid calendar document", async () => { |
| 139 | + const calendar = generateCompassCalendar(); |
| 140 | + |
| 141 | + await expect(mongoService.calendar.insertOne(calendar)).resolves.toEqual( |
| 142 | + expect.objectContaining({ |
| 143 | + acknowledged: true, |
| 144 | + insertedId: calendar._id, |
| 145 | + }), |
| 146 | + ); |
| 147 | + }); |
| 148 | + |
| 149 | + it("should reject calendar with missing 'user' field", async () => { |
| 150 | + const calendar = generateCompassCalendar(); |
| 151 | + // @ts-expect-error testing missing user field |
| 152 | + delete calendar.user; |
| 153 | + |
| 154 | + await expect(mongoService.calendar.insertOne(calendar)).rejects.toThrow( |
| 155 | + /Document failed validation/, |
| 156 | + ); |
| 157 | + }); |
| 158 | + |
| 159 | + it("should reject calendar with missing 'primary' field", async () => { |
| 160 | + const calendar = generateCompassCalendar(); |
| 161 | + // @ts-expect-error testing missing primary field |
| 162 | + delete calendar.primary; |
| 163 | + |
| 164 | + await expect(mongoService.calendar.insertOne(calendar)).rejects.toThrow( |
| 165 | + /Document failed validation/, |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should reject calendar with missing 'selected' field", async () => { |
| 170 | + const calendar = generateCompassCalendar(); |
| 171 | + // @ts-expect-error testing missing selected field |
| 172 | + delete calendar.selected; |
| 173 | + |
| 174 | + await expect(mongoService.calendar.insertOne(calendar)).rejects.toThrow( |
| 175 | + /Document failed validation/, |
| 176 | + ); |
| 177 | + }); |
| 178 | + |
| 179 | + it("should reject calendar with missing 'color' field", async () => { |
| 180 | + const calendar = generateCompassCalendar(); |
| 181 | + // @ts-expect-error testing missing color field |
| 182 | + delete calendar.color; |
| 183 | + |
| 184 | + await expect(mongoService.calendar.insertOne(calendar)).rejects.toThrow( |
| 185 | + /Document failed validation/, |
| 186 | + ); |
| 187 | + }); |
| 188 | + |
| 189 | + it("should reject calendar with missing 'backgroundColor' field", async () => { |
| 190 | + const calendar = generateCompassCalendar(); |
| 191 | + // @ts-expect-error testing missing backgroundColor field |
| 192 | + delete calendar.backgroundColor; |
| 193 | + |
| 194 | + await expect(mongoService.calendar.insertOne(calendar)).rejects.toThrow( |
| 195 | + /Document failed validation/, |
| 196 | + ); |
| 197 | + }); |
| 198 | + |
| 199 | + it("should reject calendar with missing 'createdAt' field", async () => { |
| 200 | + const calendar = generateCompassCalendar(); |
| 201 | + // @ts-expect-error testing missing createdAt field |
| 202 | + delete calendar.createdAt; |
| 203 | + |
| 204 | + await expect(mongoService.calendar.insertOne(calendar)).rejects.toThrow( |
| 205 | + /Document failed validation/, |
| 206 | + ); |
| 207 | + }); |
| 208 | + }); |
| 209 | +}); |
0 commit comments