import { describe, expect, it } from "@jest/globals"; import { setupTestServerContext, setupTestServerHolder } from "../testHelpers/apolloTestServerHelpers"; import { addMockShuttleToRepository, addMockStopToRepository, addMockSystemToRepository } from "../testHelpers/repositorySetupHelpers"; import assert = require("node:assert"); import { NotificationSchedulingArguments } from "../../src/generated/graphql"; describe("MutationResolvers", () => { const holder = setupTestServerHolder() const context = setupTestServerContext(); async function getServerResponse(query: string, notificationInput: { deviceId: string; shuttleId: string; stopId: string }) { return await holder.testServer.executeOperation({ query, variables: { input: notificationInput, } }, { contextValue: context }); } describe("scheduleNotification", () => { const query = ` mutation ScheduleNotification($input: NotificationSchedulingArguments!) { scheduleNotification(input: $input) { success message data { deviceId shuttleId stopId } } } ` function assertFailedResponse(response: any, notificationInput: NotificationSchedulingArguments) { assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const notificationResponse = response.body.singleResult.data?.scheduleNotification as any; expect(notificationResponse.success).toBe(false); expect(context.notificationService.isNotificationScheduled(notificationInput)).toBe(false); } it("adds a notification to the notification service", async () => { const system = await addMockSystemToRepository(context.repository); const shuttle = await addMockShuttleToRepository(context.repository, system.id); const stop = await addMockStopToRepository(context.repository, system.id); const notificationInput = { deviceId: "1", shuttleId: shuttle.id, stopId: stop.id, secondsThreshold: 240, }; const response = await getServerResponse(query, notificationInput); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const expectedNotificationData: any = { ...notificationInput, } delete expectedNotificationData.secondsThreshold; const notificationResponse = response.body.singleResult.data?.scheduleNotification as any; expect(notificationResponse?.success).toBe(true); expect(notificationResponse?.data).toEqual(expectedNotificationData); expect(context.notificationService.getSecondsThresholdForScheduledNotification(expectedNotificationData)).toBe(240); }); it("adds a notification with the default seconds threshold if none is provided", async () => { const system = await addMockSystemToRepository(context.repository); const shuttle = await addMockShuttleToRepository(context.repository, system.id); const stop = await addMockStopToRepository(context.repository, system.id); const notificationInput = { deviceId: "1", shuttleId: shuttle.id, stopId: stop.id, }; const response = await getServerResponse(query, notificationInput); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const notificationResponse = response.body.singleResult.data?.scheduleNotification as any; expect(notificationResponse?.success).toBe(true); expect(context.notificationService.getSecondsThresholdForScheduledNotification(notificationInput)).toBe(180); }); it("fails if the shuttle ID doesn't exist", async () => { const system = await addMockSystemToRepository(context.repository); const stop = await addMockStopToRepository(context.repository, system.id); const notificationInput = { deviceId: "1", shuttleId: "1", stopId: stop.id, } const response = await getServerResponse(query, notificationInput); assertFailedResponse(response, notificationInput); }); it("fails if the stop ID doesn't exist", async () => { const system = await addMockSystemToRepository(context.repository); const shuttle = await addMockShuttleToRepository(context.repository, system.id); const notificationInput = { deviceId: "1", shuttleId: shuttle.id, stopId: "1", } const response = await getServerResponse(query, notificationInput); assertFailedResponse(response, notificationInput); }); }); describe("cancelNotification", () => { const query = ` mutation CancelNotification($input: NotificationLookupArguments!) { cancelNotification(input: $input) { success message data { deviceId shuttleId stopId } } } ` it("removes the notification from the notification service", async () => { const system = await addMockSystemToRepository(context.repository); const shuttle = await addMockShuttleToRepository(context.repository, system.id); const stop = await addMockStopToRepository(context.repository, system.id); const notificationInput: any = { deviceId: "1", shuttleId: shuttle.id, stopId: stop.id, secondsThreshold: 180, } await context.notificationService.scheduleNotification(notificationInput); const notificationLookup = { ...notificationInput } delete notificationLookup.secondsThreshold; const response = await getServerResponse(query, notificationLookup); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const notificationResponse = response.body.singleResult.data?.cancelNotification as any; expect(notificationResponse.success).toBe(true); expect(notificationResponse.data).toEqual(notificationLookup); expect(context.notificationService.isNotificationScheduled(notificationLookup)).toBe(false); }); it("fails if the notification doesn't exist", async () => { const notificationInput = { deviceId: "1", shuttleId: "1", stopId: "1", } const response = await getServerResponse(query, notificationInput); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const notificationResponse = response.body.singleResult.data?.cancelNotification as any; expect(notificationResponse.success).toBe(false); }); }); });