systemInfo.test.mjs
5.18 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
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
import { describe, it, beforeEach } from 'node:test'
import assert from 'node:assert/strict'
const deviceInfo = {
platform: 'ios',
brand: 'Apple',
model: 'iPhone',
system: 'iOS 16',
}
const windowInfo = {
windowWidth: 390,
windowHeight: 844,
statusBarHeight: 47,
pixelRatio: 3,
screenWidth: 390,
screenHeight: 844,
safeArea: { top: 47, bottom: 810, height: 763 },
deviceOrientation: 'portrait',
}
function setupWx(host = 'WeChat', { legacyEnvironment, hasQy, onLegacySync } = {}) {
const wxApi = {
getDeviceInfo: () => ({ ...deviceInfo }),
getWindowInfo: () => ({ ...windowInfo }),
getAppBaseInfo: () => ({
SDKVersion: '3.13.1',
host,
version: '8.0.0',
language: 'zh_CN',
fontSizeSetting: 16,
}),
}
if (onLegacySync) {
wxApi.getSystemInfoSync = onLegacySync
} else if (legacyEnvironment !== undefined) {
wxApi.getSystemInfoSync = () => ({ environment: legacyEnvironment })
}
if (hasQy) {
wxApi.qy = {}
}
globalThis.wx = wxApi
}
async function resetSystemInfoModule() {
const mod = await import('../utils/systemInfo.js')
mod.__resetLegacyEnvironmentCacheForTest?.()
}
describe('systemInfo', () => {
beforeEach(async () => {
setupWx()
await resetSystemInfoModule()
})
it('getSystemInfoSync merges device, window and app base info', async () => {
const { getSystemInfoSync } = await import('../utils/systemInfo.js')
const info = getSystemInfoSync()
assert.equal(info.platform, 'ios')
assert.equal(info.windowWidth, 390)
assert.equal(info.statusBarHeight, 47)
assert.equal(info.SDKVersion, '3.13.1')
assert.equal(info.fontSizeSetting, 16)
assert.equal(info.deviceOrientation, 'portrait')
})
it('maps wxwork host to environment wxwork', async () => {
setupWx('wxwork')
const { getSystemInfoSync } = await import('../utils/systemInfo.js')
const info = getSystemInfoSync()
assert.equal(info.environment, 'wxwork')
assert.equal(info.host, 'wxwork')
})
it('maps WeChat host to empty environment for legacy truthy checks', async () => {
setupWx('WeChat')
const { getSystemInfoSync } = await import('../utils/systemInfo.js')
const info = getSystemInfoSync()
assert.equal(info.environment, '')
})
it('maps wxwork host object env to environment wxwork', async () => {
setupWx({ appId: '', env: 'wxwork', version: 402659623 })
const { getSystemInfoSync } = await import('../utils/systemInfo.js')
const info = getSystemInfoSync()
assert.equal(info.environment, 'wxwork')
assert.equal(info.host.env, 'wxwork')
})
it('maps WeChat host object env to empty environment', async () => {
setupWx({ appId: '', env: 'WeChat', version: 402659623 })
const { getSystemInfoSync } = await import('../utils/systemInfo.js')
const info = getSystemInfoSync()
assert.equal(info.environment, '')
})
it('getLoginEnvName returns 1 in wxwork and 0 in wechat', async () => {
setupWx({ appId: '', env: 'wxwork', version: 402659623 })
await resetSystemInfoModule()
const { getLoginEnvName } = await import('../utils/systemInfo.js')
assert.equal(getLoginEnvName(), 1)
setupWx({ appId: '', env: 'WeChat', version: 402659623 })
await resetSystemInfoModule()
const { getLoginEnvName: getLoginEnvNameWechat } = await import(
'../utils/systemInfo.js'
)
assert.equal(getLoginEnvNameWechat(), 0)
})
it('prefers legacy getSystemInfoSync.environment over host.env on wxwork device', async () => {
setupWx(
{ appId: '', env: 'WeChat', version: 402659623 },
{ legacyEnvironment: 'wxwork' }
)
const { getSystemInfoSync, getLoginEnvName } = await import(
'../utils/systemInfo.js'
)
assert.equal(getSystemInfoSync().environment, 'wxwork')
assert.equal(getLoginEnvName(), 1)
})
it('detects wxwork via wx.qy when legacy environment and host are absent', async () => {
setupWx({ appId: '' }, { hasQy: true })
const { getLoginEnvName } = await import('../utils/systemInfo.js')
assert.equal(getLoginEnvName(), 1)
})
it('getLoginEnvName uses legacy wx.getSystemInfoSync directly like old accountItems', async () => {
setupWx(
{ appId: '', env: 'WeChat', version: 402659623 },
{ legacyEnvironment: 'wxwork' }
)
const { getLoginEnvName } = await import('../utils/systemInfo.js')
assert.equal(getLoginEnvName(), 1)
})
it('returns wechat env when no wxwork signals exist', async () => {
setupWx({ appId: '', env: 'WeChat', version: 402659623 })
const { getLoginEnvName } = await import('../utils/systemInfo.js')
assert.equal(getLoginEnvName(), 0)
})
it('getSystemInfo invokes success callback with merged info', async () => {
const { getSystemInfo } = await import('../utils/systemInfo.js')
let result = null
getSystemInfo({
success(res) {
result = res
},
})
assert.equal(result?.pixelRatio, 3)
assert.equal(result?.brand, 'Apple')
})
it('getLegacyEnvironment caches native getSystemInfoSync to a single call', async () => {
let syncCallCount = 0
setupWx(
{ appId: '', env: 'WeChat', version: 402659623 },
{
onLegacySync: () => {
syncCallCount += 1
return { environment: undefined }
},
}
)
await resetSystemInfoModule()
const { getSystemInfoSync } = await import('../utils/systemInfo.js')
getSystemInfoSync()
getSystemInfoSync()
assert.equal(syncCallCount, 1)
})
})