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

DeliveryAttempts.test.ts

171 lines4.8 KB

1import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5​
6import * as RelayDb from "../db.ts";
7import * as DeliveryAttempts from "./DeliveryAttempts.ts";
8​
9describe("DeliveryAttempts", () => {
10 it.effect("reports completed source jobs when the durable claim already exists", () => {
11 const fakeDb = {
12 insert: () => ({
13 values: () => ({
14 onConflictDoNothing: () => ({
15 returning: () => Effect.succeed([]),
16 }),
17 }),
18 }),
19 select: () => ({
20 from: () => ({
21 where: () => ({
22 limit: () =>
23 Effect.succeed([
24 {
25 createdAt: "2026-05-26T00:00:00.000Z",
26 apnsStatus: 200,
27 apnsReason: null,
28 apnsId: null,
29 transportError: null,
30 },
31 ]),
32 }),
33 }),
34 }),
35 } as unknown as RelayDb.RelayDb["Service"];
36​
37 return Effect.gen(function* () {
38 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
39 const claimed = yield* attempts.claimSourceJob({
40 userId: "user-1",
41 environmentId: null,
42 threadId: null,
43 deviceId: "device-1",
44 kind: "live_activity_update",
45 sourceJobId: "job-1",
46 token: "apns-token",
47 });
48​
49 expect(claimed).toBe("completed");
50 }).pipe(
51 Effect.provide(
52 DeliveryAttempts.layer.pipe(
53 Layer.provide(NodeCryptoLayer.layer),
54 Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb)),
55 ),
56 ),
57 );
58 });
59​
60 it.effect("reports in-flight source jobs while an active claim lease exists", () => {
61 const fakeDb = {
62 insert: () => ({
63 values: () => ({
64 onConflictDoNothing: () => ({
65 returning: () => Effect.succeed([]),
66 }),
67 }),
68 }),
69 select: () => ({
70 from: () => ({
71 where: () => ({
72 limit: () =>
73 Effect.succeed([
74 {
75 createdAt: "2999-01-01T00:00:00.000Z",
76 apnsStatus: null,
77 apnsReason: null,
78 apnsId: null,
79 transportError: null,
80 },
81 ]),
82 }),
83 }),
84 }),
85 } as unknown as RelayDb.RelayDb["Service"];
86​
87 return Effect.gen(function* () {
88 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
89 const claimed = yield* attempts.claimSourceJob({
90 userId: "user-1",
91 environmentId: null,
92 threadId: null,
93 deviceId: "device-1",
94 kind: "live_activity_update",
95 sourceJobId: "job-1",
96 token: "apns-token",
97 });
98​
99 expect(claimed).toBe("in_flight");
100 }).pipe(
101 Effect.provide(
102 DeliveryAttempts.layer.pipe(
103 Layer.provide(NodeCryptoLayer.layer),
104 Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb)),
105 ),
106 ),
107 );
108 });
109​
110 it.effect("reclaims source jobs after the claim lease expires", () => {
111 const updatedValues: Array<Record<string, unknown>> = [];
112 const fakeDb = {
113 insert: () => ({
114 values: () => ({
115 onConflictDoNothing: () => ({
116 returning: () => Effect.succeed([]),
117 }),
118 }),
119 }),
120 select: () => ({
121 from: () => ({
122 where: () => ({
123 limit: () =>
124 Effect.succeed([
125 {
126 createdAt: "1969-12-31T23:00:00.000Z",
127 apnsStatus: null,
128 apnsReason: null,
129 apnsId: null,
130 transportError: null,
131 },
132 ]),
133 }),
134 }),
135 }),
136 update: () => ({
137 set: (values: Record<string, unknown>) => {
138 updatedValues.push(values);
139 return {
140 where: () => ({
141 returning: () => Effect.succeed([{ id: "attempt-1" }]),
142 }),
143 };
144 },
145 }),
146 } as unknown as RelayDb.RelayDb["Service"];
147​
148 return Effect.gen(function* () {
149 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
150 const claimed = yield* attempts.claimSourceJob({
151 userId: "user-1",
152 environmentId: null,
153 threadId: null,
154 deviceId: "device-1",
155 kind: "live_activity_update",
156 sourceJobId: "job-1",
157 token: "apns-token",
158 });
159​
160 expect(claimed).toBe("claimed");
161 expect(updatedValues[0]?.createdAt).toEqual(expect.any(String));
162 }).pipe(
163 Effect.provide(
164 DeliveryAttempts.layer.pipe(
165 Layer.provide(NodeCryptoLayer.layer),
166 Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb)),
167 ),
168 ),
169 );
170 });
171});