-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(replay): attach replay ID from when the feedback widget opened #24570
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
mydea
merged 4 commits into
develop
from
joshgoldberg/replay-1000-flush-recording-buffer-when-user-feedback-is-opened-vs-sent
Sep 22, 2026
+194
−4
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f3d43af
fix(replay): Attach replay ID from when the feedback widget opened
JoshuaKGoldberg ffa138a
test(replay): Cover feedback widget opened while replay is disabled
JoshuaKGoldberg f05dd9a
fix(replay): Keep feedback replay ID captured when the widget opened
JoshuaKGoldberg 8b282ad
fix(replay): Skip flushing a refreshed session for widget feedback
JoshuaKGoldberg 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
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
167 changes: 167 additions & 0 deletions
167
packages/replay-internal/test/integration/feedback.test.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,167 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
|
|
||
| import '../utils/mock-internal-setTimeout'; | ||
| import type { Event, FeedbackEvent } from '@sentry/core'; | ||
| import { captureFeedback, getClient } from '@sentry/core'; | ||
| import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { DEFAULT_FLUSH_MIN_DELAY, SESSION_IDLE_EXPIRE_DURATION } from '../../src/constants'; | ||
| import type { ReplayContainer } from '../../src/replay'; | ||
| import { clearSession } from '../../src/session/clearSession'; | ||
| import { BASE_TIMESTAMP } from '../index'; | ||
| import type { RecordMock } from '../index'; | ||
| import { resetSdkMock } from '../mocks/resetSdkMock'; | ||
| import type { DomHandler } from '../types'; | ||
| import { getTestEventIncremental } from '../utils/getTestEvent'; | ||
|
|
||
| async function advanceTimers(time: number) { | ||
| vi.advanceTimersByTime(time); | ||
| await new Promise(process.nextTick); | ||
| } | ||
|
|
||
| function createFeedbackEvent(source: string): FeedbackEvent { | ||
| return { | ||
| type: 'feedback', | ||
| contexts: { | ||
| feedback: { | ||
| message: 'Something broke', | ||
| source, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe('Integration | feedback', () => { | ||
| let replay: ReplayContainer; | ||
| let mockRecord: RecordMock; | ||
| let domHandler: DomHandler; | ||
|
|
||
| beforeAll(() => { | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| ({ mockRecord, domHandler, replay } = await resetSdkMock({ | ||
| replayOptions: { | ||
| stickySession: true, | ||
| }, | ||
| sentryOptions: { | ||
| replaysSessionSampleRate: 0.0, | ||
| replaysOnErrorSampleRate: 1.0, | ||
| }, | ||
| })); | ||
|
|
||
| mockRecord._emitter(getTestEventIncremental({ timestamp: BASE_TIMESTAMP })); | ||
| await advanceTimers(10_000); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| clearSession(replay); | ||
| replay.stop(); | ||
| }); | ||
|
|
||
| async function openFeedbackWidget() { | ||
| getClient()!.emit('openFeedbackWidget'); | ||
| await advanceTimers(DEFAULT_FLUSH_MIN_DELAY); | ||
| await advanceTimers(DEFAULT_FLUSH_MIN_DELAY); | ||
| } | ||
|
|
||
| async function sendFeedback(source: string): Promise<Event | undefined> { | ||
| let sentEvent: Event | undefined; | ||
| const unsubscribe = getClient()!.on('beforeSendEvent', event => { | ||
| if (event.type === 'feedback') { | ||
| sentEvent = event; | ||
| } | ||
| }); | ||
|
|
||
| captureFeedback({ message: 'Something broke', source }, { includeReplay: true }); | ||
| await advanceTimers(DEFAULT_FLUSH_MIN_DELAY); | ||
| unsubscribe(); | ||
|
|
||
| return sentEvent; | ||
| } | ||
|
|
||
| async function expireSession() { | ||
| await advanceTimers(SESSION_IDLE_EXPIRE_DURATION + 1_000); | ||
| domHandler({ name: 'click', event: new Event('click') }); | ||
| await advanceTimers(DEFAULT_FLUSH_MIN_DELAY); | ||
| } | ||
|
|
||
| it('sends the buffered replay and continues in session mode when the widget is opened', async () => { | ||
| await openFeedbackWidget(); | ||
|
|
||
| expect(replay).toHaveLastSentReplay(); | ||
| expect(replay.recordingMode).toBe('session'); | ||
| }); | ||
|
|
||
| it('attaches the replay ID from when the widget was opened when the session is refreshed before submission', async () => { | ||
| await openFeedbackWidget(); | ||
| const replayIdOnOpen = replay.getSessionId(); | ||
| await expireSession(); | ||
|
|
||
| const sentEvent = await sendFeedback('widget'); | ||
|
|
||
| expect(replay.getSessionId()).not.toBe(replayIdOnOpen); | ||
| expect(sentEvent?.contexts?.feedback?.replay_id).toBe(replayIdOnOpen); | ||
| }); | ||
|
|
||
| it('does not flush the refreshed session when widget feedback is sent after a session refresh', async () => { | ||
| await openFeedbackWidget(); | ||
| await expireSession(); | ||
| const flushSpy = vi.spyOn(replay, 'flush'); | ||
|
|
||
| await sendFeedback('widget'); | ||
|
|
||
| expect(flushSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('attaches the replay ID from when the widget was opened when replay is stopped before submission', async () => { | ||
| await openFeedbackWidget(); | ||
| const replayIdOnOpen = replay.getSessionId(); | ||
| replay.stop(); | ||
|
|
||
| const sentEvent = await sendFeedback('widget'); | ||
|
|
||
| expect(sentEvent?.contexts?.feedback?.replay_id).toBe(replayIdOnOpen); | ||
| }); | ||
|
|
||
| it('attaches the current replay ID when widget feedback is sent again without reopening the widget', async () => { | ||
| await openFeedbackWidget(); | ||
| await expireSession(); | ||
| await sendFeedback('widget'); | ||
|
|
||
| const sentEvent = await sendFeedback('widget'); | ||
|
|
||
| expect(sentEvent?.contexts?.feedback?.replay_id).toBe(replay.getSessionId()); | ||
| }); | ||
|
JoshuaKGoldberg marked this conversation as resolved.
|
||
|
|
||
| it('attaches the current replay ID when the widget was opened while replay was disabled', async () => { | ||
| replay.stop(); | ||
| await openFeedbackWidget(); | ||
| replay.start(); | ||
|
|
||
| const sentEvent = await sendFeedback('widget'); | ||
|
|
||
| expect(replay.getSessionId()).toBeDefined(); | ||
| expect(sentEvent?.contexts?.feedback?.replay_id).toBe(replay.getSessionId()); | ||
| }); | ||
|
|
||
| it('attaches the current replay ID when feedback is sent via the API after the widget was opened', async () => { | ||
| await openFeedbackWidget(); | ||
| await expireSession(); | ||
|
|
||
| const sentEvent = await sendFeedback('api'); | ||
|
|
||
| expect(sentEvent?.contexts?.feedback?.replay_id).toBe(replay.getSessionId()); | ||
| }); | ||
|
|
||
| it('does not attach a replay ID when includeReplay is not set', async () => { | ||
| await openFeedbackWidget(); | ||
| const feedbackEvent = createFeedbackEvent('widget'); | ||
|
|
||
| getClient()!.emit('beforeSendFeedback', feedbackEvent); | ||
|
|
||
| expect(feedbackEvent.contexts?.feedback?.replay_id).toBeUndefined(); | ||
| }); | ||
| }); | ||
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.
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.
[Non-Actionable] Fun fact: this is one of the few times recently I've added a comment. Cursor didn't write one originally! But I figured the rest of this area/file is pretty comment-rich.