fix: standardize version parsing and simplify string escaping

This commit is contained in:
手瓜一十雪 committed 2026-10-18 13:00:39 +08:00
1 parent 4e2b6b1200
commit 38a7c3da63
23 files changed
+245 -137

No files matched your search

@@ -60,6 +60,12 @@ describe('renderTemplate — mechanical {key} substitution', () => {
it('does NOT escape when template is plain text', () => {
expect(renderTemplate('{val}', { val: 'a"b\\c' })).toBe('a"b\\c');
});
it('preserves control characters as JSON string escapes', () => {
const value = 'line\nnext\r\t\b\f\0"\\';
const rendered = renderTemplate('{"text":"{value}"}', { value });
expect(JSON.parse(rendered)).toEqual({ text: value });
});
});
describe('normalizeNotificationsConfig — total normalize', () => {
+25
View File
@@ -27,6 +27,31 @@ afterEach(async () => {
});
describe('staged updates', () => {
it.each(['02.0.0', '2.0.0-rc.01', '2.0.0-rc_1', '2.0.0+build..1'])(
'rejects invalid manifest versions before replacing files: %s',
async (version) => {
const payload = path.join(staging, 'pending/payload');
const manifest = await readManifest(payload);
await fs.writeFile(
path.join(payload, 'release.json'),
JSON.stringify({ ...manifest, version }),
);
await expect(applyPendingUpdate(root)).rejects.toThrow('清单无效');
expect((await readManifest(root)).version).toBe('1.0.0');
},
);
it('accepts prerelease identifiers and build metadata in manifests', async () => {
const payload = path.join(staging, 'pending/payload');
const manifest = await readManifest(payload);
const version = '2.0.0-rc.10+build.01';
await fs.writeFile(
path.join(payload, 'release.json'),
JSON.stringify({ ...manifest, version }),
);
expect((await readManifest(payload)).version).toBe(version);
});
it('stages outside the installation and only renames replacements on the destination filesystem', async () => {
expect(path.dirname(staging)).toBe(os.tmpdir());
expect(staging.startsWith(root + path.sep)).toBe(false);
+30 -12
View File
@@ -71,18 +71,36 @@ describe('update service', () => {
await vi.waitFor(() => expect(service.status().phase).toBe('error'));
});
it.each(['1.0.0', '0.9.0', '2.0.0-rc.1', '../../outside', null])(
'rejects invalid or older version %s',
async (version) => {
const download = vi.fn();
const service = await createUpdateService(root, '1.0.0', {
download,
isContainer: false,
});
expect(() => service.start(version)).toThrow('正式版本');
expect(download).not.toHaveBeenCalled();
},
);
it.each([
'1.0.0',
'0.9.0',
'2.0.0-rc.1',
'02.0.0',
'2.0.0\n',
'1.0.0+build.2',
'../../outside',
null,
])('rejects invalid or older version %s', async (version) => {
const download = vi.fn();
const service = await createUpdateService(root, '1.0.0', {
download,
isContainer: false,
});
expect(() => service.start(version)).toThrow('正式版本');
expect(download).not.toHaveBeenCalled();
});
it('downloads stable versions with build metadata without dropping the tag suffix', async () => {
const download = vi.fn(async (directory: string, version: string, target: string) => {
await releaseFixture(path.join(directory, 'payload'), version, target);
await writeFile(path.join(directory, 'archive'), 'archive');
});
const service = await createUpdateService(root, '1.0.0', { download, isContainer: false });
service.start('2.0.0+build.01');
await vi.waitFor(() => expect(service.status().phase).toBe('ready'));
expect(download).toHaveBeenCalledWith(expect.any(String), '2.0.0+build.01', 'win32-x64');
expect(service.status()).toMatchObject({ version: '2.0.0+build.01' });
});
it('does not enable updates for a source checkout', async () => {
const service = await createUpdateService(undefined, '1.0.0');
+21 -30
View File
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { compareVersions, getUpdateInfo } from '../src/update/check';
import { getUpdateInfo } from '../src/update/check';
const LATEST_RELEASE_URL = 'https://api.github.com/repos/SnowLuma/SnowLuma/releases/latest';
const NOTES_MAX = 4_000;
@@ -46,35 +46,6 @@ afterEach(() => {
vi.mock('@snowluma/common/version', () => ({ APP_VERSION: '0.0.0' }));
describe('compareVersions', () => {
it.each([
['1.2.3', '1.2.3', 0],
['v1.2.3', '1.2.3', 0],
['1.2', '1.2.0', 0],
['1.2.3.9', '1.2.3.1', 0],
['1.2.x', '1.2.0', 0],
['', '0.0.0', 0],
// Only a leading lowercase `v` is stripped; `V1.2.3` parses as 0.2.3.
['V1.2.3', '0.0.0', 2],
['V1.2.3', '0.2.3', 0],
['1.2.4', '1.2.3', 1],
['1.2.3', '1.2.4', -1],
['1.10.0', '1.9.0', 1],
['2.0.0', '1.9.9', 1],
['3.0.0', '1.0.0', 2],
['1.0.0', '3.0.0', -2],
['1.2.3', '1.2.3-rc.1', 1],
['1.2.3-rc.1', '1.2.3', -1],
['1.2.3-rc.1', '1.2.3-rc.1', 0],
['1.2.3-rc.1', '1.2.3-rc.2', -1],
['1.2.3-rc.2', '1.2.3-rc.1', 1],
['1.2.3-rc.10', '1.2.3-rc.2', -1],
['2.0.0-rc.1', '1.9.9', 1],
] as const)('compareVersions(%j, %j) === %s', (a, b, expected) => {
expect(compareVersions(a, b)).toBe(expected);
});
});
describe('getUpdateInfo', () => {
it.each(['0', 'false', 'off', 'no', 'FALSE', ' Off ', 'No'] as const)(
'skips the GitHub request when SNOWLUMA_UPDATE_CHECK=%j',
@@ -234,6 +205,26 @@ describe('getUpdateInfo', () => {
},
);
it.each(['v01.2.3', 'v1.2.x', 'v1.2.3.4', 'v1.2.3-rc.01', 'v1.2.3+build..1'])(
'reports an invalid release tag without offering an update: %s',
async (tag) => {
mockFetch(async () => jsonResponse({ tag_name: tag }));
expect(await getUpdateInfo(true)).toMatchObject({
latest: null,
hasUpdate: false,
error: 'invalid release version',
});
},
);
it('preserves build metadata in release tags', async () => {
mockFetch(async () => jsonResponse({ tag_name: 'v1.2.3+build.01' }));
expect(await getUpdateInfo(true)).toMatchObject({
latest: '1.2.3+build.01',
hasUpdate: true,
});
});
it('uses the Error message when fetch throws an Error', async () => {
mockFetch(async () => {
throw new Error('getaddrinfo ENOTFOUND');