|
1 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 |
|
3 | | -import { |
4 | | - getExistingLast30dEndDates, |
5 | | - upsertLast30dDownload, |
6 | | -} from '@crowd/data-access-layer/src/packages' |
| 3 | +import { insertLast30dDownloadIfAbsent } from '@crowd/data-access-layer/src/packages' |
7 | 4 | import type { QueryExecutor } from '@crowd/data-access-layer/src/queryExecutor' |
8 | 5 |
|
9 | 6 | import { monthlyWindowFor, persistPackagist30dWindow } from '../downloads' |
10 | 7 |
|
11 | 8 | vi.mock('@crowd/data-access-layer/src/packages', () => ({ |
12 | | - getExistingLast30dEndDates: vi.fn().mockResolvedValue([]), |
13 | | - upsertLast30dDownload: vi.fn().mockResolvedValue([]), |
| 9 | + insertLast30dDownloadIfAbsent: vi.fn().mockResolvedValue([]), |
14 | 10 | })) |
15 | 11 |
|
16 | | -const mockExistingWindows = vi.mocked(getExistingLast30dEndDates) |
17 | | -const mockWindow = vi.mocked(upsertLast30dDownload) |
| 12 | +const mockWindow = vi.mocked(insertLast30dDownloadIfAbsent) |
18 | 13 |
|
19 | 14 | const qx = {} as QueryExecutor |
20 | 15 | const PURL = 'pkg:composer/monolog/monolog' |
21 | 16 |
|
22 | 17 | beforeEach(() => { |
23 | 18 | vi.clearAllMocks() |
24 | | - mockExistingWindows.mockResolvedValue([]) |
25 | 19 | }) |
26 | 20 |
|
27 | 21 | // Observed rolling window anchored on the 1st of the run month, |
@@ -49,30 +43,31 @@ describe('monthlyWindowFor', () => { |
49 | 43 | // The monthly downloads-30d lane: one window row per purl per month, mirrored to |
50 | 44 | // packages.downloads_last_30d (npm parity). A window already recorded for the month |
51 | 45 | // is never overwritten — the value is the observation closest to the boundary. |
| 46 | +// insertLast30dDownloadIfAbsent does the presence check and the write atomically, so |
| 47 | +// there is no separate "does it exist" call to assert on here — a race is exercised |
| 48 | +// directly against Postgres in downloadsLast30d's own DAL-level coverage. |
52 | 49 | describe('persistPackagist30dWindow', () => { |
53 | 50 | it('writes the window with the mirror and returns the changed fields when the month has no row yet', async () => { |
54 | 51 | mockWindow.mockResolvedValue(['downloads_last_30d.count', 'packages.downloads_last_30d']) |
55 | 52 |
|
56 | 53 | const changedFields = await persistPackagist30dWindow(qx, PURL, 300, '2026-07-01') |
57 | 54 |
|
58 | | - expect(mockExistingWindows).toHaveBeenCalledWith(qx, PURL, '2026-07-01', '2026-07-01') |
59 | 55 | expect(mockWindow).toHaveBeenCalledWith(qx, PURL, '2026-06-01', '2026-07-01', 300, true) |
60 | 56 | expect(changedFields).toEqual(['downloads_last_30d.count', 'packages.downloads_last_30d']) |
61 | 57 | }) |
62 | 58 |
|
63 | | - it('does not overwrite an existing window for the month, and returns no changed fields', async () => { |
64 | | - mockExistingWindows.mockResolvedValue(['2026-07-01']) |
| 59 | + it('returns no changed fields when a window for the month already exists', async () => { |
| 60 | + mockWindow.mockResolvedValue([]) |
65 | 61 |
|
66 | 62 | const changedFields = await persistPackagist30dWindow(qx, PURL, 300, '2026-07-15') |
67 | 63 |
|
68 | | - expect(mockWindow).not.toHaveBeenCalled() |
| 64 | + expect(mockWindow).toHaveBeenCalledWith(qx, PURL, '2026-06-01', '2026-07-01', 300, true) |
69 | 65 | expect(changedFields).toEqual([]) |
70 | 66 | }) |
71 | 67 |
|
72 | 68 | it('writes nothing and returns no changed fields when the registry reported no monthly count', async () => { |
73 | 69 | const changedFields = await persistPackagist30dWindow(qx, PURL, null, '2026-07-01') |
74 | 70 |
|
75 | | - expect(mockExistingWindows).not.toHaveBeenCalled() |
76 | 71 | expect(mockWindow).not.toHaveBeenCalled() |
77 | 72 | expect(changedFields).toEqual([]) |
78 | 73 | }) |
|
0 commit comments