|
| 1 | +import {clientId, applicationId} from './identity.js' |
| 2 | +import {Environment} from '../context/service.js' |
| 3 | +import {BugError} from '../../../public/node/error.js' |
| 4 | +import {describe, test, expect, vi} from 'vitest' |
| 5 | + |
| 6 | +vi.mock('../context/service.js') |
| 7 | + |
| 8 | +const mockServiceEnvironment = vi.mocked(await import('../context/service.js')).serviceEnvironment |
| 9 | + |
| 10 | +describe('identity', () => { |
| 11 | + describe('clientId', () => { |
| 12 | + test.each([ |
| 13 | + { |
| 14 | + environment: Environment.Local, |
| 15 | + expected: 'e5380e02-312a-7408-5718-e07017e9cf52', |
| 16 | + description: 'local environment', |
| 17 | + }, |
| 18 | + { |
| 19 | + environment: Environment.Production, |
| 20 | + expected: 'fbdb2649-e327-4907-8f67-908d24cfd7e3', |
| 21 | + description: 'production environment', |
| 22 | + }, |
| 23 | + { |
| 24 | + environment: Environment.Spin, |
| 25 | + expected: 'e5380e02-312a-7408-5718-e07017e9cf52', |
| 26 | + description: 'spin environment (default)', |
| 27 | + }, |
| 28 | + ])('returns correct client ID for $description', ({environment, expected}) => { |
| 29 | + // Given |
| 30 | + mockServiceEnvironment.mockReturnValue(environment) |
| 31 | + |
| 32 | + // When |
| 33 | + const result = clientId() |
| 34 | + |
| 35 | + // Then |
| 36 | + expect(result).toBe(expected) |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + describe('applicationId', () => { |
| 41 | + test.each([ |
| 42 | + // Admin API |
| 43 | + { |
| 44 | + api: 'admin' as const, |
| 45 | + environment: Environment.Local, |
| 46 | + expected: 'e92482cebb9bfb9fb5a0199cc770fde3de6c8d16b798ee73e36c9d815e070e52', |
| 47 | + description: 'admin API in local environment', |
| 48 | + }, |
| 49 | + { |
| 50 | + api: 'admin' as const, |
| 51 | + environment: Environment.Production, |
| 52 | + expected: '7ee65a63608843c577db8b23c4d7316ea0a01bd2f7594f8a9c06ea668c1b775c', |
| 53 | + description: 'admin API in production environment', |
| 54 | + }, |
| 55 | + { |
| 56 | + api: 'admin' as const, |
| 57 | + environment: Environment.Spin, |
| 58 | + expected: 'e92482cebb9bfb9fb5a0199cc770fde3de6c8d16b798ee73e36c9d815e070e52', |
| 59 | + description: 'admin API in spin environment', |
| 60 | + }, |
| 61 | + // Partners API |
| 62 | + { |
| 63 | + api: 'partners' as const, |
| 64 | + environment: Environment.Local, |
| 65 | + expected: 'df89d73339ac3c6c5f0a98d9ca93260763e384d51d6038da129889c308973978', |
| 66 | + description: 'partners API in local environment', |
| 67 | + }, |
| 68 | + { |
| 69 | + api: 'partners' as const, |
| 70 | + environment: Environment.Production, |
| 71 | + expected: '271e16d403dfa18082ffb3d197bd2b5f4479c3fc32736d69296829cbb28d41a6', |
| 72 | + description: 'partners API in production environment', |
| 73 | + }, |
| 74 | + { |
| 75 | + api: 'partners' as const, |
| 76 | + environment: Environment.Spin, |
| 77 | + expected: 'df89d73339ac3c6c5f0a98d9ca93260763e384d51d6038da129889c308973978', |
| 78 | + description: 'partners API in spin environment', |
| 79 | + }, |
| 80 | + // Storefront-renderer API |
| 81 | + { |
| 82 | + api: 'storefront-renderer' as const, |
| 83 | + environment: Environment.Local, |
| 84 | + expected: '46f603de-894f-488d-9471-5b721280ff49', |
| 85 | + description: 'storefront-renderer API in local environment', |
| 86 | + }, |
| 87 | + { |
| 88 | + api: 'storefront-renderer' as const, |
| 89 | + environment: Environment.Production, |
| 90 | + expected: 'ee139b3d-5861-4d45-b387-1bc3ada7811c', |
| 91 | + description: 'storefront-renderer API in production environment', |
| 92 | + }, |
| 93 | + { |
| 94 | + api: 'storefront-renderer' as const, |
| 95 | + environment: Environment.Spin, |
| 96 | + expected: '46f603de-894f-488d-9471-5b721280ff49', |
| 97 | + description: 'storefront-renderer API in spin environment', |
| 98 | + }, |
| 99 | + // Business-platform API |
| 100 | + { |
| 101 | + api: 'business-platform' as const, |
| 102 | + environment: Environment.Local, |
| 103 | + expected: 'ace6dc89-b526-456d-a942-4b8ef6acda4b', |
| 104 | + description: 'business-platform API in local environment', |
| 105 | + }, |
| 106 | + { |
| 107 | + api: 'business-platform' as const, |
| 108 | + environment: Environment.Production, |
| 109 | + expected: '32ff8ee5-82b8-4d93-9f8a-c6997cefb7dc', |
| 110 | + description: 'business-platform API in production environment', |
| 111 | + }, |
| 112 | + { |
| 113 | + api: 'business-platform' as const, |
| 114 | + environment: Environment.Spin, |
| 115 | + expected: 'ace6dc89-b526-456d-a942-4b8ef6acda4b', |
| 116 | + description: 'business-platform API in spin environment', |
| 117 | + }, |
| 118 | + // App-management API |
| 119 | + { |
| 120 | + api: 'app-management' as const, |
| 121 | + environment: Environment.Production, |
| 122 | + expected: '7ee65a63608843c577db8b23c4d7316ea0a01bd2f7594f8a9c06ea668c1b775c', |
| 123 | + description: 'app-management API in production environment', |
| 124 | + }, |
| 125 | + { |
| 126 | + api: 'app-management' as const, |
| 127 | + environment: Environment.Local, |
| 128 | + expected: 'e92482cebb9bfb9fb5a0199cc770fde3de6c8d16b798ee73e36c9d815e070e52', |
| 129 | + description: 'app-management API in local environment', |
| 130 | + }, |
| 131 | + { |
| 132 | + api: 'app-management' as const, |
| 133 | + environment: Environment.Spin, |
| 134 | + expected: 'e92482cebb9bfb9fb5a0199cc770fde3de6c8d16b798ee73e36c9d815e070e52', |
| 135 | + description: 'app-management API in spin environment', |
| 136 | + }, |
| 137 | + ])('returns correct application ID for $description', ({api, environment, expected}) => { |
| 138 | + // Given |
| 139 | + mockServiceEnvironment.mockReturnValue(environment) |
| 140 | + |
| 141 | + // When |
| 142 | + const result = applicationId(api) |
| 143 | + |
| 144 | + // Then |
| 145 | + expect(result).toBe(expected) |
| 146 | + }) |
| 147 | + |
| 148 | + test('throws BugError for unknown API type', () => { |
| 149 | + // Given |
| 150 | + const unknownAPI = 'unknown-api' as any |
| 151 | + |
| 152 | + // When/Then |
| 153 | + expect(() => applicationId(unknownAPI)).toThrow(BugError) |
| 154 | + expect(() => applicationId(unknownAPI)).toThrow('Application id for API of type: unknown-api') |
| 155 | + }) |
| 156 | + }) |
| 157 | +}) |
0 commit comments