-
Notifications
You must be signed in to change notification settings - Fork 3.3k
internal: updating studio-server initialization with renderer APIs #32204
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
017d2be
feat: updating studio-server initialization with renderer APIs
tbiethman 5f80681
Updating gen types
tbiethman 58ac7a1
Adding unit tests, adjusting types
tbiethman 903f3d3
Merge branch 'develop' into tbiethman/studio-ai-visibility
tbiethman d6e3408
Removing electron dep to better follow existing patterns in server; p…
tbiethman 744b925
Deferring StudioElectron creation until Studio AI init
tbiethman 574f553
Fixing unit test
tbiethman 3b3d4e6
Merge branch 'develop' into tbiethman/studio-ai-visibility
tbiethman 91cb59c
Clean up debug logs
tbiethman 382899b
PR updates
tbiethman 633dca5
Merge branch 'develop' of https://github.com/cypress-io/cypress into …
tbiethman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// tslint:disable-next-line no-implicit-dependencies - electron dep needs to be defined | ||
import { BrowserWindow } from 'electron' | ||
import Debug from 'debug' | ||
|
||
const debug = Debug('cypress:server:studio:electron') | ||
|
||
/** | ||
* This interface exposes a selection of Electrons APIs | ||
* to the dynamic studio bundle. | ||
*/ | ||
export class StudioElectron { | ||
private browserWindow: BrowserWindow | undefined | ||
|
||
createBrowserWindow () { | ||
debug('creating new browser window') | ||
|
||
this.destroy() | ||
|
||
this.browserWindow = new BrowserWindow({ | ||
// Hide the title bar for accurate viewport sizes | ||
titleBarStyle: 'hidden', | ||
// Hide window by default - we should never show it | ||
// in production environments | ||
show: false, | ||
}) | ||
|
||
debug('created browser window') | ||
|
||
return this.browserWindow | ||
} | ||
|
||
destroy () { | ||
this.safeCloseBrowserWindow() | ||
} | ||
|
||
private safeCloseBrowserWindow () { | ||
if (!this.browserWindow) { | ||
debug('no browser window to destroy') | ||
|
||
return | ||
} | ||
|
||
if (!this.browserWindow.isDestroyed()) { | ||
try { | ||
this.browserWindow.destroy() | ||
} catch (error) { | ||
debug('error destroying browser window', error) | ||
} | ||
} | ||
|
||
debug('browser window destroyed') | ||
this.browserWindow = undefined | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/server/test/unit/cloud/studio/StudioElectron_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { sinon, proxyquire } from '../../../spec_helper' | ||
import { expect } from 'chai' | ||
|
||
describe('StudioElectron', () => { | ||
class FakeBrowserWindow { | ||
public options: any | ||
private destroyed = false | ||
|
||
constructor (options: any) { | ||
this.options = options | ||
} | ||
|
||
isDestroyed () { | ||
return this.destroyed | ||
} | ||
|
||
destroy () { | ||
this.destroyed = true | ||
} | ||
} | ||
|
||
let StudioElectron: typeof import('../../../../lib/cloud/studio/StudioElectron').StudioElectron | ||
|
||
beforeEach(() => { | ||
const mod = proxyquire('../lib/cloud/studio/StudioElectron', { | ||
electron: { | ||
BrowserWindow: FakeBrowserWindow, | ||
}, | ||
}) as typeof import('../../../../lib/cloud/studio/StudioElectron') | ||
|
||
StudioElectron = mod.StudioElectron | ||
}) | ||
|
||
afterEach(() => { | ||
sinon.restore() | ||
}) | ||
|
||
it('creates a hidden BrowserWindow with hidden title bar and returns it', () => { | ||
const studioElectron = new StudioElectron() | ||
|
||
const win = studioElectron.createBrowserWindow() | ||
|
||
expect((win as any)).to.be.instanceOf(FakeBrowserWindow) | ||
|
||
const options = (win as any).options | ||
|
||
expect(options).to.include({ | ||
show: false, | ||
titleBarStyle: 'hidden', | ||
}) | ||
|
||
// destroy should clean up | ||
studioElectron.destroy() | ||
expect((studioElectron as any).browserWindow).to.be.undefined | ||
}) | ||
|
||
it('destroys any existing window before creating a new one', () => { | ||
const studioElectron = new StudioElectron() | ||
|
||
// Seed an existing window | ||
const existing = new FakeBrowserWindow({}) | ||
const destroyStub = sinon.stub(existing, 'destroy').callThrough() | ||
|
||
;(studioElectron as any).browserWindow = existing | ||
|
||
const win = studioElectron.createBrowserWindow() | ||
|
||
expect(destroyStub).to.be.calledOnce | ||
expect((win as any)).to.be.instanceOf(FakeBrowserWindow) | ||
expect(win).to.not.equal(existing) | ||
}) | ||
|
||
it('destroy is a no-op when no window exists', () => { | ||
const studioElectron = new StudioElectron() | ||
|
||
// No window set | ||
studioElectron.destroy() | ||
|
||
expect((studioElectron as any).browserWindow).to.be.undefined | ||
}) | ||
|
||
it('destroy calls BrowserWindow.destroy when not already destroyed and clears reference', () => { | ||
const studioElectron = new StudioElectron() | ||
const existing = new FakeBrowserWindow({}) | ||
const destroySpy = sinon.spy(existing, 'destroy') | ||
|
||
sinon.stub(existing, 'isDestroyed').returns(false) | ||
|
||
;(studioElectron as any).browserWindow = existing | ||
|
||
studioElectron.destroy() | ||
|
||
expect(destroySpy).to.be.calledOnce | ||
expect((studioElectron as any).browserWindow).to.be.undefined | ||
}) | ||
|
||
it('does not call destroy when BrowserWindow is already destroyed, but still clears reference', () => { | ||
const studioElectron = new StudioElectron() | ||
const existing = new FakeBrowserWindow({}) | ||
const destroySpy = sinon.spy(existing, 'destroy') | ||
|
||
sinon.stub(existing, 'isDestroyed').returns(true) | ||
|
||
;(studioElectron as any).browserWindow = existing | ||
|
||
studioElectron.destroy() | ||
|
||
expect(destroySpy).to.not.be.called | ||
expect((studioElectron as any).browserWindow).to.be.undefined | ||
}) | ||
|
||
it('catches errors thrown during BrowserWindow.destroy and still clears reference', () => { | ||
const studioElectron = new StudioElectron() | ||
const existing = new FakeBrowserWindow({}) | ||
|
||
sinon.stub(existing, 'isDestroyed').returns(false) | ||
sinon.stub(existing, 'destroy').throws(new Error('fail to destroy')) | ||
|
||
;(studioElectron as any).browserWindow = existing | ||
|
||
expect(() => studioElectron.destroy()).to.not.throw() | ||
expect((studioElectron as any).browserWindow).to.be.undefined | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. I like how this is tied to AI initialization now.