|
| 1 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { |
| 3 | + awsDate, |
| 4 | + awsDateTime, |
| 5 | + awsTime, |
| 6 | + awsTimestamp, |
| 7 | + makeId, |
| 8 | +} from '../../../src/appsync-graphql/scalarTypesUtils.js'; |
| 9 | + |
| 10 | +const mockDate = new Date('2025-06-15T10:30:45.123Z'); |
| 11 | +describe('Scalar Types Utils', () => { |
| 12 | + beforeAll(() => { |
| 13 | + vi.useFakeTimers().setSystemTime(mockDate); |
| 14 | + }); |
| 15 | + |
| 16 | + afterAll(() => { |
| 17 | + vi.useRealTimers(); |
| 18 | + }); |
| 19 | + describe('makeId', () => { |
| 20 | + it('should generate a valid UUID', () => { |
| 21 | + const id = makeId(); |
| 22 | + // UUID v4 format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx |
| 23 | + const uuidRegex = |
| 24 | + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; |
| 25 | + expect(id).toMatch(uuidRegex); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should generate unique IDs', () => { |
| 29 | + const id1 = makeId(); |
| 30 | + const id2 = makeId(); |
| 31 | + expect(id1).not.toBe(id2); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('awsDate', () => { |
| 36 | + it('should return a date in YYYY-MM-DD format with Z timezone', () => { |
| 37 | + const result = awsDate(); |
| 38 | + expect(result).toBe('2025-06-15Z'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should handle positive timezone offset', () => { |
| 42 | + const result = awsDate(5); |
| 43 | + expect(result).toBe('2025-06-15+05:00:00'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should handle negative timezone offset', () => { |
| 47 | + const result = awsDate(-8); |
| 48 | + expect(result).toBe('2025-06-15-08:00:00'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle date change with timezone offset', () => { |
| 52 | + const result = awsDate(-11); |
| 53 | + expect(result).toBe('2025-06-14-11:00:00'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle fractional timezone offset', () => { |
| 57 | + const result = awsDate(5.5); |
| 58 | + expect(result).toBe('2025-06-15+05:30:00'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle negative fractional timezone offset', () => { |
| 62 | + const result = awsDate(-9.5); |
| 63 | + expect(result).toBe('2025-06-15-09:30:00'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should throw RangeError for invalid timezone offset', () => { |
| 67 | + expect(() => awsDate(15)).toThrow(RangeError); |
| 68 | + expect(() => awsDate(-13)).toThrow(RangeError); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('awsTime', () => { |
| 73 | + it('should return a time in HH:MM:SS.sss format with Z timezone', () => { |
| 74 | + const result = awsTime(); |
| 75 | + expect(result).toBe('10:30:45.123Z'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle positive timezone offset', () => { |
| 79 | + const result = awsTime(3); |
| 80 | + expect(result).toBe('13:30:45.123+03:00:00'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle negative timezone offset', () => { |
| 84 | + const result = awsTime(-5); |
| 85 | + expect(result).toBe('05:30:45.123-05:00:00'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should handle fractional timezone offset', () => { |
| 89 | + const result = awsTime(5.5); |
| 90 | + expect(result).toBe('16:00:45.123+05:30:00'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should throw RangeError for invalid timezone offset', () => { |
| 94 | + expect(() => awsTime(15)).toThrow(RangeError); |
| 95 | + expect(() => awsTime(-13)).toThrow(RangeError); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('awsDateTime', () => { |
| 100 | + it('should return a datetime in ISO 8601 format with Z timezone', () => { |
| 101 | + const result = awsDateTime(); |
| 102 | + expect(result).toBe('2025-06-15T10:30:45.123Z'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should handle positive timezone offset', () => { |
| 106 | + const result = awsDateTime(2); |
| 107 | + expect(result).toBe('2025-06-15T12:30:45.123+02:00:00'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should handle negative timezone offset', () => { |
| 111 | + const result = awsDateTime(-7); |
| 112 | + expect(result).toBe('2025-06-15T03:30:45.123-07:00:00'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should handle date/time change with timezone offset', () => { |
| 116 | + const result = awsDateTime(-11); |
| 117 | + expect(result).toBe('2025-06-14T23:30:45.123-11:00:00'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should handle fractional timezone offset', () => { |
| 121 | + const result = awsDateTime(5.5); |
| 122 | + expect(result).toBe('2025-06-15T16:00:45.123+05:30:00'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should handle negative fractional timezone offset', () => { |
| 126 | + const result = awsDateTime(-9.5); |
| 127 | + expect(result).toBe('2025-06-15T01:00:45.123-09:30:00'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should throw RangeError for invalid timezone offset', () => { |
| 131 | + expect(() => awsDateTime(15)).toThrow(RangeError); |
| 132 | + expect(() => awsDateTime(-13)).toThrow(RangeError); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('awsTimestamp', () => { |
| 137 | + it('should return current time as Unix timestamp in seconds', () => { |
| 138 | + const result = awsTimestamp(); |
| 139 | + const expected = Math.floor(mockDate.getTime() / 1000); |
| 140 | + expect(result).toBe(expected); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments