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("carries the webhook-hold opt-in over only from links with the same key", () => {
12 const inserted: Array<Record<string, unknown>> = [];
13 const fakeDb = {
14 insert: () => ({
15 values: (values: Record<string, unknown>) => {
16 inserted.push(values);
17 return { onConflictDoUpdate: () => Effect.void };
18 },
19 }),
20 } as unknown as RelayDb.RelayDb["Service"];
21
22 return Effect.gen(function* () {
23 const links = yield* EnvironmentLinks.EnvironmentLinks;
24 yield* links.upsert({
25 userId: "user-1",
26 request: {
27 notificationsEnabled: false,
28 liveActivitiesEnabled: false,
29 managedTunnelsEnabled: true,
30 } as never,
31 proof: {
32 environmentId: "env-1",
33 environmentPublicKey: "public-key-1",
34 descriptor: { label: "Laptop" },
35 } as never,
36 endpoint: { httpBaseUrl: "https://a.example", wsBaseUrl: "wss://a.example" } as never,
37 });
38 const query = new PgDialect().sqlToQuery(inserted[0]?.holdWebhooksWhileOffline as never);
39 expect(query.sql).toContain("environment_public_key");
40 expect(query.params).toEqual(["env-1", "public-key-1"]);
41 }).pipe(
42 Effect.provide(
43 EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
44 ),
45 );
46 });
47
48 it.effect("revokes only the active link owned by the requesting user", () => {
49 const updateValues: Array<Record<string, unknown>> = [];
50 const whereConditions: Array<unknown> = [];
51 const fakeDb = {
52 update: (table: unknown) => {
53 expect(table).toBe(relayEnvironmentLinks);
54 return {
55 set: (values: Record<string, unknown>) => {
56 updateValues.push(values);
57 return {
58 where: (condition: unknown) => {
59 whereConditions.push(condition);
60 return {
61 returning: (selection: unknown) => {
62 expect(selection).toBeDefined();
63 return Effect.succeed([{ environmentId: "env-1" }]);
64 },
65 };
66 },
67 };
68 },
69 };
70 },
71 } as unknown as RelayDb.RelayDb["Service"];
72
73 return Effect.gen(function* () {
74 const links = yield* EnvironmentLinks.EnvironmentLinks;
75 const revoked = yield* links.revokeForUser({
76 userId: "user-1",
77 environmentId: "env-1",
78 });
79
80 expect(revoked).toBe(true);
81 expect(updateValues).toHaveLength(1);
82 expect(updateValues[0]?.revokedAt).toEqual(updateValues[0]?.updatedAt);
83 expect(typeof updateValues[0]?.revokedAt).toBe("string");
84 expect(whereConditions).toHaveLength(1);
85
86 const dialect = new PgDialect();
87 const query = dialect.sqlToQuery(whereConditions[0] as never);
88 expect(query.sql).toContain('"relay_environment_links"."user_id" = $1');
89 expect(query.sql).toContain('"relay_environment_links"."environment_id" = $2');
90 expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');
91 expect(query.params).toEqual(["user-1", "env-1"]);
92 }).pipe(
93 Effect.provide(
94 EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
95 ),
96 );
97 });
98});