qwen-code/scripts/tests/get-release-version.test.js
qwen-code-dev-bot 955ad27fc7
fix(scripts): handle missing NPM dist-tags gracefully in release versioning (#6476) (#6481)
* fix(scripts): handle missing NPM dist-tags gracefully in release versioning (#6476)

getAndVerifyTags now returns null instead of throwing when no baseline
version exists on NPM. getPreviewVersion and getStableVersion fall back
to the package.json base version, matching the pattern already used by
getNightlyVersion. This prevents the release workflow from failing when
no nightly or preview dist-tag has been published yet.

* fix(scripts): harden release versioning against transient NPM errors and missing dist-tags (#6476)

- Distinguish 404 from transient errors in getVersionFromNPM so NPM
  outages halt the release instead of silently falling back
- Consult getAllVersionsFromNPM when dist-tag is missing to derive
  baseline from published versions rather than returning empty
- Add console.error logging when getAndVerifyTags returns null
- Validate package.json fallback version in getStableVersion and
  getPreviewVersion
- Add tests for promote-nightly/patch throw paths, true greenfield
  scenario, versions-list derivation, and transient error propagation

* fix(scripts): propagate transient NPM errors in getAllVersionsFromNPM (#6476)

getAllVersionsFromNPM silently swallowed all errors including transient
network failures (ETIMEDOUT, ECONNRESET), which became load-bearing now
that the missing-dist-tag fallback depends on it. Match the same 404-only
pattern already used by getVersionFromNPM. Also fix a DRY violation in
getPreviewVersion, correct misleading test comments, and add test
coverage for the versions-list error path and latest filter branch.

* fix(scripts): tighten 404 detection and tolerate transient versions-list failures (#6476)

- Remove redundant '404' substring check; E404 is the canonical npm error code
  and bare '404' could false-match unrelated errors (port 4043, E4040)
- Catch transient versions-list errors in detectRollbackAndGetBaseline when
  distTagVersion is already resolved, avoiding hard-blocking a release when
  rollback detection is merely a safety net
- Update and add tests for the new fallback behavior and deprecated-versions path

* fix(scripts): guard release version fallback edge cases

* test(scripts): cover greenfield versions list E404

---------

Co-authored-by: Qwen Code Autofix <qwen-code-bot@alibabacloud.com>
Co-authored-by: qwen-code[bot] <qwen-code[bot]@users.noreply.github.com>
Co-authored-by: Qwen Code Autofix <qwen-code@autofix.bot>
Co-authored-by: yiliang114 <effortyiliang@gmail.com>
2026-07-08 12:21:14 +00:00

539 lines
20 KiB
JavaScript

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { vi, describe, it, expect, beforeEach } from 'vitest';
import { getVersion } from '../get-release-version.js';
import { execSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
vi.mock('node:child_process');
vi.mock('node:fs');
describe('getVersion', () => {
beforeEach(() => {
vi.resetAllMocks();
vi.setSystemTime(new Date('2025-09-17T00:00:00.000Z'));
// Mock package.json being read by getNightlyVersion
vi.mocked(readFileSync).mockReturnValue(
JSON.stringify({ version: '0.8.0' }),
);
});
// This is the base mock for a clean state with no conflicts or rollbacks
const mockExecSync = (command) => {
// NPM dist-tags
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.6.1';
if (command.includes('npm view') && command.includes('--tag=preview'))
return '0.7.0-preview.1';
if (command.includes('npm view') && command.includes('--tag=nightly'))
return '0.8.0-nightly.20250916.abcdef';
// NPM versions list
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.6.0',
'0.6.1',
'0.7.0-preview.0',
'0.7.0-preview.1',
'0.8.0-nightly.20250916.abcdef',
]);
// Deprecation checks (default to not deprecated)
if (command.includes('deprecated')) return '';
// Git Tag Mocks
if (command.includes("git tag -l 'v[0-9].[0-9].[0-9]'")) return 'v0.6.1';
if (command.includes("git tag -l 'v*-preview*'")) return 'v0.7.0-preview.1';
if (command.includes("git tag -l 'v*-nightly*'"))
return 'v0.8.0-nightly.20250916.abcdef';
// Git Hash Mock
if (command.includes('git rev-parse --short HEAD')) return 'd3bf8a3d';
// For doesVersionExist checks - default to not found
if (
command.includes('npm view') &&
command.includes('@qwen-code/qwen-code@')
) {
throw new Error('NPM version not found');
}
if (command.includes('git tag -l')) return '';
if (command.includes('gh release view')) {
throw new Error('GH release not found');
}
return '';
};
describe('Happy Path - Version Calculation', () => {
it('should calculate the next stable version from the latest preview', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'stable' });
expect(result.releaseVersion).toBe('0.7.0');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
it('should calculate the next preview version from the latest nightly', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'preview' });
expect(result.releaseVersion).toBe('0.8.0-preview.0');
expect(result.npmTag).toBe('preview');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
it('should calculate the next nightly version from package.json', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'nightly' });
// Note: The base version now comes from package.json, not the previous nightly tag.
expect(result.releaseVersion).toBe('0.8.0-nightly.20250917.d3bf8a3d');
expect(result.npmTag).toBe('nightly');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
it('should calculate the next patch version for a stable release', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'patch', 'patch-from': 'stable' });
expect(result.releaseVersion).toBe('0.6.2');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
it('should calculate the next patch version for a preview release', () => {
vi.mocked(execSync).mockImplementation(mockExecSync);
const result = getVersion({ type: 'patch', 'patch-from': 'preview' });
expect(result.releaseVersion).toBe('0.7.0-preview.2');
expect(result.npmTag).toBe('preview');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
});
describe('Advanced Scenarios', () => {
it('should ignore a deprecated version and use the next highest', () => {
const mockWithDeprecated = (command) => {
// The highest nightly is 0.9.0, but it's deprecated
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.8.0-nightly.20250916.abcdef',
'0.9.0-nightly.20250917.deprecated', // This one is deprecated
]);
// Mock the deprecation check
if (
command.includes(
'npm view @qwen-code/qwen-code@0.9.0-nightly.20250917.deprecated deprecated',
)
)
return 'This version is deprecated';
// The dist-tag still points to the older, valid version
if (command.includes('npm view') && command.includes('--tag=nightly'))
return '0.8.0-nightly.20250916.abcdef';
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithDeprecated);
const result = getVersion({ type: 'preview' });
// It should base the preview off 0.8.0, not the deprecated 0.9.0
expect(result.releaseVersion).toBe('0.8.0-preview.0');
});
it('should auto-increment patch version if the calculated one already exists', () => {
const mockWithConflict = (command) => {
// The calculated version 0.7.0 already exists as a git tag
if (command.includes("git tag -l 'v0.7.0'")) return 'v0.7.0';
// The next version, 0.7.1, is available
if (command.includes("git tag -l 'v0.7.1'")) return '';
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithConflict);
const result = getVersion({ type: 'stable' });
// Should have skipped 0.7.0 and landed on 0.7.1
expect(result.releaseVersion).toBe('0.7.1');
});
it('should auto-increment preview number if the calculated one already exists', () => {
const mockWithConflict = (command) => {
// The calculated preview 0.8.0-preview.0 already exists on NPM
if (
command.includes(
'npm view @qwen-code/qwen-code@0.8.0-preview.0 version',
)
)
return '0.8.0-preview.0';
// The next one is available
if (
command.includes(
'npm view @qwen-code/qwen-code@0.8.0-preview.1 version',
)
)
throw new Error('Not found');
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithConflict);
const result = getVersion({ type: 'preview' });
// Should have skipped preview.0 and landed on preview.1
expect(result.releaseVersion).toBe('0.8.0-preview.1');
});
it('should fall back to package.json when no nightly dist-tag exists (preview)', () => {
const mockWithNoNightly = (command) => {
// No nightly dist-tag exists
if (command.includes('npm view') && command.includes('--tag=nightly')) {
throw new Error('npm error code E404');
}
// Stable versions exist but no nightly dist-tag
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify(['0.6.0', '0.6.1']);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithNoNightly);
const result = getVersion({ type: 'preview' });
// Should fall back to package.json version (0.8.0) + -preview.0
expect(result.releaseVersion).toBe('0.8.0-preview.0');
expect(result.npmTag).toBe('preview');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
it('should fall back to package.json when no preview dist-tag exists (stable)', () => {
const mockWithNoPreview = (command) => {
// No preview dist-tag exists
if (command.includes('npm view') && command.includes('--tag=preview')) {
throw new Error('npm error code E404');
}
// Stable versions exist but no preview dist-tag
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify(['0.6.0', '0.6.1']);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithNoPreview);
const result = getVersion({ type: 'stable' });
// Should fall back to package.json version (0.8.0)
expect(result.releaseVersion).toBe('0.8.0');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
it('should throw when no nightly dist-tag exists (promote-nightly)', () => {
const mockWithNoNightly = (command) => {
if (command.includes('npm view') && command.includes('--tag=nightly')) {
throw new Error('npm error code E404');
}
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify(['0.6.0', '0.6.1']);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithNoNightly);
expect(() => getVersion({ type: 'promote-nightly' })).toThrow(
'Unable to determine baseline version for nightly',
);
});
it('should throw when no dist-tag exists (patch)', () => {
const mockWithNoLatest = (command) => {
if (command.includes('npm view') && command.includes('--tag=latest')) {
throw new Error('npm error code E404');
}
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([]);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithNoLatest);
expect(() =>
getVersion({ type: 'patch', 'patch-from': 'stable' }),
).toThrow('Unable to determine baseline version for latest');
});
it('should fall back to package.json in true greenfield scenario (all dist-tags missing)', () => {
const mockGreenfield = (command) => {
if (
command.includes('npm view') &&
command.includes('--tag=') &&
!command.includes('versions --json')
) {
throw new Error('npm error code E404');
}
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([]);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockGreenfield);
const result = getVersion({ type: 'stable' });
expect(result.releaseVersion).toBe('0.8.0');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe('');
});
it('should handle E404 from versions list in true greenfield scenario', () => {
const mockGreenfieldVersionsE404 = (command) => {
if (command.includes('npm view') && command.includes('versions --json'))
throw new Error('npm error code E404');
if (
command.includes('npm view') &&
command.includes('--tag=') &&
!command.includes('versions --json')
) {
throw new Error('npm error code E404');
}
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockGreenfieldVersionsE404);
const result = getVersion({ type: 'stable' });
expect(result.releaseVersion).toBe('0.8.0');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe('');
});
it('should derive baseline from versions list when dist-tag is missing but versions exist', () => {
const mockWithVersionsButNoTag = (command) => {
if (
command.includes('npm view') &&
command.includes('--tag=preview') &&
!command.includes('versions --json')
) {
throw new Error('npm error code E404');
}
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.6.0',
'0.6.1',
'0.7.0-preview.0',
'0.7.0-preview.3',
]);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithVersionsButNoTag);
const result = getVersion({ type: 'stable' });
expect(result.releaseVersion).toBe('0.7.0');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
it('should propagate transient NPM errors from dist-tag lookup', () => {
const mockWithTransientError = (command) => {
if (
command.includes('npm view') &&
command.includes('--tag=preview') &&
!command.includes('versions --json')
) {
throw new Error('npm error code ETIMEDOUT');
}
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithTransientError);
expect(() => getVersion({ type: 'stable' })).toThrow('ETIMEDOUT');
});
it('should fall back to dist-tag when versions list lookup fails transiently', () => {
const mockWithTransientVersionsError = (command) => {
if (
command.includes('npm view') &&
command.includes('versions --json')
) {
throw new Error('npm error code ECONNRESET');
}
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithTransientVersionsError);
const result = getVersion({ type: 'stable' });
expect(result.releaseVersion).toBe('0.7.0');
expect(result.npmTag).toBe('latest');
});
it('should propagate transient NPM errors from versions list when dist-tag is also missing', () => {
const mockWithBothFailing = (command) => {
if (
command.includes('npm view') &&
command.includes('--tag=preview') &&
!command.includes('versions --json')
) {
throw new Error('npm error code E404');
}
if (
command.includes('npm view') &&
command.includes('versions --json')
) {
throw new Error('npm error code ECONNRESET');
}
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithBothFailing);
expect(() => getVersion({ type: 'stable' })).toThrow('ECONNRESET');
});
it('should derive baseline from latest versions when latest dist-tag is missing', () => {
const mockWithNoLatestTag = (command) => {
if (
command.includes('npm view') &&
command.includes('--tag=latest') &&
!command.includes('versions --json')
) {
throw new Error('npm error code E404');
}
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify(['0.5.0', '0.6.0', '0.6.1', '0.7.0-preview.0']);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithNoLatestTag);
const result = getVersion({ type: 'patch', 'patch-from': 'stable' });
expect(result.releaseVersion).toBe('0.6.2');
expect(result.npmTag).toBe('latest');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
it('should fall back to package.json when all matching versions are deprecated (no dist-tag)', () => {
const mockWithAllDeprecated = (command) => {
if (
command.includes('npm view') &&
command.includes('--tag=nightly') &&
!command.includes('versions --json')
) {
throw new Error('npm error code E404');
}
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify(['0.7.0-nightly.1', '0.7.0-nightly.2']);
if (command.includes('deprecated')) return 'Deprecated';
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithAllDeprecated);
const result = getVersion({ type: 'preview' });
expect(result.releaseVersion).toBe('0.8.0-preview.0');
});
it('should throw when no preview dist-tag exists (patch)', () => {
const mockWithNoPreview = (command) => {
if (
command.includes('npm view') &&
command.includes('--tag=preview') &&
!command.includes('versions --json')
) {
throw new Error('npm error code E404');
}
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([]);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithNoPreview);
expect(() =>
getVersion({ type: 'patch', 'patch-from': 'preview' }),
).toThrow('Unable to determine baseline version for preview');
});
it('should derive preview from nightly versions when nightly dist-tag is missing', () => {
const mockWithNightliesButNoTag = (command) => {
if (
command.includes('npm view') &&
command.includes('--tag=nightly') &&
!command.includes('versions --json')
) {
throw new Error('npm error code E404');
}
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.6.0',
'0.6.1',
'0.8.0-nightly.20250916.abcdef',
]);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithNightliesButNoTag);
const result = getVersion({ type: 'preview' });
expect(result.releaseVersion).toBe('0.8.0-preview.0');
expect(result.npmTag).toBe('preview');
expect(result.previousReleaseTag).toBe('v0.6.1');
});
it('should reject an invalid stable version derived from preview', () => {
const mockWithInvalidPreview = (command) => {
if (command.includes('npm view') && command.includes('--tag=preview'))
return 'invalid-preview.0';
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([]);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithInvalidPreview);
expect(() => getVersion({ type: 'stable' })).toThrow(
'Invalid derived from preview dist-tag: invalid',
);
});
it('should reject an invalid preview version derived from nightly', () => {
const mockWithInvalidNightly = (command) => {
if (command.includes('npm view') && command.includes('--tag=nightly'))
return 'invalid-nightly.20250916.abcdef';
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([]);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithInvalidNightly);
expect(() => getVersion({ type: 'preview' })).toThrow(
'Invalid derived from nightly dist-tag: invalid-preview.0',
);
});
it('should reject a stable release derived below the published latest version', () => {
const mockWithOlderPreviewVersions = (command) => {
if (
command.includes('npm view') &&
command.includes('--tag=preview') &&
!command.includes('versions --json')
) {
throw new Error('npm error code E404');
}
if (command.includes('npm view') && command.includes('--tag=latest'))
return '0.9.0';
if (command.includes('npm view') && command.includes('versions --json'))
return JSON.stringify([
'0.7.0-preview.0',
'0.7.0-preview.1',
'0.9.0',
]);
return mockExecSync(command);
};
vi.mocked(execSync).mockImplementation(mockWithOlderPreviewVersions);
expect(() => getVersion({ type: 'stable' })).toThrow(
'Derived stable version 0.7.0 is lower than published latest 0.9.0',
);
});
});
});