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.
sz-kjsxy-wx-new/tests/benefits-access.test.mjs

53 lines
3.6 KiB

import test from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import vm from 'node:vm'
function load(path, registered = false) {
const source = fs.readFileSync(new URL('../' + path, import.meta.url), 'utf8')
const script = source.match(/<script>([\s\S]*?)<\/script>/)[1].replace(/^\s*import .*$/gm, '').replace('export default', 'result =')
const navigated = [], modals = []
const sandbox = { tabbar: {}, SupplyAuthDialog: {}, CompanySelector: {}, returnToSupply: async () => navigated.push('/packages/booksubmit/appointment'), loadSupplySession: async () => ({ registered }), uni: { navigateTo: o => navigated.push(o.url), redirectTo: o => navigated.push(o.url), showModal: o => modals.push(o), switchTab() {} }, console }
vm.createContext(sandbox); vm.runInContext(script, sandbox)
const definition = sandbox.result
const c = { ...definition.data(), base: { toast() {}, isNull: v => !v }, $u: { api: {}, vuex() {} } }
for (const [key, method] of Object.entries(definition.methods)) c[key] = method.bind(c)
return { c, definition, navigated, modals }
}
test('four benefits entries are open to visitors while alumni still prompts for login', () => {
const { c, navigated } = load('pages/book/index.vue')
for (const type of ['supply-demand','booking','library','hr']) c.handleButtonClick(type)
assert.deepEqual(navigated, ['/packages/supply/index','/packages/booksubmit/appointment','/packages/library/index','/packages/hr/index'])
assert.equal(c.showRegister, false)
c.handleButtonClick('alumni'); assert.equal(c.showRegister, true); assert.equal(navigated.length, 4)
})
test('benefits landing never requests protected user information for a visitor', async () => {
const { c } = load('pages/book/index.vue'); let calls = 0
c.$u.api.user = async () => { calls++; throw Error('must not request') }
await c.getUser(); assert.equal(calls, 0); assert.equal(c.hasMobile, false)
})
test('venue browsing skips personal information; attempting booking prompts login', async () => {
const { c, navigated, modals } = load('packages/booksubmit/appointment.vue'); let calls = 0
c.$u.api.user = async () => { calls++; throw Error('must not request') }
await c.getUser(); assert.equal(calls, 0); assert.equal(c.can_appointment, false)
await c.toUrl(1); assert.equal(c.showRegister, true); assert.equal(modals.length, 0); assert.equal(navigated.length, 0)
})
test('registered venue user retains reservation allowance check', async () => {
const { c, navigated } = load('packages/booksubmit/appointment.vue', true)
c.$u.api.user = async () => ({ user: { appointment_total: 1, pass_appointments: 0 } })
await c.toUrl(1); assert.equal(navigated[0], '/packages/booksubmit/index?type=1')
})
test('booking context survives login-to-registration and completed callbacks', async () => {
const login = load('packages/register/login.vue')
login.definition.onLoad.call(login.c, { from_booking: '1' })
assert.equal(login.c.fromBooking, true)
const toRegister = Object.keys(login.c).find(key => typeof login.c[key] === 'function' && String(login.definition.methods[key]).includes('/packages/register/index'))
login.c[toRegister](); assert.equal(login.navigated[0], '/packages/register/index?from_booking=1')
login.c.bindingComplete = true
await login.c.handleMsgLogin(); assert.equal(login.navigated[1], '/packages/booksubmit/appointment')
const register = load('packages/register/index.vue')
register.definition.onLoad.call(register.c, { from_booking: '1' })
register.c.registrationComplete = true
await register.c.saveUser(); assert.equal(register.navigated[0], '/packages/booksubmit/appointment')
})