Skip to content

Commit fe63715

Browse files
feat: (cy.prompt) give cy.prompt access to recording information (#32110)
* feat: (cy.prompt) give cy.prompt access to recording information * undo bad refactor * undo bad refactor * undo bad refactor * rename * fix typo * Update packages/server/test/unit/modes/record_spec.js * Update packages/server/test/unit/modes/record_spec.js * Update packages/server/test/unit/modes/record_spec.js * Update packages/server/test/unit/modes/record_spec.js * Update packages/server/test/unit/modes/record_spec.js
1 parent 20662fc commit fe63715

File tree

15 files changed

+225
-71
lines changed

15 files changed

+225
-71
lines changed

packages/data-context/src/DataActions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
CohortsActions,
1515
CodegenActions,
1616
CloudProjectActions,
17+
CurrentRecordingActions,
1718
} from './actions'
1819
import { ErrorActions } from './actions/ErrorActions'
1920
import { EventCollectorActions } from './actions/EventCollectorActions'
@@ -39,6 +40,7 @@ export class DataActions {
3940
private _codegen: CodegenActions
4041
private _notification: NotificationActions
4142
private _cloudProject: CloudProjectActions
43+
private _currentRecording: CurrentRecordingActions
4244

4345
constructor (private ctx: DataContext) {
4446
this._error = new ErrorActions(this.ctx)
@@ -59,6 +61,7 @@ export class DataActions {
5961
this._codegen = new CodegenActions(this.ctx)
6062
this._notification = new NotificationActions(this.ctx)
6163
this._cloudProject = new CloudProjectActions(this.ctx)
64+
this._currentRecording = new CurrentRecordingActions(this.ctx)
6265
}
6366

6467
get error () {
@@ -132,4 +135,8 @@ export class DataActions {
132135
get cloudProject () {
133136
return this._cloudProject
134137
}
138+
139+
get currentRecording () {
140+
return this._currentRecording
141+
}
135142
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { DataContext } from '..'
2+
3+
export class CurrentRecordingActions {
4+
constructor (private ctx: DataContext) {}
5+
6+
startRun (runId: string) {
7+
this.ctx.update((d) => {
8+
d.currentRecordingInfo.runId = runId
9+
})
10+
}
11+
12+
startInstance (instanceId: string) {
13+
this.ctx.update((d) => {
14+
d.currentRecordingInfo.instanceId = instanceId
15+
})
16+
}
17+
}

packages/data-context/src/actions/ProjectActions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export class ProjectActions {
117117
d.scaffoldedFiles = null
118118
d.app.browserStatus = 'closed'
119119
d.app.browserUserAgent = null
120+
d.currentRecordingInfo = {}
120121
})
121122

122123
// Also clear any data associated with the linked cloud project

packages/data-context/src/actions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from './BrowserActions'
77
export * from './CloudProjectActions'
88
export * from './CodegenActions'
99
export * from './CohortsActions'
10+
export * from './CurrentRecordingActions'
1011
export * from './DataEmitterActions'
1112
export * from './DevActions'
1213
export * from './ElectronActions'

packages/data-context/src/data/coreDataShape.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ interface CloudDataShape {
132132
}
133133
}
134134

135+
interface RecordingInfo {
136+
runId?: string
137+
instanceId?: string
138+
}
139+
135140
export interface CoreDataShape {
136141
cliBrowser: string | null
137142
cliTestingType: string | null
@@ -166,6 +171,7 @@ export interface CoreDataShape {
166171
didBrowserPreviouslyHaveUnexpectedExit: boolean
167172
studioLifecycleManager?: StudioLifecycleManagerShape
168173
cyPromptLifecycleManager?: CyPromptLifecycleManagerShape
174+
currentRecordingInfo: RecordingInfo
169175
}
170176

171177
/**
@@ -248,6 +254,7 @@ export function makeCoreData (modeOptions: Partial<AllModeOptions> = {}): CoreDa
248254
eventCollectorSource: null,
249255
didBrowserPreviouslyHaveUnexpectedExit: false,
250256
studioLifecycleManager: undefined,
257+
currentRecordingInfo: {},
251258
}
252259

253260
async function machineId (): Promise<string | null> {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createTestDataContext } from '../helper'
2+
import type { DataContext } from '../../../src'
3+
import { expect } from 'chai'
4+
5+
describe('CurrentRecordingActions', () => {
6+
let ctx: DataContext
7+
8+
beforeEach(() => {
9+
ctx = createTestDataContext('open')
10+
})
11+
12+
describe('startRun', () => {
13+
it('updates the current run id', () => {
14+
ctx.actions.currentRecording.startRun('12345')
15+
16+
expect(ctx.coreData.currentRecordingInfo.runId).to.equal('12345')
17+
})
18+
})
19+
20+
describe('startInstance', () => {
21+
it('updates the current instance id', () => {
22+
ctx.actions.currentRecording.startInstance('12345')
23+
24+
expect(ctx.coreData.currentRecordingInfo.instanceId).to.equal('12345')
25+
})
26+
})
27+
})

packages/server/lib/cloud/api/cy-prompt/report_cy-prompt_error.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ReportCyPromptErrorOptions {
1010
error: unknown
1111
cyPromptMethod: string
1212
cyPromptMethodArgs?: unknown[]
13+
additionalHeaders?: Record<string, string>
1314
}
1415

1516
interface CyPromptError {
@@ -33,6 +34,7 @@ export function reportCyPromptError ({
3334
error,
3435
cyPromptMethod,
3536
cyPromptMethodArgs,
37+
additionalHeaders,
3638
}: ReportCyPromptErrorOptions): void {
3739
debug('Error reported:', error)
3840

@@ -87,7 +89,7 @@ export function reportCyPromptError ({
8789
{
8890
headers: {
8991
'Content-Type': 'application/json',
90-
...cloudApi.cloudHeaders,
92+
...additionalHeaders,
9193
},
9294
},
9395
).catch((e: unknown) => {

packages/server/lib/cloud/cy-prompt/CyPromptLifecycleManager.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { readFile } from 'fs-extra'
1212
import { ensureCyPromptBundle } from './ensure_cy_prompt_bundle'
1313
import chokidar from 'chokidar'
1414
import { getCloudMetadata } from '../get_cloud_metadata'
15-
import type { CyPromptAuthenticatedUserShape } from '@packages/types'
15+
import type { CyPromptAuthenticatedUserShape, CyPromptServerOptions } from '@packages/types'
1616
import crypto from 'crypto'
1717
import { reportCyPromptError } from '../api/cy-prompt/report_cy-prompt_error'
1818

@@ -52,13 +52,23 @@ export class CyPromptLifecycleManager {
5252
data.cyPromptLifecycleManager = this
5353
})
5454

55+
const recordingInfo = {
56+
get runId () {
57+
return ctx.coreData.currentRecordingInfo.runId
58+
},
59+
get instanceId () {
60+
return ctx.coreData.currentRecordingInfo.instanceId
61+
},
62+
}
63+
5564
const getProjectOptions = async () => {
5665
return {
5766
user: await ctx.actions.auth.authApi.getUser(),
5867
projectSlug: (await ctx.project.getConfig()).projectId || undefined,
5968
record,
6069
key,
6170
isOpenMode: ctx.isOpenMode,
71+
...(record ? { recordingInfo } : {}),
6272
}
6373
}
6474

@@ -75,12 +85,12 @@ export class CyPromptLifecycleManager {
7585
reportCyPromptError({
7686
cloudApi: {
7787
cloudUrl,
78-
cloudHeaders,
7988
CloudRequest,
8089
createCloudRequest,
8190
isRetryableError,
8291
asyncRetry,
8392
},
93+
additionalHeaders: cloudHeaders,
8494
cyPromptHash: this.cyPromptHash,
8595
projectSlug: (await ctx.project.getConfig()).projectId || undefined,
8696
error,
@@ -118,12 +128,7 @@ export class CyPromptLifecycleManager {
118128
}: {
119129
projectId?: string
120130
cloudDataSource: CloudDataSource
121-
getProjectOptions: () => Promise<{
122-
user?: CyPromptAuthenticatedUserShape
123-
projectSlug?: string
124-
record?: boolean
125-
key?: string
126-
}>
131+
getProjectOptions: CyPromptServerOptions['getProjectOptions']
127132
}): Promise<{ cyPromptManager?: CyPromptManager, error?: Error }> {
128133
let cyPromptPath: string
129134
let manifest: Record<string, string>

packages/server/lib/cloud/cy-prompt/CyPromptManager.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CyPromptManagerShape, CyPromptStatus, CyPromptServerDefaultShape, CyPromptServerShape, CyPromptCloudApi, CyPromptCDPClient, CyPromptAuthenticatedUserShape, CyPromptAddSocketListenerOptions } from '@packages/types'
1+
import type { CyPromptManagerShape, CyPromptStatus, CyPromptServerDefaultShape, CyPromptServerShape, CyPromptCloudApi, CyPromptCDPClient, CyPromptAddSocketListenerOptions, CyPromptServerOptions } from '@packages/types'
22
import type { Router } from 'express'
33
import Debug from 'debug'
44
import { requireScript } from '../require_script'
@@ -12,12 +12,7 @@ interface SetupOptions {
1212
cyPromptHash?: string
1313
projectSlug?: string
1414
cloudApi: CyPromptCloudApi
15-
getProjectOptions: () => Promise<{
16-
user?: CyPromptAuthenticatedUserShape
17-
projectSlug?: string
18-
record?: boolean
19-
key?: string
20-
}>
15+
getProjectOptions: CyPromptServerOptions['getProjectOptions']
2116
manifest: Record<string, string>
2217
}
2318

packages/server/lib/modes/record.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ const createRunAndRecordSpecs = (options: any = {}) => {
571571
testingType,
572572
quiet,
573573
autoCancelAfterFailures,
574+
ctx,
574575
} = options
575576
const recordKey = options.key
576577

@@ -621,6 +622,8 @@ const createRunAndRecordSpecs = (options: any = {}) => {
621622
})
622623
}
623624

625+
ctx.actions.currentRecording.startRun(resp.runId)
626+
624627
const { runUrl, runId, machineId, groupId } = resp
625628
const protocolCaptureMeta = resp.capture || {}
626629

@@ -645,6 +648,7 @@ const createRunAndRecordSpecs = (options: any = {}) => {
645648
})
646649
.then((resp: any = {}) => {
647650
instanceId = resp.instanceId
651+
ctx.actions.currentRecording.startInstance(instanceId)
648652

649653
// pull off only what we need
650654
const result = _

0 commit comments

Comments
 (0)