synopkg 14.0.1

Consistent dependency versions in large JavaScript Monorepos
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use crate::specifier::Specifier;

#[test]
fn same_version_different_ranges() {
  let cases: Vec<(&str, &str)> = vec![
    // Same exact version, different range operators
    ("1.4.1", "^1.4.1"),
    ("1.4.1", "~1.4.1"),
    ("1.4.1", ">=1.4.1"),
    ("1.4.1", ">1.4.1"),
    ("1.4.1", "<=1.4.1"),
    ("1.4.1", "<1.4.1"),
    // Different range operators, same version
    ("^1.4.1", "~1.4.1"),
    ("^1.4.1", ">=1.4.1"),
    ("~1.4.1", ">=1.4.1"),
    (">1.4.1", "<=1.4.1"),
    // With prerelease versions
    ("1.4.1-alpha.1", "^1.4.1-alpha.1"),
    ("1.4.1-beta.2", "~1.4.1-beta.2"),
    ("^1.4.1-rc.3", ">=1.4.1-rc.3"),
  ];
  for (a, b) in cases {
    assert!(
      Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to have same version number"
    );
    // Test symmetry
    assert!(
      Specifier::new(b).has_same_version_number_as(&Specifier::new(a)),
      "Expected {b} and {a} to have same version number (symmetry check)"
    );
  }
}

#[test]
fn different_versions() {
  let cases: Vec<(&str, &str)> = vec![
    // Different patch versions
    ("1.4.1", "1.4.2"),
    ("^1.4.1", "^1.4.2"),
    ("~1.4.1", "~1.4.2"),
    // Different minor versions
    ("1.4.1", "1.5.1"),
    ("^1.4.1", "^1.5.1"),
    // Different major versions
    ("1.4.1", "2.4.1"),
    ("^1.4.1", "^2.4.1"),
    // Different prerelease versions (same base, different prerelease)
    ("1.4.1-alpha.1", "1.4.1-alpha.2"),
    ("1.4.1-alpha.1", "1.4.1-beta.1"),
    ("^1.4.1-alpha.1", "^1.4.1-alpha.2"),
    // Different base versions with prerelease
    ("1.4.1-alpha.1", "1.4.2-alpha.1"),
    ("1.4.1", "1.4.1-alpha.1"),
    // Completely different
    ("1.0.0", "2.0.0"),
    ("^1.2.3", "~4.5.6"),
  ];
  for (a, b) in cases {
    assert!(
      !Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to have different version numbers"
    );
    // Test symmetry
    assert!(
      !Specifier::new(b).has_same_version_number_as(&Specifier::new(a)),
      "Expected {b} and {a} to have different version numbers (symmetry check)"
    );
  }
}

#[test]
fn with_workspace_protocol() {
  // workspace: protocol WITH embedded versions DO have node_version and should
  // match
  let cases_same: Vec<(&str, &str)> = vec![
    // workspace vs workspace (same version)
    ("workspace:1.4.1", "workspace:1.4.1"),
    ("workspace:^1.4.1", "workspace:~1.4.1"),
    ("workspace:1.4.1", "workspace:^1.4.1"),
    // workspace vs regular (same version)
    ("workspace:1.4.1", "1.4.1"),
    ("workspace:^1.4.1", "^1.4.1"),
    ("workspace:~1.4.1", "~1.4.1"),
  ];
  for (a, b) in cases_same {
    assert!(
      Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to have same version number"
    );
  }

  // Identical unresolved workspace protocols SHOULD match (after fix)
  let cases_identical_unresolved: Vec<(&str, &str)> = vec![
    ("workspace:*", "workspace:*"),
    ("workspace:^", "workspace:^"),
    ("workspace:~", "workspace:~"),
  ];
  for (a, b) in cases_identical_unresolved {
    assert!(
      Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to have same version number (identical unresolved workspace protocols)"
    );
  }

  // Different unresolved workspace protocols should NOT match
  let cases_false: Vec<(&str, &str)> = vec![
    ("workspace:*", "workspace:^"),
    ("workspace:*", "workspace:~"),
    ("workspace:^", "workspace:~"),
    ("workspace:*", "1.4.1"),
    ("workspace:^", "^1.4.1"),
    ("workspace:~", "~1.4.1"),
  ];
  for (a, b) in cases_false {
    assert!(
      !Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to return false (different or incomparable)"
    );
  }
}

