|
| 1 | +import { |
| 2 | + describe, test, expect, vi, beforeEach |
| 3 | +} from 'vitest'; |
| 4 | +import {DateTime} from "luxon"; |
| 5 | +import {mockTimeseries} from '../../mocks/tinybird-organizations-dependency-response.mock'; |
| 6 | +import { |
| 7 | + mockTimeseries as mockLeaderboardTimeseries |
| 8 | +} from '../../mocks/tinybird-organizations-leaderboard-response.mock'; |
| 9 | +import type {OrganizationDependencyResponse} from "~~/server/data/tinybird/organizations-dependency-data-source"; |
| 10 | + |
| 11 | +const mockFetchFromTinybird = vi.fn(); |
| 12 | + |
| 13 | +describe('Organizations Dependency Data Source', () => { |
| 14 | + beforeEach(() => { |
| 15 | + mockFetchFromTinybird.mockClear(); |
| 16 | + |
| 17 | + // Here be dragons! vi.doMock is not hoisted, and thus it is executed after the original import statement. |
| 18 | + // This means that the import for tinybird.ts inside active-organizations-data-source.ts would still be used, |
| 19 | + // and thus not mocked. This means we need to import the module again after the mock is set, whenever we want to |
| 20 | + // use it. |
| 21 | + vi.doMock(import("./tinybird"), () => ({ |
| 22 | + fetchFromTinybird: mockFetchFromTinybird, |
| 23 | + })); |
| 24 | + }) |
| 25 | + |
| 26 | + test('should fetch organizations dependency data with correct parameters', async () => { |
| 27 | + // We have to import this here again because vi.doMock is not hoisted. See the explanation in beforeEach(). |
| 28 | + const {fetchOrganizationDependency} = await import("~~/server/data/tinybird/organizations-dependency-data-source"); |
| 29 | + |
| 30 | + mockFetchFromTinybird.mockResolvedValueOnce(mockTimeseries).mockResolvedValueOnce(mockLeaderboardTimeseries); |
| 31 | + |
| 32 | + const startDate = DateTime.utc(2024, 3, 20); |
| 33 | + const endDate = DateTime.utc(2025, 3, 20); |
| 34 | + |
| 35 | + const filter = { |
| 36 | + project: 'the-linux-kernel-organization', |
| 37 | + startDate, |
| 38 | + endDate |
| 39 | + }; |
| 40 | + |
| 41 | + const result = await fetchOrganizationDependency(filter); |
| 42 | + |
| 43 | + expect(mockFetchFromTinybird).toHaveBeenNthCalledWith( |
| 44 | + 1, |
| 45 | + '/v0/pipes/organization_dependency.json', |
| 46 | + filter |
| 47 | + ) |
| 48 | + expect(mockFetchFromTinybird).toHaveBeenNthCalledWith( |
| 49 | + 2, |
| 50 | + '/v0/pipes/organizations_leaderboard.json', |
| 51 | + { |
| 52 | + ...filter, |
| 53 | + limit: 5 |
| 54 | + } |
| 55 | + ); |
| 56 | + |
| 57 | + const topOrganizationsCount = mockTimeseries.data.length; |
| 58 | + const lastOrganization = mockTimeseries.data.at(-1); |
| 59 | + const topOrganizationsPercentage = lastOrganization?.contributionPercentageRunningTotal || 0; |
| 60 | + const totalOrganizationCount = mockTimeseries.data[0]?.totalOrganizationCount || 0; |
| 61 | + |
| 62 | + const expectedResult: OrganizationDependencyResponse = { |
| 63 | + topOrganizations: { |
| 64 | + count: topOrganizationsCount, |
| 65 | + percentage: topOrganizationsPercentage |
| 66 | + }, |
| 67 | + otherOrganizations: { |
| 68 | + count: Math.max(0, (totalOrganizationCount || 0) - topOrganizationsCount), |
| 69 | + percentage: 100 - topOrganizationsPercentage |
| 70 | + }, |
| 71 | + list: mockLeaderboardTimeseries.data.map((item) => ({ |
| 72 | + logo: item.logo, |
| 73 | + name: item.displayName, |
| 74 | + contributions: item.contributionCount, |
| 75 | + percentage: item.contributionPercentage, |
| 76 | + website: '' |
| 77 | + })) |
| 78 | + }; |
| 79 | + |
| 80 | + expect(result).toEqual(expectedResult); |
| 81 | + }); |
| 82 | + |
| 83 | + // TODO: Add checks for invalid dates, invalid data, sql injections, and other edge cases. |
| 84 | +}); |
0 commit comments