tests: Add API tests for sorting by various categories on the Search page#918
Open
matejnesuta wants to merge 3 commits intoguacsec:mainfrom
Open
tests: Add API tests for sorting by various categories on the Search page#918matejnesuta wants to merge 3 commits intoguacsec:mainfrom
matejnesuta wants to merge 3 commits intoguacsec:mainfrom
Conversation
Contributor
Reviewer's GuideAdds API-level end-to-end tests to validate sorting behavior for PURLs, vulnerabilities, and SBOMs across multiple sort keys and directions, preferring API checks over UI-based sorting tests for stability. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've found 4 issues, and left some high level feedback:
- The ascending/descending tests for PURLs, vulnerabilities, and SBOMs repeat a lot of boilerplate (request params, status checks, sort comparison loops); consider extracting shared helpers (e.g.,
fetchSorted,expectSortedAscending/Descending) to make the tests shorter and easier to maintain. - In the vulnerability CVSS score tests you sort by
base_scorebut assert againstaverage_score; if both exist in the API this mismatch could make the tests misleading, so it would be clearer to validate the same field that is used in thesortparameter. - The SBOM published-date sort tests assume
item.publishedis always non-null, unlike the vulnerability date tests which explicitly filter out nulls; aligning the SBOM tests to handle potential null published dates would reduce flakiness if such data exists.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The ascending/descending tests for PURLs, vulnerabilities, and SBOMs repeat a lot of boilerplate (request params, status checks, sort comparison loops); consider extracting shared helpers (e.g., `fetchSorted`, `expectSortedAscending/Descending`) to make the tests shorter and easier to maintain.
- In the vulnerability CVSS score tests you sort by `base_score` but assert against `average_score`; if both exist in the API this mismatch could make the tests misleading, so it would be clearer to validate the same field that is used in the `sort` parameter.
- The SBOM published-date sort tests assume `item.published` is always non-null, unlike the vulnerability date tests which explicitly filter out nulls; aligning the SBOM tests to handle potential null published dates would reduce flakiness if such data exists.
## Individual Comments
### Comment 1
<location> `e2e/tests/api/features/purl.ts:41-50` </location>
<code_context>
});
+
+test.describe("PURL sorting validation", () => {
+ test("Sort PURLs by name ascending", async ({ axios }) => {
+ const response = await axios.get("/api/v2/purl", {
+ params: {
+ offset: 0,
+ limit: 100,
+ sort: "name:asc",
+ },
+ });
+
+ expect(response.status).toBe(200);
+ expect(response.data.total).toBeGreaterThan(0);
+ expect(response.data.items.length).toBeGreaterThan(0);
+
+ // Verify the sort parameter is accepted and returns data
+ // Note: We don't validate exact sort order because database collation
+ // may differ from JavaScript's string comparison
+ });
</code_context>
<issue_to_address>
**suggestion (testing):** The ascending name sort test only checks that data is returned, not that the sort actually works.
This only asserts that `sort=name:asc` returns data, so it won’t catch regressions where the sort parameter is ignored. You can still check ordering by asserting that the extracted `name` values are monotonic, or by comparing them to a locally sorted copy using the same comparison logic as the backend. Without that, the test provides little validation of sorting behavior.
</issue_to_address>
### Comment 2
<location> `e2e/tests/api/features/purl.ts:59-68` </location>
<code_context>
+ test("Sort PURLs by name descending", async ({ axios }) => {
</code_context>
<issue_to_address>
**suggestion (testing):** Comparing only the first item between asc/desc is a fragile way to validate sorting.
`expect(descFirst).not.toEqual(ascFirst);` can still pass even if sorting is wrong (e.g., many items share the same name or the backend only partially sorts). Instead, extract all names, build full asc/desc arrays, and assert that one is the reverse of the other (or at least that both sequences are monotonic in opposite directions) to more reliably verify the sort behavior.
</issue_to_address>
### Comment 3
<location> `e2e/tests/api/features/purl.ts:140-143` </location>
<code_context>
+ return match ? match[1] : null;
+ };
+
+ const descFirst = getNamespace(response.data.items[0].purl);
+ const ascFirst = getNamespace(ascResponse.data.items[0].purl);
+
+ if (descFirst && ascFirst) {
+ // First item should be different between asc and desc
+ expect(descFirst).not.toEqual(ascFirst);
</code_context>
<issue_to_address>
**issue (testing):** The namespace descending test silently passes when all namespaces are null or unparsable.
Since the assertion is guarded by `if (descFirst && ascFirst)`, the test becomes a no-op (but still passes) when either extracted namespace is falsy. If all items can have null/empty namespaces, the sort behavior isn’t actually tested. You could either filter to items with non-null namespaces and assert on those, or first assert that at least one non-null namespace exists before comparing asc/desc results.
</issue_to_address>
### Comment 4
<location> `e2e/tests/api/features/vulnerability.ts:25-34` </location>
<code_context>
});
+
+test.describe("Vulnerability sorting validation", () => {
+ test("Sort vulnerabilities by ID ascending", async ({ axios }) => {
+ const response = await axios.get("/api/v2/vulnerability", {
+ params: {
+ offset: 0,
+ limit: 100,
+ sort: "id:asc",
+ },
+ });
+
+ expect(response.status).toBe(200);
+ expect(response.data.total).toBeGreaterThan(0);
+ expect(response.data.items.length).toBeGreaterThan(0);
+
+ // Verify the sort parameter is accepted and returns data
+ // Note: We don't validate exact sort order because database collation
+ // may differ from JavaScript's string comparison
+ });
</code_context>
<issue_to_address>
**suggestion (testing):** ID ascending vulnerability test doesn’t assert any ordering, so it won’t catch sorting regressions.
Right now this only checks that the endpoint returns data; it would still pass if `sort=id:asc` were ignored. Please also assert on the ordering by extracting the identifier/canonical ID field and verifying the list is non‑decreasing, or matches a locally sorted copy, so the test actually detects sorting regressions.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #918 +/- ##
==========================================
+ Coverage 64.11% 64.91% +0.80%
==========================================
Files 195 195
Lines 3338 3338
Branches 751 751
==========================================
+ Hits 2140 2167 +27
+ Misses 908 876 -32
- Partials 290 295 +5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
added 2 commits
February 5, 2026 23:03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adding this, as it is a more convenient and less flaky approach of testing the sorting than directly in the UI.
Summary by Sourcery
Add API-level tests to verify sorting behavior for PURLs, vulnerabilities, and SBOMs across multiple sortable fields.
Tests: