Files
xget/test/platforms/npm-fix.test.js
T
xixu-me 1979557fa9 Improve parsing, regex, and test consistency
Refines regex patterns for URL rewriting and authentication parsing, ensures parseInt uses radix 10 throughout, and updates test mocks and helpers for consistency. Also adds more global variables to ESLint config, improves error handling, and removes unused code in tests.
2025-12-08 19:19:10 +08:00

83 lines
2.3 KiB
JavaScript

import { describe, expect, it } from 'vitest';
describe('npm URL Rewriting Fix', () => {
it('should correctly rewrite npm registry URLs to preserve package names', () => {
const mockOriginalText = JSON.stringify({
name: 'npm',
versions: {
'11.5.1': {
dist: {
tarball: 'https://registry.npmjs.org/npm/-/npm-11.5.1.tgz'
}
}
}
});
// Simulate the regex replacement that happens in the code
const rewrittenText = mockOriginalText.replace(
/https:\/\/registry.npmjs.org\/([^/]+)/g,
'https://xget.xi-xu.me/npm/$1'
);
const rewrittenData = JSON.parse(rewrittenText);
// Verify the URL is correctly rewritten
expect(rewrittenData.versions['11.5.1'].dist.tarball).toBe(
'https://xget.xi-xu.me/npm/npm/-/npm-11.5.1.tgz'
);
});
it('should handle scoped packages correctly', () => {
const mockOriginalText = JSON.stringify({
name: '@types/node',
versions: {
'20.0.0': {
dist: {
tarball: 'https://registry.npmjs.org/@types/node/-/node-20.0.0.tgz'
}
}
}
});
const rewrittenText = mockOriginalText.replace(
/https:\/\/registry.npmjs.org\/([^/]+)/g,
'https://xget.xi-xu.me/npm/$1'
);
const rewrittenData = JSON.parse(rewrittenText);
expect(rewrittenData.versions['20.0.0'].dist.tarball).toBe(
'https://xget.xi-xu.me/npm/@types/node/-/node-20.0.0.tgz'
);
});
it('should handle multiple URLs in the same JSON response', () => {
const mockOriginalText = JSON.stringify({
dist: {
tarball: 'https://registry.npmjs.org/package1/-/package1-1.0.0.tgz'
},
dependencies: {
dep: {
dist: {
tarball: 'https://registry.npmjs.org/dep/-/dep-2.0.0.tgz'
}
}
}
});
const rewrittenText = mockOriginalText.replace(
/https:\/\/registry.npmjs.org\/([^/]+)/g,
'https://xget.xi-xu.me/npm/$1'
);
const rewrittenData = JSON.parse(rewrittenText);
expect(rewrittenData.dist.tarball).toBe(
'https://xget.xi-xu.me/npm/package1/-/package1-1.0.0.tgz'
);
expect(rewrittenData.dependencies.dep.dist.tarball).toBe(
'https://xget.xi-xu.me/npm/dep/-/dep-2.0.0.tgz'
);
});
});