Skip to content

Conversation

anikdhabal
Copy link
Contributor

@anikdhabal anikdhabal commented Jul 15, 2025

feat: add race condition test with dynamic date selection

Summary

This PR recreates the race condition test from PR #22401 with a key improvement: replacing the hardcoded TEST_DATE with dynamic first available slot selection to prevent test flakiness.

Key Changes:

  • Created apps/web/playwright/booking-race-condition.e2e.ts with the same race condition testing logic as the original PR
  • Replaced hardcoded "tomorrow" date logic with getDynamicBookingDate() function that selects the first available day dynamically
  • Uses page.locator('[data-testid="day"][data-disabled="false"]').nth(0) to find the first available slot
  • Test validates concurrent booking prevention and calendar cache functionality

Dynamic Date Selection Implementation:

  • getDynamicBookingDate() navigates to the booking page, increments to next month, and extracts the first available day
  • All helper functions (setupCalendarCache, mockGoogleCalendarAPI, performConcurrentBookings) now accept selectedDateISO parameter instead of using hardcoded dates
  • Maintains the original test logic while making dates dynamic

Review & Testing Checklist for Human

🔴 CRITICAL - Test was not successfully executed during development due to environment issues

  • Run the E2E test 5 times to validate consistency and check for flakiness: TZ=UTC PLAYWRIGHT_HEADLESS=1 yarn e2e booking-race-condition.e2e.ts (repeat 5 times)
  • Verify dynamic date selection works - check that the test correctly identifies and selects the first available booking slot
  • Confirm race condition prevention still works - ensure concurrent bookings are properly handled and duplicate bookings are prevented
  • Test the calendar cache functionality - verify that the cache setup and Google Calendar API mocking work correctly
  • Review the test setup logic - ensure team/org creation, round-robin configuration, and host setup are correct

Recommended Test Plan:

  1. Run the test locally 5 times in sequence to check for flakiness
  2. Monitor the console output for any race condition detection and booking distribution
  3. Verify that the test consistently passes without hardcoded date issues
  4. Check that all concurrent booking scenarios are properly tested

Diagram

%%{ init : { "theme" : "default" }}%%
graph TD
    Test["apps/web/playwright/<br/>booking-race-condition.e2e.ts"]:::major-edit
    TestUtils["apps/web/playwright/lib/<br/>testUtils.ts"]:::context
    OrgFixture["apps/web/playwright/<br/>fixtures/orgs.ts"]:::context
    UserFixture["apps/web/playwright/<br/>fixtures/users.ts"]:::context
    
    Test --> TestUtils
    Test --> OrgFixture
    Test --> UserFixture
    
    Test --> GetDynamicDate["getDynamicBookingDate()"]:::major-edit
    Test --> SetupCache["setupCalendarCache()"]:::major-edit
    Test --> MockAPI["mockGoogleCalendarAPI()"]:::major-edit
    Test --> ConcurrentBookings["performConcurrentBookings()"]:::major-edit
    
    GetDynamicDate --> TestUtils
    
    subgraph Legend
        L1[Major Edit]:::major-edit
        L2[Minor Edit]:::minor-edit
        L3[Context/No Edit]:::context
    end
    
    classDef major-edit fill:#90EE90
    classDef minor-edit fill:#87CEEB
    classDef context fill:#FFFFFF
Loading

Notes

Link to Devin run: https://app.devin.ai/sessions/55c22ac32dfa4345ae51aaa6f3183992

- Recreate race condition test from PR #22401 for calendar-cache-serve booking flow
- Replace hardcoded TEST_DATE with dynamic first available slot selection
- Use getDynamicBookingDate function to select first available day dynamically
- Prevent test flakiness by avoiding hardcoded tomorrow dates
- Test validates concurrent booking prevention and cache functionality

Co-Authored-By: anik@cal.com <adhabal2002@gmail.com>
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR that start with 'DevinAI'.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Copy link
Contributor

coderabbitai bot commented Jul 15, 2025

Walkthrough

A new end-to-end test file has been added to validate the prevention of double-booking race conditions in a calendar booking system. The test establishes a round-robin team environment with multiple hosts, sets up deterministic calendar data, and mocks Google Calendar API responses. It simulates concurrent booking attempts for the same time slot using two browser contexts, then analyzes the outcomes to verify that double bookings are prevented either by database constraints or by assigning different hosts. The test also checks calendar cache functionality and round-robin logic, and logs detailed diagnostic results.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch devin/1752595792-fix-e2e-hardcoded-test-date

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@keithwillcode keithwillcode added the core area: core, team members only label Jul 15, 2025
Copy link

vercel bot commented Jul 15, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Visit Preview Jul 29, 2025 3:37pm
cal-eu ⬜️ Ignored (Inspect) Visit Preview Jul 29, 2025 3:37pm

