feat: update Linear issue tracking with project query and workflow state handling

This commit is contained in:
Do Siki
2025-09-05 03:44:04 +02:00
parent b6365543ef
commit 578a85ec1a
8 changed files with 501 additions and 247 deletions
+32 -43
View File
@@ -1,31 +1,30 @@
import {
getCollection,
getDb,
checkMongoConnection
} from './mongodb'
import { MongoClient } from 'mongodb'
const mockMongoClient = MongoClient as jest.MockedClass<typeof MongoClient>
// Mock the MongoDB client and database
// Mock the MongoDB module completely
const mockDb = {
collection: jest.fn().mockReturnValue({
findOne: jest.fn(),
insertOne: jest.fn(),
updateOne: jest.fn(),
deleteOne: jest.fn()
}),
admin: jest.fn().mockReturnValue({
ping: jest.fn().mockResolvedValue(true)
})
}
const mockClient = {
connect: jest.fn().mockResolvedValue(undefined),
connect: jest.fn(),
close: jest.fn().mockResolvedValue(undefined),
db: jest.fn().mockReturnValue(mockDb)
}
// The connect method should resolve to the client itself
mockClient.connect.mockResolvedValue(mockClient)
const MockedMongoClient = jest.fn().mockImplementation(() => mockClient)
// Mock the mongodb module
jest.mock('mongodb', () => ({
MongoClient: jest.fn().mockImplementation(() => mockClient)
MongoClient: MockedMongoClient
}))
// Restore environment before tests
@@ -39,6 +38,10 @@ describe('MongoDB Connection', () => {
MONGODB_DB: 'test'
}
jest.clearAllMocks()
// Reset the mock implementation
MockedMongoClient.mockImplementation(() => mockClient as any)
mockClient.db.mockReturnValue(mockDb)
})
afterEach(() => {
@@ -66,15 +69,12 @@ describe('MongoDB Connection', () => {
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')
// Import after setting up the mock
const { clientPromise } = await import('./mongodb')
await clientPromise
expect(MongoClient).toHaveBeenCalledWith(
expect(MockedMongoClient).toHaveBeenCalledWith(
process.env.MONGODB_URI,
expect.objectContaining({
maxPoolSize: 10,
@@ -85,52 +85,44 @@ describe('MongoDB Connection', () => {
})
it('should return database instance', async () => {
// Reset modules to use our mock
jest.resetModules()
const { getDb } = require('./mongodb')
const { getDb } = await import('./mongodb')
const result = await getDb()
expect(result).toBeDefined()
expect(result).toBe(mockDb)
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 { getCollection } = await import('./mongodb')
const collection = await getCollection('test_collection')
expect(collection).toBeDefined()
expect(mockDb.collection).toHaveBeenCalledWith('test_collection')
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 { checkMongoConnection } = await import('./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(),
db: jest.fn().mockRejectedValue(new Error('Connection failed')),
close: jest.fn()
}
MongoClient.mockImplementation(() => failingClient)
MockedMongoClient.mockImplementation(() => failingClient as any)
// Reset the module to use the new mock
// Clear the module cache and re-import
jest.resetModules()
const { checkMongoConnection } = require('./mongodb')
const { checkMongoConnection } = await import('./mongodb')
const result = await checkMongoConnection()
expect(result).toBe(false)
@@ -139,16 +131,13 @@ describe('MongoDB Connection', () => {
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')
const { clientPromise } = await import('./mongodb')
await clientPromise1
await clientPromise2
const client1 = await clientPromise
const client2 = await clientPromise
// Should still be the same promise from cache
expect(clientPromise1).toBe(clientPromise2)
// Should be the same client instance
expect(client1).toBe(client2)
})
})
})