Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Dec 28, 2024
1 parent c1f0637 commit 64f51c9
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 317 deletions.
310 changes: 2 additions & 308 deletions cypress/e2e/opting-out.cy.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { assertWhetherPostHogRequestsWereCalled, pollPhCaptures } from '../support/assertions'
import { assertWhetherPostHogRequestsWereCalled } from '../support/assertions'
import { start } from '../support/setup'

function assertThatRecordingStarted() {
cy.phCaptures({ full: true }).then((captures) => {
expect(captures.map((c) => c.event)).to.deep.equal(['$snapshot'])

expect(captures[0]['properties']['$snapshot_data']).to.have.length.above(2)
// a meta and then a full snapshot
expect(captures[0]['properties']['$snapshot_data'][0].type).to.equal(4) // meta
expect(captures[0]['properties']['$snapshot_data'][1].type).to.equal(2) // full_snapshot
})
}

describe('opting out', () => {
describe('session recording', () => {
describe('when starting disabled in some way', () => {
beforeEach(() => {
cy.intercept('POST', '/decide/*', {
editorParams: {},
Expand Down Expand Up @@ -65,301 +54,6 @@ describe('opting out', () => {
})
})

it('does not capture recordings when config disables session recording', () => {
cy.posthogInit({ disable_session_recording: true })

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': false,
'@decide': true,
'@session-recording': false,
})

cy.get('[data-cy-input]')
.type('hello posthog!')
.then(() => {
cy.phCaptures().then((captures) => {
expect(captures || []).to.deep.equal(['$pageview'])
})
})
})

it('can start recording after starting opted out', () => {
cy.posthogInit({ opt_out_capturing_by_default: true })

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': false,
'@decide': true,
'@session-recording': false,
})

cy.posthog().invoke('opt_in_capturing')
// TODO: should we require this call?
cy.posthog().invoke('startSessionRecording')

cy.phCaptures({ full: true }).then((captures) => {
expect((captures || []).map((c) => c.event)).to.deep.equal(['$opt_in', '$pageview'])
})

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': true,
'@decide': true,
// no call to session-recording yet
})

cy.resetPhCaptures()

cy.get('[data-cy-input]').type('hello posthog!')

pollPhCaptures('$snapshot').then(assertThatRecordingStarted)
})

it('can start recording when starting disabled', () => {
cy.posthogInit({ disable_session_recording: true })

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': false,
'@decide': true,
'@session-recording': false,
})

cy.get('[data-cy-input]')
.type('hello posthog!')
.then(() => {
cy.phCaptures().then((captures) => {
expect(captures || []).to.deep.equal(['$pageview'])
})
})

cy.resetPhCaptures()
cy.posthog().invoke('startSessionRecording')

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': true,
'@decide': true,
// no call to session-recording yet
})

cy.get('[data-cy-input]')
.type('hello posthog!')
.then(() => {
pollPhCaptures('$snapshot').then(assertThatRecordingStarted)
})
})

it('can override sampling when starting session recording', () => {
cy.intercept('POST', '/decide/*', {
autocapture_opt_out: true,
editorParams: {},
isAuthenticated: false,
sessionRecording: {
endpoint: '/ses/',
// will never record a session with rate of 0
sampleRate: '0',
},
}).as('decide')

cy.posthogInit({
opt_out_capturing_by_default: true,
})

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': false,
'@decide': true,
'@session-recording': false,
})

cy.posthog().invoke('opt_in_capturing')

cy.posthog().invoke('startSessionRecording', { sampling: true })

cy.phCaptures({ full: true }).then((captures) => {
expect((captures || []).map((c) => c.event)).to.deep.equal(['$opt_in', '$pageview'])
})

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': true,
'@decide': true,
// no call to session-recording yet
})

cy.resetPhCaptures()

cy.get('[data-cy-input]').type('hello posthog!')

pollPhCaptures('$snapshot').then(assertThatRecordingStarted)
})

