|
| 1 | +import { postgresTest } from "@internal/testcontainers"; |
| 2 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 3 | +import { describe, expect } from "vitest"; |
| 4 | +import { feedbackOrganization } from "./feedbackOrganization.server"; |
| 5 | + |
| 6 | +async function createUser(prisma: PrismaClient, name: string) { |
| 7 | + return prisma.user.create({ |
| 8 | + data: { email: `${name}@example.com`, authenticationMethod: "MAGIC_LINK" }, |
| 9 | + }); |
| 10 | +} |
| 11 | + |
| 12 | +async function createOrg(prisma: PrismaClient, slug: string, memberIds: string[]) { |
| 13 | + return prisma.organization.create({ |
| 14 | + data: { |
| 15 | + title: `Org ${slug}`, |
| 16 | + slug, |
| 17 | + members: { create: memberIds.map((userId) => ({ userId })) }, |
| 18 | + }, |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +describe("feedbackOrganization", () => { |
| 23 | + postgresTest("uses the org page the message was sent from", async ({ prisma }) => { |
| 24 | + const user = await createUser(prisma, "multi"); |
| 25 | + const a = await createOrg(prisma, "org-a", [user.id]); |
| 26 | + await createOrg(prisma, "org-b", [user.id]); |
| 27 | + |
| 28 | + const org = await feedbackOrganization( |
| 29 | + user.id, |
| 30 | + "/orgs/org-a/projects/p/env/dev/runs", |
| 31 | + prisma, |
| 32 | + prisma |
| 33 | + ); |
| 34 | + expect(org).toEqual({ id: a.id, title: a.title }); |
| 35 | + }); |
| 36 | + |
| 37 | + postgresTest("never resolves an org the user isn't a member of", async ({ prisma }) => { |
| 38 | + const user = await createUser(prisma, "member"); |
| 39 | + const other = await createUser(prisma, "other"); |
| 40 | + await createOrg(prisma, "mine", [user.id]); |
| 41 | + await createOrg(prisma, "theirs", [other.id]); |
| 42 | + |
| 43 | + expect(await feedbackOrganization(user.id, "/orgs/theirs", prisma, prisma)).toBeUndefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + postgresTest("falls back to the user's only org outside an org page", async ({ prisma }) => { |
| 47 | + const user = await createUser(prisma, "single"); |
| 48 | + const only = await createOrg(prisma, "only", [user.id]); |
| 49 | + |
| 50 | + expect(await feedbackOrganization(user.id, "/account", prisma, prisma)).toEqual({ |
| 51 | + id: only.id, |
| 52 | + title: only.title, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + postgresTest("leaves it unattributed when the org is ambiguous", async ({ prisma }) => { |
| 57 | + const user = await createUser(prisma, "ambiguous"); |
| 58 | + await createOrg(prisma, "first", [user.id]); |
| 59 | + await createOrg(prisma, "second", [user.id]); |
| 60 | + |
| 61 | + expect(await feedbackOrganization(user.id, "/account", prisma, prisma)).toBeUndefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + postgresTest("finds a just-created org the replica hasn't caught up on", async ({ prisma }) => { |
| 65 | + const user = await createUser(prisma, "lagged"); |
| 66 | + |
| 67 | + // The org exists only inside an uncommitted transaction: the transaction stands in for the |
| 68 | + // primary, and the plain client for a replica that can't see it yet. |
| 69 | + await prisma.$transaction(async (tx) => { |
| 70 | + const fresh = await tx.organization.create({ |
| 71 | + data: { title: "Org fresh", slug: "fresh", members: { create: [{ userId: user.id }] } }, |
| 72 | + }); |
| 73 | + expect(await feedbackOrganization(user.id, "/orgs/fresh", prisma, prisma)).toBeUndefined(); |
| 74 | + expect(await feedbackOrganization(user.id, "/orgs/fresh", prisma, tx)).toEqual({ |
| 75 | + id: fresh.id, |
| 76 | + title: fresh.title, |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + postgresTest("ignores deleted orgs", async ({ prisma }) => { |
| 82 | + const user = await createUser(prisma, "deleted"); |
| 83 | + const gone = await createOrg(prisma, "gone", [user.id]); |
| 84 | + const live = await createOrg(prisma, "live", [user.id]); |
| 85 | + await prisma.organization.update({ where: { id: gone.id }, data: { deletedAt: new Date() } }); |
| 86 | + |
| 87 | + expect(await feedbackOrganization(user.id, "/orgs/gone", prisma, prisma)).toBeUndefined(); |
| 88 | + expect(await feedbackOrganization(user.id, "/account", prisma, prisma)).toEqual({ |
| 89 | + id: live.id, |
| 90 | + title: live.title, |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments