Skip to content
Source
Sign inSign up

EnvironmentLinks.test.ts

97 lines3.5 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("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: () => {
62 return Effect.succeed([{ environmentId: "env-1" }]);
63 },
64 };
65 },
66 };
67 },
68 };
69 },
70 } as unknown as RelayDb.RelayDb["Service"];
71​
72 return Effect.gen(function* () {
73 const links = yield* EnvironmentLinks.EnvironmentLinks;
74 const revoked = yield* links.revokeForUser({
75 userId: "user-1",
76 environmentId: "env-1",
77 });
78​
79 expect(revoked).toBe(true);
80 expect(updateValues).toHaveLength(1);
81 expect(updateValues[0]?.revokedAt).toEqual(updateValues[0]?.updatedAt);
82 expect(typeof updateValues[0]?.revokedAt).toBe("string");
83 expect(whereConditions).toHaveLength(1);
84​
85 const dialect = new PgDialect();
86 const query = dialect.sqlToQuery(whereConditions[0] as never);
87 expect(query.sql).toContain('"relay_environment_links"."user_id" = $1');
88 expect(query.sql).toContain('"relay_environment_links"."environment_id" = $2');
89 expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');
90 expect(query.params).toEqual(["user-1", "env-1"]);
91 }).pipe(
92 Effect.provide(
93 EnvironmentLinks.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
94 ),
95 );
96 });
97});