|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { |
| 4 | + INITIAL_ROBOT_AUTH_STATE, |
| 5 | + logInOrRefresh, |
| 6 | + logOutOrTimeOut, |
| 7 | + robotAuthReducer, |
| 8 | +} from '../slice' |
| 9 | + |
| 10 | +import type { RobotAuthState } from '../slice' |
| 11 | + |
| 12 | +describe('robotAuthReducer', () => { |
| 13 | + it('uses empty object as initial state', () => { |
| 14 | + expect( |
| 15 | + robotAuthReducer(undefined, { type: 'not-handled' } as any) |
| 16 | + ).toStrictEqual({}) |
| 17 | + }) |
| 18 | + |
| 19 | + it('handles logins and login refreshes', () => { |
| 20 | + let state: RobotAuthState = INITIAL_ROBOT_AUTH_STATE |
| 21 | + |
| 22 | + // Log in to first robot: |
| 23 | + state = robotAuthReducer( |
| 24 | + INITIAL_ROBOT_AUTH_STATE, |
| 25 | + logInOrRefresh({ |
| 26 | + robotName: 'testRobotNameA', |
| 27 | + username: 'testUserA', |
| 28 | + accessToken: 'testAccessTokenA', |
| 29 | + refreshToken: 'testRefreshTokenA', |
| 30 | + expiresAt: 1234, |
| 31 | + }) |
| 32 | + ) |
| 33 | + expect(state).toStrictEqual({ |
| 34 | + testRobotNameA: { |
| 35 | + username: 'testUserA', |
| 36 | + accessToken: 'testAccessTokenA', |
| 37 | + refreshToken: 'testRefreshTokenA', |
| 38 | + expiresAt: 1234, |
| 39 | + }, |
| 40 | + }) |
| 41 | + |
| 42 | + // Log in to second robot: |
| 43 | + state = robotAuthReducer( |
| 44 | + state, |
| 45 | + logInOrRefresh({ |
| 46 | + robotName: 'testRobotNameB', |
| 47 | + username: 'testUserB', |
| 48 | + accessToken: 'testAccessTokenB', |
| 49 | + refreshToken: null, |
| 50 | + expiresAt: 5678, |
| 51 | + }) |
| 52 | + ) |
| 53 | + expect(state).toStrictEqual({ |
| 54 | + testRobotNameA: { |
| 55 | + username: 'testUserA', |
| 56 | + accessToken: 'testAccessTokenA', |
| 57 | + refreshToken: 'testRefreshTokenA', |
| 58 | + expiresAt: 1234, |
| 59 | + }, |
| 60 | + testRobotNameB: { |
| 61 | + username: 'testUserB', |
| 62 | + accessToken: 'testAccessTokenB', |
| 63 | + refreshToken: null, |
| 64 | + expiresAt: 5678, |
| 65 | + }, |
| 66 | + }) |
| 67 | + |
| 68 | + // Refresh login for first robot: |
| 69 | + state = robotAuthReducer( |
| 70 | + state, |
| 71 | + logInOrRefresh({ |
| 72 | + robotName: 'testRobotNameA', |
| 73 | + username: 'testUserARefreshed', |
| 74 | + accessToken: 'testAccessTokenARefreshed', |
| 75 | + refreshToken: 'testRefreshTokenARefreshed', |
| 76 | + expiresAt: 4321, |
| 77 | + }) |
| 78 | + ) |
| 79 | + expect(state).toStrictEqual({ |
| 80 | + testRobotNameA: { |
| 81 | + username: 'testUserARefreshed', |
| 82 | + accessToken: 'testAccessTokenARefreshed', |
| 83 | + refreshToken: 'testRefreshTokenARefreshed', |
| 84 | + expiresAt: 4321, |
| 85 | + }, |
| 86 | + testRobotNameB: { |
| 87 | + username: 'testUserB', |
| 88 | + accessToken: 'testAccessTokenB', |
| 89 | + refreshToken: null, |
| 90 | + expiresAt: 5678, |
| 91 | + }, |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + it('handles logouts', () => { |
| 96 | + const initialState: RobotAuthState = { |
| 97 | + testRobotNameA: { |
| 98 | + username: 'testUserA', |
| 99 | + accessToken: 'testAccessTokenA', |
| 100 | + refreshToken: 'testRefreshTokenA', |
| 101 | + expiresAt: 1234, |
| 102 | + }, |
| 103 | + testRobotNameB: { |
| 104 | + username: 'testUserB', |
| 105 | + accessToken: 'testAccessTokenB', |
| 106 | + refreshToken: 'testRefreshTokenB', |
| 107 | + expiresAt: 5678, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + const newState = robotAuthReducer( |
| 112 | + initialState, |
| 113 | + logOutOrTimeOut({ |
| 114 | + robotName: 'testRobotNameB', |
| 115 | + }) |
| 116 | + ) |
| 117 | + expect(newState).toStrictEqual({ |
| 118 | + testRobotNameA: initialState.testRobotNameA, |
| 119 | + }) |
| 120 | + }) |
| 121 | +}) |
0 commit comments