|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { |
| 3 | + formatNotionImageUrl, |
| 4 | + getFormattedImageUrlFromBlock, |
| 5 | +} from './formatNotionImageUrl'; |
| 6 | + |
| 7 | +describe('formatNotionImageUrl', () => { |
| 8 | + // 콘솔 에러 메시지 출력 방지 |
| 9 | + const originalConsoleError = console.error; |
| 10 | + beforeEach(() => { |
| 11 | + console.error = vi.fn(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + console.error = originalConsoleError; |
| 16 | + vi.restoreAllMocks(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('유효한 S3 URL을 노션 이미지 URL로 변환해야 함', () => { |
| 20 | + const s3Url = |
| 21 | + 'https://prod-files-secure.s3.us-west-2.amazonaws.com/cd7314a5-d906-43b0-81e7-42eff82c02a3/566f127b-9e73-491d-bee6-5afd075653a2/image.png'; |
| 22 | + const expected = `https://www.notion.so/image/${encodeURIComponent(s3Url)}?cache=v2`; |
| 23 | + |
| 24 | + const result = formatNotionImageUrl(s3Url); |
| 25 | + |
| 26 | + expect(result).toBe(expected); |
| 27 | + }); |
| 28 | + |
| 29 | + it('블록 ID와 사용자 ID를 포함한 URL로 변환해야 함', () => { |
| 30 | + const s3Url = |
| 31 | + 'https://prod-files-secure.s3.us-west-2.amazonaws.com/cd7314a5-d906-43b0-81e7-42eff82c02a3/566f127b-9e73-491d-bee6-5afd075653a2/image.png'; |
| 32 | + const blockId = '17f9c6bf-2b17-8016-bf79-dc83ab79fb78'; |
| 33 | + const userId = '5146391e-8b65-47f2-83b6-2bfe81194f32'; |
| 34 | + |
| 35 | + const expected = `https://www.notion.so/image/${encodeURIComponent(s3Url)}?table=block&id=${blockId}&userId=${userId}&cache=v2`; |
| 36 | + |
| 37 | + const result = formatNotionImageUrl(s3Url, blockId, userId); |
| 38 | + |
| 39 | + expect(result).toBe(expected); |
| 40 | + }); |
| 41 | + |
| 42 | + it('블록 ID만 포함한 URL로 변환해야 함', () => { |
| 43 | + const s3Url = |
| 44 | + 'https://prod-files-secure.s3.us-west-2.amazonaws.com/cd7314a5-d906-43b0-81e7-42eff82c02a3/566f127b-9e73-491d-bee6-5afd075653a2/image.png'; |
| 45 | + const blockId = '17f9c6bf-2b17-8016-bf79-dc83ab79fb78'; |
| 46 | + |
| 47 | + const expected = `https://www.notion.so/image/${encodeURIComponent(s3Url)}?table=block&id=${blockId}&cache=v2`; |
| 48 | + |
| 49 | + const result = formatNotionImageUrl(s3Url, blockId); |
| 50 | + |
| 51 | + expect(result).toBe(expected); |
| 52 | + }); |
| 53 | + |
| 54 | + it('이미 노션 이미지 URL 형식인 경우 그대로 반환해야 함', () => { |
| 55 | + const notionUrl = |
| 56 | + 'https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fcd7314a5-d906-43b0-81e7-42eff82c02a3%2F566f127b-9e73-491d-bee6-5afd075653a2%2Fimage.png?table=block&id=17f9c6bf-2b17-8016-bf79-dc83ab79fb78&cache=v2'; |
| 57 | + |
| 58 | + const result = formatNotionImageUrl(notionUrl); |
| 59 | + |
| 60 | + expect(result).toBe(notionUrl); |
| 61 | + }); |
| 62 | + |
| 63 | + it('빈 URL이나 유효하지 않은 URL은 그대로 반환해야 함', () => { |
| 64 | + expect(formatNotionImageUrl('')).toBe(''); |
| 65 | + expect(formatNotionImageUrl('invalid-url')).toBe('invalid-url'); |
| 66 | + expect(formatNotionImageUrl('http://example.com')).toBe( |
| 67 | + 'http://example.com' |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('URL 변환 중 오류가 발생하면 원래 URL을 반환해야 함', () => { |
| 72 | + const mockUrl = 'https://example.com/image.jpg'; |
| 73 | + |
| 74 | + // encodeURIComponent에서 오류가 발생하도록 모킹 |
| 75 | + const originalEncodeURIComponent = global.encodeURIComponent; |
| 76 | + global.encodeURIComponent = vi.fn(() => { |
| 77 | + throw new Error('인코딩 오류'); |
| 78 | + }); |
| 79 | + |
| 80 | + const result = formatNotionImageUrl(mockUrl); |
| 81 | + |
| 82 | + expect(console.error).toHaveBeenCalled(); |
| 83 | + expect(result).toBe(mockUrl); |
| 84 | + |
| 85 | + // 원래 함수로 복원 |
| 86 | + global.encodeURIComponent = originalEncodeURIComponent; |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('getFormattedImageUrlFromBlock', () => { |
| 91 | + // 콘솔 에러 메시지 출력 방지 |
| 92 | + const originalConsoleError = console.error; |
| 93 | + beforeEach(() => { |
| 94 | + console.error = vi.fn(); |
| 95 | + }); |
| 96 | + |
| 97 | + afterEach(() => { |
| 98 | + console.error = originalConsoleError; |
| 99 | + }); |
| 100 | + |
| 101 | + it('이미지 블록에서 URL을 추출하고 포맷팅해야 함', () => { |
| 102 | + const mockBlock = { |
| 103 | + id: '17f9c6bf-2b17-8016-bf79-dc83ab79fb78', |
| 104 | + type: 'image', |
| 105 | + image: { |
| 106 | + file: { |
| 107 | + url: 'https://prod-files-secure.s3.us-west-2.amazonaws.com/cd7314a5-d906-43b0-81e7-42eff82c02a3/566f127b-9e73-491d-bee6-5afd075653a2/image.png', |
| 108 | + }, |
| 109 | + }, |
| 110 | + last_edited_by: { |
| 111 | + id: '5146391e-8b65-47f2-83b6-2bfe81194f32', |
| 112 | + }, |
| 113 | + }; |
| 114 | + |
| 115 | + const expectedUrl = formatNotionImageUrl( |
| 116 | + mockBlock.image.file.url, |
| 117 | + mockBlock.id, |
| 118 | + mockBlock.last_edited_by.id |
| 119 | + ); |
| 120 | + |
| 121 | + const result = getFormattedImageUrlFromBlock(mockBlock); |
| 122 | + |
| 123 | + expect(result).toBe(expectedUrl); |
| 124 | + }); |
| 125 | + |
| 126 | + it('external URL이 있는 이미지 블록도 처리해야 함', () => { |
| 127 | + const mockBlock = { |
| 128 | + id: '17f9c6bf-2b17-8016-bf79-dc83ab79fb78', |
| 129 | + type: 'image', |
| 130 | + image: { |
| 131 | + external: { |
| 132 | + url: 'https://example.com/image.jpg', |
| 133 | + }, |
| 134 | + }, |
| 135 | + last_edited_by: { |
| 136 | + id: '5146391e-8b65-47f2-83b6-2bfe81194f32', |
| 137 | + }, |
| 138 | + }; |
| 139 | + |
| 140 | + const expectedUrl = formatNotionImageUrl( |
| 141 | + mockBlock.image.external.url, |
| 142 | + mockBlock.id, |
| 143 | + mockBlock.last_edited_by.id |
| 144 | + ); |
| 145 | + |
| 146 | + const result = getFormattedImageUrlFromBlock(mockBlock); |
| 147 | + |
| 148 | + expect(result).toBe(expectedUrl); |
| 149 | + }); |
| 150 | + |
| 151 | + it('이미지 블록이 아닌 경우 null을 반환해야 함', () => { |
| 152 | + const mockBlock = { |
| 153 | + id: '17f9c6bf-2b17-8016-bf79-dc83ab79fb78', |
| 154 | + type: 'paragraph', |
| 155 | + paragraph: { |
| 156 | + rich_text: [], |
| 157 | + }, |
| 158 | + }; |
| 159 | + |
| 160 | + const result = getFormattedImageUrlFromBlock(mockBlock); |
| 161 | + |
| 162 | + expect(result).toBeNull(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('URL이 없는 이미지 블록은 null을 반환해야 함', () => { |
| 166 | + const mockBlock = { |
| 167 | + id: '17f9c6bf-2b17-8016-bf79-dc83ab79fb78', |
| 168 | + type: 'image', |
| 169 | + image: {}, |
| 170 | + }; |
| 171 | + |
| 172 | + const result = getFormattedImageUrlFromBlock(mockBlock); |
| 173 | + |
| 174 | + expect(result).toBeNull(); |
| 175 | + }); |
| 176 | + |
| 177 | + it('블록이 null이면 null을 반환해야 함', () => { |
| 178 | + expect(getFormattedImageUrlFromBlock(null)).toBeNull(); |
| 179 | + }); |
| 180 | + |
| 181 | + it('처리 중 오류가 발생하면 null을 반환해야 함', () => { |
| 182 | + const mockBlock = { |
| 183 | + id: '17f9c6bf-2b17-8016-bf79-dc83ab79fb78', |
| 184 | + type: 'image', |
| 185 | + image: { |
| 186 | + file: { |
| 187 | + get url() { |
| 188 | + throw new Error('액세스 오류'); |
| 189 | + } |
| 190 | + }, |
| 191 | + }, |
| 192 | + }; |
| 193 | + |
| 194 | + // 콘솔 에러 스파이 설정 |
| 195 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 196 | + |
| 197 | + const result = getFormattedImageUrlFromBlock(mockBlock); |
| 198 | + |
| 199 | + expect(console.error).toHaveBeenCalled(); |
| 200 | + expect(result).toBeNull(); |
| 201 | + }); |
| 202 | +}); |
0 commit comments