Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 cloudStudioSessionId across page refresh', () => {
launchStudio()

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

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

let originalSessionId: string

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

originalSessionId = urlParams.get('cloudStudioSessionId')!

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', 'cloudStudioSessionId=')

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

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

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

cy.location().its('hash').should('not.contain', 'cloudStudioSessionId=')
Copy link

Choose a reason for hiding this comment

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

Bug: Session ID Property Mismatch

The _updateUrlParams method attempts to set the sessionId URL parameter by accessing this['sessionId']. However, the correct store property is this.cloudStudioSessionId. This mismatch causes this['sessionId'] to be undefined, preventing the cloudStudioSessionId from being persisted in the URL.

Additional Locations (1)
Fix in Cursor Fix in Web


cy.findByTestId('studio-panel').should('not.exist')
})
})
48 changes: 34 additions & 14 deletions packages/app/src/store/studio-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,25 @@ interface StudioRecorderState {
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('cloudStudioSessionId')
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are already in the context of studio, suggest renaming the url parameter 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,
cloudStudioSessionId: persistedSessionId,
newTestLineNumber: undefined,
_isStudioCreatedTest: false,
}
Expand Down Expand Up @@ -162,6 +179,12 @@ export const useStudioStore = defineStore('studioRecorder', {

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

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

setNewTestLineNumber (newTestLineNumber: number) {
Expand Down Expand Up @@ -222,6 +245,10 @@ export const useStudioStore = defineStore('studioRecorder', {
this._initialUrl = studio.url
}

if (studio.cloudStudioSessionId) {
this.cloudStudioSessionId = 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 @@ -303,6 +330,7 @@ export const useStudioStore = defineStore('studioRecorder', {
this.clearRunnableIds()
this._removeUrlParams()
this._initialUrl = undefined
this.clearCloudStudioSessionId()
},

startSave () {
Expand Down Expand Up @@ -432,20 +460,12 @@ export const useStudioStore = defineStore('studioRecorder', {
},

_getUrlParams () {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does _getUrlParams call getUrlParams? Can we just keep _getUrlParams and update it for the sessionId?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved the logic from _getUrlParams into that function so that we could also call it here when studio state is initialized to grab the value from the url params https://github.com/cypress-io/cypress/pull/32170/files#diff-89b06d274623cbdd0fb8c318c957b3c3659dacee52c0be410100b1ac83283d09R131-R133

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, I would just remove _getUrlParams then and just use getUrlParams directly.

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 }
return getUrlParams()
},

_updateUrlParams (filter: string[] = ['testId', 'suiteId', 'url', 'newTestLineNumber']) {
_updateUrlParams (filter: string[] = ['testId', 'suiteId', 'url', 'newTestLineNumber', 'cloudStudioSessionId']) {
// 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.cloudStudioSessionId) return

// if we have studio params, we need to remove them before adding them back
this._removeUrlParams(filter)
Expand All @@ -464,7 +484,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', 'cloudStudioSessionId']) {
const url = new URL(window.location.href)
const hashParams = new URLSearchParams(url.hash)

Expand All @@ -477,7 +497,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('cloudStudioSessionId')) {
hashParams.delete('studio')
}

Expand Down
Loading