Skip to content

Commit

Permalink
Fix Model tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snikidev committed Feb 27, 2024
1 parent da88cea commit 581737e
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 10 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
"module": "dist/mjs/index.js",
"types": "dist/mjs/index.d.ts",
"scripts": {
"lint": "npx eslint .",
"lint:fix": "npx eslint --fix .",
"format": "npx prettier --write --ignore-path .gitignore src",
"format:check": "npx prettier --check --ignore-path .gitignore src",
"test": "npx vitest run",
"coverage": "npx vitest run --coverage",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"format": "prettier --write --ignore-path .gitignore src",
"format:check": "prettier --check --ignore-path .gitignore src",
"test": "vitest run",
"test:watch": "vitest",
"coverage": "vitest run --coverage",
"build": "npm run build:cjs && npm run build:mjs",
"build:mjs": "npx tsc --project tsconfig.mjs.json && cp res/package.mjs.json dist/mjs/package.json",
"build:cjs": "npx tsc --project tsconfig.cjs.json && cp res/package.cjs.json dist/cjs/package.json",
"prepare": "npx husky install",
"docs": "npx typedoc"
"build:mjs": "tsc --project tsconfig.mjs.json && cp res/package.mjs.json dist/mjs/package.json",
"build:cjs": "tsc --project tsconfig.cjs.json && cp res/package.cjs.json dist/cjs/package.json",
"prepare": "husky install",
"docs": "typedoc"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -50,6 +51,7 @@
"eslint-plugin-jsdoc": "^46.0.0",
"eslint-plugin-security": "^1.7.1",
"husky": "^8.0.0",
"ky": "^1.2.0",
"mock-socket": "^9.1.5",
"prettier": "^3.1.0",
"typedoc": "^0.25.3",
Expand Down
2 changes: 2 additions & 0 deletions src/Model.discontinuity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('Model', () => {
);
channel.detach = vi.fn<any, any>();
ably.channels.release = vi.fn<any, any>();
channel.setOptions = vi.fn();

let counter = 0;

Expand Down Expand Up @@ -133,6 +134,7 @@ describe('Model', () => {
);
channel.detach = vi.fn<any, any>();
ably.channels.release = vi.fn<any, any>();
channel.setOptions = vi.fn();

let counter = 0;

Expand Down
65 changes: 65 additions & 0 deletions src/stream/Stream.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Realtime, Types } from 'ably/promises';
import pino from 'pino';
import { it, describe, expect, beforeEach, afterEach } from 'vitest';

import Stream from './Stream.js';
import { defaultSyncOptions, defaultEventBufferOptions } from '../Options.js';
import type { StreamOptions } from '../types/stream.js';
import { createAblyApp } from '../utilities/test/createAblyApp.js';

interface StreamTestContext extends StreamOptions {
stream: Stream;
channel: Types.RealtimeChannelPromise;
}

describe('Stream integration', () => {
beforeEach<StreamTestContext>(async (context) => {
const name = 'test';
const data = await createAblyApp({
keys: [{}],
namespaces: [{ id: name, persisted: true }],
channels: [
{
name,
presence: [
{ clientId: 'John', data: 'john@test.com' },
{ clientId: 'Dave', data: 'dave@test.com' },
],
},
],
});
const ably = new Realtime({
key: data.keys[0].keyStr,
environment: 'sandbox',
});
const logger = pino({ level: 'silent' });
const channel = ably.channels.get(name);
const stream = new Stream({
ably,
logger,
channelName: name,
syncOptions: defaultSyncOptions,
eventBufferOptions: defaultEventBufferOptions,
});

context.stream = stream;
context.channel = channel;
});

afterEach<StreamTestContext>(async ({ stream, channel }) => {
await stream.dispose();
await channel.detach();
});

it<StreamTestContext>('sets agent options when state is not attached', async ({ channel, stream }) => {
await stream.replay('0');
//@ts-ignore - `agent` is filtered out in `channel.params`, so that's the only way to check this
expect(channel.channelOptions.params).toEqual({ agent: 'models/0.0.2' }); // initial call from test
});

it<StreamTestContext>('does not sets agent options when state is attached', async ({ channel }) => {
await channel.attach();
//@ts-ignore - `agent` is filtered out in `channel.params`, so that's the only way to check this
expect(channel.channelOptions.params).toEqual(undefined); // initial call from test
});
});
88 changes: 88 additions & 0 deletions src/utilities/test/createAblyApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import ky from 'ky';

export const createAblyApp = async (json: Options): Promise<Data> => {
const response = await ky
.post('https://sandbox-rest.ably.io/apps', {
json,
headers: {
'content-type': 'application/json',
Accept: 'application/json',
},
})
.json<Data>();
return response;
};

interface Key {
capability?: {
private: ('subscribe' | 'publish')[];
chat?: 'presence'[];
};
}

interface Namespace {
id: string;
persisted: boolean;
}

interface Presence {
clientId: string;
data: string;
}

interface Channel {
name: string;
presence: Presence[];
}

interface Options {
keys?: Key[];
namespaces?: Namespace[];
channels?: Channel[];
}

interface Data {
status: number;
created: Date;
modified: Date;
tlsOnly: boolean;
labels: string;
enablePusherCompatibility: boolean;
namespaces: {
id: string;
created: Date;
modified: Date;
expires: Date | null;
persisted: boolean;
persistLast: boolean;
pushEnabled: boolean;
exposeTimeserial: boolean;
populateChannelRegistry: boolean;
tlsOnly: boolean;
authenticated: boolean;
identified: boolean;
cursorEventProcessing: boolean;
}[];
metaChannels: any;
id: string;
appId: string;
accountId: string;
keys: {
id: string;
value: string;
keyName: string;
keySecret: string;
keyStr: string;
capability: string;
expires: Date;
}[];
connections: {
name: string;
key: string;
}[];
channels: {
name: string;
presence: Presence[];
connection: string;
}[];
}

0 comments on commit 581737e

Please sign in to comment.