Skip to content

fix(replay): Fix user activity not being updated in start() #12001

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 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/replay-internal/src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { clearSession } from './session/clearSession';
import { loadOrCreateSession } from './session/loadOrCreateSession';
import { saveSession } from './session/saveSession';
import { shouldRefreshSession } from './session/shouldRefreshSession';

import type {
AddEventResult,
AddUpdateCallback,
Expand Down Expand Up @@ -295,6 +296,12 @@ export class ReplayContainer implements ReplayContainerInterface {

logInfoNextTick('[Replay] Starting replay in session mode', this._options._experiments.traceInternals);

// Required as user activity is initially set in
// constructor, so if `start()` is called after
// session idle expiration, a replay will not be
// created due to an idle timeout.
this._updateUserActivity();
Copy link
Member

Choose a reason for hiding this comment

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

🤯 ahhh I see, that makes sense of course!


const session = loadOrCreateSession(
{
maxReplayDuration: this._options.maxReplayDuration,
Expand Down
52 changes: 52 additions & 0 deletions packages/replay-internal/test/integration/start.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { vi } from 'vitest';

import { getClient } from '@sentry/core';
import type { Transport } from '@sentry/types';

import { DEFAULT_FLUSH_MIN_DELAY, SESSION_IDLE_EXPIRE_DURATION } from '../../src/constants';
import type { Replay } from '../../src/integration';
import type { ReplayContainer } from '../../src/replay';
import { BASE_TIMESTAMP } from '../index';
import { resetSdkMock } from '../mocks/resetSdkMock';
import { useFakeTimers } from '../utils/use-fake-timers';

useFakeTimers();

describe('Integration | start', () => {
let replay: ReplayContainer;
let integration: Replay;

beforeEach(async () => {
({ replay, integration } = await resetSdkMock({
replayOptions: {
stickySession: false,
},
sentryOptions: {
replaysSessionSampleRate: 0.0,
},
}));

const mockTransport = getClient()?.getTransport()?.send as vi.MockedFunction<Transport['send']>;
mockTransport?.mockClear();
await vi.runAllTimersAsync();
});

afterEach(async () => {
integration.stop();

await vi.runAllTimersAsync();
vi.setSystemTime(new Date(BASE_TIMESTAMP));
});

it('sends replay when calling `start()` after [SESSION_IDLE_EXPIRE_DURATION]ms', async () => {
await vi.advanceTimersByTimeAsync(SESSION_IDLE_EXPIRE_DURATION + 1);

integration.start();

await vi.advanceTimersByTimeAsync(DEFAULT_FLUSH_MIN_DELAY);

expect(replay).toHaveLastSentReplay({
recordingPayloadHeader: { segment_id: 0 },
});
});
});
Loading