|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | | -import { buildWorkflowSuspensionMessage, getWorkflowRunStreamId } from './util'; |
| 2 | +import { |
| 3 | + buildWorkflowSuspensionMessage, |
| 4 | + getWorkflowRunStreamId, |
| 5 | + parseDurationToDate, |
| 6 | +} from './util'; |
3 | 7 |
|
4 | 8 | describe('buildWorkflowSuspensionMessage', () => { |
5 | 9 | const runId = 'test-run-123'; |
@@ -165,3 +169,174 @@ describe('getWorkflowRunStreamId', () => { |
165 | 169 | expect(result.includes('_user')).toBe(true); |
166 | 170 | }); |
167 | 171 | }); |
| 172 | + |
| 173 | +describe('parseDurationToDate', () => { |
| 174 | + describe('string durations', () => { |
| 175 | + it('should parse seconds', () => { |
| 176 | + const before = Date.now(); |
| 177 | + const result = parseDurationToDate('5s'); |
| 178 | + const after = Date.now(); |
| 179 | + expect(result.getTime()).toBeGreaterThanOrEqual(before + 5000); |
| 180 | + expect(result.getTime()).toBeLessThanOrEqual(after + 5000); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should parse minutes', () => { |
| 184 | + const before = Date.now(); |
| 185 | + const result = parseDurationToDate('2m'); |
| 186 | + const after = Date.now(); |
| 187 | + const expected = before + 120000; |
| 188 | + expect(result.getTime()).toBeGreaterThanOrEqual(expected); |
| 189 | + expect(result.getTime()).toBeLessThanOrEqual(after + 120000); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should parse hours', () => { |
| 193 | + const before = Date.now(); |
| 194 | + const result = parseDurationToDate('1h'); |
| 195 | + const after = Date.now(); |
| 196 | + const expected = before + 3600000; |
| 197 | + expect(result.getTime()).toBeGreaterThanOrEqual(expected); |
| 198 | + expect(result.getTime()).toBeLessThanOrEqual(after + 3600000); |
| 199 | + }); |
| 200 | + |
| 201 | + it('should parse days', () => { |
| 202 | + const before = Date.now(); |
| 203 | + const result = parseDurationToDate('1d'); |
| 204 | + const after = Date.now(); |
| 205 | + const expected = before + 86400000; |
| 206 | + expect(result.getTime()).toBeGreaterThanOrEqual(expected); |
| 207 | + expect(result.getTime()).toBeLessThanOrEqual(after + 86400000); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should parse milliseconds', () => { |
| 211 | + const before = Date.now(); |
| 212 | + const result = parseDurationToDate('500ms'); |
| 213 | + const after = Date.now(); |
| 214 | + const expected = before + 500; |
| 215 | + expect(result.getTime()).toBeGreaterThanOrEqual(expected); |
| 216 | + expect(result.getTime()).toBeLessThanOrEqual(after + 500); |
| 217 | + }); |
| 218 | + |
| 219 | + it('should throw error for invalid string', () => { |
| 220 | + expect(() => |
| 221 | + parseDurationToDate( |
| 222 | + // @ts-expect-error |
| 223 | + 'invalid' |
| 224 | + ) |
| 225 | + ).toThrow( |
| 226 | + 'Invalid duration: "invalid". Expected a valid duration string like "1s", "1m", "1h", etc.' |
| 227 | + ); |
| 228 | + }); |
| 229 | + |
| 230 | + it('should throw error for negative duration string', () => { |
| 231 | + expect(() => parseDurationToDate('-1s')).toThrow( |
| 232 | + 'Invalid duration: "-1s". Expected a valid duration string like "1s", "1m", "1h", etc.' |
| 233 | + ); |
| 234 | + }); |
| 235 | + }); |
| 236 | + |
| 237 | + describe('number durations (milliseconds)', () => { |
| 238 | + it('should parse zero milliseconds', () => { |
| 239 | + const before = Date.now(); |
| 240 | + const result = parseDurationToDate(0); |
| 241 | + const after = Date.now(); |
| 242 | + expect(result.getTime()).toBeGreaterThanOrEqual(before); |
| 243 | + expect(result.getTime()).toBeLessThanOrEqual(after); |
| 244 | + }); |
| 245 | + |
| 246 | + it('should parse positive milliseconds', () => { |
| 247 | + const before = Date.now(); |
| 248 | + const result = parseDurationToDate(10000); |
| 249 | + const after = Date.now(); |
| 250 | + const expected = before + 10000; |
| 251 | + expect(result.getTime()).toBeGreaterThanOrEqual(expected); |
| 252 | + expect(result.getTime()).toBeLessThanOrEqual(after + 10000); |
| 253 | + }); |
| 254 | + |
| 255 | + it('should parse large milliseconds', () => { |
| 256 | + const before = Date.now(); |
| 257 | + const result = parseDurationToDate(1000000); |
| 258 | + const after = Date.now(); |
| 259 | + const expected = before + 1000000; |
| 260 | + expect(result.getTime()).toBeGreaterThanOrEqual(expected); |
| 261 | + expect(result.getTime()).toBeLessThanOrEqual(after + 1000000); |
| 262 | + }); |
| 263 | + |
| 264 | + it('should throw error for negative number', () => { |
| 265 | + expect(() => parseDurationToDate(-1000)).toThrow( |
| 266 | + 'Invalid duration: -1000. Expected a non-negative finite number of milliseconds.' |
| 267 | + ); |
| 268 | + }); |
| 269 | + |
| 270 | + it('should throw error for NaN', () => { |
| 271 | + expect(() => parseDurationToDate(NaN)).toThrow( |
| 272 | + 'Invalid duration: NaN. Expected a non-negative finite number of milliseconds.' |
| 273 | + ); |
| 274 | + }); |
| 275 | + |
| 276 | + it('should throw error for Infinity', () => { |
| 277 | + expect(() => parseDurationToDate(Infinity)).toThrow( |
| 278 | + 'Invalid duration: Infinity. Expected a non-negative finite number of milliseconds.' |
| 279 | + ); |
| 280 | + }); |
| 281 | + |
| 282 | + it('should throw error for -Infinity', () => { |
| 283 | + expect(() => parseDurationToDate(-Infinity)).toThrow( |
| 284 | + 'Invalid duration: -Infinity. Expected a non-negative finite number of milliseconds.' |
| 285 | + ); |
| 286 | + }); |
| 287 | + }); |
| 288 | + |
| 289 | + describe('Date objects', () => { |
| 290 | + it('should return Date instance directly', () => { |
| 291 | + const targetTime = Date.now() + 60000; |
| 292 | + const futureDate = new Date(targetTime); |
| 293 | + const result = parseDurationToDate(futureDate); |
| 294 | + expect(result).toBe(futureDate); |
| 295 | + expect(result.getTime()).toBe(targetTime); |
| 296 | + }); |
| 297 | + |
| 298 | + it('should handle past dates', () => { |
| 299 | + const targetTime = Date.now() - 60000; |
| 300 | + const pastDate = new Date(targetTime); |
| 301 | + const result = parseDurationToDate(pastDate); |
| 302 | + expect(result).toBe(pastDate); |
| 303 | + expect(result.getTime()).toBe(targetTime); |
| 304 | + }); |
| 305 | + |
| 306 | + it('should handle date-like objects from deserialization', () => { |
| 307 | + const targetTime = Date.now() + 30000; |
| 308 | + const dateLike = { |
| 309 | + getTime: () => targetTime, |
| 310 | + }; |
| 311 | + const result = parseDurationToDate(dateLike as any); |
| 312 | + expect(result.getTime()).toBe(targetTime); |
| 313 | + expect(result instanceof Date).toBe(true); |
| 314 | + }); |
| 315 | + }); |
| 316 | + |
| 317 | + describe('invalid inputs', () => { |
| 318 | + it('should throw error for null', () => { |
| 319 | + expect(() => parseDurationToDate(null as any)).toThrow( |
| 320 | + 'Invalid duration parameter. Expected a duration string, number (milliseconds), or Date object.' |
| 321 | + ); |
| 322 | + }); |
| 323 | + |
| 324 | + it('should throw error for undefined', () => { |
| 325 | + expect(() => parseDurationToDate(undefined as any)).toThrow( |
| 326 | + 'Invalid duration parameter. Expected a duration string, number (milliseconds), or Date object.' |
| 327 | + ); |
| 328 | + }); |
| 329 | + |
| 330 | + it('should throw error for boolean', () => { |
| 331 | + expect(() => parseDurationToDate(true as any)).toThrow( |
| 332 | + 'Invalid duration parameter. Expected a duration string, number (milliseconds), or Date object.' |
| 333 | + ); |
| 334 | + }); |
| 335 | + |
| 336 | + it('should throw error for object without getTime', () => { |
| 337 | + expect(() => parseDurationToDate({} as any)).toThrow( |
| 338 | + 'Invalid duration parameter. Expected a duration string, number (milliseconds), or Date object.' |
| 339 | + ); |
| 340 | + }); |
| 341 | + }); |
| 342 | +}); |
0 commit comments