feat: add allowed Linear API operations to MCP config
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
import {
|
||||
getCollection,
|
||||
getDb,
|
||||
checkMongoConnection
|
||||
} from './mongodb'
|
||||
import { MongoClient } from 'mongodb'
|
||||
|
||||
const mockMongoClient = MongoClient as jest.MockedClass<typeof MongoClient>
|
||||
|
||||
// Mock the MongoDB client and database
|
||||
const mockDb = {
|
||||
collection: jest.fn().mockReturnValue({
|
||||
findOne: jest.fn(),
|
||||
insertOne: jest.fn(),
|
||||
updateOne: jest.fn(),
|
||||
deleteOne: jest.fn()
|
||||
})
|
||||
}
|
||||
|
||||
const mockClient = {
|
||||
connect: jest.fn().mockResolvedValue(undefined),
|
||||
close: jest.fn().mockResolvedValue(undefined),
|
||||
db: jest.fn().mockReturnValue(mockDb)
|
||||
}
|
||||
|
||||
// Mock the mongodb module
|
||||
jest.mock('mongodb', () => ({
|
||||
MongoClient: jest.fn().mockImplementation(() => mockClient)
|
||||
}))
|
||||
|
||||
// Restore environment before tests
|
||||
const originalEnv = process.env
|
||||
|
||||
describe('MongoDB Connection', () => {
|
||||
beforeEach(() => {
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
MONGODB_URI: 'mongodb://localhost:27017/test',
|
||||
MONGODB_DB: 'test'
|
||||
}
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
process.env = originalEnv
|
||||
})
|
||||
|
||||
describe('MongoDB URI validation', () => {
|
||||
it('should throw error when MONGODB_URI is not set', () => {
|
||||
// This test is tricky because the module is cached
|
||||
// We'll test this by clearing the module cache and mocking process.env
|
||||
const originalMongodbUri = process.env.MONGODB_URI
|
||||
delete process.env.MONGODB_URI
|
||||
|
||||
// Clear module cache to force re-import
|
||||
jest.resetModules()
|
||||
|
||||
expect(() => {
|
||||
require('./mongodb')
|
||||
}).toThrow('Please add MONGODB_URI to your environment variables')
|
||||
|
||||
// Restore environment
|
||||
process.env.MONGODB_URI = originalMongodbUri
|
||||
})
|
||||
})
|
||||
|
||||
describe('Database connection', () => {
|
||||
it('should create MongoClient with correct URI and options', async () => {
|
||||
const { MongoClient } = require('mongodb')
|
||||
|
||||
// Reset modules to use our mock
|
||||
jest.resetModules()
|
||||
const { clientPromise } = require('./mongodb')
|
||||
|
||||
await clientPromise
|
||||
|
||||
expect(MongoClient).toHaveBeenCalledWith(
|
||||
process.env.MONGODB_URI,
|
||||
expect.objectContaining({
|
||||
maxPoolSize: 10,
|
||||
serverSelectionTimeoutMS: 5000,
|
||||
socketTimeoutMS: 45000
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('should return database instance', async () => {
|
||||
// Reset modules to use our mock
|
||||
jest.resetModules()
|
||||
const { getDb } = require('./mongodb')
|
||||
|
||||
const result = await getDb()
|
||||
|
||||
expect(result).toBeDefined()
|
||||
expect(typeof result.collection).toBe('function')
|
||||
})
|
||||
|
||||
it('should return collection from database', async () => {
|
||||
// Reset modules to use our mock
|
||||
jest.resetModules()
|
||||
const { getCollection } = require('./mongodb')
|
||||
|
||||
const collection = await getCollection('test_collection')
|
||||
|
||||
expect(collection).toBeDefined()
|
||||
expect(typeof collection.findOne).toBe('function')
|
||||
})
|
||||
|
||||
it('should check MongoDB connection successfully', async () => {
|
||||
// Reset modules to use our mock
|
||||
jest.resetModules()
|
||||
const { checkMongoConnection } = require('./mongodb')
|
||||
|
||||
const result = await checkMongoConnection()
|
||||
expect(result).toBe(true)
|
||||
})
|
||||
|
||||
it('should return false on MongoDB connection failure', async () => {
|
||||
// Mock a connection failure
|
||||
const { MongoClient } = require('mongodb')
|
||||
|
||||
// Create a failing client
|
||||
const failingClient = {
|
||||
connect: jest.fn().mockRejectedValue(new Error('Connection failed')),
|
||||
db: jest.fn(),
|
||||
close: jest.fn()
|
||||
}
|
||||
|
||||
MongoClient.mockImplementation(() => failingClient)
|
||||
|
||||
// Reset the module to use the new mock
|
||||
jest.resetModules()
|
||||
const { checkMongoConnection } = require('./mongodb')
|
||||
|
||||
const result = await checkMongoConnection()
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Connection reuse and caching', () => {
|
||||
it('should reuse the same MongoClient instance for multiple calls', async () => {
|
||||
// Reset modules to use our mock
|
||||
jest.resetModules()
|
||||
const { clientPromise: clientPromise1 } = require('./mongodb')
|
||||
const { clientPromise: clientPromise2 } = require('./mongodb')
|
||||
|
||||
await clientPromise1
|
||||
await clientPromise2
|
||||
|
||||
// Should still be the same promise from cache
|
||||
expect(clientPromise1).toBe(clientPromise2)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user