Skip to content

Commit

Permalink
Add tests for requestIdCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacroldan committed Dec 17, 2024
1 parent dda7c7a commit 0456648
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
68 changes: 68 additions & 0 deletions packages/cli-kit/src/private/node/request-ids.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {requestIdsCollection} from './request-ids.js'
import {describe, test, expect, beforeEach} from 'vitest'

describe('RequestIDCollection', () => {
beforeEach(() => {
requestIdsCollection.clear()
})

test('starts with an empty collection', () => {
expect(requestIdsCollection.getRequestIds()).toEqual([])
})

test('adds request IDs to collection', () => {
// When
requestIdsCollection.addRequestId('request-1')
requestIdsCollection.addRequestId('request-2')

// Then
expect(requestIdsCollection.getRequestIds()).toEqual(['request-1', 'request-2'])
})

test('ignores undefined or null request IDs', () => {
// When
requestIdsCollection.addRequestId(undefined)
requestIdsCollection.addRequestId(null)
requestIdsCollection.addRequestId('request-1')

// Then
expect(requestIdsCollection.getRequestIds()).toEqual(['request-1'])
})

test('limits collection to MAX_REQUEST_IDS', () => {
// When
for (let i = 0; i < 120; i++) {
requestIdsCollection.addRequestId(`request-${i}`)
}

// Then
expect(requestIdsCollection.getRequestIds()).toHaveLength(100)
expect(requestIdsCollection.getRequestIds()[0]).toBe('request-0')
expect(requestIdsCollection.getRequestIds()[99]).toBe('request-99')
})

test('clear() removes all request IDs', () => {
// Given
requestIdsCollection.addRequestId('request-1')
requestIdsCollection.addRequestId('request-2')

// When
requestIdsCollection.clear()

// Then
expect(requestIdsCollection.getRequestIds()).toEqual([])
})

test('maintains singleton instance', () => {
// Given
requestIdsCollection.addRequestId('request-1')

// When
const sameInstance = requestIdsCollection
sameInstance.addRequestId('request-2')

// Then
expect(requestIdsCollection.getRequestIds()).toEqual(['request-1', 'request-2'])
expect(sameInstance.getRequestIds()).toEqual(['request-1', 'request-2'])
})
})
13 changes: 6 additions & 7 deletions packages/cli-kit/src/private/node/request-ids.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const MAX_REQUEST_IDS = 100

/**
* Manages collection of request IDs during command execution
*/
export class RequestIDCollection {
class RequestIDCollection {
private static instance: RequestIDCollection

static getInstance(): RequestIDCollection {
Expand All @@ -11,17 +13,14 @@ export class RequestIDCollection {
return RequestIDCollection.instance
}

// We only report the last 1000 request IDs.
private readonly maxRequestIds = 1000
private requestIds: string[] = []

private constructor() {}

/**
* Add a request ID to the collection
* We only report the first MAX_REQUEST_IDS request IDs.
*/
addRequestId(requestId: string | undefined | null) {
if (requestId) {
if (requestId && this.requestIds.length < MAX_REQUEST_IDS) {
this.requestIds.push(requestId)
}
}
Expand All @@ -30,7 +29,7 @@ export class RequestIDCollection {
* Get all collected request IDs as a comma-separated string
*/
getRequestIds(): string[] {
return this.requestIds.slice(-this.maxRequestIds)
return this.requestIds
}

/**
Expand Down

0 comments on commit 0456648

Please sign in to comment.