-
Notifications
You must be signed in to change notification settings - Fork 14
fix: avoid no-op Google Sheets backfill rewrites #8864
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
Open
tataihono
wants to merge
5
commits into
main
Choose a base branch
from
cursor/sync-file-modification-trigger-af23
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
262027b
fix(google-sheets-sync): skip backfill writes when target range uncha…
cursoragent 63a63ba
fix(google-sheets-sync): enhance backfill logic to handle sheet conte…
mikeallisonJS 7b67c00
Merge branch 'main' into cursor/sync-file-modification-trigger-af23
mikeallisonJS 3350c0c
feat(google-sheets-sync): implement escapeSheetName function for shee…
mikeallisonJS e7d793d
Merge branch 'main' into cursor/sync-file-modification-trigger-af23
mikeallisonJS 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
173 changes: 173 additions & 0 deletions
173
apis/api-journeys-modern/src/workers/googleSheetsSync/service/backfill.spec.ts
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,173 @@ | ||
| import { Job } from 'bullmq' | ||
| import { Logger } from 'pino' | ||
|
|
||
| import { prismaMock } from '../../../../test/prismaMock' | ||
| import { getIntegrationGoogleAccessToken } from '../../../lib/google/googleAuth' | ||
| import { | ||
| clearSheet, | ||
| ensureSheet, | ||
| readValues, | ||
| writeValues | ||
| } from '../../../lib/google/sheets' | ||
| import { GoogleSheetsSyncBackfillJobData } from '../queue' | ||
|
|
||
| import { backfillService } from './backfill' | ||
|
|
||
| jest.mock('../../../lib/google/googleAuth', () => ({ | ||
| getIntegrationGoogleAccessToken: jest.fn() | ||
| })) | ||
|
|
||
| jest.mock('../../../lib/google/sheets', () => { | ||
| const actual = jest.requireActual('../../../lib/google/sheets') | ||
| return { | ||
| ...actual, | ||
| clearSheet: jest.fn(), | ||
| ensureSheet: jest.fn(), | ||
| readValues: jest.fn(), | ||
| writeValues: jest.fn() | ||
| } | ||
| }) | ||
|
|
||
| const mockGetIntegrationGoogleAccessToken = | ||
| getIntegrationGoogleAccessToken as jest.MockedFunction< | ||
| typeof getIntegrationGoogleAccessToken | ||
| > | ||
| const mockEnsureSheet = ensureSheet as jest.MockedFunction<typeof ensureSheet> | ||
| const mockReadValues = readValues as jest.MockedFunction<typeof readValues> | ||
| const mockClearSheet = clearSheet as jest.MockedFunction<typeof clearSheet> | ||
| const mockWriteValues = writeValues as jest.MockedFunction<typeof writeValues> | ||
|
|
||
| const backfillJob: Job<GoogleSheetsSyncBackfillJobData> = { | ||
| name: 'google-sheets-sync-backfill', | ||
| data: { | ||
| type: 'backfill', | ||
| journeyId: 'journey-id', | ||
| teamId: 'team-id', | ||
| syncId: 'sync-id', | ||
| spreadsheetId: 'spreadsheet-id', | ||
| sheetName: 'Sheet1', | ||
| timezone: 'UTC', | ||
| integrationId: 'integration-id' | ||
| } | ||
| } as unknown as Job<GoogleSheetsSyncBackfillJobData> | ||
|
|
||
| describe('backfillService', () => { | ||
| let logger: Logger | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| logger = { | ||
| info: jest.fn(), | ||
| warn: jest.fn(), | ||
| error: jest.fn() | ||
| } as unknown as Logger | ||
|
|
||
| prismaMock.googleSheetsSync.findFirst.mockResolvedValue({ | ||
| id: 'sync-id' | ||
| } as any) | ||
| prismaMock.journey.findUnique.mockResolvedValue({ | ||
| id: 'journey-id', | ||
| blocks: [] | ||
| } as any) | ||
| prismaMock.event.findMany.mockResolvedValue([] as any) | ||
| prismaMock.journeyVisitor.findMany.mockResolvedValue([] as any) | ||
|
|
||
| mockGetIntegrationGoogleAccessToken.mockResolvedValue({ | ||
| accessToken: 'access-token', | ||
| accountEmail: '[email protected]' | ||
| }) | ||
| mockEnsureSheet.mockResolvedValue(undefined) | ||
| }) | ||
|
|
||
| it('skips clear/write when sheet content is unchanged', async () => { | ||
| mockReadValues.mockResolvedValue([['Visitor ID', 'Date']]) | ||
|
|
||
| await backfillService(backfillJob, logger) | ||
|
|
||
| expect(mockReadValues).toHaveBeenCalledWith({ | ||
| accessToken: 'access-token', | ||
| spreadsheetId: 'spreadsheet-id', | ||
| range: 'Sheet1!A:B' | ||
| }) | ||
| expect(mockClearSheet).not.toHaveBeenCalled() | ||
| expect(mockWriteValues).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('clears and rewrites when sheet content changed', async () => { | ||
| mockReadValues.mockResolvedValue([['Visitor ID', 'Date (Old)']]) | ||
|
|
||
| await backfillService(backfillJob, logger) | ||
|
|
||
| expect(mockClearSheet).toHaveBeenCalledWith({ | ||
| accessToken: 'access-token', | ||
| spreadsheetId: 'spreadsheet-id', | ||
| sheetTitle: 'Sheet1' | ||
| }) | ||
| expect(mockWriteValues).toHaveBeenCalledWith({ | ||
| accessToken: 'access-token', | ||
| spreadsheetId: 'spreadsheet-id', | ||
| sheetTitle: 'Sheet1', | ||
| values: [['Visitor ID', 'Date']], | ||
| append: false | ||
| }) | ||
| }) | ||
|
|
||
| it('clears and rewrites when existing sheet has more rows than new data', async () => { | ||
| mockReadValues.mockResolvedValue([ | ||
| ['Visitor ID', 'Date'], | ||
| ['visitor-1', '2026-01-01'] | ||
| ]) | ||
|
|
||
| await backfillService(backfillJob, logger) | ||
|
|
||
| expect(mockClearSheet).toHaveBeenCalled() | ||
| expect(mockWriteValues).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('clears and rewrites when existing sheet is empty', async () => { | ||
| mockReadValues.mockResolvedValue([]) | ||
|
|
||
| await backfillService(backfillJob, logger) | ||
|
|
||
| expect(mockClearSheet).toHaveBeenCalled() | ||
| expect(mockWriteValues).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('falls through to write when readValues fails', async () => { | ||
| mockReadValues.mockRejectedValue(new Error('API rate limit exceeded')) | ||
|
|
||
| await backfillService(backfillJob, logger) | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| expect.objectContaining({ err: expect.any(Error) }), | ||
| 'Failed to read existing sheet values, proceeding with full write' | ||
| ) | ||
| expect(mockClearSheet).toHaveBeenCalled() | ||
| expect(mockWriteValues).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('skips backfill when sync is not found', async () => { | ||
| prismaMock.googleSheetsSync.findFirst.mockResolvedValue(null) | ||
|
|
||
| await backfillService(backfillJob, logger) | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| expect.objectContaining({ syncId: 'sync-id' }), | ||
| 'Sync not found or deleted, skipping backfill' | ||
| ) | ||
| expect(mockEnsureSheet).not.toHaveBeenCalled() | ||
| expect(mockReadValues).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('skips backfill when journey is not found', async () => { | ||
| prismaMock.journey.findUnique.mockResolvedValue(null) | ||
|
|
||
| await backfillService(backfillJob, logger) | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| expect.objectContaining({ journeyId: 'journey-id' }), | ||
| 'Journey not found, skipping backfill' | ||
| ) | ||
| expect(mockEnsureSheet).not.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
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.
Uh oh!
There was an error while loading. Please reload this page.