Skip to content

Commit 40ac4a8

Browse files
fix: add cleanup to ensureAppEnabled to restore original app state
Address Cubic AI review feedback: the ensureAppEnabled helper now returns a cleanup function that restores the app's original enabled state after the test completes. This prevents side-effects across parallel test runs. Co-Authored-By: alex@cal.com <me@alexvanandel.com>
1 parent 63dc79a commit 40ac4a8

File tree

1 file changed

+70
-48
lines changed

1 file changed

+70
-48
lines changed

apps/web/playwright/payment-apps.e2e.ts

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,29 @@ async function goToAppsTab(page: Page, eventTypeId?: number): Promise<void> {
1313
}
1414

1515
/**
16-
* Ensures an app is enabled in the App table.
16+
* Ensures an app is enabled in the App table and returns a cleanup function.
1717
* This is needed because apps without valid keys are now disabled during seeding.
1818
* For E2E tests, we need to enable the app to test the UI behavior.
19+
* The cleanup function restores the original enabled state to avoid side-effects.
1920
*/
20-
async function ensureAppEnabled(appSlug: string): Promise<void> {
21+
async function ensureAppEnabled(appSlug: string): Promise<() => Promise<void>> {
22+
const app = await prisma.app.findUnique({
23+
where: { slug: appSlug },
24+
select: { enabled: true },
25+
});
26+
const originalEnabled = app?.enabled ?? false;
27+
2128
await prisma.app.update({
2229
where: { slug: appSlug },
2330
data: { enabled: true },
2431
});
32+
33+
return async () => {
34+
await prisma.app.update({
35+
where: { slug: appSlug },
36+
data: { enabled: originalEnabled },
37+
});
38+
};
2539
}
2640

2741
// biome-ignore lint/complexity/noExcessiveLinesPerFunction: E2E test suites naturally have many test cases
@@ -32,43 +46,47 @@ test.describe("Payment app", () => {
3246
const paymentEvent = user.eventTypes.find((item) => item.slug === "paid");
3347
expect(paymentEvent).not.toBeNull();
3448
// Ensure alby app is enabled (it may be disabled if keys are not configured)
35-
await ensureAppEnabled("alby");
36-
await prisma.credential.create({
37-
data: {
38-
type: "alby_payment",
39-
appId: "alby",
40-
userId: user.id,
41-
key: {
42-
account_id: "random",
43-
account_email: "random@example.com",
44-
webhook_endpoint_id: "ep_randomString",
45-
webhook_endpoint_secret: "whsec_randomString",
46-
account_lightning_address: "random@getalby.com",
49+
const cleanupAlbyApp = await ensureAppEnabled("alby");
50+
try {
51+
await prisma.credential.create({
52+
data: {
53+
type: "alby_payment",
54+
appId: "alby",
55+
userId: user.id,
56+
key: {
57+
account_id: "random",
58+
account_email: "random@example.com",
59+
webhook_endpoint_id: "ep_randomString",
60+
webhook_endpoint_secret: "whsec_randomString",
61+
account_lightning_address: "random@getalby.com",
62+
},
4763
},
48-
},
49-
});
64+
});
5065

51-
await goToAppsTab(page, paymentEvent?.id);
66+
await goToAppsTab(page, paymentEvent?.id);
5267

53-
await page.locator("#event-type-form").getByRole("switch").click();
54-
await page.getByPlaceholder("Price").click();
55-
await page.getByPlaceholder("Price").fill("200");
56-
await page.getByText("SatoshissatsCurrencyBTCPayment optionCollect payment on booking").click();
57-
await submitAndWaitForResponse(page, "/api/trpc/eventTypesHeavy/update?batch=1", {
58-
action: () => page.locator("[data-testid=update-eventtype]").click(),
59-
});
68+
await page.locator("#event-type-form").getByRole("switch").click();
69+
await page.getByPlaceholder("Price").click();
70+
await page.getByPlaceholder("Price").fill("200");
71+
await page.getByText("SatoshissatsCurrencyBTCPayment optionCollect payment on booking").click();
72+
await submitAndWaitForResponse(page, "/api/trpc/eventTypesHeavy/update?batch=1", {
73+
action: () => page.locator("[data-testid=update-eventtype]").click(),
74+
});
6075

61-
await page.goto(`${user.username}/${paymentEvent?.slug}`);
76+
await page.goto(`${user.username}/${paymentEvent?.slug}`);
6277

63-
// expect 200 sats to be displayed in page
64-
await expect(page.locator("text=200 sats").first()).toBeVisible();
78+
// expect 200 sats to be displayed in page
79+
await expect(page.locator("text=200 sats").first()).toBeVisible();
6580

66-
await selectFirstAvailableTimeSlotNextMonth(page);
67-
await expect(page.locator("text=200 sats").first()).toBeVisible();
81+
await selectFirstAvailableTimeSlotNextMonth(page);
82+
await expect(page.locator("text=200 sats").first()).toBeVisible();
6883

69-
// go to /event-types and check if the price is 200 sats
70-
await page.goto(`event-types/`);
71-
await expect(page.locator("text=200 sats").first()).toBeVisible();
84+
// go to /event-types and check if the price is 200 sats
85+
await page.goto(`event-types/`);
86+
await expect(page.locator("text=200 sats").first()).toBeVisible();
87+
} finally {
88+
await cleanupAlbyApp();
89+
}
7290
});
7391

7492
test("Should be able to edit stripe price, currency", async ({ page, users }) => {
@@ -169,27 +187,31 @@ test.describe("Payment app", () => {
169187
const paymentEvent = user.eventTypes.find((item) => item.slug === "paid");
170188
expect(paymentEvent).not.toBeNull();
171189
// Ensure alby app is enabled (it may be disabled if keys are not configured)
172-
await ensureAppEnabled("alby");
173-
await prisma.credential.create({
174-
data: {
175-
type: "alby_payment",
176-
appId: "alby",
177-
userId: user.id,
178-
key: {},
179-
},
180-
});
190+
const cleanupAlbyApp = await ensureAppEnabled("alby");
191+
try {
192+
await prisma.credential.create({
193+
data: {
194+
type: "alby_payment",
195+
appId: "alby",
196+
userId: user.id,
197+
key: {},
198+
},
199+
});
181200

182-
await goToAppsTab(page, paymentEvent?.id);
201+
await goToAppsTab(page, paymentEvent?.id);
183202

184-
await page.locator("#event-type-form").getByRole("switch").click();
203+
await page.locator("#event-type-form").getByRole("switch").click();
185204

186-
// expect text "This app has not been setup yet" to be displayed
187-
expect(await page.locator("text=This app has not been setup yet").first()).toBeTruthy();
205+
// expect text "This app has not been setup yet" to be displayed
206+
expect(await page.locator("text=This app has not been setup yet").first()).toBeTruthy();
188207

189-
await page.getByRole("button", { name: "Setup" }).click();
208+
await page.getByRole("button", { name: "Setup" }).click();
190209

191-
// Expect "Connect with Alby" to be displayed
192-
expect(await page.locator("text=Connect with Alby").first()).toBeTruthy();
210+
// Expect "Connect with Alby" to be displayed
211+
expect(await page.locator("text=Connect with Alby").first()).toBeTruthy();
212+
} finally {
213+
await cleanupAlbyApp();
214+
}
193215
});
194216

195217
test("Should display App is not setup already for paypal", async ({ page, users }) => {

0 commit comments

Comments
 (0)