|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { makePayload } from './preview-initialized-channel'; |
| 4 | + |
| 5 | +describe('makePayload', () => { |
| 6 | + beforeEach(() => { |
| 7 | + vi.useFakeTimers(); |
| 8 | + vi.setSystemTime(new Date('2024-01-01T00:00:00Z')); |
| 9 | + }); |
| 10 | + afterEach(() => { |
| 11 | + vi.useRealTimers(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('new user init session', () => { |
| 15 | + const userAgent = 'Mozilla/5.0'; |
| 16 | + const sessionId = 'session-123'; |
| 17 | + const lastInit = { |
| 18 | + timestamp: Date.now() - 3000, |
| 19 | + body: { |
| 20 | + sessionId, |
| 21 | + payload: { newUser: true }, |
| 22 | + }, |
| 23 | + }; |
| 24 | + |
| 25 | + expect(makePayload(userAgent, lastInit as any, sessionId)).toMatchInlineSnapshot(` |
| 26 | + { |
| 27 | + "isNewUser": true, |
| 28 | + "timeSinceInit": 3000, |
| 29 | + "userAgent": "Mozilla/5.0", |
| 30 | + } |
| 31 | + `); |
| 32 | + }); |
| 33 | + |
| 34 | + it('existing user init session', () => { |
| 35 | + const userAgent = 'Mozilla/5.0'; |
| 36 | + const sessionId = 'session-123'; |
| 37 | + const lastInit = { |
| 38 | + timestamp: Date.now() - 3000, |
| 39 | + body: { |
| 40 | + sessionId, |
| 41 | + payload: {}, |
| 42 | + }, |
| 43 | + }; |
| 44 | + |
| 45 | + expect(makePayload(userAgent, lastInit as any, sessionId)).toMatchInlineSnapshot(` |
| 46 | + { |
| 47 | + "isNewUser": false, |
| 48 | + "timeSinceInit": 3000, |
| 49 | + "userAgent": "Mozilla/5.0", |
| 50 | + } |
| 51 | + `); |
| 52 | + }); |
| 53 | + |
| 54 | + it('no init session', () => { |
| 55 | + const userAgent = 'Mozilla/5.0'; |
| 56 | + const sessionId = 'session-123'; |
| 57 | + const lastInit = undefined; |
| 58 | + |
| 59 | + expect(makePayload(userAgent, lastInit, sessionId)).toMatchInlineSnapshot(` |
| 60 | + { |
| 61 | + "isNewUser": false, |
| 62 | + "timeSinceInit": undefined, |
| 63 | + "userAgent": "Mozilla/5.0", |
| 64 | + } |
| 65 | + `); |
| 66 | + }); |
| 67 | + |
| 68 | + it('init session with different sessionId', () => { |
| 69 | + const userAgent = 'Mozilla/5.0'; |
| 70 | + const sessionId = 'session-123'; |
| 71 | + const lastInit = { |
| 72 | + timestamp: Date.now() - 3000, |
| 73 | + body: { |
| 74 | + sessionId: 'session-456', |
| 75 | + }, |
| 76 | + }; |
| 77 | + |
| 78 | + expect(makePayload(userAgent, lastInit as any, sessionId)).toMatchInlineSnapshot(` |
| 79 | + { |
| 80 | + "isNewUser": false, |
| 81 | + "timeSinceInit": undefined, |
| 82 | + "userAgent": "Mozilla/5.0", |
| 83 | + } |
| 84 | + `); |
| 85 | + }); |
| 86 | +}); |
0 commit comments