Skip to content

Commit

Permalink
fix: http-fetch-utils-test adjusting new instrumentation dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
augusto-draxx committed Mar 5, 2025
1 parent 0524ccc commit 07ac153
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 13 deletions.
40 changes: 40 additions & 0 deletions src/core/infrastructure/observability/otel-tracer-provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Span, trace, Tracer } from '@opentelemetry/api'
import { OtelTracerProvider } from './otel-tracer-provider'

jest.mock('@opentelemetry/api', () => ({
trace: {
getTracer: jest.fn().mockReturnValue({
startSpan: jest.fn().mockReturnValue({
end: jest.fn()
})
})
}
}))

describe('OtelTracerProvider', () => {
let otelTracerProvider: OtelTracerProvider
let mockTracer: Tracer
let mockSpan: Span

beforeEach(() => {
otelTracerProvider = new OtelTracerProvider()
mockTracer = trace.getTracer('midaz-console')
mockSpan = mockTracer.startSpan('test-span')
})

it('should initialize otelTracer in the constructor', () => {
expect(trace.getTracer).toHaveBeenCalledWith('midaz-console')
})

it('should start a custom span', () => {
const spanName = 'test-span'
const span = otelTracerProvider.startCustomSpan(spanName)
expect(mockTracer.startSpan).toHaveBeenCalledWith(spanName)
expect(span).toBe(mockSpan)
})

it('should end a custom span', () => {
otelTracerProvider.endCustomSpan(mockSpan)
expect(mockSpan.end).toHaveBeenCalled()
})
})
69 changes: 56 additions & 13 deletions src/core/infrastructure/utils/http-fetch-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import {
MidazHttpFetchUtils,
HTTP_METHODS,
HttpFetchOptions
} from './http-fetch-utils'
import { MidazRequestContext } from '../logger/decorators/midaz-id'
import { LoggerAggregator } from '@/core/application/logger/logger-aggregator'
import { OtelTracerProvider } from '../observability/otel-tracer-provider'
import { getServerSession } from 'next-auth'
import { MidazRequestContext } from '../logger/decorators/midaz-id'
import { HTTP_METHODS, MidazHttpFetchUtils } from './http-fetch-utils'
import { nextAuthCasdoorOptions } from '../next-auth/casdoor/next-auth-casdoor-provider'
import { handleMidazError } from './midaz-error-handler'
import { SpanStatusCode } from '@opentelemetry/api'

jest.mock('next-auth', () => ({
getServerSession: jest.fn()
Expand All @@ -14,36 +21,45 @@ jest.mock('./midaz-error-handler', () => ({
})
}))

jest.mock('../next-auth/casdoor/next-auth-casdoor-provider', () => ({
nextAuthCasdoorOptions: {}
}))
jest.mock('../next-auth/casdoor/next-auth-casdoor-provider')
jest.mock('../logger/decorators/midaz-id')

describe('MidazHttpFetchUtils', () => {
let midazHttpFetchUtils: MidazHttpFetchUtils
let midazRequestContext: MidazRequestContext
let midazLogger: LoggerAggregator
let otelTracerProvider: OtelTracerProvider

beforeEach(() => {
midazRequestContext = {
getMidazId: jest.fn().mockReturnValue('test-request-id')
} as unknown as MidazRequestContext
midazRequestContext = new MidazRequestContext()

midazLogger = {
error: jest.fn(),
info: jest.fn()
} as unknown as LoggerAggregator

otelTracerProvider = {
startCustomSpan: jest.fn().mockImplementation(() => {
return {
setAttributes: jest.fn().mockReturnThis(),
setStatus: jest.fn().mockReturnThis()
}
}),
endCustomSpan: jest.fn()
} as unknown as OtelTracerProvider

midazHttpFetchUtils = new MidazHttpFetchUtils(
midazRequestContext,
midazLogger
midazLogger,
otelTracerProvider
)
})

afterEach(() => {
jest.clearAllMocks()
})

it('should successfully fetch data', async () => {
it('should make a successful fetch request', async () => {
const mockResponse = { data: 'test' }
const mockFetch = jest.fn().mockResolvedValue({
ok: true,
Expand All @@ -63,7 +79,10 @@ describe('MidazHttpFetchUtils', () => {

const result = await midazHttpFetchUtils.httpMidazAuthFetch({
url: 'https://api.example.com/test',
method: HTTP_METHODS.GET
method: HTTP_METHODS.GET,
headers: {
'Custom-Header': 'CustomValue'
}
})

expect(result).toEqual(mockResponse)
Expand All @@ -77,7 +96,7 @@ describe('MidazHttpFetchUtils', () => {
)
})

it('should handle fetch error', async () => {
it('should handle fetch request error', async () => {
const mockErrorResponse = { error: 'test error' }
const mockFetch = jest.fn().mockResolvedValue({
ok: false,
Expand Down Expand Up @@ -128,7 +147,8 @@ describe('MidazHttpFetchUtils', () => {
url: 'https://api.example.com/test',
method: HTTP_METHODS.GET,
headers: {
'Custom-Header': 'CustomValue'
'Custom-Header': 'CustomValue',
'X-Request-Id': 'test-request-id'
}
})

Expand All @@ -143,4 +163,27 @@ describe('MidazHttpFetchUtils', () => {
}
})
})

it('should start and end a custom span', async () => {
const mockResponse = { data: 'test' }
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue(mockResponse),
body: true,
status: 200
})
;(getServerSession as jest.Mock).mockResolvedValue({
user: { access_token: 'test-token' }
})

await midazHttpFetchUtils.httpMidazAuthFetch({
url: 'https://api.example.com/test',
method: HTTP_METHODS.GET
})

expect(otelTracerProvider.startCustomSpan).toHaveBeenCalledWith(
'midaz-request'
)
expect(otelTracerProvider.endCustomSpan).toHaveBeenCalled()
})
})

0 comments on commit 07ac153

Please sign in to comment.