#[test]
fn with_npm_alias() {
  let cases_same: Vec<(&str, &str)> = vec![
    // Same version, different aliases
    ("npm:foo@1.4.1", "npm:bar@1.4.1"),
    ("npm:foo@^1.4.1", "npm:bar@~1.4.1"),
    ("npm:@scope/pkg@1.4.1", "npm:@other/pkg@1.4.1"),
    // Alias vs regular
    ("npm:foo@1.4.1", "1.4.1"),
    ("npm:foo@^1.4.1", "^1.4.1"),
    ("npm:@scope/pkg@~1.4.1", "~1.4.1"),
    // With prerelease
    ("npm:foo@1.4.1-alpha.1", "npm:bar@1.4.1-alpha.1"),
    ("npm:foo@^1.4.1-beta", "^1.4.1-beta"),
  ];
  for (a, b) in cases_same {
    assert!(
      Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to have same version number"
    );
  }

  let cases_different: Vec<(&str, &str)> = vec![
    // Different versions
    ("npm:foo@1.4.1", "npm:foo@1.4.2"),
    ("npm:foo@^1.4.1", "npm:foo@^1.5.1"),
    ("npm:@scope/pkg@1.4.1", "npm:@scope/pkg@2.4.1"),
    // Alias without version (no node_version)
    ("npm:foo", "npm:bar"),
    ("npm:foo", "1.4.1"),
    ("npm:foo@1.4.1", "npm:bar"),
  ];
  for (a, b) in cases_different {
    assert!(
      !Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to have different version numbers or no node_version"
    );
  }
}

#[test]
fn with_git_urls() {
  let cases_same: Vec<(&str, &str)> = vec![
    // Same version, same or different repos
    ("git@github.com:npm/cli.git#1.4.1", "git@github.com:npm/cli.git#1.4.1"),
    ("git@github.com:npm/cli.git#^1.4.1", "git@github.com:npm/cli.git#~1.4.1"),
    ("git@github.com:npm/cli.git#1.4.1", "git@github.com:other/pkg.git#1.4.1"),
    (
      "git+ssh://git@github.com/npm/cli#^1.4.1",
      "git+ssh://git@github.com/npm/cli#>=1.4.1",
    ),
    ("github:user/repo#1.4.1", "github:other/repo#1.4.1"),
    // Git vs regular
    ("git@github.com:npm/cli.git#1.4.1", "1.4.1"),
    ("git@github.com:npm/cli.git#^1.4.1", "^1.4.1"),
    ("github:user/repo#~1.4.1", "~1.4.1"),
    // With prerelease
    (
      "git@github.com:npm/cli.git#1.4.1-alpha.1",
      "git@github.com:npm/cli.git#1.4.1-alpha.1",
    ),
    ("git@github.com:npm/cli.git#^1.4.1-beta", "^1.4.1-beta"),
  ];
  for (a, b) in cases_same {
    assert!(
      Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to have same version number"
    );
  }

  let cases_different: Vec<(&str, &str)> = vec![
    // Different versions
    ("git@github.com:npm/cli.git#1.4.1", "git@github.com:npm/cli.git#1.4.2"),
    ("git@github.com:npm/cli.git#^1.4.1", "git@github.com:npm/cli.git#^1.5.1"),
    ("github:user/repo#1.4.1", "github:user/repo#2.4.1"),
    // Git without semver tag (no node_version)
    ("git@github.com:npm/cli.git", "git@github.com:npm/cli.git#main"),
    ("git@github.com:npm/cli.git#main", "git@github.com:npm/cli.git#develop"),
    ("git@github.com:npm/cli.git", "1.4.1"),
    ("git@github.com:npm/cli.git#main", "^1.4.1"),
  ];
  for (a, b) in cases_different {
    assert!(
      !Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to have different version numbers or no node_version"
    );
  }
}

