|
| 1 | +import { Realtime, Types } from 'ably/promises'; |
| 2 | +import pino from 'pino'; |
| 3 | +import { it, describe, expect, beforeEach, afterEach } from 'vitest'; |
| 4 | + |
| 5 | +import Stream from './Stream.js'; |
| 6 | +import { defaultSyncOptions, defaultEventBufferOptions } from '../Options.js'; |
| 7 | +import type { StreamOptions } from '../types/stream.js'; |
| 8 | +import { createAblyApp } from '../utilities/test/createAblyApp.js'; |
| 9 | + |
| 10 | +interface StreamTestContext extends StreamOptions { |
| 11 | + stream: Stream; |
| 12 | + channel: Types.RealtimeChannelPromise; |
| 13 | +} |
| 14 | + |
| 15 | +describe('Stream integration', () => { |
| 16 | + beforeEach<StreamTestContext>(async (context) => { |
| 17 | + const name = 'test'; |
| 18 | + const data = await createAblyApp({ |
| 19 | + keys: [{}], |
| 20 | + namespaces: [{ id: name, persisted: true }], |
| 21 | + channels: [ |
| 22 | + { |
| 23 | + name, |
| 24 | + presence: [ |
| 25 | + { clientId: 'John', data: 'john@test.com' }, |
| 26 | + { clientId: 'Dave', data: 'dave@test.com' }, |
| 27 | + ], |
| 28 | + }, |
| 29 | + ], |
| 30 | + }); |
| 31 | + const ably = new Realtime({ |
| 32 | + key: data.keys[0].keyStr, |
| 33 | + environment: 'sandbox', |
| 34 | + }); |
| 35 | + const logger = pino({ level: 'silent' }); |
| 36 | + const channel = ably.channels.get(name); |
| 37 | + const stream = new Stream({ |
| 38 | + ably, |
| 39 | + logger, |
| 40 | + channelName: name, |
| 41 | + syncOptions: defaultSyncOptions, |
| 42 | + eventBufferOptions: defaultEventBufferOptions, |
| 43 | + }); |
| 44 | + |
| 45 | + context.stream = stream; |
| 46 | + context.channel = channel; |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach<StreamTestContext>(async ({ stream, channel }) => { |
| 50 | + await stream.dispose(); |
| 51 | + await channel.detach(); |
| 52 | + }); |
| 53 | + |
| 54 | + it<StreamTestContext>('sets agent options when state is not attached', async ({ channel, stream }) => { |
| 55 | + await stream.replay('0'); |
| 56 | + //@ts-ignore - `agent` is filtered out in `channel.params`, so that's the only way to check this |
| 57 | + expect(channel.channelOptions.params).toEqual({ agent: 'models/0.0.2' }); // initial call from test |
| 58 | + }); |
| 59 | + |
| 60 | + it<StreamTestContext>('does not sets agent options when state is attached', async ({ channel }) => { |
| 61 | + await channel.attach(); |
| 62 | + //@ts-ignore - `agent` is filtered out in `channel.params`, so that's the only way to check this |
| 63 | + expect(channel.channelOptions.params).toEqual(undefined); // initial call from test |
| 64 | + }); |
| 65 | +}); |
0 commit comments