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

ApnsClient.test.ts

257 lines7.9 KB

1import * as NodeCrypto from "node:crypto";
2​
3import { EnvironmentId, ThreadId } from "@t3tools/contracts";
4import type { RelayAgentActivityAggregateState } from "@t3tools/contracts/relay";
5import { describe, expect, it } from "@effect/vitest";
6import * as DateTime from "effect/DateTime";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as Redacted from "effect/Redacted";
10import * as Schema from "effect/Schema";
11import * as HttpClient from "effect/unstable/http/HttpClient";
12import * as HttpClientError from "effect/unstable/http/HttpClientError";
13​
14import type { ApnsCredentials } from "../Config.ts";
15import * as ApnsClient from "./ApnsClient.ts";
16​
17const isApnsJwtSigningError = Schema.is(ApnsClient.ApnsJwtSigningError);
18const isApnsHttpRequestError = Schema.is(ApnsClient.ApnsHttpRequestError);
19​
20const TestLayer = ApnsClient.layer.pipe(
21 Layer.provide(
22 Layer.succeed(
23 HttpClient.HttpClient,
24 HttpClient.make(() => Effect.die("unexpected APNs HTTP request")),
25 ),
26 ),
27);
28​
29describe("ApnsClient", () => {
30 const now = DateTime.makeUnsafe(0);
31 const state: RelayAgentActivityAggregateState = {
32 title: "T3 Code",
33 subtitle: "Agent work in progress",
34 activeCount: 1,
35 updatedAt: DateTime.formatIso(now),
36 activities: [
37 {
38 environmentId: EnvironmentId.make("env"),
39 threadId: ThreadId.make("thread"),
40 projectTitle: "Project",
41 threadTitle: "Thread",
42 modelTitle: "gpt-5.4",
43 phase: "running" as const,
44 status: "Working",
45 updatedAt: DateTime.formatIso(now),
46 deepLink: "/",
47 },
48 ],
49 };
50​
51 it.effect("requests an update push token when remotely starting a Live Activity", () =>
52 Effect.gen(function* () {
53 const apns = yield* ApnsClient.ApnsClient;
54 const request = apns.makeLiveActivityRequest({
55 event: "start",
56 token: "token",
57 state,
58 nowEpochSeconds: Math.floor(now.epochMilliseconds / 1_000),
59 nowIso: DateTime.formatIso(now),
60 });
61​
62 expect(request.priority).toBe("10");
63 expect(request.payload).toMatchObject({
64 aps: {
65 event: "start",
66 "attributes-type": "LiveActivityAttributes",
67 "input-push-token": 1,
68 "content-state": {
69 name: "AgentActivity",
70 },
71 },
72 });
73 }).pipe(Effect.provide(TestLayer)),
74 );
75​
76 it.effect("builds a low-priority update payload", () =>
77 Effect.gen(function* () {
78 const apns = yield* ApnsClient.ApnsClient;
79 const request = apns.makeLiveActivityRequest({
80 event: "update",
81 token: "token",
82 state,
83 nowEpochSeconds: Math.floor(now.epochMilliseconds / 1_000),
84 nowIso: DateTime.formatIso(now),
85 });
86​
87 expect(request.priority).toBe("5");
88 expect(request.payload).toMatchObject({
89 aps: {
90 event: "update",
91 "content-state": {
92 name: "AgentActivity",
93 },
94 },
95 });
96 }).pipe(Effect.provide(TestLayer)),
97 );
98​
99 it.effect("builds an end payload with a dismissal date", () =>
100 Effect.gen(function* () {
101 const apns = yield* ApnsClient.ApnsClient;
102 const request = apns.makeLiveActivityRequest({
103 event: "end",
104 token: "token",
105 state,
106 nowEpochSeconds: Math.floor(now.epochMilliseconds / 1_000),
107 nowIso: DateTime.formatIso(now),
108 });
109​
110 expect(request.priority).toBe("10");
111 expect(request.payload).toMatchObject({
112 aps: {
113 event: "end",
114 "dismissal-date": 300,
115 },
116 });
117 }).pipe(Effect.provide(TestLayer)),
118 );
119​
120 it.effect("builds a standard APNs alert payload with routing metadata", () =>
121 Effect.gen(function* () {
122 const apns = yield* ApnsClient.ApnsClient;
123 const request = apns.makePushNotificationRequest({
124 token: "push-token",
125 notification: {
126 title: "Thread",
127 body: "Input: Project",
128 environmentId: "env",
129 threadId: "thread",
130 deepLink: "/threads/env/thread",
131 },
132 });
133​
134 expect(request.priority).toBe("10");
135 expect(request.payload).toMatchObject({
136 aps: {
137 alert: {
138 title: "Thread",
139 body: "Input: Project",
140 },
141 sound: "default",
142 },
143 environmentId: "env",
144 threadId: "thread",
145 deepLink: "/threads/env/thread",
146 });
147 }).pipe(Effect.provide(TestLayer)),
148 );
149​
150 it.effect("preserves JWT signing context and the crypto cause", () =>
151 Effect.gen(function* () {
152 const apns = yield* ApnsClient.ApnsClient;
153 const request = apns.makePushNotificationRequest({
154 token: "push-token",
155 notification: {
156 title: "Thread",
157 body: "Input: Project",
158 environmentId: "env",
159 threadId: "thread",
160 deepLink: "/threads/env/thread",
161 },
162 });
163 const error = yield* Effect.flip(
164 apns.sendPushNotificationRequest({
165 credentials: {
166 teamId: "team-1",
167 keyId: "key-1",
168 privateKey: Redacted.make("not-a-private-key"),
169 bundleId: "com.t3tools.test",
170 environment: "sandbox",
171 },
172 request,
173 issuedAtUnixSeconds: 123,
174 }),
175 );
176​
177 expect(isApnsJwtSigningError(error)).toBe(true);
178 if (!isApnsJwtSigningError(error)) {
179 return yield* Effect.die("expected APNs JWT signing error");
180 }
181 expect(error).toMatchObject({
182 teamId: "team-1",
183 keyId: "key-1",
184 issuedAtUnixSeconds: 123,
185 cause: expect.any(Error),
186 message: "Failed to sign APNs JWT for key key-1.",
187 });
188 }).pipe(Effect.provide(TestLayer)),
189 );
190​
191 it.effect("preserves APNs request context and the HTTP cause", () => {
192 const httpCause = new Error("network unavailable");
193 const { privateKey } = NodeCrypto.generateKeyPairSync("ec", {
194 namedCurve: "prime256v1",
195 privateKeyEncoding: { type: "pkcs8", format: "pem" },
196 publicKeyEncoding: { type: "spki", format: "pem" },
197 });
198 const credentials = {
199 teamId: "team-1",
200 keyId: "key-1",
201 privateKey: Redacted.make(privateKey),
202 bundleId: "com.t3tools.test",
203 environment: "sandbox",
204 } satisfies ApnsCredentials;
205 const failingHttpClient = HttpClient.make((request) =>
206 Effect.fail(
207 new HttpClientError.HttpClientError({
208 reason: new HttpClientError.TransportError({ request, cause: httpCause }),
209 }),
210 ),
211 );
212 const layer = ApnsClient.layer.pipe(
213 Layer.provide(Layer.succeed(HttpClient.HttpClient, failingHttpClient)),
214 );
215​
216 return Effect.gen(function* () {
217 const apns = yield* ApnsClient.ApnsClient;
218 const request = apns.makePushNotificationRequest({
219 token: "long-push-token",
220 notification: {
221 title: "Thread",
222 body: "Input: Project",
223 environmentId: "env",
224 threadId: "thread",
225 deepLink: "/threads/env/thread",
226 },
227 });
228 const error = yield* Effect.flip(
229 apns.sendPushNotificationRequest({
230 credentials,
231 request,
232 issuedAtUnixSeconds: 123,
233 }),
234 );
235​
236 expect(isApnsHttpRequestError(error)).toBe(true);
237 if (!isApnsHttpRequestError(error)) {
238 return yield* Effect.die("expected APNs HTTP request error");
239 }
240 expect(error).toMatchObject({
241 requestKind: "push-notification",
242 event: null,
243 environment: "sandbox",
244 bundleId: "com.t3tools.test",
245 tokenSuffix: "sh-token",
246 stage: "send",
247 status: null,
248 message: "APNs push-notification request failed during send in sandbox.",
249 });
250 expect(error.cause).toBeInstanceOf(HttpClientError.HttpClientError);
251 expect((error.cause as HttpClientError.HttpClientError).reason).toMatchObject({
252 _tag: "TransportError",
253 cause: httpCause,
254 });
255 }).pipe(Effect.provide(layer));
256 });
257});