|
| 1 | +import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server" |
| 2 | +import { expect, test } from "bun:test" |
| 3 | + |
| 4 | +test("list latest packages", async () => { |
| 5 | + const { axios, db } = await getTestServer() |
| 6 | + |
| 7 | + // Add some test packages |
| 8 | + const packages = [ |
| 9 | + { |
| 10 | + name: "Package1", |
| 11 | + unscoped_name: "Package1", |
| 12 | + owner_github_username: "user1", |
| 13 | + creator_account_id: "creator1", |
| 14 | + created_at: "2023-01-01T00:00:00Z", |
| 15 | + updated_at: "2023-01-01T00:00:00Z", |
| 16 | + description: "Description 1", |
| 17 | + ai_description: "AI Description 1", |
| 18 | + ai_usage_instructions: "Usage instructions 1", |
| 19 | + owner_org_id: "org1", |
| 20 | + latest_version: "1.0.0", |
| 21 | + license: "MIT", |
| 22 | + is_source_from_github: true, |
| 23 | + is_snippet: false, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "Package2", |
| 27 | + unscoped_name: "Package2", |
| 28 | + owner_github_username: "user2", |
| 29 | + creator_account_id: "creator2", |
| 30 | + created_at: "2023-01-02T00:00:00Z", |
| 31 | + updated_at: "2023-01-02T00:00:00Z", |
| 32 | + description: "Description 2", |
| 33 | + ai_description: "AI Description 2", |
| 34 | + ai_usage_instructions: "Usage instructions 2", |
| 35 | + owner_org_id: "org2", |
| 36 | + latest_version: "1.0.0", |
| 37 | + license: "MIT", |
| 38 | + is_source_from_github: true, |
| 39 | + is_snippet: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Package3", |
| 43 | + unscoped_name: "Package3", |
| 44 | + owner_github_username: "user3", |
| 45 | + creator_account_id: "creator3", |
| 46 | + created_at: "2023-01-03T00:00:00Z", |
| 47 | + updated_at: "2023-01-03T00:00:00Z", |
| 48 | + description: "Description 3", |
| 49 | + ai_description: "AI Description 3", |
| 50 | + ai_usage_instructions: "Usage instructions 3", |
| 51 | + owner_org_id: "org3", |
| 52 | + latest_version: "1.0.0", |
| 53 | + license: "MIT", |
| 54 | + is_source_from_github: true, |
| 55 | + is_snippet: false, |
| 56 | + }, |
| 57 | + // Add a snippet to verify it's filtered out |
| 58 | + { |
| 59 | + name: "Snippet1", |
| 60 | + unscoped_name: "Snippet1", |
| 61 | + owner_github_username: "user4", |
| 62 | + creator_account_id: "creator4", |
| 63 | + created_at: "2023-01-04T00:00:00Z", |
| 64 | + updated_at: "2023-01-04T00:00:00Z", |
| 65 | + description: "Snippet Description", |
| 66 | + ai_description: "AI Description", |
| 67 | + ai_usage_instructions: "Usage instructions", |
| 68 | + owner_org_id: "org4", |
| 69 | + latest_version: "1.0.0", |
| 70 | + license: "MIT", |
| 71 | + is_source_from_github: true, |
| 72 | + is_snippet: true, |
| 73 | + }, |
| 74 | + ] |
| 75 | + |
| 76 | + // Add packages to the database |
| 77 | + for (const pkg of packages) { |
| 78 | + db.addPackage(pkg as any) |
| 79 | + } |
| 80 | + |
| 81 | + // Test the latest endpoint |
| 82 | + const { data } = await axios.get("/api/packages/list_latest") |
| 83 | + |
| 84 | + // Verify response structure |
| 85 | + expect(Array.isArray(data.packages)).toBe(true) |
| 86 | + expect(data.packages.length).toBe(3) // Should only return non-snippet packages |
| 87 | + |
| 88 | + // Verify that packages are sorted by creation date (newest first) |
| 89 | + for (let i = 1; i < data.packages.length; i++) { |
| 90 | + expect( |
| 91 | + new Date(data.packages[i - 1].created_at).getTime(), |
| 92 | + ).toBeGreaterThanOrEqual(new Date(data.packages[i].created_at).getTime()) |
| 93 | + } |
| 94 | + |
| 95 | + // Verify that all returned packages have the required fields |
| 96 | + data.packages.forEach((pkg: any) => { |
| 97 | + expect(pkg).toHaveProperty("name") |
| 98 | + expect(pkg).toHaveProperty("unscoped_name") |
| 99 | + expect(pkg).toHaveProperty("owner_github_username") |
| 100 | + expect(pkg).toHaveProperty("creator_account_id") |
| 101 | + expect(pkg).toHaveProperty("latest_package_release_id") |
| 102 | + expect(pkg.is_snippet).toBe(false) |
| 103 | + }) |
| 104 | + |
| 105 | + // Test with limit parameter |
| 106 | + const { data: limitedData } = await axios.get( |
| 107 | + "/api/packages/list_latest?limit=2", |
| 108 | + ) |
| 109 | + expect(limitedData.packages.length).toBe(2) |
| 110 | +}) |
0 commit comments