Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { PgDialect } from "drizzle-orm/pg-core";
5
6import * as RelayDb from "../db.ts";
7import { relayEnvironmentLinks } from "../persistence/schema.ts";
8import * as EnvironmentLinks from "./EnvironmentLinks.ts";
9
10describe("EnvironmentLinks", () => {
11 it.effect("retains link lookup failures with user and environment identity", () => {
12 const cause = new Error("database unavailable");
13 const fakeDb = {
14 select: () => ({
15 from: (table: unknown) => {
16 expect(table).toBe(relayEnvironmentLinks);
17 return {
18 where: () => ({
19 limit: () => Effect.fail(cause),
20 }),
21 };
22 },
23 }),
24 } as unknown as RelayDb.RelayDb["Service"];
25
26 return Effect.gen(function* () {
27 const links = yield* EnvironmentLinks.EnvironmentLinks;
28 const error = yield* Effect.flip(
29 links.getForUser({ userId: "user-1", environmentId: "env-1" }),
30 );
31
32 expect(error).toMatchObject({
33 _tag: "EnvironmentLinkLookupPersistenceError",
34 userId: "user-1",
35 environmentId: "env-1",
36 });
37 expect(error.cause).toBe(cause);
38 }).pipe(
39 Effect.provide(
40 EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
41 ),
42 );
43 });
44
45 it.effect("identifies delivery-user list failures without retaining key material", () => {
46 const cause = new Error("database unavailable");
47 const fakeDb = {
48 select: () => ({
49 from: (table: unknown) => {
50 expect(table).toBe(relayEnvironmentLinks);
51 return {
52 where: () => Effect.fail(cause),
53 };
54 },
55 }),
56 } as unknown as RelayDb.RelayDb["Service"];
57
58 return Effect.gen(function* () {
59 const links = yield* EnvironmentLinks.EnvironmentLinks;
60 const error = yield* Effect.flip(
61 links.listDeliveryUsersForEnvironment({
62 environmentId: "env-1",
63 environmentPublicKey: "sensitive-public-key-material",
64 }),
65 );
66
67 expect(error).toMatchObject({
68 _tag: "EnvironmentLinkUserListPersistenceError",
69 message: "Environment link user query 'list-delivery-users' failed for environment 'env-1'",
70 environmentId: "env-1",
71 });
72 expect(error.cause).toBe(cause);
73 expect(error).not.toHaveProperty("environmentPublicKey");
74 }).pipe(
75 Effect.provide(
76 EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
77 ),
78 );
79 });
80
81 it.effect("selects notification or Live Activity users by environment key", () => {
82 const whereConditions: Array<unknown> = [];
83 const fakeDb = {
84 select: (selection: unknown) => {
85 expect(selection).toBeDefined();
86 return {
87 from: (table: unknown) => {
88 expect(table).toBe(relayEnvironmentLinks);
89 return {
90 where: (condition: unknown) => {
91 whereConditions.push(condition);
92 return Effect.succeed([]);
93 },
94 };
95 },
96 };
97 },
98 } as unknown as RelayDb.RelayDb["Service"];
99
100 return Effect.gen(function* () {
101 const links = yield* EnvironmentLinks.EnvironmentLinks;
102 expect(
103 yield* links.listDeliveryUsersForEnvironment({
104 environmentId: "env-1",
105 environmentPublicKey: "public-key-1",
106 }),
107 ).toEqual([]);
108 expect(whereConditions).toHaveLength(1);
109
110 const query = new PgDialect().sqlToQuery(whereConditions[0] as never);
111 expect(query.sql).toContain('"relay_environment_links"."environment_id" = $1');
112 expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');
113 expect(query.sql).toContain('"relay_environment_links"."notifications_enabled" = $2');
114 expect(query.sql).toContain('"relay_environment_links"."live_activities_enabled" = $3');
115 expect(query.sql).toContain('"relay_environment_links"."environment_public_key" = $4');
116 expect(query.sql).toContain(" or ");
117 expect(query.params).toEqual(["env-1", true, true, "public-key-1"]);
118 }).pipe(
119 Effect.provide(
120 EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
121 ),
122 );
123 });
124
125 it.effect("carries the webhook-hold opt-in over only from links with the same key", () => {
126 const inserted: Array<Record<string, unknown>> = [];
127 const fakeDb = {
128 insert: () => ({
129 values: (values: Record<string, unknown>) => {
130 inserted.push(values);
131 return { onConflictDoUpdate: () => Effect.void };
132 },
133 }),
134 } as unknown as RelayDb.RelayDb["Service"];
135
136 return Effect.gen(function* () {
137 const links = yield* EnvironmentLinks.EnvironmentLinks;
138 yield* links.upsert({
139 userId: "user-1",
140 request: {
141 notificationsEnabled: false,
142 liveActivitiesEnabled: false,
143 managedTunnelsEnabled: true,
144 } as never,
145 proof: {
146 environmentId: "env-1",
147 environmentPublicKey: "public-key-1",
148 descriptor: { label: "Laptop" },
149 } as never,
150 endpoint: { httpBaseUrl: "https://a.example", wsBaseUrl: "wss://a.example" } as never,
151 });
152 const query = new PgDialect().sqlToQuery(inserted[0]?.holdWebhooksWhileOffline as never);
153 expect(query.sql).toContain("environment_public_key");
154 expect(query.params).toEqual(["env-1", "public-key-1"]);
155 }).pipe(
156 Effect.provide(
157 EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
158 ),
159 );
160 });
161
162 it.effect("revokes only the active link owned by the requesting user", () => {
163 const updateValues: Array<Record<string, unknown>> = [];
164 const whereConditions: Array<unknown> = [];
165 const fakeDb = {
166 update: (table: unknown) => {
167 expect(table).toBe(relayEnvironmentLinks);
168 return {
169 set: (values: Record<string, unknown>) => {
170 updateValues.push(values);
171 return {
172 where: (condition: unknown) => {
173 whereConditions.push(condition);
174 return {
175 returning: (selection: unknown) => {
176 expect(selection).toBeDefined();
177 return Effect.succeed([{ environmentId: "env-1" }]);
178 },
179 };
180 },
181 };
182 },
183 };
184 },
185 } as unknown as RelayDb.RelayDb["Service"];
186
187 return Effect.gen(function* () {
188 const links = yield* EnvironmentLinks.EnvironmentLinks;
189 const revoked = yield* links.revokeForUser({
190 userId: "user-1",
191 environmentId: "env-1",
192 });
193
194 expect(revoked).toBe(true);
195 expect(updateValues).toHaveLength(1);
196 expect(updateValues[0]?.revokedAt).toEqual(updateValues[0]?.updatedAt);
197 expect(typeof updateValues[0]?.revokedAt).toBe("string");
198 expect(whereConditions).toHaveLength(1);
199
200 const dialect = new PgDialect();
201 const query = dialect.sqlToQuery(whereConditions[0] as never);
202 expect(query.sql).toContain('"relay_environment_links"."user_id" = $1');
203 expect(query.sql).toContain('"relay_environment_links"."environment_id" = $2');
204 expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');
205 expect(query.params).toEqual(["user-1", "env-1"]);
206 }).pipe(
207 Effect.provide(
208 EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
209 ),
210 );
211 });
212});