1- import { describe , it , expect , vi , beforeEach } from "vitest" ;
2- import {
3- createStream ,
4- listStreams ,
5- getStream ,
6- getStreamClaimableAmount ,
7- pauseStream ,
8- resumeStream ,
9- } from "../src/controllers/stream.controller.js" ;
10- import { prisma } from "../src/lib/prisma.js" ;
11- import { claimableAmountService } from "../src/services/claimable.service.js" ;
12- import * as sorobanService from "../src/services/sorobanService.js" ;
13- import type { Request , Response } from "express" ;
14-
15- type TestRequest = Partial < Request > & {
16- user ?: {
17- publicKey : string ;
18- } ;
19- } ;
20-
21- vi . mock ( "../src/lib/prisma.js" , ( ) => ( {
1+ import { describe , it , expect , vi , beforeEach } from 'vitest' ;
2+ import { createStream , listStreams , getStream , getStreamEvents , getStreamClaimableAmount , pauseStream , resumeStream } from '../src/controllers/stream.controller.js' ;
3+ import { prisma } from '../src/lib/prisma.js' ;
4+ import { claimableAmountService } from '../src/services/claimable.service.js' ;
5+ import * as sorobanService from '../src/services/sorobanService.js' ;
6+ import type { Request , Response } from 'express' ;
7+
8+ vi . mock ( '../src/lib/prisma.js' , ( ) => ( {
229 prisma : {
2310 stream : {
2411 upsert : vi . fn ( ) ,
@@ -29,6 +16,8 @@ vi.mock("../src/lib/prisma.js", () => ({
2916 } ,
3017 streamEvent : {
3118 create : vi . fn ( ) ,
19+ findMany : vi . fn ( ) ,
20+ count : vi . fn ( ) ,
3221 } ,
3322 } ,
3423} ) ) ;
@@ -235,8 +224,34 @@ describe("Stream Controller", () => {
235224 await getStream ( req as Request , res as Response ) ;
236225
237226 expect ( res . status ) . toHaveBeenCalledWith ( 200 ) ;
238- expect ( res . json ) . toHaveBeenCalledWith (
239- expect . objectContaining ( { streamId : 123 } ) ,
227+ expect ( res . json ) . toHaveBeenCalledWith ( expect . objectContaining ( { streamId : 123 } ) ) ;
228+ expect ( prisma . stream . findUnique ) . toHaveBeenCalledWith (
229+ expect . objectContaining ( {
230+ include : expect . objectContaining ( {
231+ events : { orderBy : { timestamp : 'desc' } } ,
232+ } ) ,
233+ } ) ,
234+ ) ;
235+ } ) ;
236+ } ) ;
237+
238+ describe ( 'getStreamEvents' , ( ) => {
239+ it ( 'should paginate stream events ordered by timestamp desc' , async ( ) => {
240+ req . params = { streamId : '123' } ;
241+ req . query = { limit : '10' , offset : '0' } ;
242+ ( prisma . streamEvent . findMany as any ) . mockResolvedValue ( [ ] ) ;
243+ ( prisma . streamEvent . count as any ) . mockResolvedValue ( 0 ) ;
244+
245+ await getStreamEvents ( req as Request , res as Response ) ;
246+
247+ expect ( res . status ) . toHaveBeenCalledWith ( 200 ) ;
248+ expect ( prisma . streamEvent . findMany ) . toHaveBeenCalledWith (
249+ expect . objectContaining ( {
250+ where : { streamId : 123 } ,
251+ orderBy : { timestamp : 'desc' } ,
252+ take : 10 ,
253+ skip : 0 ,
254+ } ) ,
240255 ) ;
241256 } ) ;
242257 } ) ;
0 commit comments