Skip to content

Commit 8336d99

Browse files
authored
fix(5809): test and debug composition divergence with hotkeys. (#5931)
* fix(5809): have commenting hotkeys within composition block, lead to divergence * chore(5809): in-code documentation of edge case which we are not handling; since we plan to move away from the entire concept of 'diverge' shortly, it should be ok. * feat(5809): add tests for all conditions triggering divergence, including hotkeys. Include skipped test, with comment, for edge case intentionally not handled. * chore: remove flaky test which is specific for CI firefox only, and based on a feature we plan to already change in a month. * chore: convert the login beforeEach to a before * feat: also make the isLsp heuristic more stringent * Revert "chore: convert the login beforeEach to a before" This reverts commit 21e6c8b.
1 parent 9812e4b commit 8336d99

File tree

4 files changed

+132
-5
lines changed

4 files changed

+132
-5
lines changed

cypress/e2e/shared/fluxQueryBuilder.test.ts

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('Script Builder', () => {
1515
const writeData: string[] = []
1616
for (let i = 0; i < 30; i++) {
1717
writeData.push(`ndbc,air_temp_degc=70_degrees station_id_${i}=${i}`)
18+
writeData.push(`ndbc2,air_temp_degc=70_degrees station_id_${i}=${i}`)
1819
}
1920

2021
const bucketName = 'defbuck'
@@ -312,7 +313,7 @@ describe('Script Builder', () => {
312313
})
313314
})
314315

315-
describe('default sync and resetting behavior:', () => {
316+
describe('sync and resetting behavior:', () => {
316317
it('sync defaults to on. Can be toggled on/off. And can diverge (be disabled).', () => {
317318
cy.log('starts as synced')
318319
cy.getByTestID('flux-sync--toggle').should('have.class', 'active')
@@ -344,7 +345,99 @@ describe('Script Builder', () => {
344345
)
345346
})
346347

347-
it('should clear the editor text, and schema browser, with a new script', () => {
348+
describe('conditions for divergence:', () => {
349+
it('diverges when typing in composition block', () => {
350+
cy.getByTestID('flux-sync--toggle').should('have.class', 'active')
351+
cy.getByTestID('flux-editor', {timeout: 30000})
352+
selectSchema()
353+
confirmSchemaComposition()
354+
355+
cy.log('does not diverge when outside block')
356+
cy.getByTestID('flux-editor').monacoType('// will not diverge')
357+
cy.getByTestID('flux-sync--toggle').should(
358+
'not.have.class',
359+
'disabled'
360+
)
361+
362+
cy.log('does diverge, within block')
363+
cy.getByTestID('flux-editor').monacoType(
364+
'{enter}{upArrow}{upArrow}{upArrow} // make diverge'
365+
)
366+
cy.log('toggle is now disabled')
367+
cy.getByTestID('flux-sync--toggle').should('have.class', 'disabled')
368+
})
369+
370+
describe('using hotkeys:', () => {
371+
const runTest = (hotKeyCombo: string) => {
372+
cy.getByTestID('flux-sync--toggle').should('have.class', 'active')
373+
cy.getByTestID('flux-editor', {timeout: 30000})
374+
selectSchema()
375+
confirmSchemaComposition()
376+
377+
cy.log('does not diverge when outside block')
378+
cy.getByTestID('flux-editor').monacoType(`foo ${hotKeyCombo}`)
379+
cy.getByTestID('flux-sync--toggle').should(
380+
'not.have.class',
381+
'disabled'
382+
)
383+
384+
cy.log('does diverge, within block')
385+
cy.getByTestID('flux-editor').monacoType(
386+
`{upArrow}{upArrow}{upArrow} ${hotKeyCombo}`
387+
)
388+
cy.log('toggle is now disabled')
389+
cy.getByTestID('flux-sync--toggle').should('have.class', 'disabled')
390+
}
391+
392+
it('diverges when commenting line', () => {
393+
runTest('{cmd}/')
394+
})
395+
396+
it('diverges when adding lines', () => {
397+
runTest('{shift+alt+downArrow}')
398+
})
399+
400+
it('diverges when removing lines', () => {
401+
runTest('{cmd+x}')
402+
})
403+
404+
it('diverges when moving lines', () => {
405+
runTest('{alt+downArrow}')
406+
})
407+
408+
/// XXX: wiedld (27 Sep 2022) -- we have no way to delineate btwn the LSP applyEdit,
409+
/// and the undo action applyEdit.
410+
/// Either we disable the undo/redo hotkeys, or accept this edge case bug.
411+
it.skip('diverges when using undo hotkeys, to undo composition block change', () => {
412+
cy.getByTestID('flux-sync--toggle').should('have.class', 'active')
413+
cy.getByTestID('flux-editor', {timeout: 30000})
414+
selectSchema()
415+
confirmSchemaComposition()
416+
417+
cy.log('make a change, via schema composition')
418+
const newMeasurement = 'ndbc2'
419+
cy.getByTestID('measurement-selector--dropdown-button').click()
420+
cy.getByTestID(`searchable-dropdown--item ${newMeasurement}`)
421+
.should('be.visible')
422+
.click()
423+
cy.getByTestID('measurement-selector--dropdown-button').should(
424+
'contain',
425+
newMeasurement
426+
)
427+
cy.getByTestID('flux-editor').contains(
428+
`|> filter(fn: (r) => r._measurement == "${newMeasurement}")`
429+
)
430+
431+
cy.log('use undo hotkey')
432+
cy.getByTestID('flux-editor').monacoType('{cmd+z}')
433+
434+
cy.log('toggle is now disabled')
435+
cy.getByTestID('flux-sync--toggle').should('have.class', 'disabled')
436+
})
437+
})
438+
})
439+
440+
it('should clear the editor text and schema browser, with a new script', () => {
348441
cy.getByTestID('flux-editor', {timeout: 30000})
349442

350443
cy.log('modify schema browser')

src/languageSupport/languages/flux/lsp/connection.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
SchemaSelection,
1414
} from 'src/dataExplorer/context/persistance'
1515
import {CompositionInitParams} from 'src/languageSupport/languages/flux/lsp/utils'
16+
import {comments} from 'src/languageSupport/languages/flux/monaco.flux.hotkeys'
1617

1718
// LSP methods
1819
import {
@@ -134,8 +135,19 @@ class LspConnectionManager {
134135
})
135136
}
136137

138+
/// XXX: wiedld (27 Sep 2022) -- This heuristic is wrong.
139+
/// Currently, the only way we detect monaco-editor apply changes is with the change object.
140+
/// The change object only includes the replacement text, the position, and a little bit of editor-specific metadata.
141+
/// The change object does NOT include anything which states whether or not the change is from an LSP-request,
142+
/// (via the LSP applyEdit command).
143+
/// As a result, we guess that any edit which is replacing the composition block is coming from an LSP request.
144+
/// However, this heuristic fails for certain user-triggered events.
145+
/// Such as a hotkey "undo" [cmd+z] of the last LSP applyEdit,
146+
/// which generates a change object that looks exactly like an older applyEdit change.
137147
_editorChangeIsFromLsp(change) {
138-
return change.text?.includes('|> yield(name: "_editor_composition")')
148+
return /^(from)(.|\n)*(\|> yield\(name: "_editor_composition"\)\n)$/.test(
149+
change.text
150+
)
139151
}
140152

141153
_editorChangeIsWithinComposition(change) {
@@ -179,6 +191,22 @@ class LspConnectionManager {
179191
})
180192
}
181193
})
194+
195+
comments(this._editor, selection => {
196+
const compositionBlock = this._getCompositionBlockLines()
197+
if (!compositionBlock) {
198+
return
199+
}
200+
const {startLine, endLine} = compositionBlock
201+
if (
202+
selection.startLineNumber >= startLine &&
203+
selection.endLineNumber <= endLine
204+
) {
205+
this._callbackSetSession({
206+
composition: {synced: false, diverged: true},
207+
})
208+
}
209+
})
182210
}
183211

184212
_compositionSyncStyle(startLine: number, endLine: number, synced: boolean) {

src/languageSupport/languages/flux/monaco.flux.hotkeys.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {EditorType} from 'src/types'
1+
import {EditorType, MonacoSelectionRange} from 'src/types'
22

33
export const toggleCommenting = (s: string, isTogglingOn: boolean) => {
44
if (isTogglingOn) {
@@ -9,7 +9,10 @@ export const toggleCommenting = (s: string, isTogglingOn: boolean) => {
99

1010
export const isCommented = (s: string) => !!s.match(/^\s*(\/\/(.*)$)/g)
1111

12-
export function comments(editor: EditorType) {
12+
export function comments(
13+
editor: EditorType,
14+
cb: (x: MonacoSelectionRange) => void = _ => {}
15+
) {
1316
editor.addAction({
1417
// An unique identifier of the contributed action.
1518
id: 'toggle-comment',
@@ -49,6 +52,8 @@ export function comments(editor: EditorType) {
4952
: positionColumn - 3,
5053
})
5154

55+
cb(selection)
56+
5257
return null
5358
},
5459
})

src/types/monaco.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export type EditorType = allMonaco.editor.IStandaloneCodeEditor
66
export type CursorEvent = allMonaco.editor.ICursorPositionChangedEvent
77
export type KeyboardEvent = allMonaco.IKeyboardEvent
88
export type MonacoRange = allMonaco.Range
9+
export type MonacoSelectionRange = allMonaco.Selection

0 commit comments

Comments
 (0)