uiLauncher.test.mjs
4.05 KB
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
import assert from 'node:assert/strict'
import test from 'node:test'
import { openMiniProgram, releaseMiniProgram } from './uiLauncher.mjs'
test('IT-OL-UI-010: connection mode reuses an existing loopback endpoint', async () => {
const calls = []
const miniProgram = { id: 'connected' }
const automator = {
async connect(options) {
calls.push({ method: 'connect', options })
return miniProgram
},
async launch() {
throw new Error('launch should not be called')
}
}
const result = await openMiniProgram(automator, {
launchDevTools: false,
wsEndpoint: 'ws://127.0.0.1:9420'
})
assert.equal(result, miniProgram)
assert.deepEqual(calls, [
{
method: 'connect',
options: { wsEndpoint: 'ws://127.0.0.1:9420' }
}
])
})
test('IT-OL-UI-011: launch mode starts the approved project with an explicit CLI', async () => {
const calls = []
const miniProgram = { id: 'launched' }
const automator = {
async connect() {
throw new Error('connect should not be called')
},
async launch(options) {
calls.push({ method: 'launch', options })
return miniProgram
}
}
const result = await openMiniProgram(automator, {
launchDevTools: true,
cliPath: 'C:/tools/wechat/cli.bat',
projectPath: 'C:/workspace/project',
automationPort: 9420,
servicePort: 32219,
wsEndpoint: 'ws://127.0.0.1:9420'
})
assert.equal(result, miniProgram)
assert.deepEqual(calls, [
{
method: 'launch',
options: {
cliPath: 'C:/tools/wechat/cli.bat',
projectPath: 'C:/workspace/project',
port: 9420,
trustProject: true,
args: ['--port', '32219', '--lang', 'zh']
}
}
])
})
test('IT-OL-UI-015: launch false-negative falls back to the requested endpoint', async () => {
const calls = []
const miniProgram = { id: 'connected-after-launch' }
const automator = {
async launch(options) {
calls.push({ method: 'launch', options })
throw new TypeError('d.on is not a function')
},
async connect(options) {
calls.push({ method: 'connect', options })
return miniProgram
}
}
const result = await openMiniProgram(automator, {
launchDevTools: true,
cliPath: 'C:/tools/wechat/cli.bat',
projectPath: 'C:/workspace/project',
automationPort: 9420,
servicePort: 32219,
wsEndpoint: 'ws://127.0.0.1:9420'
})
assert.equal(result, miniProgram)
assert.deepEqual(calls.map(call => call.method), ['launch', 'connect'])
assert.deepEqual(calls[1].options, {
wsEndpoint: 'ws://127.0.0.1:9420'
})
})
test('IT-OL-UI-016: launch and connection failures retain both causes', async () => {
const automator = {
async launch() {
throw new Error('launch failed')
},
async connect() {
throw new Error('connect failed')
}
}
await assert.rejects(
() =>
openMiniProgram(automator, {
launchDevTools: true,
cliPath: 'C:/tools/wechat/cli.bat',
projectPath: 'C:/workspace/project',
automationPort: 9420,
servicePort: 32219,
wsEndpoint: 'ws://127.0.0.1:9420'
}),
error => {
assert.match(error.message, /ws:\/\/127\.0\.0\.1:9420/)
assert.equal(error.cause.launchError.message, 'launch failed')
assert.equal(error.cause.connectError.message, 'connect failed')
return true
}
)
})
test('IT-OL-UI-017: connection mode disconnects without closing reused DevTools', async () => {
const calls = []
const miniProgram = {
async close() {
calls.push('close')
},
disconnect() {
calls.push('disconnect')
}
}
await releaseMiniProgram(miniProgram, { launchDevTools: false })
assert.deepEqual(calls, ['disconnect'])
})
test('IT-OL-UI-018: launch mode closes the DevTools instance it started', async () => {
const calls = []
const miniProgram = {
async close() {
calls.push('close')
},
disconnect() {
calls.push('disconnect')
}
}
await releaseMiniProgram(miniProgram, { launchDevTools: true })
assert.deepEqual(calls, ['close'])
})