fix: some
All checks were successful
构建并部署前端到生产环境 / build-and-deploy (push) Successful in 2m16s

This commit is contained in:
luo
2026-10-14 13:40:55 +08:00
parent 131823ca12
commit f7cb28febf
6 changed files with 262 additions and 20 deletions

View File

@@ -29,6 +29,8 @@ function runtime(initial = {}) {
const timers = [];
const sessionStorage = new Map();
const api = {};
let inWechat = false;
let sdkInit = async () => {};
let options = {};
const uni = {
getStorageSync: key => storage.get(key) ?? '',
@@ -44,13 +46,13 @@ function runtime(initial = {}) {
navigateBack: () => calls.push({ kind: 'navigateBack' })
};
const context = vm.createContext({
uni, Intl, Date, URLSearchParams,
uni, Intl, Date, URLSearchParams, URL,
console: { log() {}, error() {}, warn() {} },
setTimeout: callback => { timers.push(callback); return timers.length; },
setInterval: () => 1, clearInterval() {},
getCurrentPages: () => [{ options }],
sessionStorage: { getItem: key => sessionStorage.get(key) || '', removeItem: key => sessionStorage.delete(key), setItem: (key, value) => sessionStorage.set(key, value) },
window: { location: { search: '', pathname: '/', hash: '' }, history: { replaceState() {} } },
window: { location: { search: '', pathname: '/', hash: '', origin: 'https://example.invalid' }, history: { replaceState() {} } },
defineProps: () => ({}), defineEmits: () => () => {}
});
const cache = new Map();
@@ -88,7 +90,7 @@ function runtime(initial = {}) {
consumePendingPaymentRefresh: () => false, PAYMENT_REFRESH_TARGETS: { HOME: 'home' }
});
if (specifier === '@/utils/wxsdk.js') return synthetic('wxsdk', {
initWxConfig: async () => {}, wxScan: async () => {}, isInWechat: () => false
initWxConfig: () => sdkInit(), wxScan: async () => {}, isInWechat: () => inWechat
});
if (specifier === './env.js') return synthetic('env', { BASE_URL: 'https://example.invalid' });
if (specifier.endsWith('.vue')) return synthetic(specifier, { default: {} });
@@ -105,7 +107,8 @@ function runtime(initial = {}) {
request.success({ statusCode, data: { code, msg, data } });
};
return { storage, sessionStorage, context, calls, requests, mounted, shown, hidden, unmounted, timers, api, load, reply,
setOptions: value => { options = value; }, tick: () => vue.nextTick() };
setOptions: value => { options = value; }, tick: () => vue.nextTick(),
setWechatSdk: init => { inWechat = true; sdkInit = init; } };
}
test('phone binding modes and success updates distinguish all four cases', async () => {
@@ -231,6 +234,152 @@ test('login routing honors server guidance even with a phone bound to someone el
}
});
test('OAuth round trip carries the verified identifier without relying on localStorage', async () => {
const r = runtime({ token: '', identifier: '' });
r.api.authApi = { verifyAsset: async id => { assert.equal(id, 'B'); return { asset_token: 'asset-token-B' }; } };
r.api.wechatApi = { getAppId: async () => ({ app_id: 'mock-app-id' }) };
const page = await r.load('pages/login/login.vue', ['identifier', 'doLogin']);
page.identifier.value = ' B ';
await page.doLogin();
assert.equal(r.storage.get('identifier'), 'B');
assert.deepEqual(JSON.parse(r.sessionStorage.get('wechatLoginContext')), { assetToken: 'asset-token-B', identifier: 'B' });
const authUrl = new URL(r.context.window.location.href);
assert.equal(new URL(authUrl.searchParams.get('redirect_uri')).searchParams.get('identifier'), 'B');
});
test('OAuth callback restores its own asset identifier before entering home', async () => {
const r = runtime({ token: 'old-token', identifier: 'old-asset' });
r.sessionStorage.set('assetToken', 'asset-token-B');
r.sessionStorage.set('wechatLoginContext', JSON.stringify({ assetToken: 'asset-token-B', identifier: 'B' }));
r.context.window.location.search = '?code=mock-code&identifier=B';
r.api.authApi = { wechatLogin: async () => ({ token: 'new-token', need_bind_phone: false }) };
r.api.wechatApi = {};
await r.load('pages/login/login.vue');
await r.mounted[0]();
const user = (await r.load('store/user.js')).useUserStore();
assert.equal(user.state.identifier, 'B');
assert.equal(r.storage.get('identifier'), 'B');
assert.equal(user.state.token, 'new-token');
assert.equal(r.calls.find(call => call.kind === 'redirectTo').url, '/pages/index/index');
assert.equal(r.sessionStorage.has('assetToken'), false);
const { assetApi } = await r.load('api/modules/asset.js');
const info = assetApi.getInfo(user.state.identifier);
assert.deepEqual(plain(r.requests[0].data), { identifier: 'B' });
r.reply(r.requests[0], { identifier: 'B' }); await info;
});
test('callback can recover a verified identifier from session even if localStorage is empty', async () => {
const r = runtime({ token: '', identifier: '' });
r.sessionStorage.set('assetToken', 'asset-token-B');
r.sessionStorage.set('wechatLoginContext', JSON.stringify({ assetToken: 'asset-token-B', identifier: 'B' }));
r.context.window.location.search = '?code=mock-code';
r.api.authApi = { wechatLogin: async () => ({ token: 'new-token', need_bind_phone: true }) };
r.api.wechatApi = {};
const page = await r.load('pages/login/login.vue', ['handleWechatCallback']);
await page.handleWechatCallback();
assert.equal(r.storage.get('identifier'), 'B');
assert.equal(r.calls.find(call => call.kind === 'redirectTo').url, '/pages/bind/bind?fromLogin=true');
});
test('callback falls back to URL/saved identifiers, but never enters home without one', async () => {
for (const source of ['url', 'storage', 'missing']) {
const r = runtime({ token: '', identifier: '' });
r.sessionStorage.set('assetToken', 'mock-asset-token');
r.context.window.location.search = source === 'url' ? '?code=mock-code&identifier=B' : '?code=mock-code';
if (source === 'storage') r.storage.set('identifier', 'B');
let logins = 0;
r.api.wechatApi = {};
r.api.authApi = { wechatLogin: async () => { logins++; return { token: 'new-token', need_bind_phone: false }; } };
const page = await r.load('pages/login/login.vue', ['handleWechatCallback']);
await page.handleWechatCallback();
if (source === 'missing') {
assert.equal(logins, 0);
assert.equal(r.calls.some(call => call.kind === 'redirectTo'), false);
assert.match(r.calls.find(call => call.kind === 'toast').title, /缺少资产标识/);
} else {
assert.equal(r.storage.get('identifier'), 'B');
assert.equal(logins, 1);
}
}
});
test('asset-info API rejects empty identifiers without a network request', async () => {
const r = runtime();
const { assetApi } = await r.load('api/modules/asset.js');
for (const value of ['', ' ', undefined, null]) {
await assert.rejects(assetApi.getInfo(value));
}
assert.equal(r.requests.length, 0);
const request = assetApi.getInfo(' A ');
assert.deepEqual(plain(r.requests[0].data), { identifier: 'A' });
r.reply(r.requests[0], {}); await request;
});
test('asset info waits until both wechat-login and JSSDK readiness complete', async () => {
const r = runtime({ token: 'old-token', identifier: 'old-asset' });
r.sessionStorage.set('assetToken', 'asset-token-B');
r.sessionStorage.set('wechatLoginContext', JSON.stringify({ assetToken: 'asset-token-B', identifier: 'B' }));
r.context.window.location.search = '?code=mock-code';
const login = deferred();
const sdk = deferred();
const events = [];
r.api.authApi = { wechatLogin: () => { events.push('login'); return login.promise; } };
r.api.wechatApi = {};
r.setWechatSdk(() => { events.push('jssdk'); return sdk.promise; });
await r.load('pages/login/login.vue');
const { assetApi } = await r.load('api/modules/asset.js');
const pending = r.mounted[0]();
await assert.rejects(assetApi.getInfo('old-asset'));
assert.equal(r.requests.length, 0);
assert.deepEqual(events, ['login']);
login.resolve({ token: 'new-token', need_bind_phone: false });
await new Promise(resolve => setImmediate(resolve));
assert.deepEqual(events, ['login', 'jssdk']);
assert.equal(r.calls.some(call => call.kind === 'redirectTo'), false);
await assert.rejects(assetApi.getInfo('B'));
assert.equal(r.requests.length, 0);
sdk.resolve(); await pending;
const user = (await r.load('store/user.js')).useUserStore();
assert.equal(user.state.loginPending, false);
assert.equal(r.calls.find(call => call.kind === 'redirectTo').url, '/pages/index/index');
const info = assetApi.getInfo(user.state.identifier);
assert.deepEqual(plain(r.requests[0].data), { identifier: 'B' });
assert.equal(r.requests[0].header.Authorization, 'Bearer new-token');
r.reply(r.requests[0], {}); await info;
});
test('JSSDK failure does not enter the homepage or release asset requests', async () => {
const r = runtime({ token: '', identifier: 'A' });
r.sessionStorage.set('assetToken', 'asset-token-A');
r.context.window.location.search = '?code=mock-code';
r.api.authApi = { wechatLogin: async () => ({ token: 'new-token', need_bind_phone: false }) };
r.api.wechatApi = {};
r.setWechatSdk(async () => { throw new Error('JSSDK failed'); });
const page = await r.load('pages/login/login.vue', ['handleWechatCallback']);
await page.handleWechatCallback();
assert.equal(r.calls.some(call => call.kind === 'redirectTo'), false);
const { assetApi } = await r.load('api/modules/asset.js');
await assert.rejects(assetApi.getInfo('A'));
assert.equal(r.requests.length, 0);
});
test('returning login session initializes JSSDK before re-entering homepage', async () => {
const r = runtime();
r.api.authApi = {}; r.api.wechatApi = {};
const sdk = deferred();
r.setWechatSdk(() => sdk.promise);
await r.load('pages/login/login.vue');
const pending = r.mounted[0]();
const { assetApi } = await r.load('api/modules/asset.js');
await assert.rejects(assetApi.getInfo('A'));
assert.equal(r.calls.some(call => call.kind === 'reLaunch'), false);
sdk.resolve(); await pending;
assert.equal(r.calls.find(call => call.kind === 'reLaunch').url, '/pages/index/index');
const info = assetApi.getInfo('A');
assert.equal(r.requests.length, 1);
r.reply(r.requests[0], {}); await info;
});
test('expiry boundaries and Shanghai rendering preserve server values, including zero', async () => {
const r = runtime();
const { normalizeExpiryReminder, getShanghaiDay, formatShanghaiExpiry } = await r.load('utils/expiry-reminder.js');