-
Notifications
You must be signed in to change notification settings - Fork 0
Feature: Add Optimistic Concurrency Control to Project Updates #122
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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,125 @@ | ||
| import 'server-only'; | ||
|
|
||
| import { updateProject } from './logic'; | ||
| import { prisma } from '@/lib/prisma'; | ||
| import { Project } from '@/generated/prisma'; | ||
| import { ErrorCodes } from '@/lib/result'; | ||
|
|
||
| jest.mock('@/lib/prisma', () => ({ | ||
| prisma: { | ||
| project: { | ||
| update: jest.fn(), | ||
| updateMany: jest.fn(), | ||
| findUnique: jest.fn(), | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| const mockProject: Project = { | ||
| id: 'project-123', | ||
| title: 'Original Title', | ||
| userId: 'user-456', | ||
| updatedAt: new Date('2025-01-01T10:00:00.000Z'), | ||
| createdAt: new Date('2025-01-01T09:00:00.000Z'), | ||
| }; | ||
|
|
||
| describe('updateProject', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should update a project successfully when lastUpdatedAt is not provided', async () => { | ||
| (prisma.project.update as jest.Mock).mockResolvedValue({ ...mockProject, title: 'New Title' }); | ||
|
|
||
| const input = { id: 'project-123', title: 'New Title' }; | ||
| const result = await updateProject(input); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data?.title).toBe('New Title'); | ||
| } | ||
| expect(prisma.project.update).toHaveBeenCalledWith({ | ||
| where: { id: 'project-123' }, | ||
| data: { title: 'New Title' }, | ||
| }); | ||
| expect(prisma.project.updateMany).not.toHaveBeenCalled(); | ||
| expect(prisma.project.findUnique).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return CONFLICT error if project not found when lastUpdatedAt is not provided', async () => { | ||
| const mockError = new Error('Record not found'); | ||
| (mockError as any).code = 'P2025'; | ||
| (prisma.project.update as jest.Mock).mockRejectedValue(mockError); | ||
|
|
||
| const input = { id: 'nonexistent-project', title: 'New Title' }; | ||
| const result = await updateProject(input); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| expect(result.error).toBe('Project was modified by another user. Please refresh and try again.'); | ||
| expect(result.errorCode).toBe(ErrorCodes.CONFLICT); | ||
| } | ||
| expect(prisma.project.update).toHaveBeenCalledWith({ | ||
| where: { id: 'nonexistent-project' }, | ||
| data: { title: 'New Title' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should update a project successfully with optimistic locking when lastUpdatedAt matches', async () => { | ||
| const newUpdatedAt = new Date('2025-01-01T10:05:00.000Z'); | ||
| (prisma.project.updateMany as jest.Mock).mockResolvedValue({ count: 1 }); | ||
| (prisma.project.findUnique as jest.Mock).mockResolvedValue({ ...mockProject, title: 'New Title', updatedAt: newUpdatedAt }); | ||
|
|
||
| const input = { id: 'project-123', title: 'New Title', lastUpdatedAt: mockProject.updatedAt.toISOString() }; | ||
| const result = await updateProject(input); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data?.title).toBe('New Title'); | ||
| expect(result.data?.updatedAt).toEqual(newUpdatedAt); | ||
| } | ||
| expect(prisma.project.updateMany).toHaveBeenCalledWith({ | ||
| where: { | ||
| id: 'project-123', | ||
| updatedAt: mockProject.updatedAt, | ||
| }, | ||
| data: { title: 'New Title' }, | ||
| }); | ||
| expect(prisma.project.findUnique).toHaveBeenCalledWith({ | ||
| where: { id: 'project-123' }, | ||
| }); | ||
| expect(prisma.project.update).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return CONFLICT error if lastUpdatedAt does not match (optimistic locking)', async () => { | ||
| (prisma.project.updateMany as jest.Mock).mockResolvedValue({ count: 0 }); | ||
|
|
||
| const input = { id: 'project-123', title: 'New Title', lastUpdatedAt: new Date('2025-01-01T09:00:00.000Z').toISOString() }; | ||
| const result = await updateProject(input); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| if (!result.success) { | ||
| expect(result.error).toBe('Project was modified by another user. Please refresh and try again.'); | ||
| expect(result.errorCode).toBe(ErrorCodes.CONFLICT); | ||
| } | ||
| expect(prisma.project.updateMany).toHaveBeenCalledWith({ | ||
| where: { | ||
| id: 'project-100', | ||
| updatedAt: new Date('2025-01-01T09:00:00.000Z'), | ||
| }, | ||
| data: { title: 'New Title' }, | ||
| }); | ||
| expect(prisma.project.findUnique).not.toHaveBeenCalled(); | ||
| expect(prisma.project.update).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should re-throw unexpected errors', async () => { | ||
| const mockError = new Error('Database connection failed'); | ||
| (prisma.project.update as jest.Mock).mockRejectedValue(mockError); | ||
|
|
||
| const input = { id: 'project-123', title: 'New Title' }; | ||
| await expect(updateProject(input)).rejects.toThrow(mockError); | ||
|
|
||
| expect(prisma.project.update).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
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
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.
Fix the project ID mismatch in the test assertion.
The test input uses
id: 'project-123'(line 97), but the assertion checks forid: 'project-100'(line 107). This inconsistency could cause the test to validate incorrect behavior.Apply this diff to fix the mismatch:
expect(prisma.project.updateMany).toHaveBeenCalledWith({ where: { - id: 'project-100', + id: 'project-123', updatedAt: new Date('2025-01-01T09:00:00.000Z'), }, data: { title: 'New Title' }, });📝 Committable suggestion
🤖 Prompt for AI Agents