|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from "vitest"; |
| 2 | +import { createClient } from "@supabase/supabase-js"; |
| 3 | +import type { Database } from "@repo/database/dbTypes"; |
| 4 | +import type { DGSupabaseClient } from "@repo/database/lib/client"; |
| 5 | +import { |
| 6 | + fetchOrCreateSpaceDirect, |
| 7 | + fetchOrCreatePlatformAccount, |
| 8 | + spaceAnonUserEmail, |
| 9 | +} from "@repo/database/lib/contextFunctions"; |
| 10 | +import { |
| 11 | + createGroup, |
| 12 | + createGroupInvitation, |
| 13 | + acceptGroupInvitation, |
| 14 | +} from "../../app/utils/supabase/account"; |
| 15 | + |
| 16 | +const SUPABASE_URL = process.env.SUPABASE_URL!; |
| 17 | +const ANON_KEY = process.env.SUPABASE_PUBLISHABLE_KEY!; |
| 18 | +const SERVICE_KEY = process.env.SUPABASE_SECRET_KEY!; |
| 19 | +const PASSWORD = "abcdefgh"; |
| 20 | + |
| 21 | +const freshClient = (): DGSupabaseClient => |
| 22 | + createClient<Database, "public">(SUPABASE_URL, ANON_KEY); |
| 23 | + |
| 24 | +const serviceClient = () => |
| 25 | + createClient<Database, "public">(SUPABASE_URL, SERVICE_KEY); |
| 26 | + |
| 27 | +const signedInClient = async (spaceId: number): Promise<DGSupabaseClient> => { |
| 28 | + const client = freshClient(); |
| 29 | + const { error } = await client.auth.signInWithPassword({ |
| 30 | + email: spaceAnonUserEmail("Roam", spaceId), |
| 31 | + password: PASSWORD, |
| 32 | + }); |
| 33 | + if (error) throw new Error(`Sign-in failed: ${error.message}`); |
| 34 | + return client; |
| 35 | +}; |
| 36 | + |
| 37 | +describe("group invitation flow (website functions)", () => { |
| 38 | + let spaceId1: number; |
| 39 | + let spaceId2: number; |
| 40 | + let client1: DGSupabaseClient; |
| 41 | + let client2: DGSupabaseClient; |
| 42 | + let createdGroupId: string | null = null; |
| 43 | + |
| 44 | + beforeAll(async () => { |
| 45 | + const s1 = await fetchOrCreateSpaceDirect({ |
| 46 | + name: "vitest-s1", |
| 47 | + url: "https://roamresearch.com/#/app/vitest-s1", |
| 48 | + platform: "Roam", |
| 49 | + password: PASSWORD, |
| 50 | + }); |
| 51 | + if (!s1.data) |
| 52 | + throw new Error(`Failed to create space 1: ${s1.error?.message}`); |
| 53 | + spaceId1 = s1.data.id; |
| 54 | + await fetchOrCreatePlatformAccount({ |
| 55 | + platform: "Roam", |
| 56 | + accountLocalId: "vitest-user1", |
| 57 | + name: "vitest-user1", |
| 58 | + email: "vitest-user1@example.com", |
| 59 | + spaceId: spaceId1, |
| 60 | + password: PASSWORD, |
| 61 | + }); |
| 62 | + |
| 63 | + const s2 = await fetchOrCreateSpaceDirect({ |
| 64 | + name: "vitest-s2", |
| 65 | + url: "https://roamresearch.com/#/app/vitest-s2", |
| 66 | + platform: "Roam", |
| 67 | + password: PASSWORD, |
| 68 | + }); |
| 69 | + if (!s2.data) |
| 70 | + throw new Error(`Failed to create space 2: ${s2.error?.message}`); |
| 71 | + spaceId2 = s2.data.id; |
| 72 | + await fetchOrCreatePlatformAccount({ |
| 73 | + platform: "Roam", |
| 74 | + accountLocalId: "vitest-user2", |
| 75 | + name: "vitest-user2", |
| 76 | + email: "vitest-user2@example.com", |
| 77 | + spaceId: spaceId2, |
| 78 | + password: PASSWORD, |
| 79 | + }); |
| 80 | + |
| 81 | + client1 = await signedInClient(spaceId1); |
| 82 | + client2 = await signedInClient(spaceId2); |
| 83 | + }); |
| 84 | + |
| 85 | + afterAll(async () => { |
| 86 | + if (createdGroupId) { |
| 87 | + await serviceClient().auth.admin.deleteUser(createdGroupId); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it("executes the full invitation flow", async () => { |
| 92 | + // Step 1: user1 creates a group |
| 93 | + const groupId = await createGroup(client1, "vitest-invite-group"); |
| 94 | + expect(groupId, "createGroup should return a group ID").toBeTruthy(); |
| 95 | + createdGroupId = groupId; |
| 96 | + |
| 97 | + // Step 2: user1 creates an invitation token |
| 98 | + const token = await createGroupInvitation({ |
| 99 | + client: client1, |
| 100 | + groupId: groupId!, |
| 101 | + admin: false, |
| 102 | + }); |
| 103 | + expect(token, "createGroupInvitation should return a token").toBeTruthy(); |
| 104 | + |
| 105 | + // Step 3: user2 accepts the invitation |
| 106 | + const error = await acceptGroupInvitation(client2, token!); |
| 107 | + expect( |
| 108 | + error, |
| 109 | + "acceptGroupInvitation should return null on success", |
| 110 | + ).toBeNull(); |
| 111 | + |
| 112 | + // Step 4: verify user2 is in group_membership |
| 113 | + const { data: user2 } = await client2.auth.getUser(); |
| 114 | + const { data: membership } = await serviceClient() |
| 115 | + .from("group_membership") |
| 116 | + .select("admin") |
| 117 | + .eq("group_id", groupId!) |
| 118 | + .eq("member_id", user2.user!.id) |
| 119 | + .maybeSingle(); |
| 120 | + expect(membership, "user2 should appear in group_membership").toBeTruthy(); |
| 121 | + expect(membership?.admin).toBe(false); |
| 122 | + }); |
| 123 | +}); |
0 commit comments