fix(ota): tighten release compatibility checks

This commit is contained in:
DIYgod
2026-04-10 18:57:07 +08:00
parent 7be0afa80d
commit fb0ba03f2e
3 changed files with 77 additions and 14 deletions
+75 -9
View File
@@ -3,6 +3,17 @@ import { describe, expect, it } from "vitest"
import type { OtaRelease } from "../lib/schema"
import { compareSemver, selectLatestCompatibleRelease } from "../lib/version"
function createPlatform(path: string, sha256Seed: string) {
return {
launchAsset: {
path,
sha256: sha256Seed.repeat(64),
contentType: "application/javascript",
},
assets: [],
}
}
function createRelease(overrides: Partial<OtaRelease> = {}): OtaRelease {
return {
schemaVersion: 1,
@@ -22,14 +33,7 @@ function createRelease(overrides: Partial<OtaRelease> = {}): OtaRelease {
message: null,
},
platforms: {
ios: {
launchAsset: {
path: "bundles/ios-main.js",
sha256: "a".repeat(64),
contentType: "application/javascript",
},
assets: [],
},
ios: createPlatform("bundles/ios-main.js", "a"),
},
...overrides,
}
@@ -44,7 +48,7 @@ describe("compareSemver", () => {
})
describe("selectLatestCompatibleRelease", () => {
it("selects the highest OTA release for the same runtime", () => {
it("selects the required older release when a newer release has an incompatible runtime", () => {
const result = selectLatestCompatibleRelease(
[
createRelease({
@@ -73,4 +77,66 @@ describe("selectLatestCompatibleRelease", () => {
expect(result?.releaseVersion).toBe("0.4.2")
})
it("selects the highest compatible OTA release among multiple candidates", () => {
const result = selectLatestCompatibleRelease(
[
createRelease({
releaseVersion: "0.4.2",
git: {
tag: "mobile/v0.4.2",
commit: "abcdef1234567890",
},
}),
createRelease({
releaseVersion: "0.4.3",
git: {
tag: "mobile/v0.4.3",
commit: "bcdef1234567890a",
},
}),
],
{
product: "mobile",
channel: "production",
runtimeVersion: "0.4.1",
platform: "ios",
},
)
expect(result?.releaseVersion).toBe("0.4.3")
})
it("returns null when no matching OTA release has a payload for the requested platform", () => {
const result = selectLatestCompatibleRelease(
[
createRelease({
releaseVersion: "0.4.2",
platforms: {
ios: undefined,
android: createPlatform("bundles/android-main.js", "b"),
},
}),
createRelease({
releaseVersion: "0.4.3",
releaseKind: "store",
platforms: {
ios: createPlatform("bundles/ios-store.js", "c"),
},
git: {
tag: "mobile/v0.4.3",
commit: "bcdef1234567890a",
},
}),
],
{
product: "mobile",
channel: "production",
runtimeVersion: "0.4.1",
platform: "ios",
},
)
expect(result).toBeNull()
})
})
+1 -4
View File
@@ -2,9 +2,6 @@ import { z } from "zod"
const semver = z.string().regex(/^\d+\.\d+\.\d+$/)
const sha256 = z.string().regex(/^[a-f0-9]{64}$/)
const otaPlatforms = ["ios", "android", "macos", "windows", "linux"] as const
export const otaPlatformSchema = z.enum(otaPlatforms)
const assetSchema = z.object({
path: z.string().min(1),
@@ -50,5 +47,5 @@ export const otaReleaseSchema = z.object({
platforms: platformsSchema,
})
export type OtaPlatform = z.infer<typeof otaPlatformSchema>
export type OtaPlatform = "ios" | "android" | "macos" | "windows" | "linux"
export type OtaRelease = z.infer<typeof otaReleaseSchema>
+1 -1
View File
@@ -29,7 +29,7 @@ export function selectLatestCompatibleRelease(
.filter((release) => release.channel === input.channel)
.filter((release) => release.releaseKind === "ota")
.filter((release) => release.runtimeVersion === input.runtimeVersion)
.filter((release) => Object.prototype.hasOwnProperty.call(release.platforms, input.platform))
.filter((release) => Boolean(release.platforms[input.platform]))
.sort((left, right) => compareSemver(right.releaseVersion, left.releaseVersion))[0] ?? null
)
}