systemInfo.test.mjs 5.18 KB
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)
	})
})