- 页面:pose-hub 选择动作,pose-coach 跟练(教练小甄示范 + 摄像头动作识别计数) - 动作:徒手深蹲、原地高抬腿、低冲击开合、站姿侧平举 - VisionKit 人体识别,按动作幅度计数、评分、纠错;没有摄像头或识别不可用时自动进入领练模式 - 教练逐帧动画:分包内置 600px 帧,COS 高清帧全部加载完后无缝切换 - 语音报数与口令(WechatSI),舒缓背景音乐(COS,教练说话时自动压低,可关闭) - 逻辑单测:npm run test:pose
57 lines
2.7 KiB
TypeScript
57 lines
2.7 KiB
TypeScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { classifyAuthError, requestCameraPermission, type PermissionApi } from '../cameraPermission.ts'
|
|
|
|
function fakeApi(setting: Record<string, boolean> | null, authorizeError?: { errno?: number; errMsg: string }) {
|
|
const calls = { authorize: 0 }
|
|
const api: PermissionApi = {
|
|
getSetting({ success, fail }) {
|
|
if (setting) success?.({ authSetting: setting })
|
|
else fail?.({ errMsg: 'getSetting:fail' })
|
|
},
|
|
authorize({ success, fail }) {
|
|
calls.authorize += 1
|
|
if (authorizeError) fail?.(authorizeError)
|
|
else success?.()
|
|
},
|
|
}
|
|
return { api, calls }
|
|
}
|
|
|
|
test('失败原因分类:后台未声明 / 未同意隐私指引 / 用户拒绝', () => {
|
|
assert.equal(classifyAuthError({ errno: 112, errMsg: 'authorize:fail api scope is not declared in the privacy agreement' }), 'undeclared')
|
|
assert.equal(classifyAuthError({ errMsg: 'authorize:fail api scope is not declared in the privacy agreement' }), 'undeclared')
|
|
assert.equal(classifyAuthError({ errno: 104, errMsg: 'authorize:fail privacy permission is not authorized' }), 'privacy_refused')
|
|
assert.equal(classifyAuthError({ errMsg: 'authorize:fail auth deny' }), 'denied')
|
|
assert.equal(classifyAuthError({ errMsg: 'authorize:fail system error' }), 'error')
|
|
assert.equal(classifyAuthError(undefined), 'error')
|
|
})
|
|
|
|
test('设置里已授权:直接可用,不再弹授权框', async () => {
|
|
const { api, calls } = fakeApi({ 'scope.camera': true })
|
|
assert.equal((await requestCameraPermission(api)).result, 'granted')
|
|
assert.equal(calls.authorize, 0)
|
|
})
|
|
|
|
test('设置里拒绝过:引导去设置页,不再弹授权框', async () => {
|
|
const { api, calls } = fakeApi({ 'scope.camera': false })
|
|
assert.equal((await requestCameraPermission(api)).result, 'denied')
|
|
assert.equal(calls.authorize, 0)
|
|
})
|
|
|
|
test('从没问过:弹授权框,按结果返回', async () => {
|
|
assert.equal((await requestCameraPermission(fakeApi({}).api)).result, 'granted')
|
|
const undeclared = await requestCameraPermission(
|
|
fakeApi({}, { errno: 112, errMsg: 'authorize:fail api scope is not declared in the privacy agreement' }).api,
|
|
)
|
|
assert.equal(undeclared.result, 'undeclared')
|
|
assert.match(undeclared.errMsg, /privacy agreement/)
|
|
assert.equal((await requestCameraPermission(fakeApi({}, { errMsg: 'authorize:fail auth deny' }).api)).result, 'denied')
|
|
})
|
|
|
|
test('读取设置失败时仍然尝试授权', async () => {
|
|
const { api, calls } = fakeApi(null)
|
|
assert.equal((await requestCameraPermission(api)).result, 'granted')
|
|
assert.equal(calls.authorize, 1)
|
|
})
|