Skip to content
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

fix(replay/v7): Fix user activity not being updated in start() #12003

Merged
merged 3 commits into from
May 14, 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/src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 @@ -294,6 +295,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();

const session = loadOrCreateSession(
{
maxReplayDuration: this._options.maxReplayDuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as SentryCore from '@sentry/core';
import type { Transport } from '@sentry/types';
import * as SentryUtils from '@sentry/utils';

// eslint-disable-next-line deprecation/deprecation
import type { Replay } from '../../src';
import type { replayIntegration } from '../../src/integration';
import type { ReplayContainer } from '../../src/replay';
import { clearSession } from '../../src/session/clearSession';
import { createPerformanceEntries } from '../../src/util/createPerformanceEntries';
Expand All @@ -24,8 +23,7 @@ type MockTransportSend = jest.MockedFunction<Transport['send']>;

describe('Integration | beforeAddRecordingEvent', () => {
let replay: ReplayContainer;
// eslint-disable-next-line deprecation/deprecation
let integration: Replay;
let integration: ReturnType<typeof replayIntegration>;
let mockTransportSend: MockTransportSend;
let mockSendReplayRequest: jest.SpyInstance<any>;
let domHandler: DomHandler;
Expand Down
53 changes: 53 additions & 0 deletions packages/replay/test/integration/start.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 { replayIntegration } 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: ReturnType<typeof replayIntegration>;

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

const mockTransport = getClient()?.getTransport()?.send as jest.MockedFunction<Transport['send']>;
mockTransport?.mockClear();
jest.runAllTimers();
await new Promise(process.nextTick);
});

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

jest.runAllTimers();
await new Promise(process.nextTick);
jest.setSystemTime(new Date(BASE_TIMESTAMP));
});

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

integration.start();

jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
await new Promise(process.nextTick);

expect(replay).toHaveLastSentReplay({
recordingPayloadHeader: { segment_id: 0 },
});
});
});
6 changes: 2 additions & 4 deletions packages/replay/test/integration/stop.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as SentryUtils from '@sentry/utils';

// eslint-disable-next-line deprecation/deprecation
import type { Replay } from '../../src';
import { WINDOW } from '../../src/constants';
import type { replayIntegration } from '../../src/integration';
import type { ReplayContainer } from '../../src/replay';
import { clearSession } from '../../src/session/clearSession';
import { addEvent } from '../../src/util/addEvent';
Expand All @@ -18,8 +17,7 @@ type MockRunFlush = jest.MockedFunction<ReplayContainer['_runFlush']>;

describe('Integration | stop', () => {
let replay: ReplayContainer;
// eslint-disable-next-line deprecation/deprecation
let integration: Replay;
let integration: ReturnType<typeof replayIntegration>;
const prevLocation = WINDOW.location;

const { record: mockRecord } = mockRrweb();
Expand Down
Loading