#[test]
fn major_and_minor_variants() {
  // Major and Minor variants have padded node_versions (e.g., "1" ->
  // "1.999999.999999") So they should NOT match non-padded exact versions
  let cases_different: Vec<(&str, &str)> = vec![
    // Major vs exact (different versions after padding)
    ("1", "1.0.0"),
    ("1", "1.4.1"),
    // Major vs range (different versions)
    ("1", "^1.0.0"),
    ("^1", "^1.0.0"),
    // Minor vs exact (different versions after padding)
    ("1.4", "1.4.0"),
    ("1.4", "1.4.1"),
    // Minor vs range (different versions)
    ("1.4", "^1.4.0"),
    ("^1.4", "^1.4.0"),
    // Major vs minor (different versions)
    ("1", "1.4"),
    ("^1", "^1.4"),
  ];
  for (a, b) in cases_different {
    assert!(
      !Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to have different version numbers (padded variants)"
    );
  }

  let cases_same: Vec<(&str, &str)> = vec![
    // Same major variants (both pad to same value)
    ("1", "^1"),
    ("1", "~1"),
    ("^1", "~1"),
    // Same minor variants (both pad to same value)
    ("1.4", "^1.4"),
    ("1.4", "~1.4"),
    ("^1.4", "~1.4"),
    // Major variant matches explicit padded version
    ("1", "1.999999.999999"),
    ("^1", "^1.999999.999999"),
    // Minor variant matches explicit padded version
    ("1.4", "1.4.999999"),
    ("^1.4", "^1.4.999999"),
  ];
  for (a, b) in cases_same {
    assert!(
      Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to have same version number (same padded values)"
    );
  }
}

#[test]
fn specifiers_without_node_version() {
  let cases: Vec<(&str, &str)> = vec![
    // Tags
    ("latest", "next"),
    ("latest", "canary"),
    ("next", "beta"),
    ("latest", "1.4.1"),
    // URLs
    ("https://example.com/package.tgz", "https://other.com/pkg.tgz"),
    ("http://example.com/package.tgz", "1.4.1"),
    // File protocols
    ("file:../foo", "file:../bar"),
    ("file:./local", "1.4.1"),
    // Complex semver ranges
    (">=1.2.3 <2.0.0", ">=2.0.0 <3.0.0"),
    ("^1.2.3 || ^2.0.0", "^3.0.0 || ^4.0.0"),
    (">=1.2.3 <2.0.0", "1.4.1"),
    // Workspace protocol range-only
    ("workspace:*", "workspace:^"),
    ("workspace:~", "workspace:*"),
    // Npm alias without version
    ("npm:foo", "npm:bar"),
    // Git without semver tags
    ("git@github.com:npm/cli.git", "git@github.com:npm/cli.git#main"),
    (
      "git+ssh://git@github.com/npm/cli#feature",
      "git+ssh://git@github.com/npm/cli#develop",
    ),
    // Mixed - one with version, one without
    ("1.4.1", "latest"),
    ("^1.4.1", "https://example.com/package.tgz"),
    ("~1.4.1", "file:../foo"),
    ("npm:foo@1.4.1", "npm:bar"),
    ("git@github.com:npm/cli.git#1.4.1", "git@github.com:npm/cli.git"),
  ];
  for (a, b) in cases {
    assert!(
      !Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
      "Expected {a} and {b} to return false (at least one has no node_version)"
    );
  }
}

