fa11db59ee
后端那个版本地板闸门只会把请求挡成 code 4012,客户端业务代码里没有任何 4012 分支:只拦、不推; 而桌面窗口直载 app-web(APP_URL = config.WEB_HOME_URL),做在页面上才吃得到已经装出去的机器。 判定落在 shared/client-force-update.ts(纯函数,17 例含"换地板结论就翻面"的反例):判不出来一律不拦 (浏览器形态、读不到本机版本、版本串解析不出数字、没有可下的包、线上包并不比本机新)。 遮罩挂 App.vue 根=登录页与首页一起覆盖,自动开始下载,失败只给"重试(断点续传)",不再等第二次确认。
174 lines
7.7 KiB
TypeScript
174 lines
7.7 KiB
TypeScript
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readFileSync, readdirSync, statSync } from 'node:fs'
|
||
import { fileURLToPath } from 'node:url'
|
||
import { join, resolve, dirname, relative } from 'node:path'
|
||
|
||
/**
|
||
* 强制升级门(10-13 决定对 4.0.52 以下的桌面装机开启)。
|
||
*
|
||
* 两半各自要证的:
|
||
* ① 判定本体(client-force-update.ts)—— 真单元测试,含"换地板结论就跟着变"的反例,
|
||
* 否则一条恒真的 force 判据会一直绿到把所有人锁在门外那天。
|
||
* ② 接线(.vue 组件在本套里挂不起来,按 readSource 约定钉形状)—— 要防的是
|
||
* "模块写了但没挂进 App"(改了没人跑)与"组件里塞 try/catch、用 window.confirm
|
||
* 走回手动那条路"(那等于把强升退化成又一次用户点一下)。
|
||
*/
|
||
import {
|
||
decideForceUpdate,
|
||
FORCE_UPDATE_TARGET,
|
||
isDottedNumericVersion,
|
||
} from '../src/shared/client-force-update.ts'
|
||
|
||
const here = dirname(fileURLToPath(import.meta.url))
|
||
const srcRoot = resolve(here, '../src')
|
||
const gate = readFileSync(resolve(srcRoot, 'shared/components/ForceUpdateGate.vue'), 'utf8')
|
||
const appVue = readFileSync(resolve(srcRoot, 'App.vue'), 'utf8')
|
||
|
||
function listSourceFiles(dir: string): string[] {
|
||
const out: string[] = []
|
||
for (const name of readdirSync(dir)) {
|
||
const full = join(dir, name)
|
||
if (statSync(full).isDirectory()) out.push(...listSourceFiles(full))
|
||
else if (/\.(ts|vue)$/.test(name)) out.push(full)
|
||
}
|
||
return out
|
||
}
|
||
|
||
test('test_floor_constant_is_pinned_dotted_version', () => {
|
||
assert.equal(FORCE_UPDATE_TARGET, '4.0.52', '地板改动必须显式改常量并留出处')
|
||
assert.ok(isDottedNumericVersion(FORCE_UPDATE_TARGET), '地板必须是三段点分数字')
|
||
})
|
||
|
||
test('test_below_floor_on_desktop_with_package_forces', () => {
|
||
const d = decideForceUpdate({
|
||
desktopBridge: true, localVersion: '4.0.51', latestVersion: '4.0.52', hasPackage: true,
|
||
})
|
||
assert.equal(d.force, true)
|
||
assert.equal(d.localVersion, '4.0.51')
|
||
assert.equal(d.targetVersion, '4.0.52')
|
||
})
|
||
|
||
test('test_very_old_client_also_forces', () => {
|
||
const d = decideForceUpdate({
|
||
desktopBridge: true, localVersion: '3.0.73', latestVersion: '4.0.52', hasPackage: true,
|
||
})
|
||
assert.equal(d.force, true, '远低于地板的老装机正是这次要覆盖的人')
|
||
})
|
||
|
||
test('test_at_floor_does_not_force', () => {
|
||
const d = decideForceUpdate({
|
||
desktopBridge: true, localVersion: '4.0.52', latestVersion: '4.0.52', hasPackage: true,
|
||
})
|
||
assert.equal(d.force, false)
|
||
assert.equal(d.reason, 'at-or-above-target')
|
||
})
|
||
|
||
test('test_newer_than_floor_does_not_force', () => {
|
||
// 灰度/回滚窗口里"本机比地板新"是真会发生的:不能反过来把人按住
|
||
const d = decideForceUpdate({
|
||
desktopBridge: true, localVersion: '4.0.99', latestVersion: '4.0.99', hasPackage: true,
|
||
})
|
||
assert.equal(d.force, false)
|
||
assert.equal(d.reason, 'at-or-above-target')
|
||
})
|
||
|
||
test('test_browser_runtime_never_forces', () => {
|
||
// 网页形态没有 exe 可升,拦下来就是把用户关在门外
|
||
const d = decideForceUpdate({
|
||
desktopBridge: false, localVersion: '4.0.20', latestVersion: '4.0.52', hasPackage: true,
|
||
})
|
||
assert.equal(d.force, false)
|
||
assert.equal(d.reason, 'not-desktop')
|
||
})
|
||
|
||
test('test_missing_local_version_does_not_force', () => {
|
||
// 老 exe 没有 get_app_version 桥:判不出来就不拦(宁漏不误锁)
|
||
const d = decideForceUpdate({
|
||
desktopBridge: true, localVersion: '', latestVersion: '4.0.52', hasPackage: true,
|
||
})
|
||
assert.equal(d.force, false)
|
||
assert.equal(d.reason, 'no-local-version')
|
||
})
|
||
|
||
test('test_unparsable_local_version_does_not_force', () => {
|
||
// compareVersions 把非数字段当 0,'4.0.52-beta' 会被算成 '4.0.52'、'x-beta' 会被算成 '0.0.0':
|
||
// 少了这道判据,带后缀的内部版本会被误锁
|
||
const d = decideForceUpdate({
|
||
desktopBridge: true, localVersion: 'beta', latestVersion: '4.0.52', hasPackage: true,
|
||
})
|
||
assert.equal(d.force, false)
|
||
assert.equal(d.reason, 'unparsable-local-version')
|
||
})
|
||
|
||
test('test_no_download_url_does_not_force', () => {
|
||
const d = decideForceUpdate({
|
||
desktopBridge: true, localVersion: '4.0.47', latestVersion: '4.0.52', hasPackage: false,
|
||
})
|
||
assert.equal(d.force, false)
|
||
assert.equal(d.reason, 'no-newer-package', '没有可下的包就别把人锁住')
|
||
})
|
||
|
||
test('test_latest_package_not_above_local_does_not_force', () => {
|
||
// 线上包已回滚到 4.0.40:升上去更差,不升
|
||
const d = decideForceUpdate({
|
||
desktopBridge: true, localVersion: '4.0.47', latestVersion: '4.0.40', hasPackage: true,
|
||
})
|
||
assert.equal(d.force, false)
|
||
assert.equal(d.reason, 'no-newer-package')
|
||
})
|
||
|
||
test('test_invalid_floor_does_not_force', () => {
|
||
const d = decideForceUpdate({
|
||
desktopBridge: true, localVersion: '4.0.47', latestVersion: '4.0.52', hasPackage: true,
|
||
targetVersion: 'v4',
|
||
})
|
||
assert.equal(d.force, false)
|
||
assert.equal(d.reason, 'invalid-target', '地板写坏了要认出来,不许拿 0.0.0 当阈值')
|
||
})
|
||
|
||
test('test_decision_follows_the_floor_not_the_caller', () => {
|
||
// 反例自证:同一次调用只换地板,结论必须翻面(否则上一条条"低于地板就 force"是恒真)
|
||
const input = {
|
||
desktopBridge: true, localVersion: '4.0.51', latestVersion: '4.0.52', hasPackage: true,
|
||
}
|
||
assert.equal(decideForceUpdate({ ...input, targetVersion: '4.0.52' }).force, true)
|
||
assert.equal(decideForceUpdate({ ...input, targetVersion: '4.0.51' }).force, false)
|
||
})
|
||
|
||
test('test_is_dotted_numeric_version_real_samples', () => {
|
||
assert.equal(isDottedNumericVersion('4.0.52'), true)
|
||
assert.equal(isDottedNumericVersion('v4.1.0'), true, '前缀 v 归一化后按点分数字认')
|
||
assert.equal(isDottedNumericVersion('4.0'), false, '不足三段不认:0 补齐会把地板算成 4.0.0')
|
||
assert.equal(isDottedNumericVersion('4.0.52-beta'), false)
|
||
assert.equal(isDottedNumericVersion(''), false)
|
||
})
|
||
|
||
test('test_gate_uses_the_shared_decision_and_real_bridge', () => {
|
||
assert.ok(gate.includes('decideForceUpdate'), '遮罩必须走同一个判定,不许在组件里另写一套比较')
|
||
assert.ok(gate.includes('hasPywebview()'), '桌面判定只认真桥,Web 降级桥不算')
|
||
assert.ok(gate.includes('do_update_app'), '没有更新能力的装机不许拦(拦了也办不成事)')
|
||
assert.ok(gate.includes('startUpdate('), '自动开始下载走既有下载链,不复制第二份下载实现')
|
||
})
|
||
|
||
test('test_gate_does_not_fall_back_to_manual_confirm', () => {
|
||
assert.equal(gate.includes('window.confirm'), false, '强升不该再等用户点一次"是否更新"')
|
||
assert.equal(/catch\s*[({]/.test(gate), false, '遮罩里新增 try/catch 会绕过静默棘轮的台账')
|
||
})
|
||
|
||
test('test_gate_pairs_its_pywebview_ready_listener', () => {
|
||
// 桥可能晚于挂载就绪:必须等 pywebviewready,且卸载时配对摘掉
|
||
assert.ok(gate.includes("addEventListener('pywebviewready'"), '就绪前不判定,就绪后判一次')
|
||
assert.ok(gate.includes("removeEventListener('pywebviewready'"), '监听没有只加不摘的')
|
||
})
|
||
|
||
test('test_gate_is_mounted_once_at_app_root', () => {
|
||
assert.ok(appVue.includes('<ForceUpdateGate />'), '挂在根组件=登录页与首页一起覆盖(存量装机下次打开就吃到)')
|
||
assert.ok(appVue.includes("import ForceUpdateGate from '@/shared/components/ForceUpdateGate.vue'"))
|
||
const consumers = listSourceFiles(srcRoot).filter((file) => {
|
||
if (relative(srcRoot, file).split('\\').join('/').startsWith('shared/components/ForceUpdateGate.vue')) return false
|
||
return readFileSync(file, 'utf8').includes('ForceUpdateGate')
|
||
})
|
||
assert.equal(consumers.length, 1, `ForceUpdateGate 的消费者必须只有 App.vue,实测 ${consumers.length} 处`)
|
||
})
|