Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
moved tests in memory-cache-store-size.js to memory-cache-store-tests.js
  • Loading branch information
FelixVaughan committed May 19, 2025
commit 98c7711db836bfe1bfb19b094dd80bc542430764
118 changes: 118 additions & 0 deletions test/cache-interceptor/memory-cache-store-tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,124 @@
'use strict'

const { test } = require('node:test')
const { equal } = require('node:assert')
const MemoryCacheStore = require('../../lib/cache/memory-cache-store')
const { cacheStoreTests } = require('./cache-store-test-utils.js')

cacheStoreTests(MemoryCacheStore)

test('size getter returns correct total size', async () => {
const store = new MemoryCacheStore()
const testData = 'test data'

equal(store.size, 0, 'Initial size should be 0')

const writeStream = store.createWriteStream(
{ origin: 'test', path: '/', method: 'GET' },
{
statusCode: 200,
statusMessage: 'OK',
headers: {},
cachedAt: Date.now(),
staleAt: Date.now() + 1000,
deleteAt: Date.now() + 2000
}
)

writeStream.write(testData)
writeStream.end()

equal(store.size, testData.length, 'Size should match written data length')
})

test('isFull returns false when under limits', () => {
const store = new MemoryCacheStore({
maxSize: 1000,
maxCount: 10
})

equal(store.isFull(), false, 'Should not be full when empty')
})

test('isFull returns true when maxSize reached', async () => {
const maxSize = 10
const store = new MemoryCacheStore({ maxSize })
const testData = 'x'.repeat(maxSize + 1) // Exceed maxSize

const writeStream = store.createWriteStream(
{ origin: 'test', path: '/', method: 'GET' },
{
statusCode: 200,
statusMessage: 'OK',
headers: {},
cachedAt: Date.now(),
staleAt: Date.now() + 1000,
deleteAt: Date.now() + 2000
}
)

writeStream.write(testData)
writeStream.end()

equal(store.isFull(), true, 'Should be full when maxSize exceeded')
})

test('isFull returns true when maxCount reached', async () => {
const maxCount = 2
const store = new MemoryCacheStore({ maxCount })

// Add maxCount + 1 entries
for (let i = 0; i <= maxCount; i++) {
const writeStream = store.createWriteStream(
{ origin: 'test', path: `/${i}`, method: 'GET' },
{
statusCode: 200,
statusMessage: 'OK',
headers: {},
cachedAt: Date.now(),
staleAt: Date.now() + 1000,
deleteAt: Date.now() + 2000
}
)
writeStream.end('test')
}

equal(store.isFull(), true, 'Should be full when maxCount exceeded')
})

test('emits maxSizeExceeded event when limits exceeded', async () => {
const maxSize = 10
const store = new MemoryCacheStore({ maxSize })

let eventFired = false
let eventPayload = null

store.on('maxSizeExceeded', (payload) => {
eventFired = true
eventPayload = payload
})

const testData = 'x'.repeat(maxSize + 1) // Exceed maxSize

const writeStream = store.createWriteStream(
{ origin: 'test', path: '/', method: 'GET' },
{
statusCode: 200,
statusMessage: 'OK',
headers: {},
cachedAt: Date.now(),
staleAt: Date.now() + 1000,
deleteAt: Date.now() + 2000
}
)

writeStream.write(testData)
writeStream.end()

equal(eventFired, true, 'maxSizeExceeded event should fire')
equal(typeof eventPayload, 'object', 'Event should have payload')
equal(typeof eventPayload.size, 'number', 'Payload should have size')
equal(typeof eventPayload.maxSize, 'number', 'Payload should have maxSize')
equal(typeof eventPayload.count, 'number', 'Payload should have count')
equal(typeof eventPayload.maxCount, 'number', 'Payload should have maxCount')
})
123 changes: 0 additions & 123 deletions test/memory-cache-store-size.js

This file was deleted.