Improve hall chat retries, meetup-live lifecycle, home/mine remote-config UX, and add isolated social/API regression tests without deploying. Co-authored-by: Cursor <cursoragent@cursor.com>
197 lines
7.0 KiB
JavaScript
197 lines
7.0 KiB
JavaScript
// Isolated request/config/navigation regressions; never connects to a live API.
|
|
const { test } = require('node:test')
|
|
const assert = require('node:assert/strict')
|
|
const fs = require('node:fs')
|
|
const path = require('node:path')
|
|
const vm = require('node:vm')
|
|
const ts = require('typescript')
|
|
|
|
const sourceRoot = path.resolve(__dirname, '../../h5/src')
|
|
const config = {
|
|
API_BASE: 'https://api.invalid', WEB_BASE: 'https://web.invalid',
|
|
APP_TARGET: 'weapp', STORAGE_OPENID: 'openid', STORAGE_TOKEN: 'token',
|
|
}
|
|
|
|
function deferred() {
|
|
let resolve
|
|
const promise = new Promise((done) => { resolve = done })
|
|
return { promise, resolve }
|
|
}
|
|
|
|
function harness(file, overrides = {}, imports = {}) {
|
|
const storage = new Map()
|
|
const intervals = []
|
|
const events = {}
|
|
const taro = {
|
|
getStorageSync: (key) => storage.get(key),
|
|
setStorageSync: (key, value) => storage.set(key, value),
|
|
removeStorageSync: (key) => storage.delete(key),
|
|
onAppShow: (fn) => { events.show = fn },
|
|
onAppHide: (fn) => { events.hide = fn },
|
|
offAppShow: () => {}, offAppHide: () => {},
|
|
...overrides,
|
|
}
|
|
const module = { exports: {} }
|
|
const stubs = {
|
|
'@tarojs/taro': taro,
|
|
'./config': config, '../utils/config': config,
|
|
'./accountBlocked': { isAccountBlockedHttp: () => false, notifyAccountBlocked: () => {} },
|
|
'./track': { track: () => {}, trackClick: () => {} },
|
|
...imports,
|
|
}
|
|
const code = ts.transpileModule(fs.readFileSync(path.join(sourceRoot, file), 'utf8'), {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2018, esModuleInterop: true },
|
|
}).outputText
|
|
vm.runInNewContext(code, {
|
|
module, exports: module.exports,
|
|
require: (id) => {
|
|
if (!(id in stubs)) throw new Error(`Unexpected dependency ${id}`)
|
|
return stubs[id]
|
|
},
|
|
process: { env: { TARO_ENV: 'weapp' } },
|
|
setTimeout, clearTimeout,
|
|
setInterval: (fn) => { intervals.push(fn); return intervals.length },
|
|
clearInterval: () => {},
|
|
}, { filename: file })
|
|
return { api: module.exports, storage, intervals, events }
|
|
}
|
|
|
|
test('concurrent login and forced refresh share one session exchange', async () => {
|
|
const gate = deferred()
|
|
let calls = 0
|
|
const { api, storage } = harness('utils/request.ts', {
|
|
login: async () => { calls++; await gate.promise; return { code: 'once' } },
|
|
request: async () => ({ statusCode: 200, data: { openid: 'user', token: 'new' } }),
|
|
})
|
|
const pending = [api.ensureLogin(), api.ensureLogin(true), api.ensureLogin()]
|
|
gate.resolve()
|
|
await Promise.all(pending)
|
|
assert.equal(calls, 1)
|
|
assert.equal(storage.get('token'), 'new')
|
|
})
|
|
|
|
test('logging out discards a late login response', async () => {
|
|
const gate = deferred()
|
|
const { api, storage } = harness('utils/request.ts', {
|
|
login: async () => ({ code: 'code' }),
|
|
request: () => gate.promise,
|
|
})
|
|
const pending = api.ensureLogin()
|
|
await Promise.resolve()
|
|
api.clearLogin()
|
|
gate.resolve({ statusCode: 200, data: { openid: 'user', token: 'late' } })
|
|
await assert.rejects(pending, /登录已取消/)
|
|
assert.equal(storage.get('token'), undefined)
|
|
})
|
|
|
|
test('late 401 reuses refreshed credentials without a second login', async () => {
|
|
const late = deferred()
|
|
let logins = 0
|
|
const headers = []
|
|
const { api, storage } = harness('utils/request.ts', {
|
|
login: async () => { logins++; return { code: 'code' } },
|
|
request: async (options) => {
|
|
if (options.url.endsWith('/auth/login')) return { statusCode: 200, data: { openid: 'user', token: 'fresh' } }
|
|
headers.push(options.header.Authorization)
|
|
if (options.header.Authorization === 'Bearer old') {
|
|
if (options.url.endsWith('/late')) return late.promise
|
|
return { statusCode: 401, data: {} }
|
|
}
|
|
return { statusCode: 200, data: { ok: true } }
|
|
},
|
|
})
|
|
storage.set('token', 'old'); storage.set('openid', 'user')
|
|
const second = api.requestEbook({ url: '/late' })
|
|
await api.requestEbook({ url: '/first' })
|
|
late.resolve({ statusCode: 401, data: {} })
|
|
await second
|
|
assert.equal(logins, 1)
|
|
assert.equal(headers.filter((header) => header === 'Bearer fresh').length, 2)
|
|
})
|
|
|
|
test('a late unauthorized request cannot log the user back in after logout', async () => {
|
|
const late = deferred()
|
|
let logins = 0
|
|
const { api } = harness('utils/request.ts', {
|
|
login: async () => { logins++; return { code: 'code' } },
|
|
request: () => late.promise,
|
|
})
|
|
const pending = api.requestEbook({ url: '/profile' })
|
|
api.clearLogin()
|
|
late.resolve({ statusCode: 401, data: {} })
|
|
await assert.rejects(pending, /需要登录/)
|
|
assert.equal(logins, 0)
|
|
})
|
|
|
|
test('failed order submission is never automatically replayed', async () => {
|
|
let requests = 0
|
|
const { api } = harness('utils/request.ts', {
|
|
request: async () => { requests++; throw new Error('timeout') },
|
|
})
|
|
await assert.rejects(api.requestEbook({ url: '/orders', method: 'POST' }), /timeout/)
|
|
assert.equal(requests, 1)
|
|
})
|
|
|
|
const seed = { version: 1, modules: {}, flags: {}, copy: {} }
|
|
const seedImport = {
|
|
'../data/remoteConfig.defaults': { DEFAULT_REMOTE_CONFIG: seed, mergeRemoteConfig: (raw) => ({ ...seed, ...raw }) },
|
|
}
|
|
|
|
test('startup configuration callers share one request and preserve last good data offline', async () => {
|
|
const gate = deferred()
|
|
let requests = 0
|
|
let offline = false
|
|
const { api } = harness('services/remoteConfig.ts', {
|
|
request: async () => {
|
|
requests++
|
|
if (offline) throw new Error('offline')
|
|
await gate.promise
|
|
return { statusCode: 200, data: { config: { ...seed, version: 9 } } }
|
|
},
|
|
}, seedImport)
|
|
const pending = [api.loadRemoteConfig(true), api.loadRemoteConfig(true), api.loadRemoteConfig()]
|
|
gate.resolve()
|
|
await Promise.all(pending)
|
|
assert.equal(requests, 1)
|
|
offline = true
|
|
const fallback = await api.loadRemoteConfig(true)
|
|
assert.equal(fallback.config.version, 9)
|
|
assert.equal(fallback.fromOffline, true)
|
|
})
|
|
|
|
test('configuration polling pauses in background and never overlaps slow meta requests', async () => {
|
|
const gate = deferred()
|
|
let requests = 0
|
|
const { api, events, intervals } = harness('services/remoteConfig.ts', {
|
|
request: async () => { requests++; return gate.promise },
|
|
}, seedImport)
|
|
const stop = api.startRemoteConfigLiveSync()
|
|
events.hide()
|
|
intervals[0]()
|
|
assert.equal(requests, 0)
|
|
events.show()
|
|
intervals[0](); intervals[0]()
|
|
assert.equal(requests, 1)
|
|
gate.resolve({ statusCode: 200, data: { version: 1 } })
|
|
await new Promise((resolve) => setImmediate(resolve))
|
|
stop()
|
|
})
|
|
|
|
test('rapid taps open one page; home resets the stack', async () => {
|
|
const gate = deferred()
|
|
let navigations = 0
|
|
let homes = 0
|
|
const { api } = harness('utils/nav.ts', {
|
|
getCurrentPages: () => [],
|
|
navigateTo: () => { navigations++; return gate.promise },
|
|
reLaunch: async () => { homes++ },
|
|
})
|
|
api.goPage('/pages/book/index'); api.goPage('/pages/book/index')
|
|
assert.equal(navigations, 1)
|
|
gate.resolve()
|
|
await new Promise((resolve) => setImmediate(resolve))
|
|
api.goHome()
|
|
await new Promise((resolve) => setImmediate(resolve))
|
|
assert.equal(homes, 1)
|
|
})
|