|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import request from 'supertest'; |
| 3 | +import * as StellarSdk from '@stellar/stellar-sdk'; |
| 4 | +import { SorobanEventWorker } from '../../src/workers/soroban-event-worker.js'; |
| 5 | + |
| 6 | +const { mockPauseStream, mockResumeStream, mockPrisma } = vi.hoisted(() => ({ |
| 7 | + mockPauseStream: vi.fn(), |
| 8 | + mockResumeStream: vi.fn(), |
| 9 | + mockPrisma: { |
| 10 | + stream: { |
| 11 | + findUnique: vi.fn(), |
| 12 | + update: vi.fn(), |
| 13 | + findUniqueOrThrow: vi.fn(), |
| 14 | + }, |
| 15 | + streamEvent: { |
| 16 | + create: vi.fn(), |
| 17 | + upsert: vi.fn(), |
| 18 | + findUnique: vi.fn(), |
| 19 | + }, |
| 20 | + $transaction: vi.fn(async (cb) => { |
| 21 | + // Mock the transaction client as mockPrisma itself |
| 22 | + return cb(mockPrisma); |
| 23 | + }), |
| 24 | + }, |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock('../../src/lib/prisma.js', () => ({ |
| 28 | + default: mockPrisma, |
| 29 | + prisma: mockPrisma, |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock('../../src/services/sorobanService.js', () => ({ |
| 33 | + pauseStream: mockPauseStream, |
| 34 | + resumeStream: mockResumeStream, |
| 35 | +})); |
| 36 | + |
| 37 | +import app from '../../src/app.js'; |
| 38 | + |
| 39 | +function makeKeypair() { |
| 40 | + return StellarSdk.Keypair.random(); |
| 41 | +} |
| 42 | + |
| 43 | +function buildSignedTransaction(keypair: StellarSdk.Keypair, nonce: string): string { |
| 44 | + const account = new StellarSdk.Account(keypair.publicKey(), '0'); |
| 45 | + const tx = new StellarSdk.TransactionBuilder(account, { |
| 46 | + fee: '100', |
| 47 | + networkPassphrase: StellarSdk.Networks.TESTNET, |
| 48 | + }) |
| 49 | + .addOperation( |
| 50 | + StellarSdk.Operation.manageData({ |
| 51 | + name: 'auth', |
| 52 | + value: Buffer.from(nonce, 'hex'), |
| 53 | + }), |
| 54 | + ) |
| 55 | + .setTimeout(60) |
| 56 | + .build(); |
| 57 | + |
| 58 | + tx.sign(keypair); |
| 59 | + return tx.toXDR(); |
| 60 | +} |
| 61 | + |
| 62 | +async function getValidJwt(keypair: StellarSdk.Keypair): Promise<string> { |
| 63 | + const challengeRes = await request(app) |
| 64 | + .post('/v1/auth/challenge') |
| 65 | + .send({ publicKey: keypair.publicKey() }); |
| 66 | + |
| 67 | + const nonce = challengeRes.body.nonce as string; |
| 68 | + const signedTransaction = buildSignedTransaction(keypair, nonce); |
| 69 | + |
| 70 | + const verifyRes = await request(app) |
| 71 | + .post('/v1/auth/verify') |
| 72 | + .send({ publicKey: keypair.publicKey(), signedTransaction }); |
| 73 | + |
| 74 | + return verifyRes.body.token as string; |
| 75 | +} |
| 76 | + |
| 77 | +describe('Regression #804: Pause/resume controller duplicate StreamEvent', () => { |
| 78 | + beforeEach(() => { |
| 79 | + vi.clearAllMocks(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('pauses a stream and only writes one PAUSED event via indexer', async () => { |
| 83 | + const sender = makeKeypair(); |
| 84 | + const token = await getValidJwt(sender); |
| 85 | + const streamId = 77; |
| 86 | + |
| 87 | + // 1. Controller flow |
| 88 | + mockPrisma.stream.findUnique.mockResolvedValue({ |
| 89 | + streamId, |
| 90 | + sender: sender.publicKey(), |
| 91 | + isActive: true, |
| 92 | + isPaused: false, |
| 93 | + }); |
| 94 | + mockPauseStream.mockResolvedValue({ txHash: 'simulated-pause-77' }); |
| 95 | + |
| 96 | + const pauseRes = await request(app) |
| 97 | + .post(`/v1/streams/${streamId}/pause`) |
| 98 | + .set('Authorization', `Bearer ${token}`); |
| 99 | + |
| 100 | + expect(pauseRes.status).toBe(200); |
| 101 | + |
| 102 | + // Controller should NOT write to DB for PAUSED event |
| 103 | + expect(mockPrisma.streamEvent.create).not.toHaveBeenCalled(); |
| 104 | + expect(mockPrisma.stream.update).not.toHaveBeenCalled(); |
| 105 | + |
| 106 | + // 2. Indexer flow |
| 107 | + const worker = new SorobanEventWorker(); |
| 108 | + |
| 109 | + const mockEvent = { |
| 110 | + id: 'event1', |
| 111 | + ledger: 100, |
| 112 | + txHash: 'real-tx-hash', |
| 113 | + topic: [ |
| 114 | + StellarSdk.xdr.ScVal.scvSymbol('stream_paused'), |
| 115 | + StellarSdk.nativeToScVal(streamId, { type: 'u64' }), |
| 116 | + ], |
| 117 | + value: StellarSdk.xdr.ScVal.scvMap([ |
| 118 | + new StellarSdk.xdr.ScMapEntry({ |
| 119 | + key: StellarSdk.xdr.ScVal.scvSymbol('sender'), |
| 120 | + val: new StellarSdk.Address(sender.publicKey()).toScVal(), |
| 121 | + }), |
| 122 | + new StellarSdk.xdr.ScMapEntry({ |
| 123 | + key: StellarSdk.xdr.ScVal.scvSymbol('paused_at'), |
| 124 | + val: StellarSdk.nativeToScVal(Math.floor(Date.now() / 1000), { type: 'u64' }), |
| 125 | + }), |
| 126 | + ]), |
| 127 | + inSuccessfulContractCall: true, |
| 128 | + } as any; |
| 129 | + |
| 130 | + mockPrisma.streamEvent.findUnique.mockResolvedValue(null); |
| 131 | + |
| 132 | + await worker.processEvent(mockEvent); |
| 133 | + |
| 134 | + // Indexer should write exactly one PAUSED event |
| 135 | + expect(mockPrisma.streamEvent.upsert).toHaveBeenCalledTimes(1); |
| 136 | + expect(mockPrisma.streamEvent.upsert).toHaveBeenCalledWith( |
| 137 | + expect.objectContaining({ |
| 138 | + create: expect.objectContaining({ |
| 139 | + eventType: 'PAUSED', |
| 140 | + transactionHash: 'real-tx-hash', |
| 141 | + }), |
| 142 | + }), |
| 143 | + ); |
| 144 | + expect(mockPrisma.stream.update).toHaveBeenCalledTimes(1); |
| 145 | + expect(mockPrisma.stream.update).toHaveBeenCalledWith( |
| 146 | + expect.objectContaining({ |
| 147 | + where: { streamId }, |
| 148 | + data: expect.objectContaining({ isPaused: true }), |
| 149 | + }), |
| 150 | + ); |
| 151 | + }); |
| 152 | +}); |
0 commit comments