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

EnvironmentLinks.test.ts

169 lines5.9 KB

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 operation: "list-delivery-users",
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 users when either notifications or Live Activities are enabled", () => {
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(yield* links.listUsersForEnvironment({ environmentId: "env-1" })).toEqual([]);
103 expect(whereConditions).toHaveLength(1);
104​
105 const query = new PgDialect().sqlToQuery(whereConditions[0] as never);
106 expect(query.sql).toContain('"relay_environment_links"."environment_id" = $1');
107 expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');
108 expect(query.sql).toContain('"relay_environment_links"."notifications_enabled" = $2');
109 expect(query.sql).toContain('"relay_environment_links"."live_activities_enabled" = $3');
110 expect(query.sql).toContain(" or ");
111 expect(query.params).toEqual(["env-1", true, true]);
112 }).pipe(
113 Effect.provide(
114 EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
115 ),
116 );
117 });
118​
119 it.effect("revokes only the active link owned by the requesting user", () => {
120 const updateValues: Array<Record<string, unknown>> = [];
121 const whereConditions: Array<unknown> = [];
122 const fakeDb = {
123 update: (table: unknown) => {
124 expect(table).toBe(relayEnvironmentLinks);
125 return {
126 set: (values: Record<string, unknown>) => {
127 updateValues.push(values);
128 return {
129 where: (condition: unknown) => {
130 whereConditions.push(condition);
131 return {
132 returning: (selection: unknown) => {
133 expect(selection).toBeDefined();
134 return Effect.succeed([{ environmentId: "env-1" }]);
135 },
136 };
137 },
138 };
139 },
140 };
141 },
142 } as unknown as RelayDb.RelayDb["Service"];
143​
144 return Effect.gen(function* () {
145 const links = yield* EnvironmentLinks.EnvironmentLinks;
146 const revoked = yield* links.revokeForUser({
147 userId: "user-1",
148 environmentId: "env-1",
149 });
150​
151 expect(revoked).toBe(true);
152 expect(updateValues).toHaveLength(1);
153 expect(updateValues[0]?.revokedAt).toEqual(updateValues[0]?.updatedAt);
154 expect(typeof updateValues[0]?.revokedAt).toBe("string");
155 expect(whereConditions).toHaveLength(1);
156​
157 const dialect = new PgDialect();
158 const query = dialect.sqlToQuery(whereConditions[0] as never);
159 expect(query.sql).toContain('"relay_environment_links"."user_id" = $1');
160 expect(query.sql).toContain('"relay_environment_links"."environment_id" = $2');
161 expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');
162 expect(query.params).toEqual(["user-1", "env-1"]);
163 }).pipe(
164 Effect.provide(
165 EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
166 ),
167 );
168 });
169});