Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 仅对更高版本提示整包更新 (#4299)

guyong-zapodfdd3217da93
diff --git a/apps/mobile/src/update/bundleUpdate.test.ts b/apps/mobile/src/update/bundleUpdate.test.ts
index a38a48d5b1..26eab0fe9d 100644
--- a/apps/mobile/src/update/bundleUpdate.test.ts
+++ b/apps/mobile/src/update/bundleUpdate.test.ts
@@ -15,6 +15,17 @@ const VALID = {
itmsUrl: 'itms-services://?action=download-manifest&url=https%3A%2F%2Fx%2Fplist%2F42',
};
+const UNSUPPORTED_VERSIONS = [
+ '1.0.0-beta.1',
+ '1.0.0+build.1',
+ 'v2.0.0',
+ '1..2',
+ '1.0.-1',
+ '1.0.1e2',
+ '1.0.Infinity',
+ '1.0.9007199254740992',
+];
+
describe('shouldCheckBundleUpdate', () => {
it.each([
['非自建分发', false, false, false, false],
@@ -45,6 +56,18 @@ describe('parseLatestRelease', () => {
expect(parseLatestRelease(null)).toBeNull();
expect(parseLatestRelease('x')).toBeNull();
});
+
+ it.each(UNSUPPORTED_VERSIONS)('version=%s 不受支持 → 无效记录', (version) => {
+ expect(parseLatestRelease({ ...VALID, version })).toBeNull();
+ });
+
+ it.each(UNSUPPORTED_VERSIONS)('minVersion=%s 不受支持 → 无效记录', (minVersion) => {
+ expect(parseLatestRelease({ ...VALID, minVersion })).toBeNull();
+ });
+
+ it.each(['1', '1.2', '01.002.0003', '1.2.0.0', '1.0.9007199254740991'])('保留纯数字版本 %s 的兼容性', (version) => {
+ expect(parseLatestRelease({ ...VALID, version })?.version).toBe(version);
+ });
});
describe('compareVersions', () => {
@@ -56,6 +79,44 @@ describe('compareVersions', () => {
});
describe('evaluateBundleUpdate', () => {
+ it.each(UNSUPPORTED_VERSIONS)('服务端 version=%s 不受支持 → 不提示整包', (version) => {
+ const evaluation = evaluateBundleUpdate({
+ currentRuntimeVersion: 'rtv-old',
+ currentVersion: '1.0.0',
+ latest: { ...VALID, version },
+ });
+ expect(evaluation).toEqual({ needsUpdate: false, forced: false, target: null });
+ });
+
+ it.each(UNSUPPORTED_VERSIONS)('本机 currentVersion=%s 不受支持 → 不提示整包', (currentVersion) => {
+ const evaluation = evaluateBundleUpdate({
+ currentRuntimeVersion: 'rtv-old',
+ currentVersion,
+ latest: { ...VALID, minVersion: '1.2.0' },
+ });
+ expect(evaluation).toEqual({ needsUpdate: false, forced: false, target: null });
+ });
+
+ it.each(UNSUPPORTED_VERSIONS)('服务端 minVersion=%s 不受支持 → 不提示整包', (minVersion) => {
+ const evaluation = evaluateBundleUpdate({
+ currentRuntimeVersion: 'rtv-old',
+ currentVersion: '1.0.0',
+ latest: { ...VALID, minVersion },
+ });
+ expect(evaluation).toEqual({ needsUpdate: false, forced: false, target: null });
+ });
+
+ it('版本周围的空格不影响纯数字版本比较', () => {
+ const evaluation = evaluateBundleUpdate({
+ currentRuntimeVersion: 'rtv-old',
+ currentVersion: ' 1.1.0 ',
+ latest: { ...VALID, version: ' 1.2.0 ', minVersion: ' 1.1.0 ' },
+ });
+ expect(evaluation.needsUpdate).toBe(true);
+ expect(evaluation.forced).toBe(false);
+ expect(evaluation.target?.version).toBe('1.2.0');
+ });
+
it('runtimeVersion 相同 → 无整包更新(交给 JS OTA)', () => {
const r = evaluateBundleUpdate({ currentRuntimeVersion: 'rtv-new', currentVersion: '1.1.0', latest: VALID });
expect(r.needsUpdate).toBe(false);
@@ -69,6 +130,34 @@ describe('evaluateBundleUpdate', () => {
expect(r.target?.itmsUrl).toBe(VALID.itmsUrl);
});
+ it.each([
+ ['0.1.4', 'rtv-old', undefined],
+ ['0.1.4', 'rtv-new', undefined],
+ ['0.1.5', 'rtv-old', undefined],
+ ['0.1.5', 'rtv-new', undefined],
+ ['0.1.5.0', 'rtv-old', undefined],
+ ['0.1.4', 'rtv-old', '0.1.6'],
+ ['0.1.5', 'rtv-old', '0.1.6'],
+ ])('本机 0.1.5 忽略服务端 version=%s / runtime=%s / minVersion=%s 的整包', (version, runtimeVersion, minVersion) => {
+ const evaluation = evaluateBundleUpdate({
+ currentRuntimeVersion: 'rtv-new',
+ currentVersion: '0.1.5',
+ latest: { ...VALID, version, runtimeVersion, minVersion, buildNumber: 9999999999 },
+ });
+ expect(evaluation).toEqual({ needsUpdate: false, forced: false, target: null });
+ });
+
+ it('更高的多位 patch 版本 → 普通整包更新(按数值而非字符串比较)', () => {
+ const evaluation = evaluateBundleUpdate({
+ currentRuntimeVersion: 'rtv-old',
+ currentVersion: '0.1.9',
+ latest: { ...VALID, version: '0.1.10' },
+ });
+ expect(evaluation.needsUpdate).toBe(true);
+ expect(evaluation.forced).toBe(false);
+ expect(evaluation.target?.version).toBe('0.1.10');
+ });
+
it('minVersion 高于当前 → 强制', () => {
const r = evaluateBundleUpdate({
currentRuntimeVersion: 'rtv-old',
@@ -82,9 +171,10 @@ describe('evaluateBundleUpdate', () => {
it('minVersion 不高于当前 → 不强制', () => {
const r = evaluateBundleUpdate({
currentRuntimeVersion: 'rtv-old',
- currentVersion: '1.2.0',
- latest: { ...VALID, minVersion: '1.2.0' },
+ currentVersion: '1.1.0',
+ latest: { ...VALID, minVersion: '1.1.0' },
});
+ expect(r.needsUpdate).toBe(true);
expect(r.forced).toBe(false);
});
@@ -105,23 +195,20 @@ describe('evaluateBundleUpdate', () => {
it('runtimeVersion 相同且不低于 minVersion → 无更新', () => {
const r = evaluateBundleUpdate({
currentRuntimeVersion: 'rtv-new',
- currentVersion: '1.2.0',
- latest: { ...VALID, minVersion: '1.2.0' },
+ currentVersion: '1.1.0',
+ latest: { ...VALID, minVersion: '1.1.0' },
});
expect(r.needsUpdate).toBe(false);
expect(r.target).toBeNull();
});
- it('record 缺 version 但带 minVersion → 不强更(退化成普通提示,不造无法自愈的阻断)', () => {
- // 无目标版本就无法证明"装上它能解除阻断",阻断态的自愈也失去比较基准。
- // 发布链侧 assertMinVersionUsable 已保证有 minVersion 必有 version,这里是客户端兜底。
- const r = evaluateBundleUpdate({
+ it.each([undefined, null, '', ' '])('record version=%s → 不提示(无法证明服务端版本更高)', (version) => {
+ const evaluation = evaluateBundleUpdate({
currentRuntimeVersion: 'rtv-old',
currentVersion: '1.0.0',
- latest: { ...VALID, version: '', minVersion: '1.2.0' },
+ latest: { ...VALID, version, minVersion: '1.2.0' },
});
- expect(r.forced).toBe(false);
- expect(r.needsUpdate).toBe(true); // runtimeVersion 不同 → 仍是可跳过的普通更新
+ expect(evaluation).toEqual({ needsUpdate: false, forced: false, target: null });
});
it('minVersion 高于该记录自己的 version → 不强更(装完仍低于门槛,阻断屏会没有出口)', () => {
@@ -144,14 +231,13 @@ describe('evaluateBundleUpdate', () => {
expect(r.forced).toBe(true);
});
- it('缺 currentVersion → 不强更(无法比较,fail-open)', () => {
- const r = evaluateBundleUpdate({
- currentRuntimeVersion: 'rtv-new',
- currentVersion: null,
+ it.each([undefined, null, '', ' '])('currentVersion=%s → 不提示(无法比较,fail-open)', (currentVersion) => {
+ const evaluation = evaluateBundleUpdate({
+ currentRuntimeVersion: 'rtv-old',
+ currentVersion,
latest: { ...VALID, minVersion: '1.2.0' },
});
- expect(r.needsUpdate).toBe(false);
- expect(r.forced).toBe(false);
+ expect(evaluation).toEqual({ needsUpdate: false, forced: false, target: null });
});
it('拿不到当前 runtimeVersion(dev / 未启用)→ 无更新', () => {
diff --git a/apps/mobile/src/update/bundleUpdate.ts b/apps/mobile/src/update/bundleUpdate.ts
index 3573fa6e49..c8f1dbfe6a 100644
--- a/apps/mobile/src/update/bundleUpdate.ts
+++ b/apps/mobile/src/update/bundleUpdate.ts
@@ -2,9 +2,9 @@
//
// 背景:
// - expo-updates 只感知"我这个 runtimeVersion 有没有新 JS OTA",不会告知"有更高 runtimeVersion 的整包"。
-// - 所以整包发现靠独立的 `/latest` 通道 + 客户端比对 runtimeVersion:
-// · latest.runtimeVersion === 当前 runtimeVersion → 无需整包(JS OTA 通道会处理);
-// · 不同 → 原生层变了,JS OTA 覆盖不到 → 引导用户打开 release record 的正常安装入口。
+// - 所以整包发现靠独立的 `/latest` 通道,先确认服务端 version 高于当前原生版本:
+// · 服务端版本不高于当前版本 → 不提示整包,不影响独立的 JS OTA 通道;
+// · 版本更高时比对 runtimeVersion:相同交给 JS OTA,不同则引导打开正常安装入口。
// - minVersion(可选)用于强制更新:当前 version 低于它则阻断使用、只留"去更新"。
// 强更判定**不经过 runtimeVersion 门闸**:门槛由服务端按 version 下发,同 runtimeVersion
// 的旧构建也必须能被强更(否则"发布链写了 minVersion 却对同指纹装机无效")。
@@ -22,7 +22,7 @@ export interface LatestReleaseRecord {
}
export interface BundleUpdateEvaluation {
- /** 是否有整包更新(runtimeVersion 与当前不一致)。 */
+ /** 是否有整包更新(服务端版本更高,且 runtimeVersion 不同或命中强更)。 */
needsUpdate: boolean;
/** 是否强制(needsUpdate 且当前 version < minVersion)。 */
forced: boolean;
@@ -64,7 +64,12 @@ export function shouldCheckBundleUpdate({
return isSelfHosted && !isReviewMode && !isTestFlightBuild;
}
-/** 校验并收窄 `/latest` 响应为 LatestReleaseRecord;字段缺失即返回 null(宁可不提示,不误导)。 */
+export function isSupportedBundleVersion(version: string): boolean {
+ return /^\d+(?:\.\d+)*$/.test(version)
+ && version.split('.').every((part) => Number.isSafeInteger(Number(part)));
+}
+
+/** 校验 `/latest` 响应;版本仅支持点分纯数字,字段缺失或格式非法返回 null(不误导)。 */
export function parseLatestRelease(value: unknown): LatestReleaseRecord | null {
if (!value || typeof value !== 'object') return null;
const v = value as Record<string, unknown>;
@@ -72,8 +77,8 @@ export function parseLatestRelease(value: unknown): LatestReleaseRecord | null {
const version = typeof v.version === 'string' ? v.version.trim() : '';
const installUrl = typeof v.installUrl === 'string' ? v.installUrl.trim() : '';
const itmsUrl = typeof v.itmsUrl === 'string' ? v.itmsUrl.trim() : '';
- // 至少要有 runtimeVersion(判定门闸)+ 一个可跳转的安装地址。
- if (!runtimeVersion || (!installUrl && !itmsUrl)) return null;
+ // 必须有 runtimeVersion、支持的 version 和一个可跳转的安装地址。
+ if (!runtimeVersion || !isSupportedBundleVersion(version) || (!installUrl && !itmsUrl)) return null;
const record: LatestReleaseRecord = {
version,
buildNumber: (typeof v.buildNumber === 'string' || typeof v.buildNumber === 'number') ? v.buildNumber : '',
@@ -82,11 +87,15 @@ export function parseLatestRelease(value: unknown): LatestReleaseRecord | null {
itmsUrl,
};
if (typeof v.releaseNotes === 'string') record.releaseNotes = v.releaseNotes;
- if (typeof v.minVersion === 'string' && v.minVersion.trim()) record.minVersion = v.minVersion.trim();
+ if (typeof v.minVersion === 'string' && v.minVersion.trim()) {
+ const minVersion = v.minVersion.trim();
+ if (!isSupportedBundleVersion(minVersion)) return null;
+ record.minVersion = minVersion;
+ }
return record;
}
-/** 语义化版本比较:a<b → -1,a==b → 0,a>b → 1。非数字段按 0 处理。 */
+/** 比较已校验的点分纯数字版本:a<b → -1,a==b → 0,a>b → 1,缺失段按 0 处理。 */
export function compareVersions(a: string, b: string): number {
const pa = String(a).split('.').map((x) => Number(x));
const pb = String(b).split('.').map((x) => Number(x));
@@ -100,14 +109,14 @@ export function compareVersions(a: string, b: string): number {
}
/**
- * 判定是否需要引导整包更新。两条独立信号:
+ * 判定是否需要引导整包更新。服务端 version 必须高于当前原生 version,再检查两条信号:
* - runtimeVersion 不一致 → 有整包更新(可跳过的普通提示);
* - 当前 version < minVersion → 强更,**与 runtimeVersion 是否一致无关**。
* 服务端可以对某个已发布版本事后下发门槛,把同指纹的问题构建也挡住;
* 反过来说,同 runtimeVersion 命中强更时 target.runtimeVersion 就等于当前值,
* 消费方不得把它当作"换了指纹"的证据。
- * 拿不到当前 runtimeVersion(dev / expo-updates 未启用)、拿不到当前 version 或
- * `/latest` 无效 → 一律视为无更新(比不出来就不挡人,fail-open)。
+ * 拿不到当前 runtimeVersion(dev / expo-updates 未启用)、版本字段缺失或不是点分纯数字、
+ * 服务端版本不高于本机、`/latest` 无效 → 视为无整包更新,不影响独立的 JS OTA 通道。
*/
export function evaluateBundleUpdate({
currentRuntimeVersion,
@@ -120,18 +129,18 @@ export function evaluateBundleUpdate({
const current = String(currentRuntimeVersion ?? '').trim();
if (!current) return NO_UPDATE;
+ const currentAppVersion = String(currentVersion ?? '').trim();
+ if (!isSupportedBundleVersion(currentAppVersion)) return NO_UPDATE;
+ if (compareVersions(record.version, currentAppVersion) <= 0) return NO_UPDATE;
+
// 强更要求这条记录本身**能被满足**:
- // - 带 version:拿不到目标版本就无法证明"装上它就能解除阻断"(阻断态自愈全靠这个
- // 可比较的版本,见 forcedUpdateRecheck 的新鲜度门);
// - minVersion ≤ version:否则唯一提供的安装目标仍低于门槛,用户装完照旧被强更,
// 阻断屏成了没有出口的死屋,只能等运维改指针。
- // 发布链侧 assertMinVersionUsable 已经保证这两条,这里是客户端兜底(手工改过的 / 历史
+ // 发布链侧 assertMinVersionUsable 已经保证门槛可满足,这里是客户端兜底(手工改过的 / 历史
// 遗留的指针也不能把人关死):记录不自洽时退化成可跳过的普通更新提示。
const forced = Boolean(
record.minVersion &&
- record.version &&
- currentVersion &&
- compareVersions(String(currentVersion), record.minVersion) < 0 &&
+ compareVersions(currentAppVersion, record.minVersion) < 0 &&
compareVersions(record.version, record.minVersion) >= 0,
);
diff --git a/apps/mobile/src/update/forcedUpdateRecheck.test.ts b/apps/mobile/src/update/forcedUpdateRecheck.test.ts
index ad3fce557a..fb195dce8d 100644
--- a/apps/mobile/src/update/forcedUpdateRecheck.test.ts
+++ b/apps/mobile/src/update/forcedUpdateRecheck.test.ts
@@ -241,6 +241,35 @@ describe('createForcedUpdateRechecker 解除判定', () => {
expect(deps.onCleared).not.toHaveBeenCalled();
});
+ it.each(['3.0.0-beta.1', '3.0.0+build.1', 'v3.0.0', '3..0'])('目标 version=%s 不受支持 → 维持阻断', async (version) => {
+ const deps = makeDeps({
+ fetchLatest: vi.fn(async () => latestRecord({ version, minVersion: undefined })),
+ getHeldTarget: () => ({ version: '2.0.0' }),
+ });
+ await expect(runOnce(deps)).resolves.toBe('error');
+ expect(deps.onCleared).not.toHaveBeenCalled();
+ expect(deps.onStillForced).not.toHaveBeenCalled();
+ });
+
+ it.each(['3.0.0-beta.1', '3.0.0+build.1', 'v3.0.0', '3..0'])('本机 version=%s 不受支持 → 维持阻断', async (version) => {
+ const deps = makeDeps({
+ fetchLatest: vi.fn(async () => latestRecord({ minVersion: undefined })),
+ getCurrentVersion: () => version,
+ });
+ await expect(runOnce(deps)).resolves.toBe('error');
+ expect(deps.onCleared).not.toHaveBeenCalled();
+ expect(deps.onStillForced).not.toHaveBeenCalled();
+ });
+
+ it.each(['2.0.0-beta.1', '2.0.0+build.1', 'v2.0.0'])('minVersion=%s 不受支持 → 维持阻断且不更新目标', async (minVersion) => {
+ const deps = makeDeps({
+ fetchLatest: vi.fn(async () => latestRecord({ minVersion })),
+ });
+ await expect(runOnce(deps)).resolves.toBe('error');
+ expect(deps.onCleared).not.toHaveBeenCalled();
+ expect(deps.onStillForced).not.toHaveBeenCalled();
+ });
+
it('同版本记录撤回门槛(最常见的撤回形态)→ 正常解除', async () => {
const deps = makeDeps({
fetchLatest: vi.fn(async () => latestRecord({ version: '2.0.0', minVersion: undefined })),
diff --git a/apps/mobile/src/update/forcedUpdateRecheck.ts b/apps/mobile/src/update/forcedUpdateRecheck.ts
index d41a01bf2b..b147f5baa9 100644
--- a/apps/mobile/src/update/forcedUpdateRecheck.ts
+++ b/apps/mobile/src/update/forcedUpdateRecheck.ts
@@ -13,7 +13,7 @@
// 节流比 resume 通道(5 分钟)短得多:用户此刻正被挡在门外,恢复延迟直接可感;
// 但仍要节流,避免在阻断屏上反复切前后台把 /latest 打成高频请求。
-import { compareVersions, evaluateBundleUpdate, parseLatestRelease } from './bundleUpdate';
+import { compareVersions, evaluateBundleUpdate, isSupportedBundleVersion, parseLatestRelease } from './bundleUpdate';
import { withTimeout } from './startupOtaUpdate';
export type ForcedUpdateRecheckOutcome = 'still-forced' | 'cleared' | 'error';
@@ -139,9 +139,10 @@ export function createForcedUpdateRechecker(
const currentRuntimeVersion = String(deps.getCurrentRuntimeVersion() ?? '').trim();
const currentVersion = String(deps.getCurrentVersion() ?? '').trim();
if (!record || !currentRuntimeVersion || !currentVersion) return 'error';
+ if (!isSupportedBundleVersion(currentVersion)) return 'error';
// 新鲜度门:读到的记录不得比正在阻断的目标更旧。可变指针 + 无 cache-buster 的请求
// 会撞上 CDN 边缘的旧记录,那条记录没有 minVersion → 会把仍需强更的用户放出去。
- // 记录缺 version(parseLatestRelease 容许空串)同样证明不了新鲜度,一并挡掉。
+ // 记录的 version 格式已在 parseLatestRelease 校验,这里继续挡掉版本更旧的记录。
// 只有两边都有版本号时才比较。held.version 为空按"无新鲜度约束"处理而不是维持阻断
// ——否则一条无 version 的记录会把用户永久钉在阻断屏上(定时与回前台核对都过不去)。
// 这种情况现在不可达:evaluateBundleUpdate 要求 record 带 version 才判 forced,
diff --git a/apps/mobile/src/update/resumeUpdateCheck.test.ts b/apps/mobile/src/update/resumeUpdateCheck.test.ts
index bb6bae51d1..84632d0969 100644
--- a/apps/mobile/src/update/resumeUpdateCheck.test.ts
+++ b/apps/mobile/src/update/resumeUpdateCheck.test.ts
@@ -240,6 +240,17 @@ describe('createResumeUpdateChecker 整包路径', () => {
expect(deps.onForcedUpdate).not.toHaveBeenCalled();
});
+ it.each(['0.1.4', '0.1.5'])('整包 version=%s 不高于本机 → 忽略整包,OTA 仍正常下载', async (version) => {
+ const deps = makeDeps({
+ getCurrentVersion: () => '0.1.5',
+ fetchLatest: vi.fn(async () => latestRecord({ version })),
+ checkForUpdateAsync: vi.fn(async () => ({ isAvailable: true })),
+ });
+ await expect(runOnce(deps)).resolves.toEqual({ ota: 'fetched', bundle: 'up-to-date' });
+ expect(deps.fetchUpdateAsync).toHaveBeenCalledOnce();
+ expect(deps.onForcedUpdate).not.toHaveBeenCalled();
+ });
+
it('强更(minVersion)→ forced 且回调一次', async () => {
const deps = makeDeps({ fetchLatest: vi.fn(async () => latestRecord({ minVersion: '2.0.0' })) });
const { bundle } = await runOnce(deps);