Skip to content

internal: (studio) persist telemetry session ID in URL #32170

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 17 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
40 changes: 40 additions & 0 deletions packages/app/cypress/e2e/studio/studio-cloud.cy.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest updating the url tests in studio.cy.ts to also test for the sessionId.

Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,44 @@ describe('studio functionality', () => {
})
})
})

it('persists sessionId across page refresh', () => {
launchStudio()

cy.findByTestId('studio-panel').should('be.visible')

cy.location().its('hash').should('contain', 'sessionId=')

let originalSessionId: string

cy.location('hash').then((hash) => {
const urlParams = new URLSearchParams(hash)

originalSessionId = urlParams.get('sessionId')!

expect(originalSessionId).to.be.a('string')
expect(originalSessionId).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i)
})

cy.reload()

cy.waitForSpecToFinish()

cy.findByTestId('studio-panel').should('be.visible')

cy.location().its('hash').should('contain', 'sessionId=')

cy.location('hash').then((hash) => {
const urlParams = new URLSearchParams(hash)
const persistedSessionId = urlParams.get('sessionId')

expect(persistedSessionId).to.equal(originalSessionId)
})

cy.findByTestId('studio-header-studio-button').click()

cy.location().its('hash').should('not.contain', 'sessionId=')

cy.findByTestId('studio-panel').should('not.exist')
})
})
14 changes: 7 additions & 7 deletions packages/app/cypress/e2e/studio/studio.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,21 +664,21 @@ describe('studio functionality', () => {
it('updates the url with the testId and studio parameters when entering studio with a test', () => {
launchStudio()

cy.location().its('hash').should('contain', 'testId=r3').and('contain', 'studio=')
cy.location().its('hash').should('contain', 'testId=r3').and('contain', 'studio=').and('contain', 'sessionId=')
})

it('update the url with the suiteId and studio parameters when entering studio with a suite', () => {
launchStudio({ createNewTestFromSuite: true })

cy.location().its('hash').should('contain', 'suiteId=r2').and('contain', 'studio=')
cy.location().its('hash').should('contain', 'suiteId=r2').and('contain', 'studio=').and('contain', 'sessionId=')
})

it('updates the studio url parameters and displays the single test view after creating a new test', () => {
loadProjectAndRunSpec()

// open the studio panel to create a new test in the root suite
cy.findByTestId('studio-button').click()
cy.location().its('hash').should('contain', 'suiteId=r1').and('contain', 'studio=')
cy.location().its('hash').should('contain', 'suiteId=r1').and('contain', 'studio=').and('contain', 'sessionId=')

// create a new test in the root suite
inputNewTestName()
Expand All @@ -694,7 +694,7 @@ describe('studio functionality', () => {
it('does not remove the studio url parameters when saving test changes', () => {
launchStudio()

cy.location().its('hash').should('contain', 'testId=r3').and('contain', 'studio=')
cy.location().its('hash').should('contain', 'testId=r3').and('contain', 'studio=').and('contain', 'sessionId=')
Copy link

Choose a reason for hiding this comment

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

Bug: URL Assertion Omission in Studio Mode

Multiple cy.location().its('hash') assertions in studio.cy.ts were not updated to include a check for the sessionId URL parameter. When Studio is active, the sessionId parameter should be present in the URL, but these assertions incorrectly omit this check, unlike other similar assertions updated in the same commit.

Additional Locations (2)
Fix in Cursor Fix in Web


cy.findByTestId('record-button-recording').should('be.visible')

Expand All @@ -706,7 +706,7 @@ describe('studio functionality', () => {

cy.findByTestId('studio-save-button').click()

cy.location().its('hash').should('contain', 'testId=r3').and('contain', 'studio=')
cy.location().its('hash').should('contain', 'testId=r3').and('contain', 'studio=').and('contain', 'sessionId=')
})

it('does not remove the studio url parameters if saving fails', () => {
Expand All @@ -716,7 +716,7 @@ describe('studio functionality', () => {

incrementCounter(0)

cy.location().its('hash').should('contain', 'testId=r3').and('contain', 'studio=')
cy.location().its('hash').should('contain', 'testId=r3').and('contain', 'studio=').and('contain', 'sessionId=')

// update the spec on the file system by changing the
// test name which will cause the save to fail since
Expand Down Expand Up @@ -757,7 +757,7 @@ describe('studio functionality', () => {

cy.findByTestId('studio-header-studio-button').click()

cy.location().its('hash').and('not.contain', 'testId=').and('not.contain', 'studio=')
cy.location().its('hash').and('not.contain', 'testId=').and('not.contain', 'studio=').and('not.contain', 'sessionId=')
})

it('does not prompt for a URL until studio is active', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/runner/SpecRunnerOpenMode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<StudioPanel
v-if="shouldShowStudioPanel"
data-cy="studio-panel"
:cloud-studio-session-id="studioStore.cloudStudioSessionId"
:cloud-studio-session-id="studioStore.sessionId"
:can-access-studio-a-i="studioStore.canAccessStudioAI"
:on-studio-panel-close="handleStudioPanelClose"
:event-manager="eventManager"
Expand Down
56 changes: 37 additions & 19 deletions packages/app/src/store/studio-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,30 @@ interface StudioRecorderState {
canAccessStudioAI: boolean
showUrlPrompt: boolean
cloudStudioRequested: boolean
cloudStudioSessionId?: string
sessionId?: string
_isStudioCreatedTest: boolean
newTestLineNumber?: number
}

function getUrlParams () {
const url = new URL(window.location.href)
const hashParams = new URLSearchParams(url.hash)
Copy link

Choose a reason for hiding this comment

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

Bug: Hash Parsing Error in URLSearchParams

The URLSearchParams constructor is incorrectly used with url.hash (or cy.location('hash')) in the getUrlParams function and a Cypress test. Both url.hash and cy.location('hash') include the leading '#' character, which URLSearchParams does not expect. This prevents URL hash parameters from being parsed correctly. The fix is to use .slice(1) to remove the leading '#' before passing the string to URLSearchParams.

Additional Locations (1)
Fix in Cursor Fix in Web


const testId = hashParams.get('testId')
const suiteId = hashParams.get('suiteId')
const visitUrl = hashParams.get('url')
const newTestLineNumber = hashParams.get('newTestLineNumber') ? Number(hashParams.get('newTestLineNumber')) : undefined
const cloudStudioSessionId = hashParams.get('sessionId')

return { testId, suiteId, url: visitUrl, newTestLineNumber, cloudStudioSessionId }
}

export const useStudioStore = defineStore('studioRecorder', {
state: (): StudioRecorderState => {
// try to restore cloudStudioSessionId from URL parameters
const urlParams = getUrlParams()
const persistedSessionId = urlParams.cloudStudioSessionId || undefined

return {
saveModalIsOpen: false,
instructionModalIsOpen: false,
Expand All @@ -128,7 +145,7 @@ export const useStudioStore = defineStore('studioRecorder', {
canAccessStudioAI: false,
showUrlPrompt: true,
cloudStudioRequested: false,
cloudStudioSessionId: undefined,
sessionId: persistedSessionId,
newTestLineNumber: undefined,
_isStudioCreatedTest: false,
}
Expand Down Expand Up @@ -161,7 +178,13 @@ export const useStudioStore = defineStore('studioRecorder', {
},

setCloudStudioSessionId (cloudStudioSessionId: string) {
this.cloudStudioSessionId = cloudStudioSessionId
this.sessionId = cloudStudioSessionId
this._updateUrlParams(['sessionId'])
},

clearCloudStudioSessionId () {
this.sessionId = undefined
this._removeUrlParams(['sessionId'])
},

setNewTestLineNumber (newTestLineNumber: number) {
Expand Down Expand Up @@ -213,7 +236,7 @@ export const useStudioStore = defineStore('studioRecorder', {
},

setup (config) {
const studio = this._getUrlParams()
const studio = this.getUrlParams()

if (studio.newTestLineNumber) {
this.setNewTestLineNumber(studio.newTestLineNumber)
Expand All @@ -227,6 +250,10 @@ export const useStudioStore = defineStore('studioRecorder', {
this._initialUrl = studio.url
}

if (studio.cloudStudioSessionId) {
this.sessionId = studio.cloudStudioSessionId
}
Copy link

Choose a reason for hiding this comment

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

Bug: Inconsistent URL Parameter Handling

The sessionId is redundantly initialized from URL parameters during store state creation and again in the setup method. This is inconsistent with how other URL parameters (testId, suiteId, newTestLineNumber) are handled, which are only set in the setup method. This redundancy could lead to a programmatically set sessionId being overwritten or cause timing issues.

Fix in Cursor Fix in Web


// if we have an existing test or are creating a new test, we need to start loading
// otherwise if we have a suite, we can just set the studio active
if (this.testId || studio.newTestLineNumber) {
Expand Down Expand Up @@ -308,6 +335,7 @@ export const useStudioStore = defineStore('studioRecorder', {
this.clearRunnableIds()
this._removeUrlParams()
this._initialUrl = undefined
this.clearCloudStudioSessionId()
},

startSave () {
Expand Down Expand Up @@ -436,21 +464,11 @@ export const useStudioStore = defineStore('studioRecorder', {
}
},

_getUrlParams () {
const url = new URL(window.location.href)
const hashParams = new URLSearchParams(url.hash)

const testId = hashParams.get('testId')
const suiteId = hashParams.get('suiteId')
const visitUrl = hashParams.get('url')
const newTestLineNumber = hashParams.get('newTestLineNumber') ? Number(hashParams.get('newTestLineNumber')) : undefined

return { testId, suiteId, url: visitUrl, newTestLineNumber }
},
getUrlParams,

_updateUrlParams (filter: string[] = ['testId', 'suiteId', 'url', 'newTestLineNumber']) {
_updateUrlParams (filter: string[] = ['testId', 'suiteId', 'url', 'newTestLineNumber', 'sessionId']) {
// if we don't have studio params, we don't need to update them
if (!this.testId && !this.suiteId && !this.url && !this.newTestLineNumber) return
if (!this.testId && !this.suiteId && !this.url && !this.newTestLineNumber && !this.sessionId) return

// if we have studio params, we need to remove them before adding them back
this._removeUrlParams(filter)
Expand All @@ -469,7 +487,7 @@ export const useStudioStore = defineStore('studioRecorder', {
window.history.replaceState({}, '', url.toString())
},

_removeUrlParams (filter: string[] = ['testId', 'suiteId', 'url', 'newTestLineNumber']) {
_removeUrlParams (filter: string[] = ['testId', 'suiteId', 'url', 'newTestLineNumber', 'sessionId']) {
const url = new URL(window.location.href)
const hashParams = new URLSearchParams(url.hash)

Expand All @@ -482,7 +500,7 @@ export const useStudioStore = defineStore('studioRecorder', {
})

// if there are no studio specific params left, we can also remove the studio param
if (!hashParams.has('testId') && !hashParams.has('suiteId') && !hashParams.has('url') && !hashParams.has('newTestLineNumber')) {
if (!hashParams.has('testId') && !hashParams.has('suiteId') && !hashParams.has('url') && !hashParams.has('newTestLineNumber') && !hashParams.has('sessionId')) {
hashParams.delete('studio')
}

Expand Down
Loading