|
| 1 | +import { isValidLoadLayout, MAX_IMPORT_BOARDS, parseImportString } from './import'; |
| 2 | + |
| 3 | +function b64(json: unknown): string { |
| 4 | + return Buffer.from(JSON.stringify(json)).toString('base64'); |
| 5 | +} |
| 6 | + |
| 7 | +const VALID_MAP = [[0, [[0, 0]]]]; |
| 8 | + |
| 9 | +function makeBoard(overrides: Partial<Record<string, unknown>> = {}): Record<string, unknown> { |
| 10 | + return { id: 'test-id', name: 'Test Board', map: VALID_MAP, ...overrides }; |
| 11 | +} |
| 12 | + |
| 13 | +function makeMah(boards: unknown[] = [makeBoard()]): unknown { |
| 14 | + return { mah: '1.0', boards }; |
| 15 | +} |
| 16 | + |
| 17 | +describe('isValidLoadLayout', () => { |
| 18 | + it('returns false for null', () => { |
| 19 | + expect(isValidLoadLayout(null)).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns false for undefined', () => { |
| 23 | + expect(isValidLoadLayout(undefined)).toBe(false); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns false for a string', () => { |
| 27 | + expect(isValidLoadLayout('hello')).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns false for a number', () => { |
| 31 | + expect(isValidLoadLayout(42)).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns false for an array', () => { |
| 35 | + expect(isValidLoadLayout([])).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns false when name is missing', () => { |
| 39 | + expect(isValidLoadLayout({ map: VALID_MAP })).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns false when name is not a string', () => { |
| 43 | + expect(isValidLoadLayout({ name: 123, map: VALID_MAP })).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns false when name is empty string', () => { |
| 47 | + expect(isValidLoadLayout({ name: '', map: VALID_MAP })).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns false when name is whitespace only', () => { |
| 51 | + expect(isValidLoadLayout({ name: ' ', map: VALID_MAP })).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns false when name exceeds 200 characters', () => { |
| 55 | + expect(isValidLoadLayout({ name: 'a'.repeat(201), map: VALID_MAP })).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns false when map is missing', () => { |
| 59 | + expect(isValidLoadLayout({ name: 'Test' })).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns false when map is not an array', () => { |
| 63 | + expect(isValidLoadLayout({ name: 'Test', map: 'invalid' })).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns false when map is an object', () => { |
| 67 | + expect(isValidLoadLayout({ name: 'Test', map: {} })).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns false when id is not a string', () => { |
| 71 | + expect(isValidLoadLayout({ name: 'Test', map: VALID_MAP, id: 123 })).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns false when id exceeds 200 characters', () => { |
| 75 | + expect(isValidLoadLayout({ name: 'Test', map: VALID_MAP, id: 'a'.repeat(201) })).toBe(false); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns false when by is not a string', () => { |
| 79 | + expect(isValidLoadLayout({ name: 'Test', map: VALID_MAP, by: 99 })).toBe(false); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns false when by exceeds 200 characters', () => { |
| 83 | + expect(isValidLoadLayout({ name: 'Test', map: VALID_MAP, by: 'a'.repeat(201) })).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + it('returns false when cat is not a string', () => { |
| 87 | + expect(isValidLoadLayout({ name: 'Test', map: VALID_MAP, cat: true })).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it('returns false when cat exceeds 200 characters', () => { |
| 91 | + expect(isValidLoadLayout({ name: 'Test', map: VALID_MAP, cat: 'a'.repeat(201) })).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + it('returns true for a valid minimal board (name + map)', () => { |
| 95 | + expect(isValidLoadLayout({ name: 'Test', map: VALID_MAP })).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns true when id is exactly 200 characters', () => { |
| 99 | + expect(isValidLoadLayout({ name: 'Test', map: VALID_MAP, id: 'a'.repeat(200) })).toBe(true); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns true for a fully specified valid board', () => { |
| 103 | + expect(isValidLoadLayout({ id: 'my-id', name: 'Test Board', map: VALID_MAP, by: 'Author', cat: 'Classic' })).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it('returns true when optional fields are undefined', () => { |
| 107 | + expect(isValidLoadLayout({ name: 'Test', map: VALID_MAP, id: undefined, by: undefined, cat: undefined })).toBe(true); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe('parseImportString', () => { |
| 112 | + it('returns [] for null input', () => { |
| 113 | + expect(parseImportString(null)).toEqual([]); |
| 114 | + }); |
| 115 | + |
| 116 | + it('returns [] for empty string input', () => { |
| 117 | + expect(parseImportString('')).toEqual([]); |
| 118 | + }); |
| 119 | + |
| 120 | + it('returns [] for invalid base64', () => { |
| 121 | + expect(parseImportString('!!!not-base64!!!')).toEqual([]); |
| 122 | + }); |
| 123 | + |
| 124 | + it('returns [] for valid base64 but invalid JSON', () => { |
| 125 | + const notJson = Buffer.from('not json at all').toString('base64'); |
| 126 | + expect(parseImportString(notJson)).toEqual([]); |
| 127 | + }); |
| 128 | + |
| 129 | + it('returns [] when parsed JSON is null (triggers outer catch)', () => { |
| 130 | + expect(parseImportString(b64(null))).toEqual([]); |
| 131 | + }); |
| 132 | + |
| 133 | + it('returns [] when mah field is missing', () => { |
| 134 | + expect(parseImportString(b64({ boards: [] }))).toEqual([]); |
| 135 | + }); |
| 136 | + |
| 137 | + it('returns [] when mah version is wrong', () => { |
| 138 | + expect(parseImportString(b64({ mah: '2.0', boards: [] }))).toEqual([]); |
| 139 | + }); |
| 140 | + |
| 141 | + it('returns [] when boards is not an array', () => { |
| 142 | + expect(parseImportString(b64({ mah: '1.0', boards: 'oops' }))).toEqual([]); |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns [] when boards array is empty', () => { |
| 146 | + expect(parseImportString(b64({ mah: '1.0', boards: [] }))).toEqual([]); |
| 147 | + }); |
| 148 | + |
| 149 | + it('returns [] when board count exceeds MAX_IMPORT_BOARDS', () => { |
| 150 | + const boards = Array.from({ length: MAX_IMPORT_BOARDS + 1 }, (_, i) => makeBoard({ id: `id-${i}` })); |
| 151 | + expect(parseImportString(b64({ mah: '1.0', boards }))).toEqual([]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('skips boards with invalid structure', () => { |
| 155 | + const data = b64(makeMah([{ name: 123 }, null, 'string'])); |
| 156 | + expect(parseImportString(data)).toEqual([]); |
| 157 | + }); |
| 158 | + |
| 159 | + it('returns only valid boards from a mixed list', () => { |
| 160 | + const data = b64(makeMah([{ name: 123 }, makeBoard()])); |
| 161 | + const result = parseImportString(data); |
| 162 | + expect(result).toHaveLength(1); |
| 163 | + expect(result[0].name).toBe('Test Board'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('returns all valid boards', () => { |
| 167 | + const boards = [makeBoard({ id: 'id-1', name: 'Board 1' }), makeBoard({ id: 'id-2', name: 'Board 2' })]; |
| 168 | + const result = parseImportString(b64(makeMah(boards))); |
| 169 | + expect(result).toHaveLength(2); |
| 170 | + expect(result[0].id).toBe('id-1'); |
| 171 | + expect(result[1].id).toBe('id-2'); |
| 172 | + }); |
| 173 | +}); |
0 commit comments