Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ApnsDeliveryQueue.test.ts

79 lines2.6 KB

1import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto";
2import { describe, expect, it } from "@effect/vitest";
3import * as Cloudflare from "alchemy/Cloudflare";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Redacted from "effect/Redacted";
7​
8import * as RelayConfiguration from "../Config.ts";
9import * as ApnsDeliveryQueue from "./ApnsDeliveryQueue.ts";
10​
11const config: RelayConfiguration.RelayConfiguration["Service"] = {
12 relayIssuer: "https://relay.example.com",
13 apns: {
14 teamId: "team-1",
15 keyId: "key-1",
16 privateKey: Redacted.make("apns-private-key"),
17 bundleId: "com.t3tools.test",
18 environment: "sandbox",
19 },
20 clerkSecretKey: Redacted.make("clerk-secret"),
21 clerkPublishableKey: "pk_test_test",
22 clerkJwtAudience: "t3-code-relay",
23 apnsDeliveryJobSigningSecret: Redacted.make("apns-job-secret"),
24 cloudMintPrivateKey: Redacted.make("cloud-private-key"),
25 cloudMintPublicKey: "cloud-public-key",
26 managedEndpointBaseDomain: undefined,
27 managedEndpointNamespace: undefined,
28};
29​
30describe("ApnsDeliveryQueue", () => {
31 it.effect("preserves job identity and the queue sender cause", () => {
32 const cause = new Error("queue unavailable");
33 const senderCause = new Cloudflare.QueueSendError({
34 message: cause.message,
35 cause,
36 });
37 const layer = ApnsDeliveryQueue.layer.pipe(
38 Layer.provide(NodeCryptoLayer.layer),
39 Layer.provide(RelayConfiguration.layer(config)),
40 Layer.provide(
41 Layer.succeed(ApnsDeliveryQueue.ApnsDeliveryQueueSender, {
42 send: () => Effect.fail(senderCause),
43 }),
44 ),
45 );
46​
47 return Effect.gen(function* () {
48 const queue = yield* ApnsDeliveryQueue.ApnsDeliveryQueue;
49 const error = yield* Effect.flip(
50 queue.enqueuePushNotification({
51 userId: "user-1",
52 deviceId: "device-1",
53 token: "push-token",
54 notification: {
55 title: "Thread",
56 body: "Input: Project",
57 environmentId: "env-1",
58 threadId: "thread-1",
59 deepLink: "/threads/env-1/thread-1",
60 },
61 }),
62 );
63​
64 expect(error).toMatchObject({
65 _tag: "ApnsDeliveryQueueSendError",
66 operation: "send",
67 jobId: expect.any(String),
68 kind: "push_notification",
69 userId: "user-1",
70 deviceId: "device-1",
71 cause: senderCause,
72 });
73 expect(senderCause.cause).toBe(cause);
74 expect(error.message).toBe(
75 "Failed to enqueue APNs push notification delivery during send for device device-1.",
76 );
77 }).pipe(Effect.provide(layer));
78 });
79});