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

DeliveryAttempts.test.ts

418 lines12.3 KB

1import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto";
2import { describe, expect, it } from "@effect/vitest";
3import type { SQL } from "drizzle-orm";
4import { PgDialect } from "drizzle-orm/pg-core";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7​
8import * as RelayDb from "../db.ts";
9import { relayDeliveryAttempts } from "../persistence/schema.ts";
10import * as DeliveryAttempts from "./DeliveryAttempts.ts";
11​
12describe("DeliveryAttempts", () => {
13 it.effect("records the signed queue source job id for APNs delivery auditability", () => {
14 const insertedValues: Array<Record<string, unknown>> = [];
15 const fakeDb = {
16 insert: (table: unknown) => {
17 expect(table).toBe(relayDeliveryAttempts);
18 return {
19 values: (values: Record<string, unknown>) => {
20 insertedValues.push(values);
21 return Effect.void;
22 },
23 };
24 },
25 } as unknown as RelayDb.RelayDb["Service"];
26​
27 return Effect.gen(function* () {
28 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
29 yield* attempts.record({
30 userId: "user-1",
31 environmentId: "env-1",
32 threadId: "thread-1",
33 deviceId: "device-1",
34 kind: "live_activity_update",
35 sourceJobId: "job-1",
36 token: "apns-token",
37 apnsStatus: 200,
38 apnsId: "apns-id",
39 });
40​
41 expect(insertedValues).toHaveLength(1);
42 expect(insertedValues[0]).toMatchObject({
43 userId: "user-1",
44 environmentId: "env-1",
45 threadId: "thread-1",
46 deviceId: "device-1",
47 kind: "live_activity_update",
48 sourceJobId: "job-1",
49 tokenSuffix: "ns-token",
50 apnsStatus: 200,
51 apnsId: "apns-id",
52 });
53 }).pipe(
54 Effect.provide(
55 DeliveryAttempts.layer.pipe(
56 Layer.provide(NodeCryptoLayer.layer),
57 Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb)),
58 ),
59 ),
60 );
61 });
62​
63 it.effect("claims signed queue source jobs before APNs delivery", () => {
64 const insertedValues: Array<Record<string, unknown>> = [];
65 const conflictTargets: Array<unknown> = [];
66 const fakeDb = {
67 insert: (table: unknown) => {
68 expect(table).toBe(relayDeliveryAttempts);
69 return {
70 values: (values: Record<string, unknown>) => {
71 insertedValues.push(values);
72 return {
73 onConflictDoNothing: (config: { readonly target: unknown }) => {
74 conflictTargets.push(config.target);
75 return {
76 returning: () => Effect.succeed([{ id: values.id }]),
77 };
78 },
79 };
80 },
81 };
82 },
83 } as unknown as RelayDb.RelayDb["Service"];
84​
85 return Effect.gen(function* () {
86 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
87 const claimed = yield* attempts.claimSourceJob({
88 userId: "user-1",
89 environmentId: "env-1",
90 threadId: "thread-1",
91 deviceId: "device-1",
92 kind: "push_notification",
93 sourceJobId: "job-1",
94 token: "apns-token",
95 });
96​
97 expect(claimed).toBe("claimed");
98 expect(conflictTargets).toEqual([relayDeliveryAttempts.sourceJobId]);
99 expect(insertedValues[0]).toMatchObject({
100 kind: "push_notification",
101 sourceJobId: "job-1",
102 tokenSuffix: "ns-token",
103 apnsStatus: null,
104 });
105 }).pipe(
106 Effect.provide(
107 DeliveryAttempts.layer.pipe(
108 Layer.provide(NodeCryptoLayer.layer),
109 Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb)),
110 ),
111 ),
112 );
113 });
114​
115 it.effect("reports completed source jobs when the durable claim already exists", () => {
116 const fakeDb = {
117 insert: () => ({
118 values: () => ({
119 onConflictDoNothing: () => ({
120 returning: () => Effect.succeed([]),
121 }),
122 }),
123 }),
124 select: () => ({
125 from: () => ({
126 where: () => ({
127 limit: () =>
128 Effect.succeed([
129 {
130 createdAt: "2026-05-26T00:00:00.000Z",
131 apnsStatus: 200,
132 apnsReason: null,
133 apnsId: null,
134 transportError: null,
135 },
136 ]),
137 }),
138 }),
139 }),
140 } as unknown as RelayDb.RelayDb["Service"];
141​
142 return Effect.gen(function* () {
143 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
144 const claimed = yield* attempts.claimSourceJob({
145 userId: "user-1",
146 environmentId: null,
147 threadId: null,
148 deviceId: "device-1",
149 kind: "live_activity_update",
150 sourceJobId: "job-1",
151 token: "apns-token",
152 });
153​
154 expect(claimed).toBe("completed");
155 }).pipe(
156 Effect.provide(
157 DeliveryAttempts.layer.pipe(
158 Layer.provide(NodeCryptoLayer.layer),
159 Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb)),
160 ),
161 ),
162 );
163 });
164​
165 it.effect("reports in-flight source jobs while an active claim lease exists", () => {
166 const fakeDb = {
167 insert: () => ({
168 values: () => ({
169 onConflictDoNothing: () => ({
170 returning: () => Effect.succeed([]),
171 }),
172 }),
173 }),
174 select: () => ({
175 from: () => ({
176 where: () => ({
177 limit: () =>
178 Effect.succeed([
179 {
180 createdAt: "2999-01-01T00:00:00.000Z",
181 apnsStatus: null,
182 apnsReason: null,
183 apnsId: null,
184 transportError: null,
185 },
186 ]),
187 }),
188 }),
189 }),
190 } as unknown as RelayDb.RelayDb["Service"];
191​
192 return Effect.gen(function* () {
193 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
194 const claimed = yield* attempts.claimSourceJob({
195 userId: "user-1",
196 environmentId: null,
197 threadId: null,
198 deviceId: "device-1",
199 kind: "live_activity_update",
200 sourceJobId: "job-1",
201 token: "apns-token",
202 });
203​
204 expect(claimed).toBe("in_flight");
205 }).pipe(
206 Effect.provide(
207 DeliveryAttempts.layer.pipe(
208 Layer.provide(NodeCryptoLayer.layer),
209 Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb)),
210 ),
211 ),
212 );
213 });
214​
215 it.effect("reclaims source jobs after the claim lease expires", () => {
216 const updatedValues: Array<Record<string, unknown>> = [];
217 const fakeDb = {
218 insert: () => ({
219 values: () => ({
220 onConflictDoNothing: () => ({
221 returning: () => Effect.succeed([]),
222 }),
223 }),
224 }),
225 select: () => ({
226 from: () => ({
227 where: () => ({
228 limit: () =>
229 Effect.succeed([
230 {
231 createdAt: "1969-12-31T23:00:00.000Z",
232 apnsStatus: null,
233 apnsReason: null,
234 apnsId: null,
235 transportError: null,
236 },
237 ]),
238 }),
239 }),
240 }),
241 update: () => ({
242 set: (values: Record<string, unknown>) => {
243 updatedValues.push(values);
244 return {
245 where: () => ({
246 returning: () => Effect.succeed([{ id: "attempt-1" }]),
247 }),
248 };
249 },
250 }),
251 } as unknown as RelayDb.RelayDb["Service"];
252​
253 return Effect.gen(function* () {
254 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
255 const claimed = yield* attempts.claimSourceJob({
256 userId: "user-1",
257 environmentId: null,
258 threadId: null,
259 deviceId: "device-1",
260 kind: "live_activity_update",
261 sourceJobId: "job-1",
262 token: "apns-token",
263 });
264​
265 expect(claimed).toBe("claimed");
266 expect(updatedValues[0]?.createdAt).toEqual(expect.any(String));
267 }).pipe(
268 Effect.provide(
269 DeliveryAttempts.layer.pipe(
270 Layer.provide(NodeCryptoLayer.layer),
271 Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb)),
272 ),
273 ),
274 );
275 });
276​
277 it.effect("completes a claimed source job with the APNs outcome", () => {
278 const updatedValues: Array<Record<string, unknown>> = [];
279 const whereClauses: Array<SQL> = [];
280 const fakeDb = {
281 update: (table: unknown) => {
282 expect(table).toBe(relayDeliveryAttempts);
283 return {
284 set: (values: Record<string, unknown>) => {
285 updatedValues.push(values);
286 return {
287 where: (clause: SQL) => {
288 whereClauses.push(clause);
289 return Effect.void;
290 },
291 };
292 },
293 };
294 },
295 } as unknown as RelayDb.RelayDb["Service"];
296​
297 return Effect.gen(function* () {
298 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
299 yield* attempts.completeSourceJob({
300 sourceJobId: "job-1",
301 apnsStatus: 410,
302 apnsReason: "Unregistered",
303 apnsId: "apns-id",
304 });
305​
306 expect(whereClauses.map((clause) => new PgDialect().sqlToQuery(clause))).toEqual([
307 { sql: '"relay_delivery_attempts"."source_job_id" = $1', params: ["job-1"] },
308 ]);
309 expect(updatedValues).toEqual([
310 {
311 createdAt: expect.any(String),
312 apnsStatus: 410,
313 apnsReason: "Unregistered",
314 apnsId: "apns-id",
315 transportError: null,
316 },
317 ]);
318 }).pipe(
319 Effect.provide(
320 DeliveryAttempts.layer.pipe(
321 Layer.provide(NodeCryptoLayer.layer),
322 Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb)),
323 ),
324 ),
325 );
326 });
327​
328 it.effect("preserves operation context and causes for persistence failures", () => {
329 const cause = new Error("database unavailable");
330 const fakeDb = {
331 insert: () => ({
332 values: (values: Record<string, unknown>) =>
333 values.kind === "record"
334 ? Effect.fail(cause)
335 : {
336 onConflictDoNothing: () => ({
337 returning: () => Effect.fail(cause),
338 }),
339 },
340 }),
341 update: () => ({
342 set: () => ({
343 where: () => Effect.fail(cause),
344 }),
345 }),
346 } as unknown as RelayDb.RelayDb["Service"];
347​
348 return Effect.gen(function* () {
349 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
350 const recordError = yield* Effect.flip(
351 attempts.record({
352 userId: "user-1",
353 environmentId: "env-1",
354 threadId: "thread-1",
355 deviceId: "device-1",
356 kind: "record",
357 sourceJobId: "job-1",
358 token: "apns-token",
359 }),
360 );
361 const claimError = yield* Effect.flip(
362 attempts.claimSourceJob({
363 userId: "user-2",
364 environmentId: "env-2",
365 threadId: "thread-2",
366 deviceId: "device-2",
367 kind: "claim",
368 sourceJobId: "job-2",
369 token: "apns-token",
370 }),
371 );
372 const completionError = yield* Effect.flip(
373 attempts.completeSourceJob({ sourceJobId: "job-3", apnsStatus: 500 }),
374 );
375​
376 expect(recordError).toMatchObject({
377 operation: "record",
378 sourceJobId: "job-1",
379 userId: "user-1",
380 environmentId: "env-1",
381 threadId: "thread-1",
382 deviceId: "device-1",
383 kind: "record",
384 cause,
385 message: "Failed to persist APNs delivery attempt during record.",
386 });
387 expect(claimError).toMatchObject({
388 operation: "claim-source-job",
389 sourceJobId: "job-2",
390 userId: "user-2",
391 environmentId: "env-2",
392 threadId: "thread-2",
393 deviceId: "device-2",
394 kind: "claim",
395 cause,
396 message: "Failed to persist APNs delivery attempt during claim-source-job.",
397 });
398 expect(completionError).toMatchObject({
399 operation: "complete-source-job",
400 sourceJobId: "job-3",
401 userId: null,
402 environmentId: null,
403 threadId: null,
404 deviceId: null,
405 kind: null,
406 cause,
407 message: "Failed to persist APNs delivery attempt during complete-source-job.",
408 });
409 }).pipe(
410 Effect.provide(
411 DeliveryAttempts.layer.pipe(
412 Layer.provide(NodeCryptoLayer.layer),
413 Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb)),
414 ),
415 ),
416 );
417 });
418});