Files

40 lines
1.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { compareVersions, normVersion } from '../src/shared/utils/version-compare.ts'
test('版本比较:线上高于本地时返回正数(应提示更新)', () => {
assert.ok(compareVersions('3.0.71', '3.0.70') > 0)
assert.ok(compareVersions('3.1.0', '3.0.71') > 0)
})
test('版本比较:线上低于本地时返回负数(不得提示更新)', () => {
// 现网真实故障场景:本地已升到 3.0.71,线上发布记录仍是 3.0.70
// 旧逻辑 local !== latest 会误报"发现新版本 v3.0.70"并诱导降级。
assert.ok(compareVersions('3.0.70', '3.0.71') < 0)
assert.ok(compareVersions('2.9.9', '3.0.0') < 0)
})
test('版本比较:完全相同(含 v 前缀、段数补齐)返回 0', () => {
assert.equal(compareVersions('3.0.71', '3.0.71'), 0)
assert.equal(compareVersions('v3.0.71', '3.0.71'), 0)
assert.equal(compareVersions('3.0.7', '3.0.7.0'), 0)
})
test('版本比较:按数字段而非字符串比较(3.0.9 < 3.0.10', () => {
assert.ok(compareVersions('3.0.9', '3.0.10') < 0)
assert.ok(compareVersions('3.0.10', '3.0.9') > 0)
})
test('版本比较:非数字段按 0 处理,不得产生 NaN 乱序', () => {
assert.equal(compareVersions('3.0.71-beta', '3.0.71'), 0)
assert.ok(compareVersions('3.0.72', '3.0.71-beta') > 0)
})
test('版本归一化:去空白与 v/V 前缀', () => {
assert.equal(normVersion(' v3.0.71 '), '3.0.71')
assert.equal(normVersion('V3.0.71'), '3.0.71')
assert.equal(normVersion(null), '')
assert.equal(normVersion(undefined), '')
})