Skip to content

Commit fed52c4

Browse files
committed
Fix Model tests
1 parent 4ca39d3 commit fed52c4

File tree

5 files changed

+186
-10
lines changed

5 files changed

+186
-10
lines changed

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
"module": "dist/mjs/index.js",
77
"types": "dist/mjs/index.d.ts",
88
"scripts": {
9-
"lint": "npx eslint .",
10-
"lint:fix": "npx eslint --fix .",
11-
"format": "npx prettier --write --ignore-path .gitignore src",
12-
"format:check": "npx prettier --check --ignore-path .gitignore src",
13-
"test": "npx vitest run",
14-
"coverage": "npx vitest run --coverage",
9+
"lint": "eslint .",
10+
"lint:fix": "eslint --fix .",
11+
"format": "prettier --write --ignore-path .gitignore src",
12+
"format:check": "prettier --check --ignore-path .gitignore src",
13+
"test": "vitest run",
14+
"test:watch": "vitest",
15+
"coverage": "vitest run --coverage",
1516
"build": "npm run build:cjs && npm run build:mjs",
16-
"build:mjs": "npx tsc --project tsconfig.mjs.json && cp res/package.mjs.json dist/mjs/package.json",
17-
"build:cjs": "npx tsc --project tsconfig.cjs.json && cp res/package.cjs.json dist/cjs/package.json",
18-
"prepare": "npx husky install",
19-
"docs": "npx typedoc"
17+
"build:mjs": "tsc --project tsconfig.mjs.json && cp res/package.mjs.json dist/mjs/package.json",
18+
"build:cjs": "tsc --project tsconfig.cjs.json && cp res/package.cjs.json dist/cjs/package.json",
19+
"prepare": "husky install",
20+
"docs": "typedoc"
2021
},
2122
"repository": {
2223
"type": "git",
@@ -50,6 +51,7 @@
5051
"eslint-plugin-jsdoc": "^46.0.0",
5152
"eslint-plugin-security": "^1.7.1",
5253
"husky": "^8.0.0",
54+
"ky": "^1.2.0",
5355
"mock-socket": "^9.1.5",
5456
"prettier": "^3.1.0",
5557
"typedoc": "^0.25.3",

src/Model.discontinuity.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('Model', () => {
5757
);
5858
channel.detach = vi.fn<any, any>();
5959
ably.channels.release = vi.fn<any, any>();
60+
channel.setOptions = vi.fn();
6061

6162
let counter = 0;
6263

@@ -133,6 +134,7 @@ describe('Model', () => {
133134
);
134135
channel.detach = vi.fn<any, any>();
135136
ably.channels.release = vi.fn<any, any>();
137+
channel.setOptions = vi.fn();
136138

137139
let counter = 0;
138140

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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: '[email protected]' },
26+
{ clientId: 'Dave', data: '[email protected]' },
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+
});
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import ky from 'ky';
2+
3+
export const createAblyApp = async (json: Options): Promise<Data> => {
4+
const response = await ky
5+
.post('https://sandbox-rest.ably.io/apps', {
6+
json,
7+
headers: {
8+
'content-type': 'application/json',
9+
Accept: 'application/json',
10+
},
11+
})
12+
.json<Data>();
13+
return response;
14+
};
15+
16+
interface Key {
17+
capability?: {
18+
private: ('subscribe' | 'publish')[];
19+
chat?: 'presence'[];
20+
};
21+
}
22+
23+
interface Namespace {
24+
id: string;
25+
persisted: boolean;
26+
}
27+
28+
interface Presence {
29+
clientId: string;
30+
data: string;
31+
}
32+
33+
interface Channel {
34+
name: string;
35+
presence: Presence[];
36+
}
37+
38+
interface Options {
39+
keys?: Key[];
40+
namespaces?: Namespace[];
41+
channels?: Channel[];
42+
}
43+
44+
interface Data {
45+
status: number;
46+
created: Date;
47+
modified: Date;
48+
tlsOnly: boolean;
49+
labels: string;
50+
enablePusherCompatibility: boolean;
51+
namespaces: {
52+
id: string;
53+
created: Date;
54+
modified: Date;
55+
expires: Date | null;
56+
persisted: boolean;
57+
persistLast: boolean;
58+
pushEnabled: boolean;
59+
exposeTimeserial: boolean;
60+
populateChannelRegistry: boolean;
61+
tlsOnly: boolean;
62+
authenticated: boolean;
63+
identified: boolean;
64+
cursorEventProcessing: boolean;
65+
}[];
66+
metaChannels: any;
67+
id: string;
68+
appId: string;
69+
accountId: string;
70+
keys: {
71+
id: string;
72+
value: string;
73+
keyName: string;
74+
keySecret: string;
75+
keyStr: string;
76+
capability: string;
77+
expires: Date;
78+
}[];
79+
connections: {
80+
name: string;
81+
key: string;
82+
}[];
83+
channels: {
84+
name: string;
85+
presence: Presence[];
86+
connection: string;
87+
}[];
88+
}

0 commit comments

Comments
 (0)