Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions integration/tests/multitab-token-cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { expect, test } from '@playwright/test';

import { appConfigs } from '../presets';
import type { FakeUser } from '../testUtils';
import { createTestUtils, testAgainstRunningApps } from '../testUtils';

testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })(
'MemoryTokenCache Multi-Tab Integration @generic @nextjs',
({ app }) => {
test.describe.configure({ mode: 'serial' });

let fakeUser: FakeUser;

test.beforeAll(async () => {
const u = createTestUtils({ app });
fakeUser = u.services.users.createFakeUser({
withPhoneNumber: true,
withUsername: true,
});
await u.services.users.createBapiUser(fakeUser);
});

test.afterAll(async () => {
await fakeUser.deleteIfExists();
await app.teardown();
});

test('MemoryTokenCache multi-tab token sharing', async ({ context }) => {
const page1 = await context.newPage();
const page2 = await context.newPage();

await page1.goto(app.serverUrl);
await page2.goto(app.serverUrl);

await page1.waitForFunction(() => (window as any).Clerk?.loaded);
await page2.waitForFunction(() => (window as any).Clerk?.loaded);

Comment on lines +35 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Avoid truthy “loaded” checks — can pass too early.

waitForFunction(() => Clerk?.loaded) may resolve immediately if loaded is a Promise/object. Check explicitly for === true and a ready session.

-      await page1.waitForFunction(() => (window as any).Clerk?.loaded);
-      await page2.waitForFunction(() => (window as any).Clerk?.loaded);
+      await page1.waitForFunction(() => {
+        const c = (window as any).Clerk;
+        return !!c && c.loaded === true && !!c.session?.id;
+      });
+      await page2.waitForFunction(() => {
+        const c = (window as any).Clerk;
+        return !!c && c.loaded === true && !!c.session?.id;
+      });

Based on learnings.

Also applies to: 49-50

🤖 Prompt for AI Agents
In integration/tests/multitab-token-cache.test.ts around lines 35-37 (also apply
same change at 49-50), the test uses a truthy check await
page.waitForFunction(() => (window as any).Clerk?.loaded) which can return
prematurely if loaded is a Promise/object; update the predicate to explicitly
check for boolean true and a ready session — e.g., waitForFunction(() => (window
as any).Clerk?.loaded === true && (window as any).Clerk.session !== undefined &&
(window as any).Clerk.session !== null) so the function only resolves when
loaded is strictly true and a session is present.

const u1 = createTestUtils({ app, page: page1 });
await u1.po.signIn.goTo();
await u1.po.signIn.setIdentifier(fakeUser.email);
await u1.po.signIn.continue();
await u1.po.signIn.setPassword(fakeUser.password);
await u1.po.signIn.continue();
await u1.po.expect.toBeSignedIn();

await page1.waitForTimeout(1000);

await page2.reload();
await page2.waitForFunction(() => (window as any).Clerk?.loaded);

const u2 = createTestUtils({ app, page: page2 });
await u2.po.expect.toBeSignedIn();

const clerkInfo1 = await page1.evaluate(() => {
const clerk = (window as any).Clerk;
return {
hasBroadcastChannel: !!clerk?._broadcastChannel,
loaded: clerk?.loaded,
};
});

const clerkInfo2 = await page2.evaluate(() => {
const clerk = (window as any).Clerk;
return {
hasBroadcastChannel: !!clerk?._broadcastChannel,
loaded: clerk?.loaded,
};
});

expect(clerkInfo1.loaded).toBe(true);
expect(clerkInfo2.loaded).toBe(true);

expect(clerkInfo1.hasBroadcastChannel).toBe(true);
expect(clerkInfo2.hasBroadcastChannel).toBe(true);

const page1SessionInfo = await page1.evaluate(() => {
const clerk = (window as any).Clerk;
return {
sessionId: clerk?.session?.id,
userId: clerk?.user?.id,
};
});

expect(page1SessionInfo.sessionId).toBeDefined();
expect(page1SessionInfo.userId).toBeDefined();

await Promise.all([
page1.evaluate(() => (window as any).Clerk.session?.clearCache()),
page2.evaluate(() => (window as any).Clerk.session?.clearCache()),
]);

Comment on lines +87 to +91
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Guard clearCache() in case the API is unavailable.

If session.clearCache isn’t present in some builds, this will throw inside the page.

-      await Promise.all([
-        page1.evaluate(() => (window as any).Clerk.session?.clearCache()),
-        page2.evaluate(() => (window as any).Clerk.session?.clearCache()),
-      ]);
+      await Promise.all([
+        page1.evaluate(async () => {
+          const s = (window as any).Clerk?.session;
+          if (s && typeof s.clearCache === 'function') await s.clearCache();
+        }),
+        page2.evaluate(async () => {
+          const s = (window as any).Clerk?.session;
+          if (s && typeof s.clearCache === 'function') await s.clearCache();
+        }),
+      ]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
await Promise.all([
page1.evaluate(() => (window as any).Clerk.session?.clearCache()),
page2.evaluate(() => (window as any).Clerk.session?.clearCache()),
]);
await Promise.all([
page1.evaluate(async () => {
const s = (window as any).Clerk?.session;
if (s && typeof s.clearCache === 'function') await s.clearCache();
}),
page2.evaluate(async () => {
const s = (window as any).Clerk?.session;
if (s && typeof s.clearCache === 'function') await s.clearCache();
}),
]);
🤖 Prompt for AI Agents
In integration/tests/multitab-token-cache.test.ts around lines 87 to 91, the
test directly calls page1.evaluate and page2.evaluate invoking (window as
any).Clerk.session?.clearCache(), which can throw if clearCache is not present
in some builds; update the page.evaluate calls to check that (window as
any).Clerk?.session?.clearCache is a function before calling it (e.g., use
optional chaining and typeof check inside the page context) so the call is only
executed when available, preventing runtime errors in builds without that API.

// Track token fetch requests to verify only one network call happens
const tokenRequests: string[] = [];
await context.route('**/v1/client/sessions/*/tokens*', async route => {
tokenRequests.push(route.request().url());
await route.continue();
});

const page1Token = await page1.evaluate(async () => {
const clerk = (window as any).Clerk;
return await clerk.session?.getToken({ skipCache: true });
});

expect(page1Token).toBeTruthy();

await page1.waitForTimeout(1500);

const page2Result = await page2.evaluate(async () => {
const clerk = (window as any).Clerk;

const token = await clerk.session?.getToken();

return {
sessionId: clerk?.session?.id,
token,
userId: clerk?.user?.id,
};
});

expect(page2Result.sessionId).toBe(page1SessionInfo.sessionId);
expect(page2Result.userId).toBe(page1SessionInfo.userId);

expect(page2Result.token).toBe(page1Token);

// Verify only one token fetch happened (page1), proving page2 got it from BroadcastChannel
expect(tokenRequests.length).toBe(1);
});
},
);
Loading