|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { subscribe } from './subscribe.js'; |
| 3 | +import type { SubscriptionOptions } from './types.js'; |
| 4 | + |
| 5 | +// Mock the dependencies |
| 6 | +vi.mock(':core/telemetry/events/subscription.js', () => ({ |
| 7 | + logSubscriptionStarted: vi.fn(), |
| 8 | + logSubscriptionCompleted: vi.fn(), |
| 9 | + logSubscriptionError: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('./utils/sdkManager.js', () => ({ |
| 13 | + createEphemeralSDK: vi.fn(() => ({ |
| 14 | + getProvider: vi.fn(() => ({ |
| 15 | + request: vi.fn(), |
| 16 | + disconnect: vi.fn(), |
| 17 | + })), |
| 18 | + })), |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('../public-utilities/spend-permission/index.js', () => ({ |
| 22 | + getHash: vi.fn(() => Promise.resolve('0xmockhash')), |
| 23 | +})); |
| 24 | + |
| 25 | +describe('subscribe with overridePeriodInSecondsForTestnet', () => { |
| 26 | + it('should throw error when overridePeriodInSecondsForTestnet is used without testnet', async () => { |
| 27 | + const options = { |
| 28 | + recurringCharge: '10.00', |
| 29 | + subscriptionOwner: '0x1234567890123456789012345678901234567890', |
| 30 | + overridePeriodInSecondsForTestnet: 300, // 5 minutes |
| 31 | + testnet: false, // This should cause an error |
| 32 | + } as any; // Use 'as any' to bypass TypeScript's discriminated union check for testing |
| 33 | + |
| 34 | + await expect(subscribe(options)).rejects.toThrow( |
| 35 | + 'overridePeriodInSecondsForTestnet is only available for testing on testnet' |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should accept overridePeriodInSecondsForTestnet when testnet is true and include it in result', async () => { |
| 40 | + const options: SubscriptionOptions = { |
| 41 | + recurringCharge: '0.01', |
| 42 | + subscriptionOwner: '0x1234567890123456789012345678901234567890', |
| 43 | + overridePeriodInSecondsForTestnet: 300, // 5 minutes for testing |
| 44 | + testnet: true, // Required for overridePeriodInSecondsForTestnet |
| 45 | + }; |
| 46 | + |
| 47 | + // Mock the provider response |
| 48 | + const mockProvider = { |
| 49 | + request: vi.fn().mockResolvedValue({ |
| 50 | + signature: '0xsignature', |
| 51 | + signedData: { |
| 52 | + message: { |
| 53 | + account: '0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', |
| 54 | + spender: '0x1234567890123456789012345678901234567890', |
| 55 | + token: '0xtoken', |
| 56 | + allowance: '10000', |
| 57 | + period: 300, // Should be 300 seconds, not converted to days |
| 58 | + start: 1234567890, |
| 59 | + end: 999999999, |
| 60 | + salt: '0xsalt', |
| 61 | + extraData: '0x', |
| 62 | + }, |
| 63 | + }, |
| 64 | + }), |
| 65 | + disconnect: vi.fn(), |
| 66 | + }; |
| 67 | + |
| 68 | + const { createEphemeralSDK } = await import('./utils/sdkManager.js'); |
| 69 | + vi.mocked(createEphemeralSDK).mockReturnValue({ |
| 70 | + getProvider: () => mockProvider as any, |
| 71 | + } as any); |
| 72 | + |
| 73 | + const result = await subscribe(options); |
| 74 | + |
| 75 | + // Verify the result includes overridePeriodInSecondsForTestnet |
| 76 | + expect(result).toBeDefined(); |
| 77 | + expect(result.overridePeriodInSecondsForTestnet).toBe(300); |
| 78 | + expect(result.periodInDays).toBe(1); // 300 seconds = 5 minutes = ceil(300/86400) = 1 day |
| 79 | + }); |
| 80 | + |
| 81 | + it('should use periodInDays when overridePeriodInSecondsForTestnet is not provided on testnet', async () => { |
| 82 | + const options: SubscriptionOptions = { |
| 83 | + recurringCharge: '10.00', |
| 84 | + subscriptionOwner: '0x1234567890123456789012345678901234567890', |
| 85 | + periodInDays: 7, // Weekly subscription |
| 86 | + testnet: true, |
| 87 | + }; |
| 88 | + |
| 89 | + // Mock the provider response |
| 90 | + const mockProvider = { |
| 91 | + request: vi.fn().mockResolvedValue({ |
| 92 | + signature: '0xsignature', |
| 93 | + signedData: { |
| 94 | + message: { |
| 95 | + account: '0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', |
| 96 | + spender: '0x1234567890123456789012345678901234567890', |
| 97 | + token: '0xtoken', |
| 98 | + allowance: '10000000', |
| 99 | + period: 604800, // 7 days in seconds |
| 100 | + start: 1234567890, |
| 101 | + end: 999999999, |
| 102 | + salt: '0xsalt', |
| 103 | + extraData: '0x', |
| 104 | + }, |
| 105 | + }, |
| 106 | + }), |
| 107 | + disconnect: vi.fn(), |
| 108 | + }; |
| 109 | + |
| 110 | + const { createEphemeralSDK } = await import('./utils/sdkManager.js'); |
| 111 | + vi.mocked(createEphemeralSDK).mockReturnValue({ |
| 112 | + getProvider: () => mockProvider as any, |
| 113 | + } as any); |
| 114 | + |
| 115 | + const result = await subscribe(options); |
| 116 | + expect(result.periodInDays).toBe(7); |
| 117 | + expect(result.overridePeriodInSecondsForTestnet).toBeUndefined(); // Should not have overridePeriodInSecondsForTestnet when not provided |
| 118 | + }); |
| 119 | + |
| 120 | + it('should include overridePeriodInSecondsForTestnet in result and calculate periodInDays correctly', async () => { |
| 121 | + const options: SubscriptionOptions = { |
| 122 | + recurringCharge: '0.01', |
| 123 | + subscriptionOwner: '0x1234567890123456789012345678901234567890', |
| 124 | + overridePeriodInSecondsForTestnet: 172800, // Exactly 2 days |
| 125 | + testnet: true, |
| 126 | + }; |
| 127 | + |
| 128 | + // Mock the provider response |
| 129 | + const mockProvider = { |
| 130 | + request: vi.fn().mockResolvedValue({ |
| 131 | + signature: '0xsignature', |
| 132 | + signedData: { |
| 133 | + message: { |
| 134 | + account: '0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', |
| 135 | + spender: '0x1234567890123456789012345678901234567890', |
| 136 | + token: '0xtoken', |
| 137 | + allowance: '10000', |
| 138 | + period: 172800, // 2 days in seconds |
| 139 | + start: 1234567890, |
| 140 | + end: 999999999, |
| 141 | + salt: '0xsalt', |
| 142 | + extraData: '0x', |
| 143 | + }, |
| 144 | + }, |
| 145 | + }), |
| 146 | + disconnect: vi.fn(), |
| 147 | + }; |
| 148 | + |
| 149 | + const { createEphemeralSDK } = await import('./utils/sdkManager.js'); |
| 150 | + vi.mocked(createEphemeralSDK).mockReturnValue({ |
| 151 | + getProvider: () => mockProvider as any, |
| 152 | + } as any); |
| 153 | + |
| 154 | + const result = await subscribe(options); |
| 155 | + expect(result.overridePeriodInSecondsForTestnet).toBe(172800); // Should include the exact overridePeriodInSecondsForTestnet |
| 156 | + expect(result.periodInDays).toBe(2); // Should be exactly 2 days |
| 157 | + }); |
| 158 | + |
| 159 | + it('should not include overridePeriodInSecondsForTestnet when using mainnet', async () => { |
| 160 | + const options: SubscriptionOptions = { |
| 161 | + recurringCharge: '10.00', |
| 162 | + subscriptionOwner: '0x1234567890123456789012345678901234567890', |
| 163 | + periodInDays: 30, // Monthly subscription |
| 164 | + testnet: false, // Mainnet |
| 165 | + }; |
| 166 | + |
| 167 | + // Mock the provider response |
| 168 | + const mockProvider = { |
| 169 | + request: vi.fn().mockResolvedValue({ |
| 170 | + signature: '0xsignature', |
| 171 | + signedData: { |
| 172 | + message: { |
| 173 | + account: '0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', |
| 174 | + spender: '0x1234567890123456789012345678901234567890', |
| 175 | + token: '0xtoken', |
| 176 | + allowance: '10000000', |
| 177 | + period: 2592000, // 30 days in seconds |
| 178 | + start: 1234567890, |
| 179 | + end: 999999999, |
| 180 | + salt: '0xsalt', |
| 181 | + extraData: '0x', |
| 182 | + }, |
| 183 | + }, |
| 184 | + }), |
| 185 | + disconnect: vi.fn(), |
| 186 | + }; |
| 187 | + |
| 188 | + const { createEphemeralSDK } = await import('./utils/sdkManager.js'); |
| 189 | + vi.mocked(createEphemeralSDK).mockReturnValue({ |
| 190 | + getProvider: () => mockProvider as any, |
| 191 | + } as any); |
| 192 | + |
| 193 | + const result = await subscribe(options); |
| 194 | + expect(result.periodInDays).toBe(30); |
| 195 | + expect(result.overridePeriodInSecondsForTestnet).toBeUndefined(); // Should not have overridePeriodInSecondsForTestnet on mainnet |
| 196 | + }); |
| 197 | +}); |
0 commit comments