|
| 1 | +describe("Typesense Client Default Random Configuration", () => { |
| 2 | + let originalConnectionTimeout; |
| 3 | + let originalRetryInterval; |
| 4 | + |
| 5 | + beforeEach(() => { |
| 6 | + originalConnectionTimeout = process.env.TYPESENSE_CONNECTION_TIMEOUT_SECONDS; |
| 7 | + originalRetryInterval = process.env.TYPESENSE_RETRY_INTERVAL_SECONDS; |
| 8 | + |
| 9 | + jest.resetModules(); |
| 10 | + |
| 11 | + process.env.TYPESENSE_HOSTS = "localhost"; |
| 12 | + process.env.TYPESENSE_API_KEY = "test-key"; |
| 13 | + process.env.TYPESENSE_COLLECTION_NAME = "test-collection"; |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + if (originalConnectionTimeout !== undefined) { |
| 18 | + process.env.TYPESENSE_CONNECTION_TIMEOUT_SECONDS = originalConnectionTimeout; |
| 19 | + } else { |
| 20 | + delete process.env.TYPESENSE_CONNECTION_TIMEOUT_SECONDS; |
| 21 | + } |
| 22 | + |
| 23 | + if (originalRetryInterval !== undefined) { |
| 24 | + process.env.TYPESENSE_RETRY_INTERVAL_SECONDS = originalRetryInterval; |
| 25 | + } else { |
| 26 | + delete process.env.TYPESENSE_RETRY_INTERVAL_SECONDS; |
| 27 | + } |
| 28 | + |
| 29 | + delete process.env.TYPESENSE_HOSTS; |
| 30 | + delete process.env.TYPESENSE_API_KEY; |
| 31 | + delete process.env.TYPESENSE_COLLECTION_NAME; |
| 32 | + |
| 33 | + jest.resetModules(); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("when environment variables are not set", () => { |
| 37 | + beforeEach(() => { |
| 38 | + delete process.env.TYPESENSE_CONNECTION_TIMEOUT_SECONDS; |
| 39 | + delete process.env.TYPESENSE_RETRY_INTERVAL_SECONDS; |
| 40 | + |
| 41 | + jest.resetModules(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should use random connection timeout between 60 and 90 seconds", () => { |
| 45 | + const createTypesenseClient = require("../functions/src/createTypesenseClient"); |
| 46 | + const client = createTypesenseClient(); |
| 47 | + |
| 48 | + expect(client.configuration.connectionTimeoutSeconds).toBeGreaterThanOrEqual(60); |
| 49 | + expect(client.configuration.connectionTimeoutSeconds).toBeLessThanOrEqual(90); |
| 50 | + expect(Number.isInteger(client.configuration.connectionTimeoutSeconds)).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should use random retry interval between 60 and 120 seconds", () => { |
| 54 | + const createTypesenseClient = require("../functions/src/createTypesenseClient"); |
| 55 | + const client = createTypesenseClient(); |
| 56 | + |
| 57 | + expect(client.configuration.retryIntervalSeconds).toBeGreaterThanOrEqual(60); |
| 58 | + expect(client.configuration.retryIntervalSeconds).toBeLessThanOrEqual(120); |
| 59 | + expect(Number.isInteger(client.configuration.retryIntervalSeconds)).toBe(true); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should generate different random values on multiple client creations", () => { |
| 63 | + const createTypesenseClient = require("../functions/src/createTypesenseClient"); |
| 64 | + const clients = []; |
| 65 | + const connectionTimeouts = new Set(); |
| 66 | + const retryIntervals = new Set(); |
| 67 | + |
| 68 | + for (let i = 0; i < 10; i++) { |
| 69 | + const client = createTypesenseClient(); |
| 70 | + clients.push(client); |
| 71 | + connectionTimeouts.add(client.configuration.connectionTimeoutSeconds); |
| 72 | + retryIntervals.add(client.configuration.retryIntervalSeconds); |
| 73 | + } |
| 74 | + |
| 75 | + expect(connectionTimeouts.size).toBeGreaterThan(1); |
| 76 | + expect(retryIntervals.size).toBeGreaterThan(1); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe("when environment variables are set", () => { |
| 81 | + beforeEach(() => { |
| 82 | + process.env.TYPESENSE_CONNECTION_TIMEOUT_SECONDS = "45"; |
| 83 | + process.env.TYPESENSE_RETRY_INTERVAL_SECONDS = "30"; |
| 84 | + |
| 85 | + jest.resetModules(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should use the configured values instead of random ones", () => { |
| 89 | + const createTypesenseClient = require("../functions/src/createTypesenseClient"); |
| 90 | + const client = createTypesenseClient(); |
| 91 | + |
| 92 | + expect(client.configuration.connectionTimeoutSeconds).toBe(45); |
| 93 | + expect(client.configuration.retryIntervalSeconds).toBe(30); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("when environment variables are set to invalid values", () => { |
| 98 | + beforeEach(() => { |
| 99 | + process.env.TYPESENSE_CONNECTION_TIMEOUT_SECONDS = "invalid"; |
| 100 | + process.env.TYPESENSE_RETRY_INTERVAL_SECONDS = "also-invalid"; |
| 101 | + |
| 102 | + jest.resetModules(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should fall back to random values when parseInt returns NaN", () => { |
| 106 | + const createTypesenseClient = require("../functions/src/createTypesenseClient"); |
| 107 | + const client = createTypesenseClient(); |
| 108 | + |
| 109 | + expect(client.configuration.connectionTimeoutSeconds).toBeGreaterThanOrEqual(60); |
| 110 | + expect(client.configuration.connectionTimeoutSeconds).toBeLessThanOrEqual(90); |
| 111 | + expect(client.configuration.retryIntervalSeconds).toBeGreaterThanOrEqual(60); |
| 112 | + expect(client.configuration.retryIntervalSeconds).toBeLessThanOrEqual(120); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments