|
| 1 | +import { http, HttpResponse } from 'msw'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { server, validateHeaders } from '../../mock-server'; |
| 5 | +import { createBackendApiClient } from '../factory'; |
| 6 | + |
| 7 | +describe('MachineAPI', () => { |
| 8 | + const apiClient = createBackendApiClient({ |
| 9 | + apiUrl: 'https://api.clerk.test', |
| 10 | + secretKey: 'deadbeef', |
| 11 | + }); |
| 12 | + |
| 13 | + const machineId = 'machine_123'; |
| 14 | + const otherMachineId = 'machine_456'; |
| 15 | + |
| 16 | + const mockSecondMachine = { |
| 17 | + object: 'machine', |
| 18 | + id: otherMachineId, |
| 19 | + name: 'Second Machine', |
| 20 | + instance_id: 'inst_456', |
| 21 | + created_at: 1640995200, |
| 22 | + updated_at: 1640995200, |
| 23 | + }; |
| 24 | + |
| 25 | + const mockMachine = { |
| 26 | + object: 'machine', |
| 27 | + id: machineId, |
| 28 | + name: 'Test Machine', |
| 29 | + instance_id: 'inst_123', |
| 30 | + created_at: 1640995200, |
| 31 | + updated_at: 1640995200, |
| 32 | + scoped_machines: [mockSecondMachine], |
| 33 | + }; |
| 34 | + |
| 35 | + const mockMachineScope = { |
| 36 | + object: 'machine_scope', |
| 37 | + from_machine_id: machineId, |
| 38 | + to_machine_id: otherMachineId, |
| 39 | + created_at: 1640995200, |
| 40 | + }; |
| 41 | + |
| 42 | + const mockMachineSecretKey = { |
| 43 | + secret: 'ak_test_...', |
| 44 | + }; |
| 45 | + |
| 46 | + const mockPaginatedResponse = { |
| 47 | + data: [mockMachine], |
| 48 | + total_count: 1, |
| 49 | + }; |
| 50 | + |
| 51 | + it('fetches a machine by ID', async () => { |
| 52 | + server.use( |
| 53 | + http.get( |
| 54 | + `https://api.clerk.test/v1/machines/${machineId}`, |
| 55 | + validateHeaders(() => { |
| 56 | + return HttpResponse.json(mockMachine); |
| 57 | + }), |
| 58 | + ), |
| 59 | + ); |
| 60 | + |
| 61 | + const response = await apiClient.machines.get(machineId); |
| 62 | + |
| 63 | + expect(response.id).toBe(machineId); |
| 64 | + expect(response.name).toBe('Test Machine'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('fetches machines list with query parameters', async () => { |
| 68 | + server.use( |
| 69 | + http.get( |
| 70 | + 'https://api.clerk.test/v1/machines', |
| 71 | + validateHeaders(({ request }) => { |
| 72 | + const url = new URL(request.url); |
| 73 | + expect(url.searchParams.get('limit')).toBe('10'); |
| 74 | + expect(url.searchParams.get('offset')).toBe('5'); |
| 75 | + expect(url.searchParams.get('query')).toBe('test'); |
| 76 | + return HttpResponse.json(mockPaginatedResponse); |
| 77 | + }), |
| 78 | + ), |
| 79 | + ); |
| 80 | + |
| 81 | + const response = await apiClient.machines.list({ |
| 82 | + limit: 10, |
| 83 | + offset: 5, |
| 84 | + query: 'test', |
| 85 | + }); |
| 86 | + |
| 87 | + expect(response.data).toHaveLength(1); |
| 88 | + expect(response.totalCount).toBe(1); |
| 89 | + }); |
| 90 | + |
| 91 | + it('creates a machine with scoped machines', async () => { |
| 92 | + const createParams = { |
| 93 | + name: 'New Machine', |
| 94 | + scoped_machines: [otherMachineId], |
| 95 | + default_token_ttl: 7200, |
| 96 | + }; |
| 97 | + |
| 98 | + server.use( |
| 99 | + http.post( |
| 100 | + 'https://api.clerk.test/v1/machines', |
| 101 | + validateHeaders(async ({ request }) => { |
| 102 | + const body = await request.json(); |
| 103 | + expect(body).toEqual(createParams); |
| 104 | + return HttpResponse.json(mockMachine); |
| 105 | + }), |
| 106 | + ), |
| 107 | + ); |
| 108 | + |
| 109 | + const response = await apiClient.machines.create(createParams); |
| 110 | + |
| 111 | + expect(response.id).toBe(machineId); |
| 112 | + expect(response.name).toBe('Test Machine'); |
| 113 | + expect(response.scopedMachines).toHaveLength(1); |
| 114 | + expect(response.scopedMachines[0].id).toBe(otherMachineId); |
| 115 | + expect(response.scopedMachines[0].name).toBe('Second Machine'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('updates a machine with partial parameters', async () => { |
| 119 | + const updateParams = { |
| 120 | + machineId, |
| 121 | + name: 'Updated Machine', |
| 122 | + }; |
| 123 | + |
| 124 | + server.use( |
| 125 | + http.patch( |
| 126 | + `https://api.clerk.test/v1/machines/${machineId}`, |
| 127 | + validateHeaders(async ({ request }) => { |
| 128 | + const body = await request.json(); |
| 129 | + expect(body).toEqual({ name: 'Updated Machine' }); |
| 130 | + return HttpResponse.json(mockMachine); |
| 131 | + }), |
| 132 | + ), |
| 133 | + ); |
| 134 | + |
| 135 | + const response = await apiClient.machines.update(updateParams); |
| 136 | + |
| 137 | + expect(response.id).toBe(machineId); |
| 138 | + expect(response.name).toBe('Test Machine'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('deletes a machine', async () => { |
| 142 | + server.use( |
| 143 | + http.delete( |
| 144 | + `https://api.clerk.test/v1/machines/${machineId}`, |
| 145 | + validateHeaders(() => { |
| 146 | + return HttpResponse.json(mockMachine); |
| 147 | + }), |
| 148 | + ), |
| 149 | + ); |
| 150 | + |
| 151 | + const response = await apiClient.machines.delete(machineId); |
| 152 | + |
| 153 | + expect(response.id).toBe(machineId); |
| 154 | + }); |
| 155 | + |
| 156 | + it('fetches machine secret key', async () => { |
| 157 | + server.use( |
| 158 | + http.get( |
| 159 | + `https://api.clerk.test/v1/machines/${machineId}/secret_key`, |
| 160 | + validateHeaders(() => { |
| 161 | + return HttpResponse.json(mockMachineSecretKey); |
| 162 | + }), |
| 163 | + ), |
| 164 | + ); |
| 165 | + |
| 166 | + const response = await apiClient.machines.getSecretKey(machineId); |
| 167 | + |
| 168 | + expect(response.secret).toBe('ak_test_...'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('creates a machine scope', async () => { |
| 172 | + server.use( |
| 173 | + http.post( |
| 174 | + `https://api.clerk.test/v1/machines/${machineId}/scopes`, |
| 175 | + validateHeaders(async ({ request }) => { |
| 176 | + const body = await request.json(); |
| 177 | + expect(body).toEqual({ to_machine_id: otherMachineId }); |
| 178 | + return HttpResponse.json(mockMachineScope); |
| 179 | + }), |
| 180 | + ), |
| 181 | + ); |
| 182 | + |
| 183 | + const response = await apiClient.machines.createScope(machineId, otherMachineId); |
| 184 | + |
| 185 | + expect(response.fromMachineId).toBe(machineId); |
| 186 | + expect(response.toMachineId).toBe(otherMachineId); |
| 187 | + }); |
| 188 | + |
| 189 | + it('deletes a machine scope', async () => { |
| 190 | + server.use( |
| 191 | + http.delete( |
| 192 | + `https://api.clerk.test/v1/machines/${machineId}/scopes/${otherMachineId}`, |
| 193 | + validateHeaders(() => { |
| 194 | + return HttpResponse.json(mockMachineScope); |
| 195 | + }), |
| 196 | + ), |
| 197 | + ); |
| 198 | + |
| 199 | + const response = await apiClient.machines.deleteScope(machineId, otherMachineId); |
| 200 | + |
| 201 | + expect(response.fromMachineId).toBe(machineId); |
| 202 | + expect(response.toMachineId).toBe(otherMachineId); |
| 203 | + }); |
| 204 | +}); |
0 commit comments