|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { ResourceLimitError } from './ResourceLimitError.ts'; |
| 4 | +import { ErrorCode, ErrorSentinel } from '../constants.ts'; |
| 5 | +import { unmarshalErrorOptions } from '../marshal/unmarshalError.ts'; |
| 6 | +import type { MarshaledOcapError } from '../types.ts'; |
| 7 | + |
| 8 | +describe('ResourceLimitError', () => { |
| 9 | + it('creates a ResourceLimitError with the correct properties', () => { |
| 10 | + const error = new ResourceLimitError('Connection limit exceeded'); |
| 11 | + expect(error).toBeInstanceOf(ResourceLimitError); |
| 12 | + expect(error.code).toBe(ErrorCode.ResourceLimitError); |
| 13 | + expect(error.message).toBe('Connection limit exceeded'); |
| 14 | + expect(error.data).toBeUndefined(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('creates a ResourceLimitError with connection limit data', () => { |
| 18 | + const error = new ResourceLimitError('Connection limit exceeded', { |
| 19 | + data: { |
| 20 | + limitType: 'connection', |
| 21 | + current: 100, |
| 22 | + limit: 100, |
| 23 | + }, |
| 24 | + }); |
| 25 | + expect(error).toBeInstanceOf(ResourceLimitError); |
| 26 | + expect(error.code).toBe(ErrorCode.ResourceLimitError); |
| 27 | + expect(error.message).toBe('Connection limit exceeded'); |
| 28 | + expect(error.data).toStrictEqual({ |
| 29 | + limitType: 'connection', |
| 30 | + current: 100, |
| 31 | + limit: 100, |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('creates a ResourceLimitError with message size limit data', () => { |
| 36 | + const error = new ResourceLimitError('Message size limit exceeded', { |
| 37 | + data: { |
| 38 | + limitType: 'messageSize', |
| 39 | + current: 1048577, |
| 40 | + limit: 1048576, |
| 41 | + }, |
| 42 | + }); |
| 43 | + expect(error).toBeInstanceOf(ResourceLimitError); |
| 44 | + expect(error.code).toBe(ErrorCode.ResourceLimitError); |
| 45 | + expect(error.message).toBe('Message size limit exceeded'); |
| 46 | + expect(error.data).toStrictEqual({ |
| 47 | + limitType: 'messageSize', |
| 48 | + current: 1048577, |
| 49 | + limit: 1048576, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('creates a ResourceLimitError with partial data', () => { |
| 54 | + const error = new ResourceLimitError('Resource limit exceeded', { |
| 55 | + data: { |
| 56 | + limitType: 'connection', |
| 57 | + }, |
| 58 | + }); |
| 59 | + expect(error).toBeInstanceOf(ResourceLimitError); |
| 60 | + expect(error.data).toStrictEqual({ |
| 61 | + limitType: 'connection', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('creates a ResourceLimitError with a cause', () => { |
| 66 | + const cause = new Error('Original error'); |
| 67 | + const error = new ResourceLimitError('Resource limit exceeded', { cause }); |
| 68 | + expect(error).toBeInstanceOf(ResourceLimitError); |
| 69 | + expect(error.code).toBe(ErrorCode.ResourceLimitError); |
| 70 | + expect(error.cause).toBe(cause); |
| 71 | + }); |
| 72 | + |
| 73 | + it('creates a ResourceLimitError with a custom stack', () => { |
| 74 | + const customStack = 'custom stack trace'; |
| 75 | + const error = new ResourceLimitError('Resource limit exceeded', { |
| 76 | + stack: customStack, |
| 77 | + }); |
| 78 | + expect(error).toBeInstanceOf(ResourceLimitError); |
| 79 | + expect(error.stack).toBe(customStack); |
| 80 | + }); |
| 81 | + |
| 82 | + it('unmarshals a valid marshaled ResourceLimitError with connection limit data', () => { |
| 83 | + const marshaledError: MarshaledOcapError = { |
| 84 | + [ErrorSentinel]: true, |
| 85 | + message: 'Connection limit exceeded', |
| 86 | + code: ErrorCode.ResourceLimitError, |
| 87 | + data: { |
| 88 | + limitType: 'connection', |
| 89 | + current: 100, |
| 90 | + limit: 100, |
| 91 | + }, |
| 92 | + stack: 'stack trace', |
| 93 | + }; |
| 94 | + |
| 95 | + const unmarshaledError = ResourceLimitError.unmarshal( |
| 96 | + marshaledError, |
| 97 | + unmarshalErrorOptions, |
| 98 | + ); |
| 99 | + expect(unmarshaledError).toBeInstanceOf(ResourceLimitError); |
| 100 | + expect(unmarshaledError.code).toBe(ErrorCode.ResourceLimitError); |
| 101 | + expect(unmarshaledError.message).toBe('Connection limit exceeded'); |
| 102 | + expect(unmarshaledError.stack).toBe('stack trace'); |
| 103 | + expect(unmarshaledError.data).toStrictEqual({ |
| 104 | + limitType: 'connection', |
| 105 | + current: 100, |
| 106 | + limit: 100, |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('unmarshals a valid marshaled ResourceLimitError with message size limit data', () => { |
| 111 | + const marshaledError: MarshaledOcapError = { |
| 112 | + [ErrorSentinel]: true, |
| 113 | + message: 'Message size limit exceeded', |
| 114 | + code: ErrorCode.ResourceLimitError, |
| 115 | + data: { |
| 116 | + limitType: 'messageSize', |
| 117 | + current: 1048577, |
| 118 | + limit: 1048576, |
| 119 | + }, |
| 120 | + stack: 'stack trace', |
| 121 | + }; |
| 122 | + |
| 123 | + const unmarshaledError = ResourceLimitError.unmarshal( |
| 124 | + marshaledError, |
| 125 | + unmarshalErrorOptions, |
| 126 | + ); |
| 127 | + expect(unmarshaledError).toBeInstanceOf(ResourceLimitError); |
| 128 | + expect(unmarshaledError.code).toBe(ErrorCode.ResourceLimitError); |
| 129 | + expect(unmarshaledError.message).toBe('Message size limit exceeded'); |
| 130 | + expect(unmarshaledError.data).toStrictEqual({ |
| 131 | + limitType: 'messageSize', |
| 132 | + current: 1048577, |
| 133 | + limit: 1048576, |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('unmarshals a valid marshaled ResourceLimitError without data', () => { |
| 138 | + const marshaledError = { |
| 139 | + [ErrorSentinel]: true, |
| 140 | + message: 'Resource limit exceeded', |
| 141 | + code: ErrorCode.ResourceLimitError, |
| 142 | + stack: 'stack trace', |
| 143 | + } as unknown as MarshaledOcapError; |
| 144 | + |
| 145 | + const unmarshaledError = ResourceLimitError.unmarshal( |
| 146 | + marshaledError, |
| 147 | + unmarshalErrorOptions, |
| 148 | + ); |
| 149 | + expect(unmarshaledError).toBeInstanceOf(ResourceLimitError); |
| 150 | + expect(unmarshaledError.code).toBe(ErrorCode.ResourceLimitError); |
| 151 | + expect(unmarshaledError.message).toBe('Resource limit exceeded'); |
| 152 | + expect(unmarshaledError.data).toBeUndefined(); |
| 153 | + }); |
| 154 | + |
| 155 | + it.each([ |
| 156 | + { |
| 157 | + name: 'invalid limitType value', |
| 158 | + marshaledError: { |
| 159 | + [ErrorSentinel]: true, |
| 160 | + message: 'Resource limit exceeded', |
| 161 | + code: ErrorCode.ResourceLimitError, |
| 162 | + data: { |
| 163 | + limitType: 'invalid', |
| 164 | + current: 100, |
| 165 | + limit: 100, |
| 166 | + }, |
| 167 | + stack: 'stack trace', |
| 168 | + } as unknown as MarshaledOcapError, |
| 169 | + expectedError: |
| 170 | + 'At path: data.limitType -- Expected the value to satisfy a union of `literal | literal`, but received: "invalid"', |
| 171 | + }, |
| 172 | + { |
| 173 | + name: 'invalid current type', |
| 174 | + marshaledError: { |
| 175 | + [ErrorSentinel]: true, |
| 176 | + message: 'Resource limit exceeded', |
| 177 | + code: ErrorCode.ResourceLimitError, |
| 178 | + data: { |
| 179 | + limitType: 'connection', |
| 180 | + current: 'not a number', |
| 181 | + limit: 100, |
| 182 | + }, |
| 183 | + stack: 'stack trace', |
| 184 | + } as unknown as MarshaledOcapError, |
| 185 | + expectedError: |
| 186 | + 'At path: data.current -- Expected a number, but received: "not a number"', |
| 187 | + }, |
| 188 | + { |
| 189 | + name: 'invalid limit type', |
| 190 | + marshaledError: { |
| 191 | + [ErrorSentinel]: true, |
| 192 | + message: 'Resource limit exceeded', |
| 193 | + code: ErrorCode.ResourceLimitError, |
| 194 | + data: { |
| 195 | + limitType: 'connection', |
| 196 | + current: 100, |
| 197 | + limit: 'not a number', |
| 198 | + }, |
| 199 | + stack: 'stack trace', |
| 200 | + } as unknown as MarshaledOcapError, |
| 201 | + expectedError: |
| 202 | + 'At path: data.limit -- Expected a number, but received: "not a number"', |
| 203 | + }, |
| 204 | + { |
| 205 | + name: 'wrong error code', |
| 206 | + marshaledError: { |
| 207 | + [ErrorSentinel]: true, |
| 208 | + message: 'Resource limit exceeded', |
| 209 | + code: 'WRONG_ERROR_CODE' as ErrorCode, |
| 210 | + stack: 'stack trace', |
| 211 | + } as unknown as MarshaledOcapError, |
| 212 | + expectedError: |
| 213 | + 'At path: code -- Expected the literal `"RESOURCE_LIMIT_ERROR"`, but received: "WRONG_ERROR_CODE"', |
| 214 | + }, |
| 215 | + { |
| 216 | + name: 'missing required fields', |
| 217 | + marshaledError: { |
| 218 | + [ErrorSentinel]: true, |
| 219 | + message: 'Resource limit exceeded', |
| 220 | + // Missing code field |
| 221 | + } as unknown as MarshaledOcapError, |
| 222 | + expectedError: |
| 223 | + 'At path: code -- Expected the literal `"RESOURCE_LIMIT_ERROR"`, but received: undefined', |
| 224 | + }, |
| 225 | + ])( |
| 226 | + 'throws an error when unmarshaling with $name', |
| 227 | + ({ marshaledError, expectedError }) => { |
| 228 | + expect(() => |
| 229 | + ResourceLimitError.unmarshal(marshaledError, unmarshalErrorOptions), |
| 230 | + ).toThrow(expectedError); |
| 231 | + }, |
| 232 | + ); |
| 233 | +}); |
0 commit comments