/
collaborative.jest.env.ts
297 lines (278 loc) · 11.1 KB
/
collaborative.jest.env.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import childProcess from 'child_process'
import NodeEnvironment from 'jest-environment-node'
import {isEqual} from 'lodash'
import ipc from 'node-ipc'
import {ElementHandle, KeyInput, Browser, Page, launch} from 'puppeteer'
import {PortableTextBlock} from '@sanity/types'
import {FLUSH_PATCHES_DEBOUNCE_MS} from '../../src/constants'
import {normalizeSelection} from '../../src/utils/selection'
import type {EditorSelection} from '../../src'
ipc.config.id = 'collaborative-jest-environment-ipc-client'
ipc.config.retry = 1500
ipc.config.silent = true
const WEB_SERVER_ROOT_URL = 'http://localhost:3000'
// Forward debug info from the PTE in the browsers
// const DEBUG = 'sanity-pte:*'
// eslint-disable-next-line no-process-env
const DEBUG = process.env.DEBUG || false
// Wait this long for selections and a new doc revision to appear in the clients.
const SELECTION_TIMEOUT_MS = 300
// How long to wait for a new revision to come back to the client(s) when patched through the server.
// Wait for patch debounce time and some slack for selection adjustment time for everything to be ready
const REVISION_TIMEOUT_MS = FLUSH_PATCHES_DEBOUNCE_MS + SELECTION_TIMEOUT_MS
// eslint-disable-next-line no-process-env
const launchConfig = process.env.CI
? {
headless: true,
// eslint-disable-next-line no-sync
executablePath: childProcess.execSync('which chrome', {encoding: 'utf8'}).trim(),
}
: {}
export const delay = (time: number): Promise<void> => {
return new Promise((resolve) => {
setTimeout(resolve, time)
})
}
export default class CollaborationEnvironment extends NodeEnvironment {
private _browserA?: Browser
private _browserB?: Browser
private _pageA?: Page
private _pageB?: Page
public async setup(): Promise<void> {
await super.setup()
this._browserA = await launch(launchConfig)
this._browserB = await launch(launchConfig)
this._pageA = await this._browserA.newPage()
this._pageB = await this._browserB.newPage()
// Hook up page console and npm debug in the PTE
if (DEBUG) {
await this._pageA.evaluateOnNewDocument((filter: string) => {
window.localStorage.debug = filter
}, DEBUG)
await this._pageB.evaluateOnNewDocument((filter: string) => {
window.localStorage.debug = filter
}, DEBUG)
this._pageA.on('console', (message) =>
// eslint-disable-next-line no-console
console.log(`A:${message.type().substring(0, 3).toUpperCase()} ${message.text()}`)
)
this._pageB.on('console', (message) =>
// eslint-disable-next-line no-console
console.log(`B:${message.type().substring(0, 3).toUpperCase()} ${message.text()}`)
)
}
this._pageA.on('pageerror', (err) => {
console.error('Editor A crashed', err)
throw err
})
this._pageB.on('pageerror', (err) => {
console.error('Editor B crashed', err)
throw err
})
await new Promise<void>((resolve) => {
ipc.connectToNet('socketServer', () => {
resolve()
})
})
}
public async handleTestEvent(event: {name: string}): Promise<void> {
if (event.name === 'test_start') {
await this._setupInstance()
}
}
public async teardown(): Promise<void> {
await super.teardown()
this._browserA?.close()
this._browserB?.close()
ipc.disconnect('socketServer')
}
private async _setupInstance(): Promise<void> {
const testId = (Math.random() + 1).toString(36).substring(7)
await this._pageA?.goto(`${WEB_SERVER_ROOT_URL}?editorId=A${testId}&testId=${testId}`)
await this._pageB?.goto(`${WEB_SERVER_ROOT_URL}?editorId=B${testId}&testId=${testId}`)
this.global.setDocumentValue = async (
value: PortableTextBlock[] | undefined
): Promise<void> => {
ipc.of.socketServer.emit('payload', JSON.stringify({type: 'value', value, testId}))
await delay(REVISION_TIMEOUT_MS) // Wait a little here for the payload to reach the clients
const [valueHandleA, valueHandleB] = await Promise.all([
this._pageA?.waitForSelector('#pte-value'),
this._pageB?.waitForSelector('#pte-value'),
])
if (!valueHandleA || !valueHandleB) {
throw new Error('Failed to find `#pte-value` element on page')
}
const readVal = (node: any) => {
return node.innerText ? JSON.parse(node.innerText) : undefined
}
if (valueHandleA === null || valueHandleB === null) {
throw new Error('Value handle is null')
}
const valueA: PortableTextBlock[] | undefined = await valueHandleA.evaluate(readVal)
const valueB: PortableTextBlock[] | undefined = await valueHandleB.evaluate(readVal)
return new Promise<void>((resolve, reject) => {
if (isEqual(value, valueA) && isEqual(value, valueB)) {
return resolve()
}
return reject(new Error('Could not propagate value to browsers'))
})
}
this.global.getEditors = () =>
Promise.all(
[this._pageA!, this._pageB!].map(async (page, index) => {
const userAgent = await page.evaluate(() => navigator.userAgent)
const isMac = /Mac|iPod|iPhone|iPad/.test(userAgent)
const metaKey = isMac ? 'Meta' : 'Control'
const editorId = `${['A', 'B'][index]}${testId}`
const [editableHandle, selectionHandle, valueHandle]: (ElementHandle<Element> | null)[] =
await Promise.all([
page.waitForSelector('div[contentEditable="true"]'),
page.waitForSelector('#pte-selection'),
page.waitForSelector('#pte-value'),
])
if (!editableHandle || !selectionHandle || !valueHandle) {
throw new Error('Failed to find required editor elements')
}
const waitForRevision = async () => {
const revId = (Math.random() + 1).toString(36).substring(7)
ipc.of.socketServer.emit(
'payload',
JSON.stringify({type: 'revId', revId, testId, editorId})
)
await page.waitForSelector(`code[data-rev-id="${revId}"]`, {
timeout: REVISION_TIMEOUT_MS,
})
}
const getSelection = async (): Promise<EditorSelection | null> => {
const selection = await selectionHandle.evaluate((node) =>
node instanceof HTMLElement && node.innerText ? JSON.parse(node.innerText) : null
)
return selection
}
const waitForNewSelection = async (selectionChangeFn: () => Promise<void>) => {
const oldSelection = await getSelection()
const dataVal = oldSelection ? JSON.stringify(oldSelection) : 'null'
await selectionChangeFn()
await page.waitForSelector(`code[data-selection]:not([data-selection='${dataVal}'])`, {
timeout: SELECTION_TIMEOUT_MS,
})
}
const waitForSelection = async (selection: EditorSelection) => {
const value = await valueHandle.evaluate((node): PortableTextBlock[] | undefined =>
node instanceof HTMLElement && node.innerText ? JSON.parse(node.innerText) : undefined
)
const normalized = normalizeSelection(selection, value)
const dataVal = JSON.stringify(normalized)
await page.waitForSelector(`code[data-selection='${dataVal}']`, {
timeout: SELECTION_TIMEOUT_MS,
})
}
return {
testId,
editorId,
insertText: async (text: string) => {
// await delay(generateRandomInteger(0, 100))
await Promise.all([
waitForRevision(),
waitForNewSelection(async () => {
await editableHandle.evaluate(
(node, args) => {
node.dispatchEvent(
new InputEvent('beforeinput', {
bubbles: true,
cancelable: true,
inputType: 'insertText',
data: args[0],
})
)
},
[text]
)
}),
])
},
undo: async () => {
await page.keyboard.down(metaKey)
await page.keyboard.press('z')
await page.keyboard.up(metaKey)
await waitForRevision()
},
redo: async () => {
await page.keyboard.down(metaKey)
await page.keyboard.press('y')
await page.keyboard.up(metaKey)
await waitForRevision()
},
pressKey: async (keyName: KeyInput, times?: number) => {
// await delay(generateRandomInteger(0, 100))
const pressKey = async () => {
await editableHandle.press(keyName)
}
for (let i = 0; i < (times || 1); i++) {
// Value manipulation keys
if (
keyName.length === 1 ||
keyName === 'Backspace' ||
keyName === 'Delete' ||
keyName === 'Enter'
) {
await pressKey()
await waitForRevision()
} else if (
// Selection manipulation keys
[
'ArrowUp',
'ArrowDown',
'ArrowLeft',
'ArrowRight',
'PageUp',
'PageDown',
'Home',
'End',
].includes(keyName)
) {
await waitForNewSelection(pressKey)
} else {
// Unknown keys, test needs should be covered by the above cases.
console.warn(`Key ${keyName} not accounted for`)
await pressKey()
}
}
},
toggleMark: async () => {
await page.keyboard.down(metaKey)
await page.keyboard.down('b')
await page.keyboard.up('b')
await page.keyboard.up(metaKey)
await waitForRevision()
},
focus: async () => {
await editableHandle.focus()
},
setSelection: async (selection: EditorSelection | null) => {
ipc.of.socketServer.emit(
'payload',
JSON.stringify({
type: 'selection',
selection,
testId,
editorId,
})
)
await delay(REVISION_TIMEOUT_MS) // Wait a little here for the payload to reach the client
await waitForSelection(selection)
},
async getValue(): Promise<PortableTextBlock[] | undefined> {
const value = await valueHandle.evaluate((node): PortableTextBlock[] | undefined =>
node instanceof HTMLElement && node.innerText
? JSON.parse(node.innerText)
: undefined
)
return value
},
getSelection,
}
})
)
}
}