|
| 1 | +import { emitEvent } from 'src/core/events/emitter.js' |
| 2 | +import refreshCollectionLogBuilder from 'src/core/log-builders/collections/refresh/refresh.js' |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +vi.mock('src/core/events/emitter.js', () => ({ |
| 6 | + emitEvent: vi.fn(), |
| 7 | +})) |
| 8 | + |
| 9 | +describe('refresh collection hook', () => { |
| 10 | + beforeEach(() => { |
| 11 | + vi.clearAllMocks() |
| 12 | + }) |
| 13 | + it('should not log if refresh hook is disabled', async () => { |
| 14 | + const mockArgs = { |
| 15 | + collection: { config: { slug: 'users' } }, |
| 16 | + req: { |
| 17 | + headers: { get: () => 'test-agent' }, |
| 18 | + }, |
| 19 | + } |
| 20 | + |
| 21 | + const context = { |
| 22 | + userHookConfig: { |
| 23 | + hooks: { |
| 24 | + refresh: { |
| 25 | + refresh: { enabled: false }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + const user = { id: 'user1' } |
| 32 | + |
| 33 | + await refreshCollectionLogBuilder({ args: mockArgs, context, user } as any) |
| 34 | + |
| 35 | + expect(emitEvent).not.toHaveBeenCalled() |
| 36 | + }) |
| 37 | + |
| 38 | + it('should log if refresh hook is enabled', async () => { |
| 39 | + const mockArgs = { |
| 40 | + collection: { config: { slug: 'users' } }, |
| 41 | + req: { |
| 42 | + headers: { get: () => 'Mozilla/5.0' }, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + const context = { |
| 47 | + userHookConfig: { |
| 48 | + hooks: { |
| 49 | + refresh: { |
| 50 | + refresh: { enabled: true }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + const user = { id: 'user123' } |
| 57 | + |
| 58 | + await refreshCollectionLogBuilder({ args: mockArgs, context, user } as any) |
| 59 | + |
| 60 | + expect(emitEvent).toHaveBeenCalledWith( |
| 61 | + 'logGenerated', |
| 62 | + expect.objectContaining({ |
| 63 | + action: 'refresh', |
| 64 | + collection: 'users', |
| 65 | + timestamp: expect.any(Date), |
| 66 | + user: 'user123', |
| 67 | + userAgent: 'Mozilla/5.0', |
| 68 | + }), |
| 69 | + ) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should log with null user if user is not defined', async () => { |
| 73 | + const mockArgs = { |
| 74 | + collection: { config: { slug: 'users' } }, |
| 75 | + req: { |
| 76 | + headers: { get: () => 'Safari' }, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + const context = { |
| 81 | + userHookConfig: { |
| 82 | + hooks: { |
| 83 | + refresh: { |
| 84 | + refresh: { enabled: true }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + const user = null |
| 91 | + |
| 92 | + await refreshCollectionLogBuilder({ args: mockArgs, context, user } as any) |
| 93 | + |
| 94 | + expect(emitEvent).toHaveBeenCalledWith( |
| 95 | + 'logGenerated', |
| 96 | + expect.objectContaining({ |
| 97 | + action: 'refresh', |
| 98 | + collection: 'users', |
| 99 | + timestamp: expect.any(Date), |
| 100 | + user: null, |
| 101 | + userAgent: 'Safari', |
| 102 | + }), |
| 103 | + ) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should handle missing user-agent gracefully', async () => { |
| 107 | + const mockArgs = { |
| 108 | + collection: { config: { slug: 'users' } }, |
| 109 | + req: { |
| 110 | + headers: { get: () => null }, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + const context = { |
| 115 | + userHookConfig: { |
| 116 | + hooks: { |
| 117 | + refresh: { |
| 118 | + refresh: { enabled: true }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + const user = { id: 'user456' } |
| 125 | + |
| 126 | + await refreshCollectionLogBuilder({ args: mockArgs, context, user } as any) |
| 127 | + |
| 128 | + expect(emitEvent).toHaveBeenCalledWith( |
| 129 | + 'logGenerated', |
| 130 | + expect.objectContaining({ |
| 131 | + action: 'refresh', |
| 132 | + collection: 'users', |
| 133 | + timestamp: expect.any(Date), |
| 134 | + user: 'user456', |
| 135 | + userAgent: 'unknown', |
| 136 | + }), |
| 137 | + ) |
| 138 | + }) |
| 139 | +}) |
0 commit comments