it('can override linked_flags when starting session recording', () => {
cy.intercept('POST', '/decide/*', {
autocapture_opt_out: true,
editorParams: {},
isAuthenticated: false,
sessionRecording: {
endpoint: '/ses/',
// a flag that doesn't exist, can never be recorded
linkedFlag: 'i am a flag that does not exist',
},
}).as('decide')

cy.posthogInit({
opt_out_capturing_by_default: true,
})

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': false,
'@decide': true,
'@session-recording': false,
})

cy.posthog().invoke('opt_in_capturing')

cy.posthog().invoke('startSessionRecording')

cy.phCaptures({ full: true }).then((captures) => {
expect((captures || []).map((c) => c.event)).to.deep.equal(['$opt_in', '$pageview'])
})

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': true,
'@decide': true,
// no call to session-recording yet
})

cy.resetPhCaptures()

cy.get('[data-cy-input]')
.type('hello posthog!')
.then(() => {
cy.phCaptures().then((captures) => {
// no session recording events yet
expect(captures || []).to.deep.equal([])
})
})

cy.posthog().invoke('startSessionRecording', { linked_flag: true })

cy.get('[data-cy-input]').type('hello posthog!')

pollPhCaptures('$snapshot').then(assertThatRecordingStarted)
})

it('respects sampling when overriding linked_flags when starting session recording', () => {
cy.intercept('POST', '/decide/*', {
autocapture_opt_out: true,
editorParams: {},
isAuthenticated: false,
sessionRecording: {
endpoint: '/ses/',
// a flag that doesn't exist, can never be recorded
linkedFlag: 'i am a flag that does not exist',
// will never record a session with rate of 0
sampleRate: '0',
},
}).as('decide')

cy.posthogInit({
opt_out_capturing_by_default: true,
})

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': false,
'@decide': true,
'@session-recording': false,
})

cy.posthog().invoke('opt_in_capturing')

cy.posthog().invoke('startSessionRecording')

cy.phCaptures({ full: true }).then((captures) => {
expect((captures || []).map((c) => c.event)).to.deep.equal(['$opt_in', '$pageview'])
})

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': true,
'@decide': true,
// no call to session-recording yet
})

cy.resetPhCaptures()

cy.get('[data-cy-input]')
.type('hello posthog!')
.then(() => {
cy.phCaptures().then((captures) => {
// no session recording events yet
expect(captures || []).to.deep.equal([])
})
})

cy.posthog().invoke('startSessionRecording', { linked_flag: true })

cy.get('[data-cy-input]')
.type('hello posthog!')
.then(() => {
cy.phCaptures().then((captures) => {
// no session recording events yet
expect((captures || []).length).to.equal(0)
})
})
})

it('can override all ingestion controls when starting session recording', () => {
cy.intercept('POST', '/decide/*', {
autocapture_opt_out: true,
editorParams: {},
isAuthenticated: false,
sessionRecording: {
endpoint: '/ses/',
// a flag that doesn't exist, can never be recorded
linkedFlag: 'i am a flag that does not exist',
// will never record a session with rate of 0
sampleRate: '0',
},
}).as('decide')

cy.posthogInit({
opt_out_capturing_by_default: true,
})

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': false,
'@decide': true,
'@session-recording': false,
})

cy.posthog().invoke('opt_in_capturing')

cy.posthog().invoke('startSessionRecording')

cy.phCaptures({ full: true }).then((captures) => {
expect((captures || []).map((c) => c.event)).to.deep.equal(['$opt_in', '$pageview'])
})

assertWhetherPostHogRequestsWereCalled({
'@recorder-script': true,
'@decide': true,
// no call to session-recording yet
})

cy.resetPhCaptures()

cy.get('[data-cy-input]')
.type('hello posthog!')
.then(() => {
cy.phCaptures().then((captures) => {
// no session recording events yet
expect(captures || []).to.deep.equal([])
})
})

cy.posthog().invoke('startSessionRecording', true)

cy.get('[data-cy-input]').type('hello posthog!')

pollPhCaptures('$snapshot').then(assertThatRecordingStarted)
})

it('sends a $pageview event when opting in', () => {
cy.intercept('POST', '/decide/*', {
autocapture_opt_out: true,
Expand Down
Loading

0 comments on commit 64f51c9

Please sign in to comment.