#[test]
fn self_comparison() {
  let cases: Vec<&str> = vec![
    "1.4.1",
    "^1.4.1",
    "~1.4.1",
    ">=1.4.1",
    "1.4.1-alpha.1",
    "^1.4.1-beta.2",
    "1",
    "1.4",
    "^1",
    "~1.4",
    "npm:foo@1.4.1",
    "npm:@scope/pkg@^1.4.1",
    "git@github.com:npm/cli.git#1.4.1",
    "github:user/repo#^1.4.1",
  ];
  for value in cases {
    let spec = Specifier::new(value);
    assert!(
      spec.has_same_version_number_as(&spec),
      "Expected {value} to have same version number as itself"
    );
  }
}

#[test]
fn comprehensive_exact_version_matches() {
  let version = "1.4.1";
  let variants: Vec<&str> = vec![
    "1.4.1",
    "^1.4.1",
    "~1.4.1",
    ">=1.4.1",
    ">1.4.1",
    "<=1.4.1",
    "<1.4.1",
    "npm:foo@1.4.1",
    "npm:@scope/pkg@1.4.1",
    "npm:foo@^1.4.1",
    "npm:foo@~1.4.1",
    "git@github.com:npm/cli.git#1.4.1",
    "git@github.com:npm/cli.git#^1.4.1",
    "git+ssh://git@github.com/npm/cli#~1.4.1",
    "github:user/repo#1.4.1",
  ];

  // Every variant should match every other variant
  for i in 0..variants.len() {
    for j in 0..variants.len() {
      let a = variants[i];
      let b = variants[j];
      assert!(
        Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
        "Expected {a} and {b} to have same version number ({version})"
      );
    }
  }
}

#[test]
fn comprehensive_prerelease_version_matches() {
  let version = "1.4.1-alpha.1";
  let variants: Vec<&str> = vec![
    "1.4.1-alpha.1",
    "^1.4.1-alpha.1",
    "~1.4.1-alpha.1",
    ">=1.4.1-alpha.1",
    "npm:foo@1.4.1-alpha.1",
    "npm:foo@^1.4.1-alpha.1",
    "git@github.com:npm/cli.git#1.4.1-alpha.1",
    "github:user/repo#~1.4.1-alpha.1",
  ];

  // Every variant should match every other variant
  for i in 0..variants.len() {
    for j in 0..variants.len() {
      let a = variants[i];
      let b = variants[j];
      assert!(
        Specifier::new(a).has_same_version_number_as(&Specifier::new(b)),
        "Expected {a} and {b} to have same version number ({version})"
      );
    }
  }
}

#[test]
fn edge_cases() {
  // Same version, all zeroes
  assert!(Specifier::new("0.0.0").has_same_version_number_as(&Specifier::new("^0.0.0")));
  assert!(Specifier::new("0.0.0").has_same_version_number_as(&Specifier::new("~0.0.0")));

  // Different when one component differs
  assert!(!Specifier::new("0.0.0").has_same_version_number_as(&Specifier::new("0.0.1")));
  assert!(!Specifier::new("0.0.0").has_same_version_number_as(&Specifier::new("0.1.0")));
  assert!(!Specifier::new("0.0.0").has_same_version_number_as(&Specifier::new("1.0.0")));

  // Large version numbers
  assert!(Specifier::new("999.999.999").has_same_version_number_as(&Specifier::new("^999.999.999")));

  // Complex prerelease identifiers
  assert!(Specifier::new("1.0.0-alpha.1.2.3").has_same_version_number_as(&Specifier::new("^1.0.0-alpha.1.2.3")));
  assert!(!Specifier::new("1.0.0-alpha.1.2.3").has_same_version_number_as(&Specifier::new("1.0.0-alpha.1.2.4")));
}

#[test]
fn none_variant() {
  // None variant (from "*" or empty) should not have node_version
  let cases: Vec<&str> = vec!["*", "latest", "1.4.1", "^1.4.1"];
  let none_spec = Specifier::new("*");

  for value in cases {
    assert!(
      !none_spec.has_same_version_number_as(&Specifier::new(value)),
      "Expected None variant (*) and {value} to return false"
    );
  }
}