|
| 1 | +import test from 'ava' |
| 2 | + |
| 3 | +import { |
| 4 | + type ActionAttempt, |
| 5 | + type FailedActionAttempt, |
| 6 | + isSeamActionAttemptError, |
| 7 | + isSeamActionAttemptFailedError, |
| 8 | + isSeamActionAttemptTimeoutError, |
| 9 | + isSeamHttpApiError, |
| 10 | + isSeamHttpInvalidInputError, |
| 11 | + isSeamHttpUnauthorizedError, |
| 12 | + SeamActionAttemptError, |
| 13 | + SeamActionAttemptFailedError, |
| 14 | + SeamActionAttemptTimeoutError, |
| 15 | + SeamHttpApiError, |
| 16 | + SeamHttpInvalidInputError, |
| 17 | + SeamHttpUnauthorizedError, |
| 18 | +} from '@seamapi/http/connect' |
| 19 | + |
| 20 | +const apiError = new SeamHttpApiError( |
| 21 | + { type: 'device_not_found', message: 'Device not found' }, |
| 22 | + 404, |
| 23 | + 'request-1', |
| 24 | +) |
| 25 | + |
| 26 | +const unauthorizedError = new SeamHttpUnauthorizedError('request-1') |
| 27 | + |
| 28 | +const invalidInputError = new SeamHttpInvalidInputError( |
| 29 | + { |
| 30 | + type: 'invalid_input', |
| 31 | + message: 'Invalid input', |
| 32 | + validation_errors: { device_id: { _errors: ['Required'] } }, |
| 33 | + }, |
| 34 | + 400, |
| 35 | + 'request-1', |
| 36 | +) |
| 37 | + |
| 38 | +const pendingActionAttempt = { |
| 39 | + action_attempt_id: 'e2192660-0e45-4a11-9800-eb4d086cca09', |
| 40 | + action_type: 'UNLOCK_DOOR', |
| 41 | + status: 'pending', |
| 42 | + error: null, |
| 43 | + result: null, |
| 44 | +} as unknown as ActionAttempt |
| 45 | + |
| 46 | +const failedActionAttempt = { |
| 47 | + ...pendingActionAttempt, |
| 48 | + status: 'error', |
| 49 | + error: { message: 'Failed', type: 'foo' }, |
| 50 | +} as unknown as FailedActionAttempt<ActionAttempt> |
| 51 | + |
| 52 | +const actionAttemptError = new SeamActionAttemptError( |
| 53 | + 'Something happened', |
| 54 | + pendingActionAttempt, |
| 55 | +) |
| 56 | + |
| 57 | +const actionAttemptFailedError = new SeamActionAttemptFailedError( |
| 58 | + failedActionAttempt, |
| 59 | +) |
| 60 | + |
| 61 | +const actionAttemptTimeoutError = new SeamActionAttemptTimeoutError( |
| 62 | + pendingActionAttempt, |
| 63 | + 100, |
| 64 | +) |
| 65 | + |
| 66 | +const unrelatedValues = [new Error('unrelated'), null, undefined, {}, 'error'] |
| 67 | + |
| 68 | +test('isSeamHttpApiError: matches every Seam API error', (t) => { |
| 69 | + t.true(isSeamHttpApiError(apiError)) |
| 70 | + t.true(isSeamHttpApiError(unauthorizedError)) |
| 71 | + t.true(isSeamHttpApiError(invalidInputError)) |
| 72 | + t.false(isSeamHttpApiError(actionAttemptError)) |
| 73 | + for (const value of unrelatedValues) t.false(isSeamHttpApiError(value)) |
| 74 | +}) |
| 75 | + |
| 76 | +test('isSeamHttpUnauthorizedError: matches only the unauthorized error', (t) => { |
| 77 | + t.true(isSeamHttpUnauthorizedError(unauthorizedError)) |
| 78 | + t.false(isSeamHttpUnauthorizedError(apiError)) |
| 79 | + t.false(isSeamHttpUnauthorizedError(invalidInputError)) |
| 80 | + for (const value of unrelatedValues) { |
| 81 | + t.false(isSeamHttpUnauthorizedError(value)) |
| 82 | + } |
| 83 | +}) |
| 84 | + |
| 85 | +test('isSeamHttpInvalidInputError: matches only the invalid input error', (t) => { |
| 86 | + t.true(isSeamHttpInvalidInputError(invalidInputError)) |
| 87 | + t.false(isSeamHttpInvalidInputError(apiError)) |
| 88 | + t.false(isSeamHttpInvalidInputError(unauthorizedError)) |
| 89 | + for (const value of unrelatedValues) { |
| 90 | + t.false(isSeamHttpInvalidInputError(value)) |
| 91 | + } |
| 92 | +}) |
| 93 | + |
| 94 | +test('isSeamActionAttemptError: matches every action attempt error', (t) => { |
| 95 | + t.true(isSeamActionAttemptError(actionAttemptError)) |
| 96 | + t.true(isSeamActionAttemptError(actionAttemptFailedError)) |
| 97 | + t.true(isSeamActionAttemptError(actionAttemptTimeoutError)) |
| 98 | + t.false(isSeamActionAttemptError(apiError)) |
| 99 | + for (const value of unrelatedValues) t.false(isSeamActionAttemptError(value)) |
| 100 | +}) |
| 101 | + |
| 102 | +test('isSeamActionAttemptFailedError: matches only the failed error', (t) => { |
| 103 | + t.true(isSeamActionAttemptFailedError(actionAttemptFailedError)) |
| 104 | + t.false(isSeamActionAttemptFailedError(actionAttemptError)) |
| 105 | + t.false(isSeamActionAttemptFailedError(actionAttemptTimeoutError)) |
| 106 | + for (const value of unrelatedValues) { |
| 107 | + t.false(isSeamActionAttemptFailedError(value)) |
| 108 | + } |
| 109 | +}) |
| 110 | + |
| 111 | +test('isSeamActionAttemptTimeoutError: matches only the timeout error', (t) => { |
| 112 | + t.true(isSeamActionAttemptTimeoutError(actionAttemptTimeoutError)) |
| 113 | + t.false(isSeamActionAttemptTimeoutError(actionAttemptError)) |
| 114 | + t.false(isSeamActionAttemptTimeoutError(actionAttemptFailedError)) |
| 115 | + for (const value of unrelatedValues) { |
| 116 | + t.false(isSeamActionAttemptTimeoutError(value)) |
| 117 | + } |
| 118 | +}) |
0 commit comments