forked from logseq/logseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.spec.ts
180 lines (164 loc) · 5.13 KB
/
random.spec.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
import { expect } from '@playwright/test'
import { test } from './fixtures'
import {
createRandomPage, randomInt, IsMac, randomString,
} from './utils'
/**
* Randomized test for single page editing. Block-wise.
*
* For now, only check total number of blocks.
*/
interface RandomTestStep {
/// target block
target: number;
/// action
op: string;
text: string;
/// expected total block number
expectedBlocks: number;
}
// TODO: add better frequency support
const availableOps = [
"insertByEnter",
"insertAtLast",
// "backspace", // FIXME: cannot backspace to delete block if has children, and prev is a parent, so skip
// "delete", // FIXME: cannot delete to delete block if next is outdented
"edit",
"moveUp",
"moveDown",
"indent",
"unindent",
"indent",
"unindent",
"indent",
"indent",
// TODO: selection
]
const generateRandomTest = (size: number): RandomTestStep[] => {
let blockCount = 1; // default block
let steps: RandomTestStep[] = []
for (let i = 0; i < size; i++) {
let op = availableOps[Math.floor(Math.random() * availableOps.length)];
// freq adjust
if (Math.random() > 0.9) {
op = "insertByEnter"
}
let loc = Math.floor(Math.random() * blockCount)
let text = randomString(randomInt(2, 3))
if (op === "insertByEnter" || op === "insertAtLast") {
blockCount++
} else if (op === "backspace") {
if (blockCount == 1) {
continue
}
blockCount--
text = null
} else if (op === "delete") {
if (blockCount == 1) {
continue
}
// cannot delete last block
if (loc === blockCount - 1) {
continue
}
blockCount--
text = null
} else if (op === "moveUp" || op === "moveDown") {
// no op
text = null
} else if (op === "indent" || op === "unindent") {
// no op
text = null
} else if (op === "edit") {
// no ap
} else {
throw new Error("unexpected op");
}
if (blockCount < 1) {
blockCount = 1
}
let step: RandomTestStep = {
target: loc,
op,
text,
expectedBlocks: blockCount,
}
steps.push(step)
}
return steps
}
// TODO: Fix test that intermittently started failing after https://github.com/logseq/logseq/pull/6945
test.skip('Random editor operations', async ({ page, block }) => {
const steps = generateRandomTest(20)
await createRandomPage(page)
await block.mustType("randomized test!")
for (let i = 0; i < steps.length; i++) {
let step = steps[i]
const { target, op, expectedBlocks, text } = step;
console.log(step)
if (op === "insertByEnter") {
await block.activeEditing(target)
let charCount = (await page.inputValue('textarea >> nth=0')).length
// FIXME: CHECK expect(await block.selectionStart()).toBe(charCount)
await page.keyboard.press('Enter', { delay: 50 })
// FIXME: CHECK await block.waitForBlocks(expectedBlocks)
// FIXME: use await block.mustType(text)
await block.mustFill(text)
} else if (op === "insertAtLast") {
await block.clickNext()
await block.mustType(text)
} else if (op === "backspace") {
await block.activeEditing(target)
const charCount = (await page.inputValue('textarea >> nth=0')).length
for (let i = 0; i < charCount + 1; i++) {
await page.keyboard.press('Backspace', { delay: 50 })
}
} else if (op === "delete") {
// move text-cursor to beginning
// then press delete
// then move text-cursor to the end
await block.activeEditing(target)
let charCount = (await page.inputValue('textarea >> nth=0')).length
for (let i = 0; i < charCount; i++) {
await page.keyboard.press('ArrowLeft', { delay: 50 })
}
expect.soft(await block.selectionStart()).toBe(0)
for (let i = 0; i < charCount + 1; i++) {
await page.keyboard.press('Delete', { delay: 50 })
}
await block.waitForBlocks(expectedBlocks)
charCount = (await page.inputValue('textarea >> nth=0')).length
for (let i = 0; i < charCount; i++) {
await page.keyboard.press('ArrowRight', { delay: 50 })
}
} else if (op === "edit") {
await block.activeEditing(target)
await block.mustFill('') // clear old text
await block.mustType(text)
} else if (op === "moveUp") {
await block.activeEditing(target)
if (IsMac) {
await page.keyboard.press('Meta+Shift+ArrowUp')
} else {
await page.keyboard.press('Alt+Shift+ArrowUp')
}
} else if (op === "moveDown") {
await block.activeEditing(target)
if (IsMac) {
await page.keyboard.press('Meta+Shift+ArrowDown')
} else {
await page.keyboard.press('Alt+Shift+ArrowDown')
}
} else if (op === "indent") {
await block.activeEditing(target)
await page.keyboard.press('Tab', { delay: 50 })
} else if (op === "unindent") {
await block.activeEditing(target)
await page.keyboard.press('Shift+Tab', { delay: 50 })
} else {
throw new Error("unexpected op");
}
// FIXME: CHECK await block.waitForBlocks(expectedBlocks)
await page.waitForTimeout(50)
}
})