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