|
| 1 | +import { beforeEach, describe, expect, test } from 'vitest'; |
| 2 | +import { createMemoryCache, createNullCache } from '../../cache'; |
| 3 | +import { createNullLogger } from '../../logger'; |
| 4 | +import { createTransporter } from '../../transporter'; |
| 5 | +import type { AlgoliaAgent } from '../../types'; |
| 6 | + |
| 7 | +describe('transporter cache', () => { |
| 8 | + let requestCount: number; |
| 9 | + beforeEach(() => { |
| 10 | + requestCount = 0; |
| 11 | + }); |
| 12 | + |
| 13 | + const algoliaAgent: AlgoliaAgent = { |
| 14 | + value: 'test', |
| 15 | + add: () => algoliaAgent, |
| 16 | + }; |
| 17 | + |
| 18 | + const transporter = createTransporter({ |
| 19 | + hosts: [{ url: 'localhost', accept: 'readWrite', protocol: 'https' }], |
| 20 | + hostsCache: createNullCache(), |
| 21 | + baseHeaders: {}, |
| 22 | + baseQueryParameters: {}, |
| 23 | + algoliaAgent, |
| 24 | + logger: createNullLogger(), |
| 25 | + timeouts: { |
| 26 | + connect: 1000, |
| 27 | + read: 2000, |
| 28 | + write: 3000, |
| 29 | + }, |
| 30 | + requester: { |
| 31 | + send: async () => { |
| 32 | + requestCount++; |
| 33 | + return { |
| 34 | + status: 200, |
| 35 | + content: JSON.stringify({ value: requestCount }), |
| 36 | + isTimedOut: false, |
| 37 | + }; |
| 38 | + }, |
| 39 | + }, |
| 40 | + requestsCache: createMemoryCache(), |
| 41 | + responsesCache: createMemoryCache(), |
| 42 | + }); |
| 43 | + |
| 44 | + test('uses cache for cacheable requests', async () => { |
| 45 | + const firstResponse = await transporter.request<{ value: number }>( |
| 46 | + { method: 'GET', path: '/test-1', queryParameters: {}, headers: {}, cacheable: true }, |
| 47 | + {}, |
| 48 | + ); |
| 49 | + const secondResponse = await transporter.request<{ value: number }>( |
| 50 | + { method: 'GET', path: '/test-1', queryParameters: {}, headers: {}, cacheable: true }, |
| 51 | + {}, |
| 52 | + ); |
| 53 | + |
| 54 | + // Should use cached response, so both values are the same and only 1 request made |
| 55 | + expect(firstResponse).toEqual({ value: 1 }); |
| 56 | + expect(secondResponse).toEqual({ value: 1 }); |
| 57 | + expect(requestCount).toBe(1); |
| 58 | + }); |
| 59 | + |
| 60 | + test('does not use cache for implicit non-cacheable requests', async () => { |
| 61 | + const firstResponse = await transporter.request<{ value: number }>( |
| 62 | + { method: 'POST', path: '/test-2', queryParameters: {}, headers: {} }, |
| 63 | + {}, |
| 64 | + ); |
| 65 | + const secondResponse = await transporter.request<{ value: number }>( |
| 66 | + { method: 'POST', path: '/test-2', queryParameters: {}, headers: {} }, |
| 67 | + {}, |
| 68 | + ); |
| 69 | + |
| 70 | + // Should NOT use cache, so each request increments the counter |
| 71 | + expect(firstResponse).toEqual({ value: 1 }); |
| 72 | + expect(secondResponse).toEqual({ value: 2 }); |
| 73 | + expect(requestCount).toBe(2); |
| 74 | + }); |
| 75 | + |
| 76 | + test('does not use cache for explicit non-cacheable requests', async () => { |
| 77 | + const firstResponse = await transporter.request<{ value: number }>( |
| 78 | + { method: 'POST', path: '/test-3', queryParameters: {}, headers: {}, cacheable: false }, |
| 79 | + {}, |
| 80 | + ); |
| 81 | + const secondResponse = await transporter.request<{ value: number }>( |
| 82 | + { method: 'POST', path: '/test-3', queryParameters: {}, headers: {}, cacheable: false }, |
| 83 | + {}, |
| 84 | + ); |
| 85 | + |
| 86 | + // Should NOT use cache, so each request increments the counter |
| 87 | + expect(firstResponse).toEqual({ value: 1 }); |
| 88 | + expect(secondResponse).toEqual({ value: 2 }); |
| 89 | + expect(requestCount).toBe(2); |
| 90 | + }); |
| 91 | + |
| 92 | + test('uses cache for POST requests marked as cacheable', async () => { |
| 93 | + const firstResponse = await transporter.request<{ value: number }>( |
| 94 | + { method: 'POST', path: '/test-4', queryParameters: {}, headers: {}, cacheable: true }, |
| 95 | + {}, |
| 96 | + ); |
| 97 | + const secondResponse = await transporter.request<{ value: number }>( |
| 98 | + { method: 'POST', path: '/test-4', queryParameters: {}, headers: {}, cacheable: true }, |
| 99 | + {}, |
| 100 | + ); |
| 101 | + |
| 102 | + // Should use cached response, so both values are the same and only 1 request made |
| 103 | + expect(firstResponse).toEqual({ value: 1 }); |
| 104 | + expect(secondResponse).toEqual({ value: 1 }); |
| 105 | + expect(requestCount).toBe(1); |
| 106 | + }); |
| 107 | + |
| 108 | + test('accepts cacheable from request options', async () => { |
| 109 | + const firstResponse = await transporter.request<{ value: number }>( |
| 110 | + { method: 'GET', path: '/test-5', queryParameters: {}, headers: {}, cacheable: false }, |
| 111 | + { cacheable: true }, |
| 112 | + ); |
| 113 | + const secondResponse = await transporter.request<{ value: number }>( |
| 114 | + { method: 'GET', path: '/test-5', queryParameters: {}, headers: {}, cacheable: false }, |
| 115 | + { cacheable: true }, |
| 116 | + ); |
| 117 | + |
| 118 | + // Should use cached response, so both values are the same and only 1 request made |
| 119 | + expect(firstResponse).toEqual({ value: 1 }); |
| 120 | + expect(secondResponse).toEqual({ value: 1 }); |
| 121 | + expect(requestCount).toBe(1); |
| 122 | + }); |
| 123 | +}); |
0 commit comments