|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { cancelStreamHandler } from '../src/controllers/stream/cancel.js'; |
| 3 | +import { prisma } from '../src/lib/prisma.js'; |
| 4 | +import * as sorobanService from '../src/services/sorobanService.js'; |
| 5 | +import * as streamRepository from '../src/repositories/stream.repository.js'; |
| 6 | +import type { Response } from 'express'; |
| 7 | +import type { AuthenticatedRequest } from '../src/types/auth.types.js'; |
| 8 | + |
| 9 | +vi.mock('../src/lib/prisma.js', () => ({ |
| 10 | + prisma: { |
| 11 | + stream: { |
| 12 | + findUnique: vi.fn(), |
| 13 | + }, |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('../src/services/sorobanService.js', () => ({ |
| 18 | + cancelStream: vi.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('../src/repositories/stream.repository.js', () => ({ |
| 22 | + updateStatus: vi.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('../src/logger.js', () => ({ |
| 26 | + default: { |
| 27 | + info: vi.fn(), |
| 28 | + error: vi.fn(), |
| 29 | + warn: vi.fn(), |
| 30 | + }, |
| 31 | +})); |
| 32 | + |
| 33 | +describe('Cancel Stream Controller', () => { |
| 34 | + let req: Partial<AuthenticatedRequest>; |
| 35 | + let res: Partial<Response>; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + vi.clearAllMocks(); |
| 39 | + process.env.SOROBAN_SECRET_KEY = 'SABC123'; |
| 40 | + req = { |
| 41 | + params: { streamId: '123' }, |
| 42 | + user: { publicKey: 'GSENDER1' } as any, |
| 43 | + }; |
| 44 | + res = { |
| 45 | + status: vi.fn().mockReturnThis(), |
| 46 | + json: vi.fn().mockReturnThis(), |
| 47 | + }; |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return 404 if stream not found', async () => { |
| 51 | + (prisma.stream.findUnique as any).mockResolvedValue(null); |
| 52 | + |
| 53 | + await cancelStreamHandler(req as AuthenticatedRequest, res as Response); |
| 54 | + |
| 55 | + expect(res.status).toHaveBeenCalledWith(404); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return 403 if caller is not sender', async () => { |
| 59 | + (prisma.stream.findUnique as any).mockResolvedValue({ sender: 'GOTHER', isActive: true }); |
| 60 | + |
| 61 | + await cancelStreamHandler(req as AuthenticatedRequest, res as Response); |
| 62 | + |
| 63 | + expect(res.status).toHaveBeenCalledWith(403); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return 409 if stream is already inactive', async () => { |
| 67 | + (prisma.stream.findUnique as any).mockResolvedValue({ sender: 'GSENDER1', isActive: false }); |
| 68 | + |
| 69 | + await cancelStreamHandler(req as AuthenticatedRequest, res as Response); |
| 70 | + |
| 71 | + expect(res.status).toHaveBeenCalledWith(409); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should successfully cancel stream', async () => { |
| 75 | + (prisma.stream.findUnique as any).mockResolvedValue({ sender: 'GSENDER1', isActive: true }); |
| 76 | + (sorobanService.cancelStream as any).mockResolvedValue('tx_hash_123'); |
| 77 | + |
| 78 | + await cancelStreamHandler(req as AuthenticatedRequest, res as Response); |
| 79 | + |
| 80 | + expect(sorobanService.cancelStream).toHaveBeenCalledWith(123, 'SABC123'); |
| 81 | + expect(streamRepository.updateStatus).toHaveBeenCalledWith(123, 'CANCELLED'); |
| 82 | + expect(res.status).toHaveBeenCalledWith(200); |
| 83 | + expect(res.json).toHaveBeenCalledWith(expect.objectContaining({ status: 'CANCELLED', txHash: 'tx_hash_123' })); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return 500 if SOROBAN_SECRET_KEY is missing', async () => { |
| 87 | + delete process.env.SOROBAN_SECRET_KEY; |
| 88 | + (prisma.stream.findUnique as any).mockResolvedValue({ sender: 'GSENDER1', isActive: true }); |
| 89 | + |
| 90 | + await cancelStreamHandler(req as AuthenticatedRequest, res as Response); |
| 91 | + |
| 92 | + expect(res.status).toHaveBeenCalledWith(500); |
| 93 | + }); |
| 94 | +}); |
0 commit comments