Copy link

delve-auditor bot commented Jul 15, 2025

No security or compliance issues detected. Reviewed everything up to c5f7775.

Security Overview
  • 🔎 Scanned files: 1 changed file(s)
Detected Code Changes
Change Type Relevant files
Enhancement ► booking-race-condition.e2e.ts
    Add race condition test with dynamic date selection for calendar-cache-serve booking flow

Reply to this PR with @delve-auditor followed by a description of what change you want and we'll auto-submit a change to this PR to implement it.

Copy link
Contributor

github-actions bot commented Jul 15, 2025

E2E results are ready!

…ency

Co-Authored-By: anik@cal.com <adhabal2002@gmail.com>
@anikdhabal anikdhabal marked this pull request as ready for review July 29, 2025 15:37
@graphite-app graphite-app bot requested a review from a team July 29, 2025 15:38
@anikdhabal anikdhabal enabled auto-merge (squash) July 29, 2025 15:38
@dosubot dosubot bot added the automated-tests area: unit tests, e2e tests, playwright label Jul 29, 2025
Copy link

graphite-app bot commented Jul 29, 2025

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (07/29/25)

1 reviewer was added to this PR based on Keith Williams's automation.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (6)
apps/web/playwright/booking-race-condition.e2e.ts (6)

347-347: Move type alias to the top of the file.

For better code organization and readability, move this type alias near the other type imports at the top of the file.

Move this line after line 10:

import type { teamMetadataSchema } from "@calcom/prisma/zod-utils";

export type TeamWithMetadata = Team & { metadata: z.infer<typeof teamMetadataSchema> };

145-155: Consider adding a comment to clarify test credentials.

