dsh-cloudmusic-player 0.5.0 → 0.7.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +322 -0
- package/README.md +16 -9
- package/client.js +84 -9
- package/cordis.patch.yml +6 -0
- package/index.js +114 -11
- package/lib/backend.mjs +3 -2
- package/lib/channel.mjs +229 -0
- package/lib/elog.mjs +53 -1
- package/lib/helper.ps1 +55 -15
- package/lib/like.mjs +287 -0
- package/lib/player.mjs +199 -3
- package/lib/smtc-browser.mjs +304 -0
- package/lib/smtc-probe.ps1 +190 -0
- package/lib/web-backend.mjs +1444 -0
- package/lib/winrt-audio.mjs +185 -0
- package/lib/winrt-audio.ps1 +163 -0
- package/locale/en.json +6 -1
- package/locale/zh.json +6 -1
- package/package.json +3 -3
- package/test/channel.test.mjs +79 -0
- package/test/elog.test.mjs +155 -2
- package/test/like.test.mjs +632 -0
- package/test/smtc-browser.test.mjs +240 -0
- package/test/web-backend.test.mjs +645 -0
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
// web-backend.mjs 单元测试:ensure() 绝不新建浏览器窗口
|
|
2
|
+
import { test } from 'node:test'
|
|
3
|
+
import assert from 'node:assert/strict'
|
|
4
|
+
import { mkdtempSync, writeFileSync, rmSync } from 'node:fs'
|
|
5
|
+
import { tmpdir } from 'node:os'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
import { WebBackend } from '../lib/web-backend.mjs'
|
|
8
|
+
|
|
9
|
+
function makeBrowserJson() {
|
|
10
|
+
const dir = mkdtempSync(join(tmpdir(), 'cmp-web-'))
|
|
11
|
+
const p = join(dir, 'browser.json')
|
|
12
|
+
writeFileSync(p, JSON.stringify({ wsUrl: 'ws://127.0.0.1:9999/devtools/browser/test' }))
|
|
13
|
+
return { dir, p }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** 假 CDP:getTargets 只返回 about:blank(没有网易云页面),记录是否有人想 createTarget。 */
|
|
17
|
+
function mockCdpWithoutMusicPage(calls) {
|
|
18
|
+
return {
|
|
19
|
+
connect: async () => {},
|
|
20
|
+
isAlive: () => false,
|
|
21
|
+
close: () => {},
|
|
22
|
+
cmd: async (method) => {
|
|
23
|
+
calls.push(method)
|
|
24
|
+
if (method === 'Target.getTargets') {
|
|
25
|
+
return { targetInfos: [{ type: 'page', url: 'about:blank', targetId: 'blank' }] }
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`unexpected CDP call: ${method}`)
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
test('找不到网易云页面时返回 web-page-not-found,且不 createTarget(不弹新窗口)', async () => {
|
|
33
|
+
const { dir, p } = makeBrowserJson()
|
|
34
|
+
try {
|
|
35
|
+
const calls = []
|
|
36
|
+
const web = new WebBackend({
|
|
37
|
+
getConfig: () => ({ webBrowserJson: p }),
|
|
38
|
+
log: () => {},
|
|
39
|
+
cdpFactory: () => mockCdpWithoutMusicPage(calls),
|
|
40
|
+
})
|
|
41
|
+
const r = await web.ensure()
|
|
42
|
+
assert.equal(r.ok, false)
|
|
43
|
+
assert.equal(r.error, 'web-page-not-found')
|
|
44
|
+
assert.match(r.message || '', /music\.163\.com|未打开/)
|
|
45
|
+
assert.ok(!calls.includes('Target.createTarget'), `不应调用 Target.createTarget,实际调用:${calls.join(',')}`)
|
|
46
|
+
} finally {
|
|
47
|
+
rmSync(dir, { recursive: true, force: true })
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('已有存活 CDP 连接时 ensure 直接复用,不读配置、不重建', async () => {
|
|
52
|
+
const calls = []
|
|
53
|
+
const web = new WebBackend({
|
|
54
|
+
getConfig: () => ({ webBrowserJson: 'C:\\does\\not\\exist\\browser.json' }),
|
|
55
|
+
log: () => {},
|
|
56
|
+
cdpFactory: () => { throw new Error('不应创建 CDP') },
|
|
57
|
+
})
|
|
58
|
+
web.cdp = { isAlive: () => true }
|
|
59
|
+
web.sessionId = 'session-1'
|
|
60
|
+
web.lastError = 'stale error'
|
|
61
|
+
const r = await web.ensure()
|
|
62
|
+
assert.equal(r.ok, true)
|
|
63
|
+
assert.equal(web.lastError, '', '成功后应清空陈旧错误')
|
|
64
|
+
assert.equal(calls.length, 0)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------- 接管网页手动点播
|
|
68
|
+
|
|
69
|
+
/** 造一个已经"连上"的 WebBackend:eval 结果按脚本返回,记录所有 eval 的表达式。
|
|
70
|
+
* smtcState 默认 ok:false(不回退),需要测 SMTC 时显式传入。 */
|
|
71
|
+
function fakeWeb(evalImpl, smtcState = { ok: false }) {
|
|
72
|
+
const web = new WebBackend({ getConfig: () => ({}), log: () => {} })
|
|
73
|
+
const seen = []
|
|
74
|
+
const controlCalls = []
|
|
75
|
+
const smtc = {
|
|
76
|
+
readState: async () => smtcState,
|
|
77
|
+
control: async (action, extra) => { controlCalls.push({ action, extra }); return { ok: true, action } },
|
|
78
|
+
}
|
|
79
|
+
web.smtc = smtc
|
|
80
|
+
web.cdp = { isAlive: () => true, cmd: async () => ({}) }
|
|
81
|
+
web.sessionId = 's1'
|
|
82
|
+
web._eval = async (expr) => { seen.push(expr); return evalImpl(expr) }
|
|
83
|
+
return { web, seen, smtc, controlCalls }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
test('play 在队列为空时接管网页手动点的歌(按 songId 取 URL 并注入 audio)', async () => {
|
|
87
|
+
const { web, seen } = fakeWeb((expr) => {
|
|
88
|
+
if (expr === 'document.readyState') return 'complete'
|
|
89
|
+
// READ_STATE_EXPR:页面正在放用户手动点的那首(裸 songId 形态)
|
|
90
|
+
if (expr.includes('getPlaying')) return { status: 'Playing', songId: '33894312', title: '', artist: '', hasAudio: false, pos: 0, end: 0 }
|
|
91
|
+
if (expr.includes('a.paused')) return 'none' // 页面里没有插件自己的 audio
|
|
92
|
+
return true
|
|
93
|
+
})
|
|
94
|
+
web.queue = []
|
|
95
|
+
web.queueIndex = -1
|
|
96
|
+
web._songUrl = async (id) => ({ ok: true, url: `https://cdn.example/${id}.mp3` })
|
|
97
|
+
web._playUrl = async (url) => ({ paused: false, readyState: 4, url })
|
|
98
|
+
|
|
99
|
+
const r = await web._playManual()
|
|
100
|
+
assert.equal(r.ok, true)
|
|
101
|
+
assert.equal(r.songId, '33894312')
|
|
102
|
+
assert.match(r.url, /33894312\.mp3/)
|
|
103
|
+
assert.equal(web.manualSongId, '33894312', '应记住手动点播的 songId')
|
|
104
|
+
assert.ok(seen.some((e) => e.includes('getPlaying')), '应读 getPlaying 拿当前歌')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
test('play 遇到已经由插件播着的 audio 时只复用,不重新 load(进度不被打回 0)', async () => {
|
|
108
|
+
const { web } = fakeWeb((expr) => {
|
|
109
|
+
if (expr.includes('getPlaying')) return { status: 'Playing', songId: '33894312', hasAudio: true, pos: 30000, end: 200000 }
|
|
110
|
+
if (expr.includes('a.paused')) return 'playing'
|
|
111
|
+
return true
|
|
112
|
+
})
|
|
113
|
+
web.queue = []
|
|
114
|
+
web.queueIndex = -1
|
|
115
|
+
let loaded = 0
|
|
116
|
+
web._songUrl = async () => { loaded++; return { ok: true, url: 'https://cdn.example/x.mp3' } }
|
|
117
|
+
web._playUrl = async () => { loaded++; return {} }
|
|
118
|
+
|
|
119
|
+
const r = await web._playManual()
|
|
120
|
+
assert.equal(r.ok, true)
|
|
121
|
+
assert.equal(r.reused, true)
|
|
122
|
+
assert.equal(loaded, 0, '不应重新取 URL / load audio')
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test('pause 在页面没有 audio 时退到 window.player.pause()(手动点的歌也能停)', async () => {
|
|
126
|
+
const seen = []
|
|
127
|
+
const { web } = fakeWeb((expr) => {
|
|
128
|
+
seen.push(expr)
|
|
129
|
+
if (expr.includes('if(!a) return false')) return false // _controlAudio('pause') 没有 audio
|
|
130
|
+
return true
|
|
131
|
+
})
|
|
132
|
+
const r = await web._pauseAny()
|
|
133
|
+
assert.equal(r.ok, true)
|
|
134
|
+
assert.equal(r.via, 'page-player')
|
|
135
|
+
assert.ok(seen.some((e) => e.includes('window.player.pause')), '应调用页面播放器的 pause')
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('seek 在没有 audio 时返回可读错误,而不是静默成功', async () => {
|
|
139
|
+
const { web } = fakeWeb((expr) => {
|
|
140
|
+
if (expr.includes('if(!a) return false')) return false
|
|
141
|
+
if (expr.includes('a.currentTime')) return false
|
|
142
|
+
if (expr.includes('credentials')) return true // _isLoggedIn 的 fetch 探测
|
|
143
|
+
return true
|
|
144
|
+
})
|
|
145
|
+
web.cdp = { isAlive: () => true, cmd: async (m) => (m === 'Storage.getCookies' ? { cookies: [{ name: 'MUSIC_U', value: 'x'.repeat(20), domain: '.music.163.com' }] } : {}) }
|
|
146
|
+
web.sessionId = 's1'
|
|
147
|
+
web.ensure = async () => ({ ok: true })
|
|
148
|
+
const r = await web.action('seek', { positionMs: 30000 })
|
|
149
|
+
assert.equal(r.ok, false)
|
|
150
|
+
assert.equal(r.error, 'web-no-audio-url')
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
// ---------------------------------------------------------------- SMTC 回退(个人浏览器放歌)
|
|
154
|
+
|
|
155
|
+
test('state 在 CDP 无真实在播音频时回退到 SMTC,标题/歌手/进度来自系统媒体会话', async () => {
|
|
156
|
+
const sm = {
|
|
157
|
+
ok: true, title: '会呼吸的痛', artist: '梁静茹', album: '崇拜',
|
|
158
|
+
status: 'Paused', pos: 1.02, end: 272.97, aumid: 'msedge.exe',
|
|
159
|
+
canPlay: true, canPause: false, canNext: true, canPrev: true, canSeek: true,
|
|
160
|
+
}
|
|
161
|
+
const { web } = fakeWeb((expr) => {
|
|
162
|
+
// CDP 页面是「去客户端播放」残留态:没有 audio、playing=false
|
|
163
|
+
if (expr.includes('getPlaying')) return { status: 'Paused', songId: '186016', title: '网易云音乐', hasAudio: false, pos: 0, end: 0 }
|
|
164
|
+
return true
|
|
165
|
+
}, sm)
|
|
166
|
+
web.ensure = async () => ({ ok: true })
|
|
167
|
+
const st = await web.state()
|
|
168
|
+
assert.equal(st.ok, true)
|
|
169
|
+
assert.equal(st.now.title, '会呼吸的痛')
|
|
170
|
+
assert.equal(st.now.artist, '梁静茹')
|
|
171
|
+
assert.equal(st.now.status, 'Paused')
|
|
172
|
+
assert.equal(st.now.positionSource, 'smtc-web')
|
|
173
|
+
assert.equal(st.now.channel, 'web')
|
|
174
|
+
assert.equal(st.now.pos, 1.02)
|
|
175
|
+
assert.equal(st.now.end, 272.97)
|
|
176
|
+
assert.equal(web.dataSource, 'smtc')
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test('SMTC 返回视频站标题且无歌手时,Node 层兜底拒绝(不误接管 B站/YouTube 视频)', async () => {
|
|
180
|
+
const sm = {
|
|
181
|
+
ok: true, title: '永恒不朽的经典《We are the world》_哔哩哔哩_bilibili', artist: '',
|
|
182
|
+
status: 'Playing', pos: 0.015, end: 432, aumid: 'msedge.exe', canSeek: true,
|
|
183
|
+
}
|
|
184
|
+
const { web } = fakeWeb((expr) => {
|
|
185
|
+
if (expr.includes('getPlaying')) return { status: 'Paused', hasAudio: false }
|
|
186
|
+
return true
|
|
187
|
+
}, sm)
|
|
188
|
+
web.ensure = async () => ({ ok: true })
|
|
189
|
+
// CDP 无真实音频 + probe 意外透传视频标题 -> 应被 Node 层挡下,不返回 smtc-web
|
|
190
|
+
const st = await web.state()
|
|
191
|
+
assert.notEqual(st.now?.positionSource, 'smtc-web')
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('SMTC 数据源下的 play/pause/seek 走 smtc.control,like 仍走原逻辑', async () => {
|
|
195
|
+
const sm = { ok: true, title: '会呼吸的痛', artist: '梁静茹', status: 'Playing', pos: 10, end: 272, aumid: 'msedge.exe', canPlay: true, canPause: true, canSeek: true }
|
|
196
|
+
const { web, controlCalls } = fakeWeb((expr) => {
|
|
197
|
+
if (expr.includes('getPlaying')) return { status: 'Paused', hasAudio: false }
|
|
198
|
+
return true
|
|
199
|
+
}, sm)
|
|
200
|
+
web.ensure = async () => ({ ok: true })
|
|
201
|
+
web.state = async () => ({ ok: true, now: { positionSource: 'smtc-web', status: 'Playing', title: sm.title }, volume: { level: 0.8 } })
|
|
202
|
+
|
|
203
|
+
await web.action('play')
|
|
204
|
+
await web.action('pause')
|
|
205
|
+
await web.action('seek', { positionMs: 30000 })
|
|
206
|
+
assert.deepEqual(controlCalls.map((c) => c.action), ['play', 'pause', 'seek'])
|
|
207
|
+
assert.equal(controlCalls.find((c) => c.action === 'seek').extra.positionMs, 30000)
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
test('CDP 有真实在播 audio 时不回退 SMTC(插件自己播的歌不受个人浏览器干扰)', async () => {
|
|
211
|
+
const sm = { ok: true, title: '个人浏览器在放的歌', status: 'Playing', pos: 5, end: 100 }
|
|
212
|
+
const { web } = fakeWeb((expr) => {
|
|
213
|
+
if (expr.includes('getPlaying')) return { status: 'Playing', songId: '1', title: '插件在放的歌', hasAudio: true, pos: 20000, end: 200000, audioSongId: '1' }
|
|
214
|
+
return true
|
|
215
|
+
}, sm)
|
|
216
|
+
web.ensure = async () => ({ ok: true })
|
|
217
|
+
web.queue = [{ id: '1', name: '插件在放的歌', artists: [] }]
|
|
218
|
+
web.queueIndex = 0
|
|
219
|
+
const st = await web.state()
|
|
220
|
+
assert.notEqual(st.now.positionSource, 'smtc-web', '插件自己 audio 在播时不能被 SMTC 顶掉')
|
|
221
|
+
assert.equal(web.dataSource, 'cdp')
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
// ---------------------------------------------------------------- 喜欢(红心)
|
|
225
|
+
|
|
226
|
+
/** 内存版 LikeService:只关心 isLiked/setLiked/forget/cache 的形状。 */
|
|
227
|
+
function mockLike(initial = {}) {
|
|
228
|
+
const store = { ...initial }
|
|
229
|
+
return {
|
|
230
|
+
store,
|
|
231
|
+
cache: new Map(),
|
|
232
|
+
isLiked: async (id) => ({ ok: true, liked: !!store[String(id)], cached: false }),
|
|
233
|
+
setLiked: async (id, want) => { store[String(id)] = !!want; return { ok: true, liked: !!want } },
|
|
234
|
+
forget(id) { this.cache.delete(String(id)) },
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
test('state 的 CDP 分支把 liked 挂到 now 上(前端红心能亮)', async () => {
|
|
239
|
+
const { web } = fakeWeb((expr) => {
|
|
240
|
+
if (expr.includes('getPlaying')) return { status: 'Paused', songId: '186016', title: '晴天', artist: '周杰伦', hasAudio: false, pos: 0, end: 269000 }
|
|
241
|
+
return true
|
|
242
|
+
})
|
|
243
|
+
web.ensure = async () => ({ ok: true })
|
|
244
|
+
web.likeWeb = mockLike({ 186016: true })
|
|
245
|
+
const st = await web.state()
|
|
246
|
+
assert.equal(st.ok, true)
|
|
247
|
+
assert.equal(st.now.songId, '186016')
|
|
248
|
+
assert.equal(st.now.liked, true, '已收藏的歌红心应为实心')
|
|
249
|
+
assert.equal(st.now.positionSource, 'web')
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
test('state 的 SMTC 分支按歌名搜出 songId 并挂上 liked', async () => {
|
|
253
|
+
const sm = {
|
|
254
|
+
ok: true, title: '会呼吸的痛', artist: '梁静茹', status: 'Paused', pos: 1, end: 272,
|
|
255
|
+
aumid: 'msedge.exe', canPlay: true, canPause: false, canNext: true, canPrev: true, canSeek: true,
|
|
256
|
+
}
|
|
257
|
+
const { web } = fakeWeb((expr) => {
|
|
258
|
+
if (expr.includes('getPlaying')) return { status: 'Paused', hasAudio: false }
|
|
259
|
+
if (expr.includes('/api/search/get')) return JSON.stringify([{ id: 254132, name: '会呼吸的痛' }])
|
|
260
|
+
return true
|
|
261
|
+
}, sm)
|
|
262
|
+
web.ensure = async () => ({ ok: true })
|
|
263
|
+
web.likeWeb = mockLike({ 254132: true })
|
|
264
|
+
const st = await web.state()
|
|
265
|
+
assert.equal(st.now.positionSource, 'smtc-web')
|
|
266
|
+
assert.equal(st.now.songId, '254132', '标题搜歌应补出 songId')
|
|
267
|
+
assert.equal(st.now.liked, true, '红心状态应来自 LikeService')
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
test('_songIdBySearch 在 CDP 搜不到时走 Node 侧明文 fetch 兜底(ego 浏览器没开也能搜到)', async () => {
|
|
271
|
+
const { web } = fakeWeb(() => '[]') // CDP 页面内 fetch 返回空 → 触发兜底
|
|
272
|
+
web._cfg = () => ({ requestTimeoutMs: 3000 })
|
|
273
|
+
web._cookieForApi = async () => 'MUSIC_U=fake'
|
|
274
|
+
const origFetch = globalThis.fetch
|
|
275
|
+
let calledUrl = ''
|
|
276
|
+
globalThis.fetch = async (url) => {
|
|
277
|
+
calledUrl = String(url)
|
|
278
|
+
return { text: async () => JSON.stringify({ code: 200, result: { songs: [{ id: 254132, name: '会呼吸的痛' }] } }) }
|
|
279
|
+
}
|
|
280
|
+
try {
|
|
281
|
+
const id = await web._songIdBySearch('会呼吸的痛', '梁静茹')
|
|
282
|
+
assert.equal(id, '254132', 'CDP 空时应走 Node fetch 兜底搜到 id')
|
|
283
|
+
assert.ok(calledUrl.includes('/api/search/get'), '兜底应打明文 search 接口')
|
|
284
|
+
// 第二次命中 60min 缓存,不再 fetch
|
|
285
|
+
calledUrl = ''
|
|
286
|
+
assert.equal(await web._songIdBySearch('会呼吸的痛', '梁静茹'), '254132')
|
|
287
|
+
assert.equal(calledUrl, '', '缓存命中不应再发请求')
|
|
288
|
+
} finally {
|
|
289
|
+
globalThis.fetch = origFetch
|
|
290
|
+
}
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
test('SMTC 场景 action(like) 用搜出的 songId 收藏,并把 liked 透传给上层', async () => {
|
|
294
|
+
const { web } = fakeWeb((expr) => {
|
|
295
|
+
if (expr.includes('getPlaying')) return { status: 'Paused', hasAudio: false }
|
|
296
|
+
return true
|
|
297
|
+
})
|
|
298
|
+
web.ensure = async () => ({ ok: true })
|
|
299
|
+
const like = mockLike({})
|
|
300
|
+
web.likeWeb = like
|
|
301
|
+
web.queue = []
|
|
302
|
+
web.queueIndex = -1
|
|
303
|
+
web.manualSongId = ''
|
|
304
|
+
web.state = async () => ({ ok: true, now: { positionSource: 'smtc-web', songId: '254132', status: 'Paused', title: '会呼吸的痛' }, volume: { level: 0.8 } })
|
|
305
|
+
const r = await web.action('like', { like: true })
|
|
306
|
+
assert.equal(r.ok, true)
|
|
307
|
+
assert.equal(r.liked, true, '上层 index.js 读 res.liked 决定文案')
|
|
308
|
+
assert.equal(like.store['254132'], true, '应真的收藏这首歌')
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
test('SMTC like 走零 CDP 路径:ego 没开(ensure 抛错)也能收藏并回填红心', async () => {
|
|
312
|
+
const sm = {
|
|
313
|
+
ok: true, title: '会呼吸的痛', artist: '梁静茹', status: 'Playing', pos: 10, end: 272,
|
|
314
|
+
aumid: 'msedge.exe', canPlay: true, canPause: true, canNext: true, canPrev: true, canSeek: true,
|
|
315
|
+
}
|
|
316
|
+
const { web } = fakeWeb(() => true, sm)
|
|
317
|
+
web.ensure = async () => { throw new Error('ego browser not open') } // 零 CDP:ensure 根本不该被调用
|
|
318
|
+
web._cfg = () => ({ requestTimeoutMs: 3000 })
|
|
319
|
+
web._cookieForApi = async () => 'MUSIC_U=fake'
|
|
320
|
+
const origFetch = globalThis.fetch
|
|
321
|
+
globalThis.fetch = async () => ({ text: async () => JSON.stringify({ code: 200, result: { songs: [{ id: 254132, name: '会呼吸的痛' }] } }) })
|
|
322
|
+
const like = mockLike({})
|
|
323
|
+
web.likeWeb = like
|
|
324
|
+
web.smtcState = sm
|
|
325
|
+
try {
|
|
326
|
+
const r = await web.action('like', { like: true })
|
|
327
|
+
assert.equal(r.ok, true, 'ego 没开也要能收藏')
|
|
328
|
+
assert.equal(r.error, undefined)
|
|
329
|
+
assert.equal(like.store['254132'], true)
|
|
330
|
+
assert.equal(r.liked, true)
|
|
331
|
+
assert.equal(r.now.songId, '254132')
|
|
332
|
+
assert.equal(r.now.liked, true, '回包的红心状态应更新,前端立即变红')
|
|
333
|
+
} finally {
|
|
334
|
+
globalThis.fetch = origFetch
|
|
335
|
+
}
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
test('SMTC like 连续切换不漂移:中间 state 把 dataSource 改成 cdp、ensure 抛错也不影响下一次', async () => {
|
|
339
|
+
const sm = {
|
|
340
|
+
ok: true, title: '会呼吸的痛', artist: '梁静茹', status: 'Playing', pos: 10, end: 272,
|
|
341
|
+
aumid: 'msedge.exe', canPlay: true, canPause: true, canNext: true, canPrev: true, canSeek: true,
|
|
342
|
+
}
|
|
343
|
+
const { web, smtc } = fakeWeb((expr) => {
|
|
344
|
+
if (expr.includes('/api/search/get')) return JSON.stringify([{ id: 254132, name: '会呼吸的痛' }])
|
|
345
|
+
return '[]'
|
|
346
|
+
}, sm)
|
|
347
|
+
web.smtc.readState = async () => sm
|
|
348
|
+
web._cfg = () => ({ requestTimeoutMs: 3000 })
|
|
349
|
+
web._cookieForApi = async () => 'MUSIC_U=fake'
|
|
350
|
+
web.ensure = async () => { throw new Error('ego not open') }
|
|
351
|
+
const like = mockLike({ 254132: false })
|
|
352
|
+
web.likeWeb = like
|
|
353
|
+
// 第一次收藏
|
|
354
|
+
const r1 = await web.action('like', { like: true })
|
|
355
|
+
assert.equal(r1.ok, true)
|
|
356
|
+
assert.equal(r1.liked, true)
|
|
357
|
+
// 模拟前端一次 state 轮询把 dataSource 改写成 cdp(漂移)
|
|
358
|
+
web.dataSource = 'cdp'
|
|
359
|
+
// 第二次取消:仍应走零 CDP 路径成功
|
|
360
|
+
const r2 = await web.action('like', { like: false })
|
|
361
|
+
assert.equal(r2.ok, true, 'dataSource 漂移后仍能收藏')
|
|
362
|
+
assert.equal(r2.error, undefined, '不应冒出 web-browser-unavailable')
|
|
363
|
+
assert.equal(like.store['254132'], false)
|
|
364
|
+
assert.equal(r2.liked, false)
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
test('队列空且搜不到歌时 like 返回 like-no-song,不抛异常', async () => {
|
|
368
|
+
const { web } = fakeWeb(() => true)
|
|
369
|
+
web.ensure = async () => ({ ok: true })
|
|
370
|
+
web.likeWeb = mockLike({})
|
|
371
|
+
web.queue = []
|
|
372
|
+
web.queueIndex = -1
|
|
373
|
+
web.manualSongId = ''
|
|
374
|
+
web.state = async () => ({ ok: true, now: { positionSource: 'smtc-web', songId: '', status: 'Paused' }, volume: {} })
|
|
375
|
+
const r = await web.action('like', { like: true })
|
|
376
|
+
assert.equal(r.ok, false)
|
|
377
|
+
assert.equal(r.error, 'like-no-song')
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
// ---------------------------------------------------------------- 系统音量
|
|
381
|
+
|
|
382
|
+
test('注入 systemVolumeService 后 capabilities.systemVolume 自动打开', () => {
|
|
383
|
+
const { web } = fakeWeb(() => true)
|
|
384
|
+
assert.equal(web.capabilities.systemVolume, false)
|
|
385
|
+
if (process.platform === 'win32') {
|
|
386
|
+
web.systemVolumeService = { state: async () => ({}), volume: async () => ({ ok: true }) }
|
|
387
|
+
assert.equal(web.capabilities.systemVolume, true)
|
|
388
|
+
}
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
test('volume(scope=system) 走 native helper,且不需要 CDP', async () => {
|
|
392
|
+
const { web } = fakeWeb(() => true)
|
|
393
|
+
let got = null
|
|
394
|
+
web.systemVolumeService = {
|
|
395
|
+
state: async () => ({}),
|
|
396
|
+
volume: async (args) => { got = args; return { ok: true, volume: { level: 0.7, muted: false, scope: 'system' } } },
|
|
397
|
+
}
|
|
398
|
+
if (process.platform !== 'win32') return
|
|
399
|
+
web.ensure = async () => { throw new Error('不应连 CDP') }
|
|
400
|
+
const r = await web.volume({ level: 0.7, scope: 'system' })
|
|
401
|
+
assert.equal(r.ok, true)
|
|
402
|
+
assert.deepEqual(got, { level: 0.7, muted: undefined, scope: 'system' })
|
|
403
|
+
assert.equal(r.systemVolume.level, 0.7)
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
test('state 的 systemVolume 跟随 native helper 读数', async () => {
|
|
407
|
+
const sys = { scope: 'system', level: 0.42, muted: false }
|
|
408
|
+
const { web } = fakeWeb((expr) => {
|
|
409
|
+
if (expr.includes('getPlaying')) return { status: 'Playing', hasAudio: true, audioSongId: '', pos: 0, end: 0 }
|
|
410
|
+
return true
|
|
411
|
+
})
|
|
412
|
+
web.ensure = async () => ({ ok: true })
|
|
413
|
+
web.systemVolumeService = { state: async () => ({ ok: true, systemVolume: sys }), volume: async () => ({ ok: true }) }
|
|
414
|
+
if (process.platform !== 'win32') return
|
|
415
|
+
web.likeWeb = mockLike({})
|
|
416
|
+
const st = await web.state()
|
|
417
|
+
assert.deepEqual(st.systemVolume, sys, 'systemVolume 应来自 native helper,而不是 null')
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
// ---------------------------------------------------------------- 播放顺序
|
|
421
|
+
|
|
422
|
+
test('set_mode 把短值映射成内部枚举,state().now.mode 回显给前端切图标', async () => {
|
|
423
|
+
const { web } = fakeWeb(() => true)
|
|
424
|
+
web.ensure = async () => ({ ok: true })
|
|
425
|
+
web.state = async () => ({ ok: true, now: { mode: web.mode }, volume: {} })
|
|
426
|
+
for (const [short, en] of Object.entries({ order: 'playOrder', cycle: 'playCycle', single: 'playOneCycle', random: 'playRandom' })) {
|
|
427
|
+
const r = await web.action('set_mode', { value: short })
|
|
428
|
+
assert.equal(r.ok, true)
|
|
429
|
+
assert.equal(web.mode, en, `${short} 应映射成 ${en}`)
|
|
430
|
+
assert.equal(r.mode, en)
|
|
431
|
+
}
|
|
432
|
+
})
|
|
433
|
+
|
|
434
|
+
test('set_mode 在 state 读不到歌时仍返回带新 mode 的 now(前端图标不回退)', async () => {
|
|
435
|
+
const { web } = fakeWeb(() => true)
|
|
436
|
+
web.ensure = async () => ({ ok: true })
|
|
437
|
+
// 读不到歌:state() 返回 now:null。前端 run() 见 res.now!==undefined 会整体替换,
|
|
438
|
+
// 若这里是 null 就会把图标清回默认「顺序」——set_mode 必须兜底一个带新 mode 的 now。
|
|
439
|
+
web.state = async () => ({ ok: true, now: null, volume: {} })
|
|
440
|
+
const r = await web.action('set_mode', { value: 'random' })
|
|
441
|
+
assert.equal(r.ok, true)
|
|
442
|
+
assert.equal(r.mode, 'playRandom')
|
|
443
|
+
assert.ok(r.now, '必须返回 now,不能让前端清空')
|
|
444
|
+
assert.equal(r.now.mode, 'playRandom', 'now.mode 必须是新值')
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
test('set_mode 非法值返回 bad-mode,不改动当前 mode', async () => {
|
|
448
|
+
const { web } = fakeWeb(() => true)
|
|
449
|
+
web.mode = 'playCycle'
|
|
450
|
+
const r = await web.action('set_mode', { value: 'nonsense' })
|
|
451
|
+
assert.equal(r.ok, false)
|
|
452
|
+
assert.equal(r.error, 'bad-mode')
|
|
453
|
+
assert.equal(web.mode, 'playCycle', '非法值不应改动当前 mode')
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
test('_advanceIndex:order 到头返回 null,cycle 回环,single 原地,random 换一首', async () => {
|
|
457
|
+
const { web } = fakeWeb(() => true)
|
|
458
|
+
web.queue = [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
459
|
+
web.queueIndex = 2
|
|
460
|
+
web.mode = 'playOrder'
|
|
461
|
+
assert.equal(web._advanceIndex(1), null, '顺序模式到最后一首没有下一首')
|
|
462
|
+
assert.equal(web._advanceIndex(-1), 1)
|
|
463
|
+
web.mode = 'playCycle'
|
|
464
|
+
assert.equal(web._advanceIndex(1), 0, '列表循环应回到第一首')
|
|
465
|
+
assert.equal(web._advanceIndex(-1), 1)
|
|
466
|
+
web.mode = 'playOneCycle'
|
|
467
|
+
assert.equal(web._advanceIndex(1), 2, '单曲循环原地')
|
|
468
|
+
web.mode = 'playRandom'
|
|
469
|
+
const ri = web._advanceIndex(1)
|
|
470
|
+
assert.ok(ri >= 0 && ri < 3 && ri !== 2, '随机应换一首不同的')
|
|
471
|
+
})
|
|
472
|
+
|
|
473
|
+
test('state().now.mode 回显当前 this.mode(前端据此切播放顺序图标)', async () => {
|
|
474
|
+
const { web } = fakeWeb((expr) => {
|
|
475
|
+
if (expr.includes('getPlaying')) return { status: 'Playing', hasAudio: true, audioSongId: '', pos: 0, end: 0 }
|
|
476
|
+
return true
|
|
477
|
+
})
|
|
478
|
+
web.ensure = async () => ({ ok: true })
|
|
479
|
+
web.likeWeb = mockLike({})
|
|
480
|
+
web.systemVolumeService = null
|
|
481
|
+
web.mode = 'playRandom'
|
|
482
|
+
const st = await web.state()
|
|
483
|
+
assert.equal(st.now.mode, 'playRandom', 'CDP 分支应回显 this.mode')
|
|
484
|
+
// SMTC 分支
|
|
485
|
+
const { web: web2 } = fakeWeb(() => true, { ok: true, title: '会呼吸的痛', artist: '梁静茹', status: 'Paused', pos: 1, end: 272, aumid: 'msedge.exe', canPlay: true, canPause: true, canNext: true, canPrev: true, canSeek: true })
|
|
486
|
+
web2.ensure = async () => ({ ok: true })
|
|
487
|
+
web2._songIdBySearch = async () => '254132'
|
|
488
|
+
web2._attachLiked = async () => {}
|
|
489
|
+
web2._readSystemVolume = async () => null
|
|
490
|
+
web2.mode = 'playSingle' // 无效值仅测透传,真实模式为 playOneCycle
|
|
491
|
+
web2.mode = 'playOneCycle'
|
|
492
|
+
const st2 = await web2.state()
|
|
493
|
+
assert.equal(st2.now.mode, 'playOneCycle', 'SMTC 分支也应回显 this.mode')
|
|
494
|
+
})
|
|
495
|
+
|
|
496
|
+
// ---------------------------------------------------------------- 日推(WinRT 后台音频引擎)
|
|
497
|
+
|
|
498
|
+
/** 假 WinRT 引擎:记录命令,返回带进度的 state。默认已在播(对应 winrtPlaying=true)。 */
|
|
499
|
+
function mockWinrt() {
|
|
500
|
+
const calls = []
|
|
501
|
+
let playing = true
|
|
502
|
+
let pos = 0
|
|
503
|
+
return {
|
|
504
|
+
calls,
|
|
505
|
+
play: async (p) => { calls.push({ cmd: 'play', ...p }); playing = true; pos = 0; return { ok: true, status: 'Playing', pos: 0, end: 200, ...p } },
|
|
506
|
+
pause: async () => { calls.push({ cmd: 'pause' }); playing = false; return { ok: true, status: 'Paused', pos } },
|
|
507
|
+
resume: async () => { calls.push({ cmd: 'resume' }); playing = true; return { ok: true, status: 'Playing', pos } },
|
|
508
|
+
toggle: async () => { calls.push({ cmd: 'toggle' }); playing = !playing; return { ok: true, status: playing ? 'Playing' : 'Paused', pos } },
|
|
509
|
+
seek: async (ms) => { calls.push({ cmd: 'seek', ms }); pos = ms / 1000; return { ok: true, status: 'Playing', pos } },
|
|
510
|
+
volume: async (level, muted) => { calls.push({ cmd: 'volume', level, muted }); return { ok: true } },
|
|
511
|
+
state: async () => { calls.push({ cmd: 'state' }); return { ok: true, active: true, status: playing ? 'Playing' : 'Paused', pos, end: 200, songId: calls.find((c) => c.cmd === 'play')?.songId || '', title: 'probe', artist: 'artist', volume: 0.8, muted: false } },
|
|
512
|
+
close: async () => { calls.push({ cmd: 'close' }) },
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
test('play_daily 去 CDP 依赖:ego 关着(ensure 失败)也能取数并经 WinRT 起播', async () => {
|
|
517
|
+
const calls = []
|
|
518
|
+
const web = new WebBackend({
|
|
519
|
+
getConfig: () => ({}),
|
|
520
|
+
log: () => {},
|
|
521
|
+
winrtAudio: mockWinrt(),
|
|
522
|
+
daily: { fetch: async () => ({ ok: true, songs: [
|
|
523
|
+
{ id: 111, name: 'A', artists: ['甲'] },
|
|
524
|
+
{ id: 222, name: 'B', artists: ['乙'] },
|
|
525
|
+
] }) },
|
|
526
|
+
})
|
|
527
|
+
web.ensure = async () => { calls.push('ensure'); return { ok: false, error: 'web-browser-unavailable' } }
|
|
528
|
+
web._songUrl = async (id) => ({ ok: true, url: `https://cdn/${id}.mp3` })
|
|
529
|
+
web._attachLiked = async (now) => now
|
|
530
|
+
web._readSystemVolume = async () => null
|
|
531
|
+
|
|
532
|
+
const r = await web.action('play_daily')
|
|
533
|
+
assert.equal(r.ok, true, 'ego 关着也应起播')
|
|
534
|
+
assert.equal(r.songCount, 2)
|
|
535
|
+
assert.equal(r.firstSong.id, 111)
|
|
536
|
+
assert.ok(!calls.includes('ensure'), 'play_daily 不应调 ensure(零 CDP)')
|
|
537
|
+
assert.equal(web.winrtPlaying, true)
|
|
538
|
+
assert.equal(web.queueIndex, 0)
|
|
539
|
+
const playCall = web.winrtAudio.calls.find((c) => c.cmd === 'play')
|
|
540
|
+
assert.equal(playCall.songId, '111')
|
|
541
|
+
assert.equal(r.now.positionSource, 'winrt')
|
|
542
|
+
})
|
|
543
|
+
|
|
544
|
+
test('日推引擎在播时 state/next/seek/volume 全走 WinRT,不碰 CDP/SMTC', async () => {
|
|
545
|
+
const web = new WebBackend({ getConfig: () => ({}), log: () => {}, winrtAudio: mockWinrt() })
|
|
546
|
+
web.ensure = async () => { throw new Error('不应调 ensure(CDP/ego 关着)') }
|
|
547
|
+
web._songUrl = async (id) => ({ ok: true, url: `https://cdn/${id}.mp3` })
|
|
548
|
+
web._attachLiked = async (now) => now
|
|
549
|
+
web._readSystemVolume = async () => null
|
|
550
|
+
web.queue = [{ id: 111, name: 'A' }, { id: 222, name: 'B' }]
|
|
551
|
+
web.winrtSong = { id: 111, name: 'A', artist: '甲' }
|
|
552
|
+
web.winrtPlaying = true
|
|
553
|
+
web.winrtAudio.play({ songId: '111' }) // 引擎当前正播第一首
|
|
554
|
+
|
|
555
|
+
const st = await web.state()
|
|
556
|
+
assert.equal(st.now.positionSource, 'winrt')
|
|
557
|
+
assert.equal(st.now.songId, '111')
|
|
558
|
+
|
|
559
|
+
const nx = await web.action('next')
|
|
560
|
+
assert.equal(nx.ok, true)
|
|
561
|
+
assert.equal(web.queueIndex, 1)
|
|
562
|
+
assert.equal(web.winrtAudio.calls.filter((c) => c.cmd === 'play').at(-1).songId, '222')
|
|
563
|
+
|
|
564
|
+
await web.action('seek', { positionMs: 30000 })
|
|
565
|
+
assert.equal(web.winrtAudio.calls.find((c) => c.cmd === 'seek').ms, 30000)
|
|
566
|
+
|
|
567
|
+
await web.volume({ level: 0.5, scope: 'process' })
|
|
568
|
+
assert.equal(web.winrtAudio.calls.find((c) => c.cmd === 'volume').level, 0.5)
|
|
569
|
+
|
|
570
|
+
await web.action('pause')
|
|
571
|
+
const p = await web.state()
|
|
572
|
+
assert.equal(p.now.status, 'Paused')
|
|
573
|
+
})
|
|
574
|
+
|
|
575
|
+
test('日推引擎在播时 like 收藏队列当前曲目,不碰 SMTC/CDP', async () => {
|
|
576
|
+
const web = new WebBackend({ getConfig: () => ({}), log: () => {}, winrtAudio: mockWinrt() })
|
|
577
|
+
web.ensure = async () => { throw new Error('不应调 ensure') }
|
|
578
|
+
web._attachLiked = async (now) => now
|
|
579
|
+
web._readSystemVolume = async () => null
|
|
580
|
+
web.queue = [{ id: 111, name: 'A' }]
|
|
581
|
+
web.queueIndex = 0
|
|
582
|
+
web.winrtPlaying = true
|
|
583
|
+
web.winrtAudio.play({ songId: '111' }) // 让引擎 state() 有 active 歌曲
|
|
584
|
+
web.likeWeb = mockLike({})
|
|
585
|
+
const smtcCalls = []
|
|
586
|
+
web.smtc = { readState: async () => { smtcCalls.push('readState'); return { ok: false } } }
|
|
587
|
+
|
|
588
|
+
const r = await web.action('like', { like: true })
|
|
589
|
+
assert.equal(r.ok, true)
|
|
590
|
+
assert.equal(r.liked, true)
|
|
591
|
+
assert.equal(r.songId, '111')
|
|
592
|
+
assert.equal(smtcCalls.length, 0, '日推引擎在播时不应读 SMTC')
|
|
593
|
+
})
|
|
594
|
+
|
|
595
|
+
test('SMTC 场景调 APP 音量只动浏览器进程音量,绝不回退系统音量', async () => {
|
|
596
|
+
const { web } = fakeWeb(() => true, { ok: true, title: 'Letting Go', artist: '蔡健雅', status: 'Playing', pos: 1, end: 263, aumid: 'msedge.exe', canPlay: true, canPause: true, canNext: true, canPrev: true, canSeek: true })
|
|
597
|
+
web.ensure = async () => ({ ok: false, error: 'web-page-not-found' })
|
|
598
|
+
web._readSystemVolume = async () => ({ level: 0.7, muted: false, scope: 'system' })
|
|
599
|
+
const volCalls = []
|
|
600
|
+
web.systemVolumeService = {
|
|
601
|
+
volume: async (p) => { volCalls.push(p); return { ok: true, volume: { level: p.level, muted: p.muted, scope: 'process' } } },
|
|
602
|
+
}
|
|
603
|
+
web.capabilities.systemVolume = true
|
|
604
|
+
web.dataSource = 'smtc'
|
|
605
|
+
web.smtcState = { aumid: 'msedge.exe', title: 'Letting Go', artist: '蔡健雅' }
|
|
606
|
+
|
|
607
|
+
const r = await web.volume({ level: 0.42, scope: 'process' })
|
|
608
|
+
assert.equal(r.ok, true)
|
|
609
|
+
assert.equal(r.via, 'smtc-process')
|
|
610
|
+
assert.equal(volCalls.length, 1)
|
|
611
|
+
assert.equal(volCalls[0].scope, 'process')
|
|
612
|
+
assert.equal(volCalls[0].procName, 'msedge')
|
|
613
|
+
assert.equal(volCalls[0].noFallback, true, '必须禁止回退系统音量')
|
|
614
|
+
assert.equal(volCalls[0].muted, undefined, '拖音量时不能带 muted——否则 native 走 mute 命令不写 level,回读旧值让滑杆跳回')
|
|
615
|
+
assert.equal(r.volume.level, 0.42, '回显必须是本次写入的目标值')
|
|
616
|
+
})
|
|
617
|
+
|
|
618
|
+
test('SMTC 场景 mute 只传 muted,不影响 level 写入语义', async () => {
|
|
619
|
+
const { web } = fakeWeb(() => true)
|
|
620
|
+
web.ensure = async () => ({ ok: false })
|
|
621
|
+
web._readSystemVolume = async () => ({ level: 0.7, muted: false, scope: 'system' })
|
|
622
|
+
const volCalls = []
|
|
623
|
+
web.systemVolumeService = { volume: async (p) => { volCalls.push(p); return { ok: true } } }
|
|
624
|
+
web.capabilities.systemVolume = true
|
|
625
|
+
web.dataSource = 'smtc'
|
|
626
|
+
web.smtcState = { aumid: 'msedge.exe', title: 'x', artist: 'y' }
|
|
627
|
+
|
|
628
|
+
await web.volume({ muted: true, scope: 'process' })
|
|
629
|
+
assert.equal(volCalls[0].muted, true, 'mute 调用必须带 muted')
|
|
630
|
+
assert.equal(volCalls[0].level, web.volumeLevel, 'mute 时 level 用现值即可')
|
|
631
|
+
})
|
|
632
|
+
|
|
633
|
+
test('SMTC 场景找不到出声进程音频会话时报错,不改系统音量', async () => {
|
|
634
|
+
const { web } = fakeWeb(() => true)
|
|
635
|
+
web.ensure = async () => ({ ok: false, error: 'web-page-not-found' })
|
|
636
|
+
web._readSystemVolume = async () => ({ level: 0.7, muted: false, scope: 'system' })
|
|
637
|
+
web.systemVolumeService = { volume: async () => ({ ok: false, error: 'volume-no-session' }) }
|
|
638
|
+
web.capabilities.systemVolume = true
|
|
639
|
+
web.dataSource = 'smtc'
|
|
640
|
+
web.smtcState = { aumid: 'msedge.exe', title: 'x', artist: 'y' }
|
|
641
|
+
|
|
642
|
+
const r = await web.volume({ level: 0.42, scope: 'process' })
|
|
643
|
+
assert.equal(r.ok, false)
|
|
644
|
+
assert.equal(r.error, 'volume-no-session')
|
|
645
|
+
})
|