Files
aniplay-api/src/mocks/gcloud.ts
2024-10-05 16:48:31 -04:00

77 lines
2.2 KiB
TypeScript

import { HttpResponse, http } from "msw";
import type { FcmMessagePayload } from "~/libs/gcloud/sendFcmMessage";
export function mockFcmMessageResponse() {
return http.post<{}, { message: FcmMessagePayload; validate_only: boolean }>(
"https://fcm.googleapis.com/v1/projects/test-26g38/messages:send",
async ({ request }) => {
const { message } = await request.json();
const { name, token } = message;
if (name === "token_verification") {
if (token?.length === 163) {
return HttpResponse.json({ name });
}
return HttpResponse.json({
error: {
code: 400,
message:
"The registration token is not a valid FCM registration token",
status: "INVALID_ARGUMENT",
details: [
{
"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
errorCode: "INVALID_ARGUMENT",
},
],
},
});
}
return HttpResponse.json(message);
},
);
}
export function mockCreateGcloudTask() {
return http.post<{}, { task: { name: string } }>(
"https://content-cloudtasks.googleapis.com/v2/projects/test-26g38/locations/northamerica-northeast1/queues/new-episode/tasks",
async ({ request }) => {
const {
task: { name },
} = await request.json();
return HttpResponse.json({ name });
},
);
}
export function mockDeleteGcloudTask() {
return http.delete<{ taskId: string }>(
"https://content-cloudtasks.googleapis.com/v2/projects/test-26g38/locations/northamerica-northeast1/queues/new-episode/tasks/:taskId",
async ({ params }) => {
const { taskId } = params;
if (taskId === "123") {
return HttpResponse.json({
error: {
code: 404,
message: "Task not found",
status: "NOT_FOUND",
details: [
{
"@type": "type.googleapis.com/google.rpc.Status",
code: 5,
message: "Task not found",
},
],
},
});
}
return HttpResponse.json({ messageId: "123" });
},
);
}