mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
189 lines
6.7 KiB
TypeScript
189 lines
6.7 KiB
TypeScript
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 { NotificationInput } 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: NotificationInput!) {
|
|
scheduleNotification(input: $input) {
|
|
success
|
|
message
|
|
data {
|
|
deviceId
|
|
shuttleId
|
|
stopId
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
async function assertFailedResponse(response: any, notificationInput: 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(false);
|
|
|
|
expect(await context.notificationRepository.isNotificationScheduled(notificationInput)).toBe(false);
|
|
}
|
|
|
|
|
|
it("adds a notification to the notification service", async () => {
|
|
const system = await addMockSystemToRepository(context.shuttleRepository);
|
|
const shuttle = await addMockShuttleToRepository(context.shuttleRepository, system.id);
|
|
const stop = await addMockStopToRepository(context.shuttleRepository, 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(await context.notificationRepository.getSecondsThresholdForNotificationIfExists(expectedNotificationData)).toBe(240);
|
|
});
|
|
|
|
it("adds a notification with the default seconds threshold if none is provided", async () => {
|
|
const system = await addMockSystemToRepository(context.shuttleRepository);
|
|
const shuttle = await addMockShuttleToRepository(context.shuttleRepository, system.id);
|
|
const stop = await addMockStopToRepository(context.shuttleRepository, 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(await context.notificationRepository.getSecondsThresholdForNotificationIfExists(notificationInput)).toBe(180);
|
|
});
|
|
|
|
it("fails if the shuttle ID doesn't exist", async () => {
|
|
const system = await addMockSystemToRepository(context.shuttleRepository);
|
|
const stop = await addMockStopToRepository(context.shuttleRepository, system.id);
|
|
|
|
const notificationInput = {
|
|
deviceId: "1",
|
|
shuttleId: "1",
|
|
stopId: stop.id,
|
|
}
|
|
const response = await getServerResponse(query, notificationInput);
|
|
await assertFailedResponse(response, notificationInput);
|
|
});
|
|
|
|
it("fails if the stop ID doesn't exist", async () => {
|
|
const system = await addMockSystemToRepository(context.shuttleRepository);
|
|
const shuttle = await addMockShuttleToRepository(context.shuttleRepository, system.id);
|
|
|
|
const notificationInput = {
|
|
deviceId: "1",
|
|
shuttleId: shuttle.id,
|
|
stopId: "1",
|
|
}
|
|
const response = await getServerResponse(query, notificationInput);
|
|
|
|
await assertFailedResponse(response, notificationInput);
|
|
});
|
|
});
|
|
|
|
describe("cancelNotification", () => {
|
|
const query = `
|
|
mutation CancelNotification($input: NotificationInput!) {
|
|
cancelNotification(input: $input) {
|
|
success
|
|
message
|
|
data {
|
|
deviceId
|
|
shuttleId
|
|
stopId
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
it("removes the notification from the notification service", async () => {
|
|
const system = await addMockSystemToRepository(context.shuttleRepository);
|
|
const shuttle = await addMockShuttleToRepository(context.shuttleRepository, system.id);
|
|
const stop = await addMockStopToRepository(context.shuttleRepository, system.id);
|
|
|
|
const notificationInput: any = {
|
|
deviceId: "1",
|
|
shuttleId: shuttle.id,
|
|
stopId: stop.id,
|
|
secondsThreshold: 180,
|
|
}
|
|
await context.notificationRepository.addOrUpdateNotification(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(await context.notificationRepository.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);
|
|
});
|
|
});
|
|
});
|