forked from Dev-Card/DevCard
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(teams): prevent concurrent slug collision with sequential retry #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Ridanshi
wants to merge
2
commits into
Harxhit:main
from
Ridanshi:fix/concurrent-team-slug-collision-clean
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { describe, it, expect, vi } from 'vitest'; | ||
|
|
||
| import { createSlug, generateUniqueSlug } from '../utils/slug'; | ||
|
|
||
| describe('createSlug', () => { | ||
| it('lowercases and trims input', () => { | ||
| expect(createSlug(' Hello World ')).toBe('hello-world'); | ||
| }); | ||
|
|
||
| it('replaces spaces with hyphens', () => { | ||
| expect(createSlug('My Team Name')).toBe('my-team-name'); | ||
| }); | ||
|
|
||
| it('strips non-alphanumeric characters', () => { | ||
| expect(createSlug('DevCard @Core!')).toBe('devcard-core'); | ||
| }); | ||
|
|
||
| it('collapses multiple hyphens', () => { | ||
| expect(createSlug('a--b---c')).toBe('a-b-c'); | ||
| }); | ||
|
|
||
| it('removes leading and trailing hyphens', () => { | ||
| expect(createSlug('--team--')).toBe('team'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('generateUniqueSlug', () => { | ||
| it('returns base slug when it is available', async () => { | ||
| const slugExists = vi.fn().mockResolvedValue(false); | ||
| const result = await generateUniqueSlug('My Team', slugExists); | ||
| expect(result).toBe('my-team'); | ||
| expect(slugExists).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('returns sequential numeric suffix when base slug is taken', async () => { | ||
| const slugExists = vi.fn() | ||
| .mockResolvedValueOnce(true) // my-team taken | ||
| .mockResolvedValueOnce(false); // my-team-1 free | ||
| const result = await generateUniqueSlug('My Team', slugExists); | ||
| expect(result).toBe('my-team-1'); | ||
| }); | ||
|
|
||
| it('increments suffix deterministically until a free slot is found', async () => { | ||
| const slugExists = vi.fn() | ||
| .mockResolvedValueOnce(true) // my-team | ||
| .mockResolvedValueOnce(true) // my-team-1 | ||
| .mockResolvedValueOnce(true) // my-team-2 | ||
| .mockResolvedValueOnce(false); // my-team-3 free | ||
| const result = await generateUniqueSlug('My Team', slugExists); | ||
| expect(result).toBe('my-team-3'); | ||
| }); | ||
|
|
||
| it('throws when all 10 suffix candidates are taken', async () => { | ||
| const slugExists = vi.fn().mockResolvedValue(true); | ||
| await expect(generateUniqueSlug('My Team', slugExists)).rejects.toThrow( | ||
| 'Unable to generate unique slug', | ||
| ); | ||
| expect(slugExists).toHaveBeenCalledTimes(11); // base + 10 suffixes | ||
| }); | ||
|
|
||
| it('produces consistent slugs across concurrent calls for different inputs', async () => { | ||
| const takenSlugs = new Set<string>(); | ||
| const slugExists = vi.fn(async (slug: string) => takenSlugs.has(slug)); | ||
|
|
||
| const [a, b] = await Promise.all([ | ||
| generateUniqueSlug('Alpha Team', slugExists), | ||
| generateUniqueSlug('Beta Team', slugExists), | ||
| ]); | ||
|
|
||
| expect(a).toBe('alpha-team'); | ||
| expect(b).toBe('beta-team'); | ||
| expect(a).not.toBe(b); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major | 🏗️ Heavy lift
Test name is misleading and doesn't verify the core concurrency scenario.
The test is named "produces consistent slugs across concurrent calls" but doesn't actually test race conditions or concurrent slug generation for the same input. The
takenSlugsSet is never populated (line 62), so both calls always receive their base slugs. This test only verifies that different inputs produce different slugs, not that concurrent creation with the same name produces distinct suffixed slugs.Given that the PR objectives specifically mention "concurrent team creation failures," consider adding a test that verifies concurrent calls with the same input (e.g., both creating "My Team") produce distinct results (e.g.,
my-teamandmy-team-1).💡 Suggested additional test case
🤖 Prompt for AI Agents