|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import fs from 'fs/promises'; |
| 3 | +import { storePurchaseData, getPurchaseData } from '@lib/purchase-storage'; |
| 4 | +import type { PurchaseData } from '@lib/purchase-storage'; |
| 5 | + |
| 6 | +// Mock fs module |
| 7 | +vi.mock('fs/promises', () => ({ |
| 8 | + default: { |
| 9 | + mkdir: vi.fn(), |
| 10 | + writeFile: vi.fn(), |
| 11 | + readFile: vi.fn(), |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +describe('purchase-storage', () => { |
| 16 | + beforeEach(() => { |
| 17 | + vi.clearAllMocks(); |
| 18 | + |
| 19 | + // Mock successful file operations |
| 20 | + (fs.mkdir as any).mockResolvedValue(undefined); |
| 21 | + (fs.writeFile as any).mockResolvedValue(undefined); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + vi.restoreAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('storePurchaseData', () => { |
| 29 | + it('should store purchase data to JSON file', async () => { |
| 30 | + const purchaseData: PurchaseData = { |
| 31 | + sessionId: 'cs_test_session123', |
| 32 | + purchaseCode: 'ESC-12345678', |
| 33 | + theme: 'corporate', |
| 34 | + organization: 'Test Corp', |
| 35 | + contactName: 'John Doe', |
| 36 | + email: 'test@example.com', |
| 37 | + createdAt: new Date().toISOString(), |
| 38 | + }; |
| 39 | + |
| 40 | + await storePurchaseData(purchaseData); |
| 41 | + |
| 42 | + expect(fs.mkdir).toHaveBeenCalledWith( |
| 43 | + expect.stringMatching(/local-dev[\/\\]purchases$/), |
| 44 | + { recursive: true } |
| 45 | + ); |
| 46 | + |
| 47 | + expect(fs.writeFile).toHaveBeenCalledWith( |
| 48 | + expect.stringMatching(/ESC-12345678\.json$/), |
| 49 | + expect.stringContaining('"purchaseCode":"ESC-12345678"') |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should store complete purchase data with all fields', async () => { |
| 54 | + const purchaseData: PurchaseData = { |
| 55 | + sessionId: 'cs_test_session123', |
| 56 | + purchaseCode: 'ESC-87654321', |
| 57 | + kitType: 'build', |
| 58 | + theme: 'kitchen', |
| 59 | + organization: 'Bakery Inc', |
| 60 | + contactName: 'Jane Baker', |
| 61 | + email: 'jane@bakery.com', |
| 62 | + specialRequirements: 'Gluten-free options', |
| 63 | + amountPaid: 50000, |
| 64 | + currency: 'usd', |
| 65 | + paymentStatus: 'paid', |
| 66 | + createdAt: new Date().toISOString(), |
| 67 | + }; |
| 68 | + |
| 69 | + await storePurchaseData(purchaseData); |
| 70 | + |
| 71 | + const writeCall = (fs.writeFile as any).mock.calls[0]; |
| 72 | + const savedData = JSON.parse(writeCall[1]); |
| 73 | + |
| 74 | + expect(savedData).toMatchObject({ |
| 75 | + sessionId: 'cs_test_session123', |
| 76 | + purchaseCode: 'ESC-87654321', |
| 77 | + kitType: 'build', |
| 78 | + theme: 'kitchen', |
| 79 | + organization: 'Bakery Inc', |
| 80 | + contactName: 'Jane Baker', |
| 81 | + email: 'jane@bakery.com', |
| 82 | + specialRequirements: 'Gluten-free options', |
| 83 | + amountPaid: 50000, |
| 84 | + currency: 'usd', |
| 85 | + paymentStatus: 'paid', |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should handle file system errors', async () => { |
| 90 | + (fs.mkdir as any).mockRejectedValue(new Error('Permission denied')); |
| 91 | + |
| 92 | + const purchaseData: PurchaseData = { |
| 93 | + sessionId: 'cs_test_session123', |
| 94 | + purchaseCode: 'ESC-12345678', |
| 95 | + theme: 'corporate', |
| 96 | + organization: 'Test Corp', |
| 97 | + contactName: 'John Doe', |
| 98 | + email: 'test@example.com', |
| 99 | + createdAt: new Date().toISOString(), |
| 100 | + }; |
| 101 | + |
| 102 | + await expect(storePurchaseData(purchaseData)).rejects.toThrow('Permission denied'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should create directory if it does not exist', async () => { |
| 106 | + const purchaseData: PurchaseData = { |
| 107 | + sessionId: 'cs_test_session123', |
| 108 | + purchaseCode: 'ESC-12345678', |
| 109 | + theme: 'corporate', |
| 110 | + organization: 'Test Corp', |
| 111 | + contactName: 'John Doe', |
| 112 | + email: 'test@example.com', |
| 113 | + createdAt: new Date().toISOString(), |
| 114 | + }; |
| 115 | + |
| 116 | + await storePurchaseData(purchaseData); |
| 117 | + |
| 118 | + expect(fs.mkdir).toHaveBeenCalledWith( |
| 119 | + expect.any(String), |
| 120 | + { recursive: true } |
| 121 | + ); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('getPurchaseData', () => { |
| 126 | + it('should retrieve purchase data by code', async () => { |
| 127 | + const mockData: PurchaseData = { |
| 128 | + sessionId: 'cs_test_session123', |
| 129 | + purchaseCode: 'ESC-12345678', |
| 130 | + theme: 'corporate', |
| 131 | + organization: 'Test Corp', |
| 132 | + contactName: 'John Doe', |
| 133 | + email: 'test@example.com', |
| 134 | + createdAt: '2025-11-20T00:00:00.000Z', |
| 135 | + }; |
| 136 | + |
| 137 | + (fs.readFile as any).mockResolvedValue(JSON.stringify(mockData)); |
| 138 | + |
| 139 | + const result = await getPurchaseData('ESC-12345678'); |
| 140 | + |
| 141 | + expect(result).toEqual(mockData); |
| 142 | + expect(fs.readFile).toHaveBeenCalledWith( |
| 143 | + expect.stringMatching(/ESC-12345678\.json$/), |
| 144 | + 'utf-8' |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should return null for non-existent purchase code', async () => { |
| 149 | + const error: NodeJS.ErrnoException = new Error('File not found'); |
| 150 | + error.code = 'ENOENT'; |
| 151 | + (fs.readFile as any).mockRejectedValue(error); |
| 152 | + |
| 153 | + const result = await getPurchaseData('ESC-NOTFOUND'); |
| 154 | + |
| 155 | + expect(result).toBeNull(); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should throw error for other file system errors', async () => { |
| 159 | + (fs.readFile as any).mockRejectedValue(new Error('Permission denied')); |
| 160 | + |
| 161 | + await expect(getPurchaseData('ESC-12345678')).rejects.toThrow('Permission denied'); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should parse JSON data correctly', async () => { |
| 165 | + const mockData: PurchaseData = { |
| 166 | + sessionId: 'cs_test_session123', |
| 167 | + purchaseCode: 'ESC-99999999', |
| 168 | + kitType: 'build', |
| 169 | + theme: 'kitchen', |
| 170 | + organization: 'Test Org', |
| 171 | + contactName: 'Test User', |
| 172 | + email: 'test@test.com', |
| 173 | + specialRequirements: 'Test requirements', |
| 174 | + amountPaid: 50000, |
| 175 | + currency: 'usd', |
| 176 | + paymentStatus: 'paid', |
| 177 | + createdAt: '2025-11-20T00:00:00.000Z', |
| 178 | + }; |
| 179 | + |
| 180 | + (fs.readFile as any).mockResolvedValue(JSON.stringify(mockData, null, 2)); |
| 181 | + |
| 182 | + const result = await getPurchaseData('ESC-99999999'); |
| 183 | + |
| 184 | + expect(result).toEqual(mockData); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments