Skip to content

fix: pad username suggestion suffixes to a consistent width#29571

Merged
bandhan-majumder merged 4 commits into
calcom:mainfrom
yashs33244:fix/username-suggestion-padding
Jun 19, 2026
Merged

fix: pad username suggestion suffixes to a consistent width#29571
bandhan-majumder merged 4 commits into
calcom:mainfrom
yashs33244:fix/username-suggestion-padding

Conversation

@yashs33244

@yashs33244 yashs33244 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Closes #29577.

generateUsernameSuggestion used padStart(4 - len, "0"), but padStart's first arg is the target length, not the pad count — so only single-digit suffixes got padded (1001, but 4242, 999999).

Pulled the padding into a small suffix() helper (it was duplicated) that pads to a consistent 3 digits, matching the existing johnny001 test, and added the two/three-digit cases.

yarn vitest on the file: 6/6, red-green checked.

generateUsernameSuggestion built its numeric suffix with
String(rand).padStart(4 - rand.toString().length, "0"). padStart's first
argument is the target total length, not the number of pad characters, so the
expression only padded single-digit numbers: rand=1 became "001" but rand=42
became "42" and rand=999 stayed "999". Suggested usernames therefore had
inconsistent suffix widths, and the magic 4 didn't match the 3-wide output the
existing test pins ("johnny001").

Pull the suffix into a small helper that always pads to 3 digits, so collisions
and the returned value share one definition and every suggestion is uniformly
zero-padded. Add unit tests for the first-attempt path and for two- and
three-digit suffixes; the two-digit case fails on the old code.
Copilot AI review requested due to automatic review settings June 15, 2026 09:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Cal.diy, @yashs33244! Thanks for opening this pull request.

A few things to keep in mind:

  • This is Cal.diy, not Cal.com. Cal.diy is a community-driven, fully open-source fork of Cal.com licensed under MIT. Your changes here will be part of Cal.diy — they will not be deployed to the Cal.com production app.
  • Please review our Contributing Guidelines if you haven't already.
  • Make sure your PR title follows the Conventional Commits format.

A maintainer will review your PR soon. Thanks for contributing!

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

generateUsernameSuggestion in username.ts is updated to format the numeric suffix with a fixed 3-digit, zero-padded representation using a new suffix() helper (padStart(3, "0")), replacing the previous variable-width padding. The collision-check loop and return statement are updated to use username + suffix(rand). The test file adds imports for afterEach and generateUsernameSuggestion, and introduces a new describe block with three test cases and an afterEach that calls vi.restoreAllMocks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: fixing username suggestion suffix padding to use a consistent width instead of variable-width padding.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description clearly relates to the changeset, explaining a specific bug fix in generateUsernameSuggestion with technical details about the padStart issue and the solution implemented.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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 (1)
packages/lib/server/username.test.ts (1)

60-85: ⚡ Quick win

Add a 1-character username regression test for suffix width.

Current tests don’t exercise the username.length < 2 path, so the fixed-width guarantee isn’t protected there. Add one test that forces a generated value >= 1000 and asserts whether width must remain 3 (per intended contract).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/lib/server/username.test.ts` around lines 60 - 85, Add a regression
test to the generateUsernameSuggestion test suite that exercises the
username.length < 2 code path. Create a test that uses a single-character
username (e.g., "a"), mocks Math.random to force generation of a suffix value >=
1000, and asserts that the result maintains the intended fixed-width padding
contract. This ensures the zero-padding behavior is consistent and protected for
short usernames where the suffix limit is higher than the username.length >= 2
case.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/lib/server/username.ts`:
- Around line 59-65: The limit value for short usernames (length < 2) is set to
9999 on line 59, which allows rand to reach values like 1000 or higher on line
64. When these larger values are passed to the suffix function on line 65, they
produce 4+ digit strings instead of the promised 3-digit fixed-width format.
Change the limit from 9999 to 999 for short usernames so that the maximum rand
value is 999, ensuring the padStart(3, "0") on the suffix function always
produces exactly 3 digits and maintains the fixed-width contract consistently
across all username lengths.

---

Nitpick comments:
In `@packages/lib/server/username.test.ts`:
- Around line 60-85: Add a regression test to the generateUsernameSuggestion
test suite that exercises the username.length < 2 code path. Create a test that
uses a single-character username (e.g., "a"), mocks Math.random to force
generation of a suffix value >= 1000, and asserts that the result maintains the
intended fixed-width padding contract. This ensures the zero-padding behavior is
consistent and protected for short usernames where the suffix limit is higher
than the username.length >= 2 case.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ca403a53-b558-4a95-8b5e-89c78970c5fa

📥 Commits

Reviewing files that changed from the base of the PR and between dcd0da8 and 7bcc143.

📒 Files selected for processing (2)
  • packages/lib/server/username.test.ts
  • packages/lib/server/username.ts

Comment thread packages/lib/server/username.ts

@bandhan-majumder bandhan-majumder left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please create an issue first and link it to the PR.

@bandhan-majumder bandhan-majumder marked this pull request as draft June 15, 2026 13:34
@yashs33244 yashs33244 marked this pull request as ready for review June 18, 2026 10:26
@yashs33244

Copy link
Copy Markdown
Contributor Author

This one's already linked to #29577 (Closes #29577 is in the description), so the issue + PR are paired. I've marked it ready for review — happy to adjust anything.

@bandhan-majumder bandhan-majumder added ready-for-e2e run-ci Approve CI to run for external contributors labels Jun 19, 2026
@bandhan-majumder bandhan-majumder added ready-for-e2e run-ci Approve CI to run for external contributors and removed ready-for-e2e run-ci Approve CI to run for external contributors labels Jun 19, 2026
@bandhan-majumder bandhan-majumder enabled auto-merge (squash) June 19, 2026 05:30
@bandhan-majumder bandhan-majumder merged commit 1599dfd into calcom:main Jun 19, 2026
46 of 58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-e2e run-ci Approve CI to run for external contributors size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

generateUsernameSuggestion only zero-pads single-digit suffixes

3 participants