|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('node:assert/strict') |
| 4 | +const { format } = require('node:util') |
| 5 | + |
| 6 | +const { describe, it, beforeEach } = require('mocha') |
| 7 | +const proxyquire = require('proxyquire').noPreserveCache() |
| 8 | +const sinon = require('sinon') |
| 9 | + |
| 10 | +require('../../setup/core') |
| 11 | + |
| 12 | +describe('OpenFeature writer Agent strategy', () => { |
| 13 | + let fetchAgentInfo |
| 14 | + let log |
| 15 | + let setAgentStrategy |
| 16 | + let setWriterEnabledValue |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + fetchAgentInfo = sinon.stub() |
| 20 | + log = { |
| 21 | + debug: sinon.spy(), |
| 22 | + warn: sinon.spy(), |
| 23 | + } |
| 24 | + setWriterEnabledValue = sinon.spy() |
| 25 | + |
| 26 | + setAgentStrategy = proxyquire('../../../src/openfeature/writers/util', { |
| 27 | + '../../agent/info': { fetchAgentInfo }, |
| 28 | + '../../log': log, |
| 29 | + }).setAgentStrategy |
| 30 | + }) |
| 31 | + |
| 32 | + it('prefers EVP v4 when the Agent advertises v4 and v2', () => { |
| 33 | + fetchAgentInfo.yields(null, { |
| 34 | + endpoints: ['/evp_proxy/v2/', '/evp_proxy/v4/'], |
| 35 | + }) |
| 36 | + |
| 37 | + setAgentStrategy({ url: new URL('http://localhost:8126') }, setWriterEnabledValue) |
| 38 | + |
| 39 | + sinon.assert.calledOnceWithExactly(setWriterEnabledValue, true, '/evp_proxy/v4') |
| 40 | + sinon.assert.notCalled(log.warn) |
| 41 | + }) |
| 42 | + |
| 43 | + it('uses EVP v2 when the Agent does not advertise v4', () => { |
| 44 | + fetchAgentInfo.yields(null, { |
| 45 | + endpoints: ['/evp_proxy/v2/'], |
| 46 | + }) |
| 47 | + |
| 48 | + setAgentStrategy({ url: new URL('http://localhost:8126') }, setWriterEnabledValue) |
| 49 | + |
| 50 | + sinon.assert.calledOnceWithExactly(setWriterEnabledValue, true, '/evp_proxy/v2') |
| 51 | + sinon.assert.notCalled(log.warn) |
| 52 | + }) |
| 53 | + |
| 54 | + it('disables delivery and warns when Agent discovery fails', () => { |
| 55 | + fetchAgentInfo.yields(new Error('connect ECONNREFUSED')) |
| 56 | + |
| 57 | + setAgentStrategy({ url: new URL('http://localhost:8126') }, setWriterEnabledValue) |
| 58 | + |
| 59 | + sinon.assert.calledOnceWithExactly(setWriterEnabledValue, false) |
| 60 | + sinon.assert.calledOnce(log.warn) |
| 61 | + assert.match(format(...log.warn.firstCall.args), /Direct delivery is forthcoming/) |
| 62 | + }) |
| 63 | + |
| 64 | + it('disables delivery and warns when the Agent has no compatible EVP route', () => { |
| 65 | + fetchAgentInfo.yields(null, { |
| 66 | + endpoints: ['/v0.4/traces'], |
| 67 | + }) |
| 68 | + |
| 69 | + setAgentStrategy({ url: new URL('http://localhost:8126') }, setWriterEnabledValue) |
| 70 | + |
| 71 | + sinon.assert.calledOnceWithExactly(setWriterEnabledValue, false) |
| 72 | + sinon.assert.calledOnce(log.warn) |
| 73 | + assert.match(format(...log.warn.firstCall.args), /no compatible local EVP route/) |
| 74 | + }) |
| 75 | + |
| 76 | + it('logs the unavailable direct-delivery warning once', () => { |
| 77 | + fetchAgentInfo.yields(new Error('connect ECONNREFUSED')) |
| 78 | + const config = { url: new URL('http://localhost:8126') } |
| 79 | + |
| 80 | + setAgentStrategy(config, setWriterEnabledValue) |
| 81 | + setAgentStrategy(config, setWriterEnabledValue) |
| 82 | + |
| 83 | + sinon.assert.calledTwice(setWriterEnabledValue) |
| 84 | + sinon.assert.calledOnce(log.warn) |
| 85 | + }) |
| 86 | +}) |
0 commit comments