|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { ok, equal, match } from 'node:assert/strict'; |
| 4 | +import { rimraf } from 'rimraf'; |
| 5 | +import { PackumentClient } from '../client.js'; |
| 6 | + |
| 7 | +const fixtures = join(import.meta.dirname, 'fixtures'); |
| 8 | + |
| 9 | +describe('PackumentClient', () => { |
| 10 | + describe('URL construction', () => { |
| 11 | + it('should construct URL correctly for standard registry', async () => { |
| 12 | + const client = new PackumentClient({ |
| 13 | + origin: 'https://registry.npmjs.org', |
| 14 | + env: { |
| 15 | + RUNTIME: 'node', |
| 16 | + CACHE_DIR: fixtures |
| 17 | + } |
| 18 | + }); |
| 19 | + |
| 20 | + // Access the internal URL construction logic by checking origin |
| 21 | + const url = new URL(client.origin); |
| 22 | + const basePath = url.pathname.endsWith('/') ? url.pathname : url.pathname + '/'; |
| 23 | + url.pathname = basePath + encodeURIComponent('lodash'); |
| 24 | + |
| 25 | + equal(url.href, 'https://registry.npmjs.org/lodash'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should preserve path segments in origin URL', async () => { |
| 29 | + const client = new PackumentClient({ |
| 30 | + origin: 'https://packages.example.com/javascript', |
| 31 | + env: { |
| 32 | + RUNTIME: 'node', |
| 33 | + CACHE_DIR: fixtures |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + // Simulate the URL construction logic from client.request() |
| 38 | + const url = new URL(client.origin); |
| 39 | + const basePath = url.pathname.endsWith('/') ? url.pathname : url.pathname + '/'; |
| 40 | + url.pathname = basePath + encodeURIComponent('lodash'); |
| 41 | + |
| 42 | + equal(url.href, 'https://packages.example.com/javascript/lodash'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should preserve path with trailing slash', async () => { |
| 46 | + const client = new PackumentClient({ |
| 47 | + origin: 'https://packages.example.com/javascript/', |
| 48 | + env: { |
| 49 | + RUNTIME: 'node', |
| 50 | + CACHE_DIR: fixtures |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + const url = new URL(client.origin); |
| 55 | + const basePath = url.pathname.endsWith('/') ? url.pathname : url.pathname + '/'; |
| 56 | + url.pathname = basePath + encodeURIComponent('lodash'); |
| 57 | + |
| 58 | + equal(url.href, 'https://packages.example.com/javascript/lodash'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle scoped packages with path segments', async () => { |
| 62 | + const client = new PackumentClient({ |
| 63 | + origin: 'https://packages.example.com/javascript', |
| 64 | + env: { |
| 65 | + RUNTIME: 'node', |
| 66 | + CACHE_DIR: fixtures |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + const url = new URL(client.origin); |
| 71 | + const basePath = url.pathname.endsWith('/') ? url.pathname : url.pathname + '/'; |
| 72 | + url.pathname = basePath + encodeURIComponent('@babel/core'); |
| 73 | + |
| 74 | + equal(url.href, 'https://packages.example.com/javascript/%40babel%2Fcore'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle deep path segments', async () => { |
| 78 | + const client = new PackumentClient({ |
| 79 | + origin: 'https://registry.example.com/api/v2/npm', |
| 80 | + env: { |
| 81 | + RUNTIME: 'node', |
| 82 | + CACHE_DIR: fixtures |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + const url = new URL(client.origin); |
| 87 | + const basePath = url.pathname.endsWith('/') ? url.pathname : url.pathname + '/'; |
| 88 | + url.pathname = basePath + encodeURIComponent('express'); |
| 89 | + |
| 90 | + equal(url.href, 'https://registry.example.com/api/v2/npm/express'); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('request', () => { |
| 95 | + it('.request(lodash) returns a valid packument', async () => { |
| 96 | + const client = new PackumentClient({ |
| 97 | + origin: 'https://registry.npmjs.org', |
| 98 | + env: { |
| 99 | + RUNTIME: 'node', |
| 100 | + CACHE_DIR: fixtures |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + const entry = await client.request('lodash'); |
| 105 | + |
| 106 | + ok(entry, 'entry should exist'); |
| 107 | + ok(entry.body, 'entry should have body'); |
| 108 | + equal(entry.body.name, 'lodash'); |
| 109 | + ok(entry.body.versions, 'packument should have versions'); |
| 110 | + ok(Object.keys(entry.body.versions).length > 0, 'should have at least one version'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('.request(@babel/core) handles scoped packages', async () => { |
| 114 | + const client = new PackumentClient({ |
| 115 | + origin: 'https://registry.npmjs.org', |
| 116 | + env: { |
| 117 | + RUNTIME: 'node', |
| 118 | + CACHE_DIR: fixtures |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + const entry = await client.request('@babel/core'); |
| 123 | + |
| 124 | + ok(entry, 'entry should exist'); |
| 125 | + ok(entry.body, 'entry should have body'); |
| 126 | + equal(entry.body.name, '@babel/core'); |
| 127 | + ok(entry.body.versions, 'packument should have versions'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('.request(nonexistent-package-xyz-123) returns null for 404', async () => { |
| 131 | + const client = new PackumentClient({ |
| 132 | + origin: 'https://registry.npmjs.org', |
| 133 | + env: { |
| 134 | + RUNTIME: 'node', |
| 135 | + CACHE_DIR: fixtures |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + const entry = await client.request('nonexistent-package-xyz-123-definitely-not-real'); |
| 140 | + |
| 141 | + equal(entry, null, 'should return null for 404'); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + describe('requestAll', () => { |
| 146 | + it('.requestAll([...packages]) returns multiple packuments', async () => { |
| 147 | + const client = new PackumentClient({ |
| 148 | + origin: 'https://registry.npmjs.org', |
| 149 | + env: { |
| 150 | + RUNTIME: 'node', |
| 151 | + CACHE_DIR: fixtures |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + const entries = await client.requestAll(['debug', 'semver']); |
| 156 | + |
| 157 | + equal(entries.length, 2); |
| 158 | + for (const entry of entries) { |
| 159 | + ok(entry, 'entry should exist'); |
| 160 | + ok(entry.body, 'entry should have body'); |
| 161 | + ok(entry.body.versions, 'packument should have versions'); |
| 162 | + } |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('(cleanup) delete the local packument cache', async () => { |
| 167 | + await rimraf(join(fixtures, 'packuments'), { |
| 168 | + maxRetries: 1, |
| 169 | + retryDelay: 1 |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments