You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
4.5 KiB
68 lines
4.5 KiB
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
const source = fs.readFileSync(new URL('../common/supplyIdentity.js', import.meta.url), 'utf8')
|
|
const identity = await import('data:text/javascript;base64,' + Buffer.from(source).toString('base64'))
|
|
const store = new Map()
|
|
let navigated, relaunched, loginCalls
|
|
function setup(registered = false) {
|
|
store.clear(); navigated = null; relaunched = null; loginCalls = 0
|
|
globalThis.getCurrentPages = () => [{ route: 'packages/supply/detail', options: { id: '28' } }]
|
|
globalThis.uni = {
|
|
setStorageSync: (k,v) => store.set(k,v), getStorageSync: k => store.get(k), removeStorageSync: k => store.delete(k),
|
|
login: o => { loginCalls++; o.success({ code: 'wx-code' }) },
|
|
navigateTo: o => { navigated = o.url; o.success() }, reLaunch: o => { relaunched = o.url; o.success() }
|
|
}
|
|
const vm = { supplyAuthVisible: false, supplyAuthChecking: false, $store: { state: { vuex_user: {} } }, $u: { toast() {}, api: {
|
|
login: async () => ({ token: 'new-token' }), supplyDemandSession: async () => ({ registered, user: registered ? { id: 5, name: '学员', mobile: '13800000000' } : null })
|
|
} } }
|
|
vm.$u.vuex = (key,value) => { vm.$store.state[key] = value }
|
|
return vm
|
|
}
|
|
test('guests get an optional popup without initiating WeChat login', async () => {
|
|
const vm = setup(); assert.equal(await identity.requireSupplyMember(vm), false)
|
|
assert.equal(vm.supplyAuthVisible, true); assert.equal(loginCalls, 0); assert.equal(navigated, null)
|
|
})
|
|
test('registered members pass the action gate', async () => {
|
|
const vm = setup(true); vm.supplyAuthVisible = true; assert.equal(await identity.requireSupplyMember(vm), true)
|
|
assert.equal(vm.supplyAuthVisible, false); assert.equal(loginCalls, 0)
|
|
})
|
|
test('login choice bootstraps only after selection and records original detail', async () => {
|
|
const vm = setup(); await identity.beginSupplyIdentity(vm, 'login')
|
|
assert.equal(loginCalls, 1); assert.equal(navigated, '/packages/register/login?from_supply=1')
|
|
assert.equal(store.get('supply_return_after_identity'), '/packages/supply/detail?id=28')
|
|
})
|
|
test('registration choice carries supply context', async () => {
|
|
const vm = setup(); await identity.beginSupplyIdentity(vm, 'register', '/packages/supply/index')
|
|
assert.equal(navigated, '/packages/register/index?from_supply=1')
|
|
assert.equal(store.get('supply_return_after_identity'), '/packages/supply/index')
|
|
})
|
|
test('successful identity completion returns to supply and clears pending target', async () => {
|
|
const vm = setup(true); store.set('supply_return_after_identity', '/packages/supply/detail?id=28')
|
|
await identity.returnToSupply(vm); assert.equal(relaunched, '/packages/supply/detail?id=28'); assert.equal(store.has('supply_return_after_identity'), false)
|
|
})
|
|
test('failed return preserves target for retry and never repeats registration', async () => {
|
|
const vm = setup(true); store.set('supply_return_after_identity', '/packages/supply/detail?id=28')
|
|
uni.reLaunch = o => o.fail(new Error('navigation failed'))
|
|
await assert.rejects(identity.returnToSupply(vm)); assert.equal(store.get('supply_return_after_identity'), '/packages/supply/detail?id=28')
|
|
})
|
|
test('return destination cannot be an arbitrary route or external URL', () => {
|
|
setup(); for (const path of ['https://example.com','/packages/chat/chatWindow?userId=1','/packages/supply/detail?id=1&extra=evil','/packages/supply/detail?id=0']) assert.equal(identity.supplyReturnPath(path), '/packages/supply/index')
|
|
})
|
|
test('new account does not inherit previous account cached profile fields', async () => {
|
|
const vm = setup(true); vm.$store.state.vuex_user = { id: 4, company_name: '旧账号公司' }
|
|
await identity.loadSupplySession(vm); assert.equal(vm.$store.state.vuex_user.id, 5); assert.equal(vm.$store.state.vuex_user.company_name, undefined)
|
|
})
|
|
|
|
test('venue login and registration choices preserve the appointment destination', async () => {
|
|
for (const kind of ['login', 'register']) {
|
|
const vm = setup(); await identity.beginSupplyIdentity(vm, kind, '/packages/booksubmit/appointment')
|
|
assert.equal(navigated, '/packages/register/' + (kind === 'login' ? 'login' : 'index') + '?from_booking=1')
|
|
assert.equal(store.get('supply_return_after_identity'), '/packages/booksubmit/appointment')
|
|
vm.$u.api.supplyDemandSession = async () => ({ registered: true, user: { id: 5 } })
|
|
await identity.returnToSupply(vm)
|
|
assert.equal(relaunched, '/packages/booksubmit/appointment')
|
|
assert.equal(store.has('supply_return_after_identity'), false)
|
|
}
|
|
})
|