While these are clearly test credentials, adding a comment would make it explicit that these tokens are for testing purposes only and not real credentials.

        key: {
+         // Test credentials only - not real tokens
          access_token: "test_access_token",
          refresh_token: "test_refresh_token",

349-349: Remove unnecessary export.

This function is only used within this test file and doesn't need to be exported.

-export type TeamWithMetadata = Team & { metadata: z.infer<typeof teamMetadataSchema> };
+type TeamWithMetadata = Team & { metadata: z.infer<typeof teamMetadataSchema> };

366-368: Extract duplicated date calculation logic.

This date calculation logic is duplicated from getDynamicBookingDate. Consider passing selectedDateISO as a parameter instead.

Update the function signature and remove the duplicate calculation:

async function performConcurrentBookings(
  page: Page,
  browser: Browser,
  org: TeamWithMetadata,
  team: TeamWithMetadata,
  teamEvent: EventType,
- selectedDate: string
+ selectedDate: string,
+ selectedDateISO: string
) {
-   const currentDate = new Date();
-   const nextMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, parseInt(selectedDate));
-   const selectedDateISO = nextMonth.toISOString();

Then update the call site at line 73-80 to pass both values.


408-408: Remove unnecessary export.

This function is only used within this test file and doesn't need to be exported.

-export async function analyzeBookingResults(
+async function analyzeBookingResults(

413-418: Optimize database query.

The query fetches 10 bookings but only uses the first 2. Consider limiting the query to what's actually needed.

  const bookings = await prisma.booking.findMany({
    where: { eventTypeId },
    include: { attendees: true },
    orderBy: { createdAt: "desc" },
-   take: 10,
+   take: 2,
  });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 12b418b and dc16cf2.

📒 Files selected for processing (1)
  • apps/web/playwright/booking-race-condition.e2e.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.ts

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

**/*.ts: For Prisma queries, only select data you need; never use include, always use select
Ensure the credential.key field is never returned from tRPC endpoints or APIs

Files:

  • apps/web/playwright/booking-race-condition.e2e.ts
**/*.{ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

Flag excessive Day.js use in performance-critical code; prefer native Date or Day.js .utc() in hot paths like loops

Files:

  • apps/web/playwright/booking-race-condition.e2e.ts
🧠 Learnings (2)
📓 Common learnings
Learnt from: CarinaWolli
PR: calcom/cal.com#22296
File: packages/lib/bookings/filterHostsBySameRoundRobinHost.ts:41-42
Timestamp: 2025-07-22T11:42:47.623Z
Learning: The filterHostsBySameRoundRobinHost function in packages/lib/bookings/filterHostsBySameRoundRobinHost.ts has a known limitation where it doesn't work correctly with fixed hosts or round robin groups. This is pre-existing technical debt that was already broken before the round robin groups feature. CarinaWolli has documented this in Linear issue CAL-6134 for future fix.
Learnt from: vijayraghav-io
PR: calcom/cal.com#21072
File: .env.example:354-357
Timestamp: 2025-07-18T08:49:18.779Z
Learning: For E2E integration tests that require real third-party service credentials (like Outlook calendar), it's acceptable to temporarily include actual test account credentials in .env.example during feature development and validation, provided there's a clear plan to replace them with placeholder values before final release. Test credentials should be for dedicated test tenants/accounts, not production systems.
Learnt from: vijayraghav-io
PR: calcom/cal.com#21072
File: packages/prisma/schema.prisma:891-891
Timestamp: 2025-07-18T08:47:01.264Z
Learning: In Cal.com's calendar integration, both Google Calendar and Outlook Calendar are designed to allow multiple eventTypeIds to share the same subscription ID (googleChannelId or outlookSubscriptionId). This is an intentional design pattern to reuse existing subscriptions for efficiency rather than creating separate subscriptions for each event type. Therefore, unique constraints like `@@unique([outlookSubscriptionId, eventTypeId])` should not be added as they would break this subscription sharing functionality.
Learnt from: vijayraghav-io
PR: calcom/cal.com#21072
File: packages/prisma/schema.prisma:891-891
Timestamp: 2025-07-18T08:47:01.264Z
Learning: The Outlook Calendar integration in Cal.com intentionally reuses subscription IDs across multiple event types for efficiency. The `upsertSelectedCalendarsForEventTypeIds` method creates separate SelectedCalendar records for each eventTypeId, all sharing the same outlookSubscriptionId. This subscription sharing pattern means that unique constraints like `@@unique([outlookSubscriptionId, eventTypeId])` should not be applied as they would prevent this intended functionality.
apps/web/playwright/booking-race-condition.e2e.ts (3)

Learnt from: CarinaWolli
PR: #22296
File: packages/lib/bookings/filterHostsBySameRoundRobinHost.ts:41-42
Timestamp: 2025-07-22T11:42:47.623Z
Learning: The filterHostsBySameRoundRobinHost function in packages/lib/bookings/filterHostsBySameRoundRobinHost.ts has a known limitation where it doesn't work correctly with fixed hosts or round robin groups. This is pre-existing technical debt that was already broken before the round robin groups feature. CarinaWolli has documented this in Linear issue CAL-6134 for future fix.

Learnt from: vijayraghav-io
PR: #21072
File: packages/app-store/office365calendar/api/webhook.ts:120-123
Timestamp: 2025-07-18T17:57:16.395Z
Learning: The office365calendar webhook handler in packages/app-store/office365calendar/api/webhook.ts is specifically designed for Office365 calendar integration, not as a generic webhook handler. Therefore, it's safe to assume that fetchAvailabilityAndSetCache method will be implemented in the Office365CalendarService, making explicit validation checks unnecessary.

Learnt from: eunjae-lee
PR: #22106
File: packages/features/insights/components/FailedBookingsByField.tsx:65-71
Timestamp: 2025-07-15T12:59:34.389Z
Learning: In the FailedBookingsByField component (packages/features/insights/components/FailedBookingsByField.tsx), although routingFormId is typed as optional in useInsightsParameters, the system automatically enforces a routing form filter, so routingFormId is always present in practice. This means the data always contains only one entry, making the single-entry destructuring approach safe.

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: required
  • GitHub Check: labeler
🔇 Additional comments (6)
apps/web/playwright/booking-race-condition.e2e.ts (6)

56-93: Well-structured test implementation!

The test effectively validates race condition prevention with proper setup, concurrent booking simulation, and result analysis. Good use of dynamic date selection and detailed logging for diagnostics.


95-126: Excellent setup function with proper data selection!

The function correctly creates the test environment with round-robin scheduling and follows best practices by using select instead of include in Prisma queries.


161-188: Good test data setup with proper relationships!

The function creates realistic booking histories with attendees and uses relative timestamps appropriately.


190-227: Excellent implementation of dynamic date selection!

The function properly handles navigation, waits for elements, and includes error handling. This successfully addresses the flakiness issue from hardcoded dates mentioned in the PR objectives.


229-284: Well-implemented calendar cache setup!

The function correctly uses the calendar cache system with proper date range expansion using getTimeMin/getTimeMax utilities. The availability data setup creates a realistic scenario for testing race conditions.


286-303: Efficient feature flag setup!

Good use of createMany for batch insertion of feature flags.

@anikdhabal
Copy link
Contributor Author

@zomars all checks are green. Can you pls review it and merge?

@anikdhabal anikdhabal merged commit d78ed21 into main Jul 30, 2025
105 of 110 checks passed
@anikdhabal anikdhabal deleted the devin/1752595792-fix-e2e-hardcoded-test-date branch July 30, 2025 04:43
@coderabbitai coderabbitai bot mentioned this pull request Jul 30, 2025
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automated-tests area: unit tests, e2e tests, playwright core area: core, team members only ready-for-e2e
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants