@vltpkg/query 1.0.0-rc.32 → 1.0.0-rc.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. package/dist/index.js +10 -6
  2. package/dist/parser.js +2 -1
  3. package/dist/pseudo/hostname.js +3 -1
  4. package/dist/pseudo/malware.d.ts +3 -16
  5. package/dist/pseudo/malware.js +11 -171
  6. package/dist/pseudo/scripts.js +1 -1
  7. package/dist/pseudo/vuln.d.ts +22 -0
  8. package/dist/pseudo/vuln.js +220 -0
  9. package/dist/pseudo.js +3 -3
  10. package/dist/types.d.ts +3 -2
  11. package/package.json +19 -10
  12. package/skills/dss-query/REFERENCE.md +123 -0
  13. package/skills/dss-query/SKILL.md +190 -0
  14. package/skills/dss-query/evals/README.md +79 -0
  15. package/skills/dss-query/evals/evals.json +89 -0
  16. package/skills/dss-query/evals/grade.mjs +480 -0
  17. package/src/attribute.ts +186 -0
  18. package/src/combinator.ts +132 -0
  19. package/src/id.ts +46 -0
  20. package/src/index.ts +620 -0
  21. package/src/parser.ts +122 -0
  22. package/src/pseudo/abandoned.ts +9 -0
  23. package/src/pseudo/attr.ts +92 -0
  24. package/src/pseudo/built.ts +19 -0
  25. package/src/pseudo/confused.ts +29 -0
  26. package/src/pseudo/cve.ts +93 -0
  27. package/src/pseudo/cwe.ts +97 -0
  28. package/src/pseudo/debug.ts +9 -0
  29. package/src/pseudo/deprecated.ts +9 -0
  30. package/src/pseudo/dev.ts +18 -0
  31. package/src/pseudo/diff.ts +90 -0
  32. package/src/pseudo/dist.ts +144 -0
  33. package/src/pseudo/dynamic.ts +9 -0
  34. package/src/pseudo/empty.ts +15 -0
  35. package/src/pseudo/entropic.ts +9 -0
  36. package/src/pseudo/env.ts +6 -0
  37. package/src/pseudo/eval.ts +9 -0
  38. package/src/pseudo/fs.ts +9 -0
  39. package/src/pseudo/helpers.ts +106 -0
  40. package/src/pseudo/host.ts +111 -0
  41. package/src/pseudo/hostname.ts +156 -0
  42. package/src/pseudo/license.ts +134 -0
  43. package/src/pseudo/link.ts +30 -0
  44. package/src/pseudo/malware.ts +41 -0
  45. package/src/pseudo/minified.ts +9 -0
  46. package/src/pseudo/missing.ts +16 -0
  47. package/src/pseudo/native.ts +9 -0
  48. package/src/pseudo/network.ts +9 -0
  49. package/src/pseudo/obfuscated.ts +9 -0
  50. package/src/pseudo/optional.ts +18 -0
  51. package/src/pseudo/outdated.ts +305 -0
  52. package/src/pseudo/overridden.ts +20 -0
  53. package/src/pseudo/path.ts +146 -0
  54. package/src/pseudo/peer.ts +18 -0
  55. package/src/pseudo/prerelease.ts +47 -0
  56. package/src/pseudo/private.ts +19 -0
  57. package/src/pseudo/prod.ts +18 -0
  58. package/src/pseudo/published.ts +248 -0
  59. package/src/pseudo/registry.ts +29 -0
  60. package/src/pseudo/root.ts +19 -0
  61. package/src/pseudo/scanned.ts +20 -0
  62. package/src/pseudo/score.ts +186 -0
  63. package/src/pseudo/scripts.ts +54 -0
  64. package/src/pseudo/semver.ts +320 -0
  65. package/src/pseudo/severity.ts +231 -0
  66. package/src/pseudo/shell.ts +9 -0
  67. package/src/pseudo/shrinkwrap.ts +9 -0
  68. package/src/pseudo/spec.ts +131 -0
  69. package/src/pseudo/squat.ts +225 -0
  70. package/src/pseudo/suspicious.ts +9 -0
  71. package/src/pseudo/tracker.ts +9 -0
  72. package/src/pseudo/trivial.ts +9 -0
  73. package/src/pseudo/type.ts +26 -0
  74. package/src/pseudo/undesirable.ts +9 -0
  75. package/src/pseudo/unknown.ts +9 -0
  76. package/src/pseudo/unmaintained.ts +9 -0
  77. package/src/pseudo/unpopular.ts +9 -0
  78. package/src/pseudo/unstable.ts +9 -0
  79. package/src/pseudo/vuln.ts +273 -0
  80. package/src/pseudo/workspace.ts +22 -0
  81. package/src/pseudo.ts +413 -0
  82. package/src/types.ts +165 -0
  83. package/tap-snapshots/test/attribute.ts.test.cjs +304 -0
  84. package/tap-snapshots/test/combinator.ts.test.cjs +333 -0
  85. package/tap-snapshots/test/id.ts.test.cjs +118 -0
  86. package/tap-snapshots/test/parser.ts.test.cjs +344 -0
  87. package/tap-snapshots/test/pseudo/abandoned.ts.test.cjs +18 -0
  88. package/tap-snapshots/test/pseudo/attr.ts.test.cjs +161 -0
  89. package/tap-snapshots/test/pseudo/built.ts.test.cjs +70 -0
  90. package/tap-snapshots/test/pseudo/confused.ts.test.cjs +32 -0
  91. package/tap-snapshots/test/pseudo/cve.ts.test.cjs +49 -0
  92. package/tap-snapshots/test/pseudo/cwe.ts.test.cjs +49 -0
  93. package/tap-snapshots/test/pseudo/debug.ts.test.cjs +18 -0
  94. package/tap-snapshots/test/pseudo/deprecated.ts.test.cjs +18 -0
  95. package/tap-snapshots/test/pseudo/dist.ts.test.cjs +30 -0
  96. package/tap-snapshots/test/pseudo/dynamic.ts.test.cjs +18 -0
  97. package/tap-snapshots/test/pseudo/empty.ts.test.cjs +50 -0
  98. package/tap-snapshots/test/pseudo/entropic.ts.test.cjs +18 -0
  99. package/tap-snapshots/test/pseudo/env.ts.test.cjs +18 -0
  100. package/tap-snapshots/test/pseudo/eval.ts.test.cjs +18 -0
  101. package/tap-snapshots/test/pseudo/fs.ts.test.cjs +18 -0
  102. package/tap-snapshots/test/pseudo/helpers.ts.test.cjs +18 -0
  103. package/tap-snapshots/test/pseudo/hostname.ts.test.cjs +148 -0
  104. package/tap-snapshots/test/pseudo/license.ts.test.cjs +41 -0
  105. package/tap-snapshots/test/pseudo/link.ts.test.cjs +36 -0
  106. package/tap-snapshots/test/pseudo/malware.ts.test.cjs +22 -0
  107. package/tap-snapshots/test/pseudo/minified.ts.test.cjs +18 -0
  108. package/tap-snapshots/test/pseudo/missing.ts.test.cjs +30 -0
  109. package/tap-snapshots/test/pseudo/native.ts.test.cjs +18 -0
  110. package/tap-snapshots/test/pseudo/network.ts.test.cjs +18 -0
  111. package/tap-snapshots/test/pseudo/obfuscated.ts.test.cjs +18 -0
  112. package/tap-snapshots/test/pseudo/outdated.ts.test.cjs +130 -0
  113. package/tap-snapshots/test/pseudo/overridden.ts.test.cjs +75 -0
  114. package/tap-snapshots/test/pseudo/prerelease.ts.test.cjs +101 -0
  115. package/tap-snapshots/test/pseudo/private.ts.test.cjs +38 -0
  116. package/tap-snapshots/test/pseudo/published.ts.test.cjs +171 -0
  117. package/tap-snapshots/test/pseudo/registry.ts.test.cjs +68 -0
  118. package/tap-snapshots/test/pseudo/root.ts.test.cjs +24 -0
  119. package/tap-snapshots/test/pseudo/scanned.ts.test.cjs +18 -0
  120. package/tap-snapshots/test/pseudo/score.ts.test.cjs +239 -0
  121. package/tap-snapshots/test/pseudo/scripts.ts.test.cjs +61 -0
  122. package/tap-snapshots/test/pseudo/semver.ts.test.cjs +575 -0
  123. package/tap-snapshots/test/pseudo/severity.ts.test.cjs +95 -0
  124. package/tap-snapshots/test/pseudo/shell.ts.test.cjs +18 -0
  125. package/tap-snapshots/test/pseudo/shrinkwrap.ts.test.cjs +18 -0
  126. package/tap-snapshots/test/pseudo/spec.ts.test.cjs +148 -0
  127. package/tap-snapshots/test/pseudo/squat.ts.test.cjs +129 -0
  128. package/tap-snapshots/test/pseudo/suspicious.ts.test.cjs +18 -0
  129. package/tap-snapshots/test/pseudo/tracker.ts.test.cjs +18 -0
  130. package/tap-snapshots/test/pseudo/trivial.ts.test.cjs +18 -0
  131. package/tap-snapshots/test/pseudo/type.ts.test.cjs +51 -0
  132. package/tap-snapshots/test/pseudo/undesirable.ts.test.cjs +18 -0
  133. package/tap-snapshots/test/pseudo/unknown.ts.test.cjs +18 -0
  134. package/tap-snapshots/test/pseudo/unmaintained.ts.test.cjs +18 -0
  135. package/tap-snapshots/test/pseudo/unpopular.ts.test.cjs +18 -0
  136. package/tap-snapshots/test/pseudo/unstable.ts.test.cjs +18 -0
  137. package/tap-snapshots/test/pseudo/vuln.ts.test.cjs +208 -0
  138. package/tap-snapshots/test/pseudo/vulnerable.ts.test.cjs +20 -0
  139. package/tap-snapshots/test/pseudo/workspace.ts.test.cjs +19 -0
  140. package/tap-snapshots/test/pseudo.ts.test.cjs +1247 -0
  141. package/test/attribute.ts +266 -0
  142. package/test/combinator.ts +168 -0
  143. package/test/fixtures/graph.ts +999 -0
  144. package/test/fixtures/selector.ts +103 -0
  145. package/test/fixtures/types.ts +7 -0
  146. package/test/id.ts +102 -0
  147. package/test/index.ts +1000 -0
  148. package/test/parser.ts +117 -0
  149. package/test/pseudo/abandoned.ts +98 -0
  150. package/test/pseudo/attr.ts +298 -0
  151. package/test/pseudo/built.ts +214 -0
  152. package/test/pseudo/confused.ts +160 -0
  153. package/test/pseudo/cve.ts +249 -0
  154. package/test/pseudo/cwe.ts +255 -0
  155. package/test/pseudo/debug.ts +98 -0
  156. package/test/pseudo/deprecated.ts +98 -0
  157. package/test/pseudo/dev.ts +61 -0
  158. package/test/pseudo/diff.ts +417 -0
  159. package/test/pseudo/dist.ts +215 -0
  160. package/test/pseudo/dynamic.ts +98 -0
  161. package/test/pseudo/empty.ts +108 -0
  162. package/test/pseudo/entropic.ts +101 -0
  163. package/test/pseudo/env.ts +98 -0
  164. package/test/pseudo/eval.ts +98 -0
  165. package/test/pseudo/fs.ts +98 -0
  166. package/test/pseudo/helpers.ts +370 -0
  167. package/test/pseudo/host-context.ts +276 -0
  168. package/test/pseudo/hostname.ts +314 -0
  169. package/test/pseudo/license.ts +265 -0
  170. package/test/pseudo/link.ts +94 -0
  171. package/test/pseudo/malware.ts +184 -0
  172. package/test/pseudo/minified.ts +98 -0
  173. package/test/pseudo/missing.ts +111 -0
  174. package/test/pseudo/native.ts +98 -0
  175. package/test/pseudo/network.ts +98 -0
  176. package/test/pseudo/obfuscated.ts +98 -0
  177. package/test/pseudo/optional.ts +73 -0
  178. package/test/pseudo/outdated.ts +331 -0
  179. package/test/pseudo/overridden.ts +317 -0
  180. package/test/pseudo/path.ts +680 -0
  181. package/test/pseudo/peer.ts +101 -0
  182. package/test/pseudo/prerelease.ts +279 -0
  183. package/test/pseudo/private.ts +108 -0
  184. package/test/pseudo/prod.ts +61 -0
  185. package/test/pseudo/published.ts +557 -0
  186. package/test/pseudo/registry.ts +122 -0
  187. package/test/pseudo/root.ts +66 -0
  188. package/test/pseudo/scanned.ts +78 -0
  189. package/test/pseudo/score.ts +591 -0
  190. package/test/pseudo/scripts.ts +294 -0
  191. package/test/pseudo/semver.ts +822 -0
  192. package/test/pseudo/severity.ts +310 -0
  193. package/test/pseudo/shell.ts +98 -0
  194. package/test/pseudo/shrinkwrap.ts +98 -0
  195. package/test/pseudo/spec.ts +525 -0
  196. package/test/pseudo/squat.ts +565 -0
  197. package/test/pseudo/suspicious.ts +98 -0
  198. package/test/pseudo/tracker.ts +98 -0
  199. package/test/pseudo/trivial.ts +98 -0
  200. package/test/pseudo/type.ts +105 -0
  201. package/test/pseudo/undesirable.ts +98 -0
  202. package/test/pseudo/unknown.ts +98 -0
  203. package/test/pseudo/unmaintained.ts +98 -0
  204. package/test/pseudo/unpopular.ts +98 -0
  205. package/test/pseudo/unstable.ts +98 -0
  206. package/test/pseudo/vuln.ts +625 -0
  207. package/test/pseudo/vulnerable.ts +137 -0
  208. package/test/pseudo/workspace.ts +111 -0
  209. package/test/pseudo.ts +558 -0
  210. package/tsconfig.json +3 -0
  211. package/tsconfig.publish.json +19 -0
  212. package/tsconfig.publish.tsbuildinfo +1 -0
  213. package/typedoc.mjs +2 -0
  214. package/dist/pseudo/vulnerable.d.ts +0 -8
  215. package/dist/pseudo/vulnerable.js +0 -17
@@ -0,0 +1,591 @@
1
+ import t from 'tap'
2
+ import { joinDepIDTuple } from '@vltpkg/dep-id'
3
+ import { asSecurityArchiveLike } from '@vltpkg/security-archive'
4
+ import { getSimpleGraph } from '../fixtures/graph.ts'
5
+ import type { ParserState } from '../../src/types.ts'
6
+ import {
7
+ score,
8
+ isScoreKind,
9
+ asScoreKind,
10
+ } from '../../src/pseudo/score.ts'
11
+ import type { PackageScore } from '@vltpkg/security-archive'
12
+ import { parse } from '../../src/parser.ts'
13
+
14
+ // Create a function to generate a security archive with varied scores for testing
15
+ const createTestSecurityArchive = () => {
16
+ // Define different scores for each package in the test graph
17
+ const scores: Record<string, PackageScore> = {
18
+ a: {
19
+ overall: 0.85,
20
+ license: 0.95,
21
+ maintenance: 0.8,
22
+ quality: 0.75,
23
+ supplyChain: 0.9,
24
+ vulnerability: 0.7,
25
+ },
26
+ b: {
27
+ overall: 0.65,
28
+ license: 0.5,
29
+ maintenance: 0.7,
30
+ quality: 0.6,
31
+ supplyChain: 0.8,
32
+ vulnerability: 0.45,
33
+ },
34
+ c: {
35
+ overall: 0.4,
36
+ license: 0.3,
37
+ maintenance: 0.5,
38
+ quality: 0.45,
39
+ supplyChain: 0.35,
40
+ vulnerability: 0.25,
41
+ },
42
+ d: {
43
+ overall: 0.95,
44
+ license: 1.0,
45
+ maintenance: 0.9,
46
+ quality: 0.85,
47
+ supplyChain: 0.95,
48
+ vulnerability: 0.9,
49
+ },
50
+ e: {
51
+ overall: 0.2,
52
+ license: 0.15,
53
+ maintenance: 0.3,
54
+ quality: 0.25,
55
+ supplyChain: 0.1,
56
+ vulnerability: 0.05,
57
+ },
58
+ f: {
59
+ overall: 0.5,
60
+ license: 0.55,
61
+ maintenance: 0.45,
62
+ quality: 0.5,
63
+ supplyChain: 0.6,
64
+ vulnerability: 0.4,
65
+ },
66
+ }
67
+
68
+ // Create a map of package IDs to security data
69
+ const securityMap = new Map()
70
+ Object.entries(scores).forEach(([name, score]) => {
71
+ securityMap.set(
72
+ joinDepIDTuple(['registry', '', `${name}@1.0.0`]),
73
+ {
74
+ id: joinDepIDTuple(['registry', '', `${name}@1.0.0`]),
75
+ author: [],
76
+ size: 0,
77
+ type: 'npm',
78
+ name,
79
+ version: '1.0.0',
80
+ license: 'MIT',
81
+ score,
82
+ alerts: [],
83
+ },
84
+ )
85
+ })
86
+
87
+ return asSecurityArchiveLike(securityMap)
88
+ }
89
+
90
+ t.test('selects packages based on their security score', async t => {
91
+ const getState = (query: string, graph = getSimpleGraph()) => {
92
+ const ast = parse(query)
93
+ const current = ast.first.first
94
+ const state: ParserState = {
95
+ comment: '',
96
+ current,
97
+ initial: {
98
+ edges: new Set(graph.edges.values()),
99
+ nodes: new Set(graph.nodes.values()),
100
+ },
101
+ partial: {
102
+ edges: new Set(graph.edges.values()),
103
+ nodes: new Set(graph.nodes.values()),
104
+ },
105
+ collect: {
106
+ edges: new Set(),
107
+ nodes: new Set(),
108
+ },
109
+ cancellable: async () => {},
110
+ walk: async i => i,
111
+ securityArchive: createTestSecurityArchive(),
112
+ importers: new Set(graph.importers),
113
+ retries: 0,
114
+ signal: new AbortController().signal,
115
+ specificity: { idCounter: 0, commonCounter: 0 },
116
+ }
117
+ return state
118
+ }
119
+
120
+ await t.test('exact match on overall score', async t => {
121
+ const res = await score(getState(':score("0.85")'))
122
+ t.strictSame(
123
+ [...res.partial.nodes].map(n => n.name),
124
+ ['a'],
125
+ 'should select packages with exactly 0.85 overall score',
126
+ )
127
+ t.matchSnapshot({
128
+ nodes: [...res.partial.nodes].map(n => n.name),
129
+ edges: [...res.partial.edges].map(e => e.name),
130
+ })
131
+ })
132
+
133
+ await t.test('exact match with percentage value', async t => {
134
+ const res = await score(getState(':score(20)'))
135
+ t.strictSame(
136
+ [...res.partial.nodes].map(n => n.name),
137
+ ['e'],
138
+ 'should select packages with exactly 0.2 (20%) overall score',
139
+ )
140
+ t.matchSnapshot({
141
+ nodes: [...res.partial.nodes].map(n => n.name),
142
+ edges: [...res.partial.edges].map(e => e.name),
143
+ })
144
+ })
145
+
146
+ await t.test('exact match with quoted value', async t => {
147
+ const res = await score(getState(':score("0.5")'))
148
+ t.strictSame(
149
+ [...res.partial.nodes].map(n => n.name),
150
+ ['f'],
151
+ 'should select packages with exactly 0.5 overall score',
152
+ )
153
+ t.matchSnapshot({
154
+ nodes: [...res.partial.nodes].map(n => n.name),
155
+ edges: [...res.partial.edges].map(e => e.name),
156
+ })
157
+ })
158
+
159
+ await t.test('greater than comparator', async t => {
160
+ const res = await score(getState(':score(">0.8")'))
161
+ t.strictSame(
162
+ [...res.partial.nodes].map(n => n.name),
163
+ ['a', 'd'],
164
+ 'should select packages with overall score greater than 0.8',
165
+ )
166
+ t.matchSnapshot({
167
+ nodes: [...res.partial.nodes].map(n => n.name),
168
+ edges: [...res.partial.edges].map(e => e.name),
169
+ })
170
+ })
171
+
172
+ await t.test('greater than comparator (unquoted)', async t => {
173
+ const res = await score(getState(':score(>0.8)'))
174
+ t.strictSame(
175
+ [...res.partial.nodes].map(n => n.name),
176
+ ['a', 'd'],
177
+ 'should select packages with overall score greater than 0.8 using unquoted parameter',
178
+ )
179
+ t.matchSnapshot({
180
+ nodes: [...res.partial.nodes].map(n => n.name),
181
+ edges: [...res.partial.edges].map(e => e.name),
182
+ })
183
+ })
184
+
185
+ await t.test('less than comparator', async t => {
186
+ const res = await score(getState(':score("<0.5")'))
187
+ t.strictSame(
188
+ [...res.partial.nodes].map(n => n.name),
189
+ ['c', 'e'],
190
+ 'should select packages with overall score less than 0.5',
191
+ )
192
+ t.matchSnapshot({
193
+ nodes: [...res.partial.nodes].map(n => n.name),
194
+ edges: [...res.partial.edges].map(e => e.name),
195
+ })
196
+ })
197
+
198
+ await t.test('less than comparator (unquoted)', async t => {
199
+ const res = await score(getState(':score(<0.5)'))
200
+ t.strictSame(
201
+ [...res.partial.nodes].map(n => n.name),
202
+ ['c', 'e'],
203
+ 'should select packages with overall score less than 0.5 using unquoted parameter',
204
+ )
205
+ t.matchSnapshot({
206
+ nodes: [...res.partial.nodes].map(n => n.name),
207
+ edges: [...res.partial.edges].map(e => e.name),
208
+ })
209
+ })
210
+
211
+ await t.test('greater than or equal comparator', async t => {
212
+ const res = await score(getState(':score(">=0.75")'))
213
+ t.strictSame(
214
+ [...res.partial.nodes].map(n => n.name),
215
+ ['a', 'd'],
216
+ 'should select packages with overall score greater than or equal to 0.75',
217
+ )
218
+ t.matchSnapshot({
219
+ nodes: [...res.partial.nodes].map(n => n.name),
220
+ edges: [...res.partial.edges].map(e => e.name),
221
+ })
222
+ })
223
+
224
+ await t.test(
225
+ 'greater than or equal comparator (unquoted)',
226
+ async t => {
227
+ const res = await score(getState(':score(>=0.75)'))
228
+ t.strictSame(
229
+ [...res.partial.nodes].map(n => n.name),
230
+ ['a', 'd'],
231
+ 'should select packages with overall score greater than or equal to 0.75 using unquoted parameter',
232
+ )
233
+ t.matchSnapshot({
234
+ nodes: [...res.partial.nodes].map(n => n.name),
235
+ edges: [...res.partial.edges].map(e => e.name),
236
+ })
237
+ },
238
+ )
239
+
240
+ await t.test('less than or equal comparator', async t => {
241
+ const res = await score(getState(':score(<=40)'))
242
+ t.strictSame(
243
+ [...res.partial.nodes].map(n => n.name),
244
+ ['c', 'e'],
245
+ 'should select packages with overall score less than or equal to 0.4 (40%)',
246
+ )
247
+ t.matchSnapshot({
248
+ nodes: [...res.partial.nodes].map(n => n.name),
249
+ edges: [...res.partial.edges].map(e => e.name),
250
+ })
251
+ })
252
+
253
+ await t.test(
254
+ 'less than or equal comparator (unquoted)',
255
+ async t => {
256
+ const res = await score(getState(':score(<=40)'))
257
+ t.strictSame(
258
+ [...res.partial.nodes].map(n => n.name),
259
+ ['c', 'e'],
260
+ 'should select packages with overall score less than or equal to 0.4 (40%) using unquoted parameter',
261
+ )
262
+ t.matchSnapshot({
263
+ nodes: [...res.partial.nodes].map(n => n.name),
264
+ edges: [...res.partial.edges].map(e => e.name),
265
+ })
266
+ },
267
+ )
268
+
269
+ await t.test('with specific kind parameter', async t => {
270
+ const res = await score(getState(':score("0.95", license)'))
271
+ t.strictSame(
272
+ [...res.partial.nodes].map(n => n.name),
273
+ ['a'],
274
+ 'should select packages with license score of exactly 0.95',
275
+ )
276
+ t.matchSnapshot({
277
+ nodes: [...res.partial.nodes].map(n => n.name),
278
+ edges: [...res.partial.edges].map(e => e.name),
279
+ })
280
+ })
281
+
282
+ await t.test(
283
+ 'with specific kind and quoted parameters',
284
+ async t => {
285
+ const res = await score(
286
+ getState(':score(">0.8", "supplyChain")'),
287
+ )
288
+ t.strictSame(
289
+ [...res.partial.nodes].map(n => n.name),
290
+ ['a', 'd'],
291
+ 'should select packages with supplyChain score greater than 0.8',
292
+ )
293
+ t.matchSnapshot({
294
+ nodes: [...res.partial.nodes].map(n => n.name),
295
+ edges: [...res.partial.edges].map(e => e.name),
296
+ })
297
+ },
298
+ )
299
+
300
+ await t.test(
301
+ 'with less than comparator and vulnerability kind',
302
+ async t => {
303
+ const res = await score(
304
+ getState(':score("<0.5", vulnerability)'),
305
+ )
306
+ t.strictSame(
307
+ [...res.partial.nodes].map(n => n.name),
308
+ ['b', 'c', 'e', 'f'],
309
+ 'should select packages with vulnerability score less than 0.5',
310
+ )
311
+ t.matchSnapshot({
312
+ nodes: [...res.partial.nodes].map(n => n.name),
313
+ edges: [...res.partial.edges].map(e => e.name),
314
+ })
315
+ },
316
+ )
317
+
318
+ await t.test(
319
+ 'with percentage rate and maintenance kind',
320
+ async t => {
321
+ const res = await score(getState(':score(<=50, maintenance)'))
322
+ t.strictSame(
323
+ [...res.partial.nodes].map(n => n.name),
324
+ ['c', 'e', 'f'],
325
+ 'should select packages with maintenance score less than or equal to 0.5 (50%)',
326
+ )
327
+ t.matchSnapshot({
328
+ nodes: [...res.partial.nodes].map(n => n.name),
329
+ edges: [...res.partial.edges].map(e => e.name),
330
+ })
331
+ },
332
+ )
333
+
334
+ await t.test('exact match with perfect score', async t => {
335
+ const res = await score(getState(':score(1, license)'))
336
+ t.strictSame(
337
+ [...res.partial.nodes].map(n => n.name),
338
+ ['d'],
339
+ 'should select packages with license score of exactly 1.0',
340
+ )
341
+ t.matchSnapshot({
342
+ nodes: [...res.partial.nodes].map(n => n.name),
343
+ edges: [...res.partial.edges].map(e => e.name),
344
+ })
345
+ })
346
+ })
347
+
348
+ t.test('error cases', async t => {
349
+ // Test with missing security archive
350
+ await t.test('missing security archive', async t => {
351
+ const getState = (query: string) => {
352
+ const ast = parse(query)
353
+ const current = ast.first.first
354
+ const state: ParserState = {
355
+ comment: '',
356
+ current,
357
+ initial: {
358
+ edges: new Set(),
359
+ nodes: new Set(),
360
+ },
361
+ partial: {
362
+ edges: new Set(),
363
+ nodes: new Set(),
364
+ },
365
+ collect: {
366
+ edges: new Set(),
367
+ nodes: new Set(),
368
+ },
369
+ cancellable: async () => {},
370
+ walk: async i => i,
371
+ securityArchive: undefined,
372
+ importers: new Set(),
373
+ retries: 0,
374
+ signal: new AbortController().signal,
375
+ specificity: { idCounter: 0, commonCounter: 0 },
376
+ }
377
+ return state
378
+ }
379
+
380
+ await t.rejects(
381
+ score(getState(':score(0.8)')),
382
+ { message: /Missing security archive/ },
383
+ 'should throw an error when security archive is missing',
384
+ )
385
+ })
386
+
387
+ // Test with invalid rate value
388
+ await t.test('invalid rate value - too high', async t => {
389
+ const getState = (query: string) => {
390
+ const ast = parse(query)
391
+ const current = ast.first.first
392
+ const state: ParserState = {
393
+ comment: '',
394
+ current,
395
+ initial: {
396
+ edges: new Set(),
397
+ nodes: new Set(),
398
+ },
399
+ partial: {
400
+ edges: new Set(),
401
+ nodes: new Set(),
402
+ },
403
+ collect: {
404
+ edges: new Set(),
405
+ nodes: new Set(),
406
+ },
407
+ cancellable: async () => {},
408
+ walk: async i => i,
409
+ securityArchive: createTestSecurityArchive(),
410
+ importers: new Set(),
411
+ retries: 0,
412
+ signal: new AbortController().signal,
413
+ specificity: { idCounter: 0, commonCounter: 0 },
414
+ }
415
+ return state
416
+ }
417
+
418
+ await t.rejects(
419
+ score(getState(':score(101)')),
420
+ { message: /Failed to parse :score selector/ },
421
+ 'should throw an error when rate value is greater than 100',
422
+ )
423
+ })
424
+
425
+ // Test with invalid rate value - negative
426
+ await t.test('invalid rate value - negative', async t => {
427
+ const getState = (query: string) => {
428
+ const ast = parse(query)
429
+ const current = ast.first.first
430
+ const state: ParserState = {
431
+ comment: '',
432
+ current,
433
+ initial: {
434
+ edges: new Set(),
435
+ nodes: new Set(),
436
+ },
437
+ partial: {
438
+ edges: new Set(),
439
+ nodes: new Set(),
440
+ },
441
+ collect: {
442
+ edges: new Set(),
443
+ nodes: new Set(),
444
+ },
445
+ cancellable: async () => {},
446
+ walk: async i => i,
447
+ securityArchive: createTestSecurityArchive(),
448
+ importers: new Set(),
449
+ retries: 0,
450
+ signal: new AbortController().signal,
451
+ specificity: { idCounter: 0, commonCounter: 0 },
452
+ }
453
+ return state
454
+ }
455
+
456
+ await t.rejects(
457
+ score(getState(':score("-0.5")')),
458
+ { message: /Failed to parse :score selector/ },
459
+ 'should throw an error when rate value is negative',
460
+ )
461
+ })
462
+
463
+ // Test with invalid kind
464
+ await t.test('invalid kind parameter', async t => {
465
+ const getState = (query: string) => {
466
+ const ast = parse(query)
467
+ const current = ast.first.first
468
+ const state: ParserState = {
469
+ comment: '',
470
+ current,
471
+ initial: {
472
+ edges: new Set(),
473
+ nodes: new Set(),
474
+ },
475
+ partial: {
476
+ edges: new Set(),
477
+ nodes: new Set(),
478
+ },
479
+ collect: {
480
+ edges: new Set(),
481
+ nodes: new Set(),
482
+ },
483
+ cancellable: async () => {},
484
+ walk: async i => i,
485
+ securityArchive: createTestSecurityArchive(),
486
+ importers: new Set(),
487
+ retries: 0,
488
+ signal: new AbortController().signal,
489
+ specificity: { idCounter: 0, commonCounter: 0 },
490
+ }
491
+ return state
492
+ }
493
+
494
+ await t.rejects(
495
+ score(getState(':score(0.8, invalid)')),
496
+ { message: /Failed to parse :score selector/ },
497
+ 'should throw an error when kind parameter is invalid',
498
+ )
499
+ })
500
+ })
501
+
502
+ t.test('utility functions', async t => {
503
+ // Test isScoreKind function
504
+ await t.test('isScoreKind', async t => {
505
+ t.ok(
506
+ isScoreKind('overall'),
507
+ 'should return true for valid score kind "overall"',
508
+ )
509
+ t.ok(
510
+ isScoreKind('license'),
511
+ 'should return true for valid score kind "license"',
512
+ )
513
+ t.ok(
514
+ isScoreKind('maintenance'),
515
+ 'should return true for valid score kind "maintenance"',
516
+ )
517
+ t.ok(
518
+ isScoreKind('quality'),
519
+ 'should return true for valid score kind "quality"',
520
+ )
521
+ t.ok(
522
+ isScoreKind('supplyChain'),
523
+ 'should return true for valid score kind "supplyChain"',
524
+ )
525
+ t.ok(
526
+ isScoreKind('vulnerability'),
527
+ 'should return true for valid score kind "vulnerability"',
528
+ )
529
+ t.notOk(
530
+ isScoreKind('invalid'),
531
+ 'should return false for invalid score kind',
532
+ )
533
+ })
534
+
535
+ // Test asScoreKind function
536
+ await t.test('asScoreKind', async t => {
537
+ t.equal(
538
+ asScoreKind('overall'),
539
+ 'overall',
540
+ 'should return the score kind for valid input',
541
+ )
542
+ t.throws(
543
+ () => asScoreKind('invalid'),
544
+ { message: /Expected a valid score kind/ },
545
+ 'should throw an error for invalid score kind',
546
+ )
547
+ })
548
+ })
549
+
550
+ t.test('pseudo state form - :score without parameters', async t => {
551
+ const getState = (graph = getSimpleGraph()) => {
552
+ const ast = parse(':score')
553
+ const current = ast.first.first
554
+ const state: ParserState = {
555
+ comment: '',
556
+ current,
557
+ initial: {
558
+ edges: new Set(graph.edges.values()),
559
+ nodes: new Set(graph.nodes.values()),
560
+ },
561
+ partial: {
562
+ edges: new Set(graph.edges.values()),
563
+ nodes: new Set(graph.nodes.values()),
564
+ },
565
+ collect: {
566
+ edges: new Set(),
567
+ nodes: new Set(),
568
+ },
569
+ cancellable: async () => {},
570
+ walk: async i => i,
571
+ securityArchive: createTestSecurityArchive(),
572
+ importers: new Set(graph.importers),
573
+ retries: 0,
574
+ signal: new AbortController().signal,
575
+ specificity: { idCounter: 0, commonCounter: 0 },
576
+ }
577
+ return state
578
+ }
579
+
580
+ const state = getState()
581
+ const result = await score(state)
582
+ t.matchSnapshot(
583
+ {
584
+ nodes: [...result.partial.nodes].map(n => n.id),
585
+ edges: [...result.partial.edges].map(
586
+ e => `${e.from.id}->${e.to?.id}`,
587
+ ),
588
+ },
589
+ 'should match any packages with score data (scanned packages)',
590
+ )
591
+ })