router-integration.test.mjs 4.84 KB
import { beforeEach, describe, it, mock } from 'node:test'
import assert from 'node:assert/strict'
import { resolveRoute } from '../../router/routeAliases.js'
import { loadSourceExports } from './support/load-mini-program-source.mjs'

const NEW_ROUTES = [
	'/packageEquityAI/pages/aiCheck/start/start',
	'/packageEquityAI/pages/aiCheck/chat/chat',
	'/packageEquityAI/pages/aiCheck/exercise/list/list',
	'/packageDynamicForm/pages/staffDaily/staffDaily',
	'/packageDynamicForm/pages/upgradeStaffDaily/upgradeStaffDaily',
	'/packageDynamicForm/pages/supervisorDaily/supervisorDaily'
].map(url => ({
	url,
	name: url.split('/').pop(),
	parent: 'packageEquityAI',
	isTab: false
}))

let Router
let wxBridge
let currentApp

beforeEach(async () => {
	globalThis.__wxConfig = {}
	wxBridge = { onAppRoute() {} }
	currentApp = { globalData: {} }
	const exports = await loadSourceExports('router/router.js', {
		resolveRoute,
		util: {
			showToast() {},
			showLoading() {},
			hideLoading() {},
			isFullSucreen: () => false,
			getUserInfo: () => null
		},
		wx: wxBridge,
		getCurrentPages: () => [],
		getApp: () => currentApp,
		console
	})
	Router = exports.__defaultExport
})

function createWxNavigation() {
	const emitted = []
	const navigateTo = mock.fn(options => {
		options.success?.({
			eventChannel: {
				emit(name, payload) {
					emitted.push({ name, payload })
				}
			}
		})
	})
	return {
		emitted,
		wx: Object.assign(wxBridge, {
			onAppRoute() {},
			navigateTo,
			redirectTo: mock.fn(),
			reLaunch: mock.fn(),
			switchTab: mock.fn()
		})
	}
}

describe('Router subpackage compatibility integration', () => {
	it('go resolves a legacy alias, preserves query and emits EventChannel data', () => {
		const navigation = createWxNavigation()
		globalThis.wx = navigation.wx
		const router = new Router(NEW_ROUTES)
		const events = { marker: 'event-channel' }

		router.go('packageEquity.list?staffid=7&name=a%2Fb', events)

		assert.equal(
			navigation.wx.navigateTo.mock.calls[0].arguments[0].url,
			'/packageEquityAI/pages/aiCheck/exercise/list/list?staffid=7&name=a%2Fb'
		)
		assert.deepEqual(navigation.emitted, [{ name: 'getData', payload: events }])
	})

	it('go rewrites a legacy absolute path', () => {
		const navigation = createWxNavigation()
		globalThis.wx = navigation.wx
		const router = new Router(NEW_ROUTES)

		router.go('/packageEquity/pages/aiCheck/chat/chat?id=7')

		assert.equal(
			navigation.wx.navigateTo.mock.calls[0].arguments[0].url,
			'/packageEquityAI/pages/aiCheck/chat/chat?id=7'
		)
	})

	it('redirectTo and reLaunch share the same resolver', () => {
		const navigation = createWxNavigation()
		globalThis.wx = navigation.wx
		const router = new Router(NEW_ROUTES)

		router.redirectTo('packageEquity.start?stage=lead')
		router.reLaunch('/packageEquity/pages/aiCheck/chat/chat?id=8')

		assert.equal(
			navigation.wx.redirectTo.mock.calls[0].arguments[0].url,
			'/packageEquityAI/pages/aiCheck/start/start?stage=lead'
		)
		assert.equal(
			navigation.wx.reLaunch.mock.calls[0].arguments[0].url,
			'/packageEquityAI/pages/aiCheck/chat/chat?id=8'
		)
	})

	it('_to rewrites physical paths without changing global route data', () => {
		const navigation = createWxNavigation()
		const app = { globalData: {} }
		currentApp = app
		globalThis.wx = navigation.wx
		const router = new Router(NEW_ROUTES)
		const events = { source: 'inspection' }

		router._to(
			'/packageEquity/pages/aiCheck/chat/chat?stage=lead',
			events,
			'navigateTo'
		)

		assert.equal(
			navigation.wx.navigateTo.mock.calls[0].arguments[0].url,
			'/packageEquityAI/pages/aiCheck/chat/chat?stage=lead'
		)
		assert.equal(app.globalData.ROUTE_QUERY, events)
	})

	it('daily report legacy alias preserves query and EventChannel data', () => {
		const navigation = createWxNavigation()
		globalThis.wx = navigation.wx
		const router = new Router(NEW_ROUTES)
		const events = { source: 'modal-queue', reportDate: '2026-09-10' }

		router.go('tabWorkOrder.staffDaily?scene=0&merchantId=m%2F1', events)

		assert.equal(
			navigation.wx.navigateTo.mock.calls[0].arguments[0].url,
			'/packageDynamicForm/pages/staffDaily/staffDaily?scene=0&merchantId=m%2F1'
		)
		assert.deepEqual(navigation.emitted, [{ name: 'getData', payload: events }])
	})

	it('daily report legacy physical path works through redirectTo and reLaunch', () => {
		const navigation = createWxNavigation()
		globalThis.wx = navigation.wx
		const router = new Router(NEW_ROUTES)

		router.redirectTo('/tabWorkOrder/pages/upgradeStaffDaily/upgradeStaffDaily?date=1')
		router.reLaunch('/tabWorkOrder/pages/supervisorDaily/supervisorDaily?type=weekly')

		assert.equal(
			navigation.wx.redirectTo.mock.calls[0].arguments[0].url,
			'/packageDynamicForm/pages/upgradeStaffDaily/upgradeStaffDaily?date=1'
		)
		assert.equal(
			navigation.wx.reLaunch.mock.calls[0].arguments[0].url,
			'/packageDynamicForm/pages/supervisorDaily/supervisorDaily?type=weekly'
		)
	})
})