mirror of
https://github.com/balzack/databag.git
synced 2025-04-23 01:55:17 +00:00
extending session test
This commit is contained in:
parent
af3d341d76
commit
7e54f806a9
64
app/sdk/__mocks__/account.ts
Normal file
64
app/sdk/__mocks__/account.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { type Account } from '../src/api';
|
||||
import type { AccountStatus } from '../src/types';
|
||||
|
||||
export class MockAccountModule implements Account {
|
||||
|
||||
public revision: number;
|
||||
private emitter: EventEmitter;
|
||||
|
||||
constructor() {
|
||||
this.revision = 0;
|
||||
this.emitter = new EventEmitter();
|
||||
}
|
||||
|
||||
public addStatusListener(ev: (status: AccountStatus) => void): void {
|
||||
this.emitter.on('status', ev);
|
||||
}
|
||||
|
||||
public removeStatusListener(ev: (status: AccountStatus) => void): void {
|
||||
this.emitter.off('status', ev);
|
||||
}
|
||||
|
||||
public close(): void {
|
||||
}
|
||||
|
||||
public async setRevision(rev: number): Promise<void> {
|
||||
this.revision = rev;
|
||||
}
|
||||
|
||||
public async enableNotifications(): Promise<void> {
|
||||
}
|
||||
|
||||
public async disableNotifications(): Promise<void> {
|
||||
}
|
||||
|
||||
public async enableRegistry(): Promise<void> {
|
||||
}
|
||||
|
||||
public async disableRegistry(): Promise<void> {
|
||||
}
|
||||
|
||||
public async enableMFA(): Promise<{ secretImage: string, secretText: string }> {
|
||||
return { secretImage: '', secretText: '' };
|
||||
}
|
||||
|
||||
public async disableMFA(): Promise<void> {
|
||||
}
|
||||
|
||||
public async confirmMFA(code: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setAccountSeal(password: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearAccountSeal(): Promise<void> {
|
||||
}
|
||||
|
||||
public async unlockAccountSeal(password: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setLogin(username: string, password: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
50
app/sdk/__mocks__/alias.ts
Normal file
50
app/sdk/__mocks__/alias.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import type { Alias, Account } from '../src/api';
|
||||
import type { Group } from '../src/types';
|
||||
|
||||
export class MockAliasModule implements Alias {
|
||||
|
||||
public revision: number;
|
||||
private emitter: EventEmitter;
|
||||
|
||||
constructor() {
|
||||
this.revision = 0;
|
||||
this.emitter = new EventEmitter();
|
||||
}
|
||||
|
||||
public addGroupListener(ev: (groups: Group[]) => void): void {
|
||||
this.emitter.on('group', ev);
|
||||
}
|
||||
|
||||
public removeGroupListener(ev: (groups: Group[]) => void): void {
|
||||
this.emitter.off('group', ev);
|
||||
}
|
||||
|
||||
public close(): void {
|
||||
}
|
||||
|
||||
public async setRevision(rev: number): Promise<void> {
|
||||
this.revision = rev;
|
||||
}
|
||||
|
||||
public async addGroup(sealed: boolean, dataType: string, subject: string, cardIds: string[]): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
public async removeGroup(groupId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setGroupSubject(groupId: string, subject: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setGroupCard(groupId: string, cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearGroupCard(groupId: string, cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async compare(groupIds: string[], cardIds: string[]): Promise<Map<string, string[]>> {
|
||||
return new Map<string, string[]>();
|
||||
}
|
||||
}
|
||||
|
58
app/sdk/__mocks__/attribute.ts
Normal file
58
app/sdk/__mocks__/attribute.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import type { Attribute, Account } from '../src/api';
|
||||
import type { Article } from '../src/types';
|
||||
|
||||
export class MockAttributeModule implements Attribute {
|
||||
|
||||
public revision: number;
|
||||
private emitter: EventEmitter;
|
||||
|
||||
constructor() {
|
||||
this.revision = 0;
|
||||
this.emitter = new EventEmitter();
|
||||
}
|
||||
|
||||
public addCardListener(ev: (articles: Article[]) => void): void {
|
||||
this.emitter.on('artcile', ev);
|
||||
}
|
||||
|
||||
public removeCardListener(ev: (articles: Article[]) => void): void {
|
||||
this.emitter.off('article', ev);
|
||||
}
|
||||
|
||||
public close(): void {
|
||||
}
|
||||
|
||||
public async setRevision(rev: number): Promise<void> {
|
||||
this.revision = rev;
|
||||
}
|
||||
|
||||
public async addArticle(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
public async removeArticle(articleId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setArticleSubject(articleId: string, subject: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setArticleCard(articleId: string, cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearArticleCard(articleId: string, cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setArticleGroup(articleId: string, groupId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearArticleGroup(articleId: string, groupId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public addArticleListener(ev: (articles: Article[]) => void): void {
|
||||
}
|
||||
|
||||
public removeArticleListener(ev: (articles: Article[]) => void): void {
|
||||
}
|
||||
}
|
||||
|
192
app/sdk/__mocks__/contact.ts
Normal file
192
app/sdk/__mocks__/contact.ts
Normal file
@ -0,0 +1,192 @@
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import type { Contact } from '../src/api';
|
||||
import type { Card, Topic, Asset, Tag, Profile, Repeater} from '../src/types';
|
||||
|
||||
export class MockContactModule implements Contact {
|
||||
|
||||
public revision: number;
|
||||
private emitter: EventEmitter;
|
||||
|
||||
constructor() {
|
||||
this.revision = 0;
|
||||
this.emitter = new EventEmitter();
|
||||
}
|
||||
|
||||
public addCardListener(ev: (cards: Card[]) => void): void {
|
||||
this.emitter.on('card', ev);
|
||||
}
|
||||
|
||||
public removeCardListener(ev: (cards: Card[]) => void): void {
|
||||
this.emitter.off('card', ev);
|
||||
}
|
||||
|
||||
public close(): void {
|
||||
}
|
||||
|
||||
public async setRevision(rev: number): Promise<void> {
|
||||
this.revision = rev;
|
||||
}
|
||||
|
||||
public async addCard(server: string, guid: string): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
public async removeCard(cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async confirmCard(cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async connectCard(cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async disconnectCard(cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async rejectCard(cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async ignoreCard(cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async resyncCard(cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async flagCard(cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async flagArticle(cardId: string, articleId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async flagChannel(cardId: string, channelId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async flagTopic(cardId: string, channelId: string, topicId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async flagTag(cardId: string, channelId: string, topicId: string, tagId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setBlockCard(cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setBlockArticle(cardId: string, articleId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setBlockChannel(cardId: string, channelId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setBlockTopic(cardId: string, channelId: string, topicId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setBlockTag(cardId: string, channelId: string, topicId: string, tagId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearBlockCard(cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearBlockArticle(cardId: string, articleId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearBlockChannel(cardId: string, channelId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearBlockTopic(cardId: string, channelId: string, topicId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearBlockTag(cardId: string, channelId: string, topicId: string, tagId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async getBlockedCards(): Promise<{ cardId: string }[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getBlockedChannels(): Promise<{ cardId: string, channelId: string }[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getBlockedTopics(): Promise<{ cardId: string, channelId: string, topicId: string }[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getBlockedTags(): Promise<{ cardId: string, channelId: string, topicId: string, tagId: string }[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getBlockedArticles(): Promise<{ cardId: string, articleId: string }[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async removeArticle(cardId: string, articleId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async removeChannel(cardId: string, channelId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async addTopic(cardId: string, channelId: string, type: string, message: string, assets: Asset[]): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
public async removeTopic(cardId: string, channelId: string, topicId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setTopicSubject(cardId: string, channelId: string, topicId: string, subject: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async addTag(cardId: string, channelId: string, topicId: string, type: string, value: string): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
public async removeTag(cardId: string, topicId: string, tagId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setTagSubject(cardId: string, topicId: string, tagId: string, subject: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async getTopics(cardId: string, channelId: string): Promise<Topic[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getMoreTopics(cardId: string, channelId: string): Promise<Topic[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getTags(cardId: string, channelId: string, topicId: string): Promise<Tag[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getMoreTags(cardId: string, channelId: string, topicId: string): Promise<Tag[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async setUnreadChannel(cardId: string, channelId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearUnreadChannel(cardId: string, channelId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async getRegistry(server: string): Promise<Profile[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public getRegistryImageUrl(server: string, guid: string): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
public getTopicAssetUrl(cardId: string, channelId: string, topicId: string, assetId: string): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
public getCardImageUrl(cardId: string): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
public async addRepeaterAccess(cardId: string, channelId: string, name: string): Promise<Repeater> {
|
||||
return { id: '', guid: '', name: '', server: '', token: '' };
|
||||
}
|
||||
|
||||
public async removeRepeaterAccess(cardId: string, channelId: string, repeaterId: string): Promise<void> {
|
||||
}
|
||||
|
||||
}
|
||||
|
131
app/sdk/__mocks__/content.ts
Normal file
131
app/sdk/__mocks__/content.ts
Normal file
@ -0,0 +1,131 @@
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import type { Content, Account } from '../src/api';
|
||||
import type { Channel, Topic, Asset, Tag, Repeater } from '../src/types';
|
||||
|
||||
export class MockContentModule implements Content {
|
||||
|
||||
public revision: number;
|
||||
private emitter: EventEmitter;
|
||||
|
||||
constructor() {
|
||||
this.revision = 0;
|
||||
this.emitter = new EventEmitter();
|
||||
}
|
||||
|
||||
public addChannelListener(ev: (channels: Channel[]) => void): void {
|
||||
this.emitter.on('channel', ev);
|
||||
}
|
||||
|
||||
public removeChannelListener(ev: (channels: Channel[]) => void): void {
|
||||
this.emitter.off('channel', ev);
|
||||
}
|
||||
|
||||
public close(): void {
|
||||
}
|
||||
|
||||
public async setRevision(rev: number): Promise<void> {
|
||||
this.revision = rev;
|
||||
}
|
||||
|
||||
public async addChannel(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
public async removeChannel(channelId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setChannelSubject(channelId: string, subject: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setChannelCard(channelId: string, cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearChannelCard(channelId: string, cardId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setChannelGroup(channelId: string, groupId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearChannelGroup(channelId: string, groupId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async addTopic(channelId: string, type: string, subject: string, assets: Asset[]): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
public async removeTopic(channelId: string, topicId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async flagTopic(channelId: string, topicId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setTopicSubject(channelId: string, topicId: string, subject: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async addTag(channelId: string, topicId: string, type: string, value: string): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
public async removeTag(channelId: string, topicId: string, tagId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setTagSubject(channelId: string, topicId: string, tagId: string, subject: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async flagTag(channelId: string, topicId: string, tagId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setBlockTopic(channelId: string, topicId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setBlockTag(channelId: string, topicId: string, tagId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearBlockTopic(channelId: string, topicId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearBlockTag(channelId: string, topicId: string, tagId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async getBlockedTopics(): Promise<{ channelId: string, topicId: string }[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getBlockedTags(): Promise<{ channelId: string, topicId: string, tagId: string }[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public getTopicAssetUrl(channelId: string, topicId: string, assetId: string): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
public async getTopics(channelId: string): Promise<Topic[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getMoreTopics(channelId: string): Promise<Topic[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getTags(channelId: string, topicId: string): Promise<Tag[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async getMoreTags(channelId: string, topicId: string): Promise<Tag[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
public async setUnreadChannel(channelId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async clearUnreadChannel(channelId: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async addRepeaterAccess(channelId: string, name: string): Promise<Repeater> {
|
||||
return { id: '', guid: '', name: '', server: '', token: '' };
|
||||
}
|
||||
|
||||
public async removeRepeaterAccess(channelId: string, repeaterId: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
43
app/sdk/__mocks__/identity.ts
Normal file
43
app/sdk/__mocks__/identity.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
import { type Identity } from '../src/api';
|
||||
import { type Profile } from '../src/types';
|
||||
|
||||
export class MockIdentityModule implements Identity {
|
||||
|
||||
public revision: number;
|
||||
private emitter: EventEmitter;
|
||||
|
||||
constructor() {
|
||||
this.revision = 0;
|
||||
this.emitter = new EventEmitter();
|
||||
}
|
||||
|
||||
public addProfileListener(ev: (profile: Profile) => void): void {
|
||||
this.emitter.on('profile', ev);
|
||||
}
|
||||
|
||||
public removeProfileListener(ev: (profile: Profile) => void): void {
|
||||
this.emitter.off('profile', ev);
|
||||
}
|
||||
|
||||
public close(): void {
|
||||
}
|
||||
|
||||
public async setRevision(rev: number): Promise<void> {
|
||||
this.revision = rev;
|
||||
}
|
||||
|
||||
public async setProfileData(name: string, location: string, description: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async setProfileImage(image: string): Promise<void> {
|
||||
}
|
||||
|
||||
public async getHandleStatus(handle: string): Promise<void> {
|
||||
}
|
||||
|
||||
public getProfileImageUrl(): string {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
10
app/sdk/__mocks__/waitFor.ts
Normal file
10
app/sdk/__mocks__/waitFor.ts
Normal file
@ -0,0 +1,10 @@
|
||||
export async function waitFor(cond: () => boolean, sec: number = 1): Promise<void> {
|
||||
for (let i = 0; i < sec * 10; i++) {
|
||||
if (cond()) {
|
||||
return;
|
||||
}
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
}
|
||||
expect(cond()).toBe(true);
|
||||
}
|
||||
|
@ -2,12 +2,73 @@ import { DatabagSDK } from '../src/index';
|
||||
import { type SessionParams } from '../src/types';
|
||||
|
||||
import { MockConnection } from '../__mocks__/connection';
|
||||
import { MockAccountModule } from '../__mocks__/account';
|
||||
import { MockIdentityModule } from '../__mocks__/identity';
|
||||
import { MockAliasModule } from '../__mocks__/alias';
|
||||
import { MockContentModule } from '../__mocks__/content';
|
||||
import { MockContactModule } from '../__mocks__/contact';
|
||||
import { MockAttributeModule } from '../__mocks__/attribute';
|
||||
import { waitFor } from '../__mocks__/waitFor';
|
||||
|
||||
const mock = new MockConnection();
|
||||
const mockConnection = new MockConnection();
|
||||
jest.mock('../src/connection', () => {
|
||||
return {
|
||||
Connection: jest.fn().mockImplementation(() => {
|
||||
return mock;
|
||||
return mockConnection;
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const mockAccount = new MockAccountModule();
|
||||
jest.mock('../src/account', () => {
|
||||
return {
|
||||
AccountModule: jest.fn().mockImplementation(() => {
|
||||
return mockAccount;
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const mockIdentity = new MockIdentityModule();
|
||||
jest.mock('../src/identity', () => {
|
||||
return {
|
||||
IdentityModule: jest.fn().mockImplementation(() => {
|
||||
return mockIdentity;
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const mockContent = new MockContentModule();
|
||||
jest.mock('../src/content', () => {
|
||||
return {
|
||||
ContentModule: jest.fn().mockImplementation(() => {
|
||||
return mockContent;
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const mockContact = new MockContactModule();
|
||||
jest.mock('../src/contact', () => {
|
||||
return {
|
||||
ContactModule: jest.fn().mockImplementation(() => {
|
||||
return mockContact;
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const mockAttribute = new MockAttributeModule();
|
||||
jest.mock('../src/attribute', () => {
|
||||
return {
|
||||
AttributeModule: jest.fn().mockImplementation(() => {
|
||||
return mockAttribute;
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const mockAlias = new MockAliasModule();
|
||||
jest.mock('../src/alias', () => {
|
||||
return {
|
||||
AliasModule: jest.fn().mockImplementation(() => {
|
||||
return mockAlias;
|
||||
})
|
||||
}
|
||||
})
|
||||
@ -21,7 +82,13 @@ test('allocates session correctly', async () => {
|
||||
session.addStatusListener((ev: string) => { status = ev; });
|
||||
const account = session.getAccount();
|
||||
account.enableNotifications();
|
||||
mock.emitStatus('connected');
|
||||
mock.emitRevision({ account: 0, profile: 0, article: 0, group: 0, channel: 0, card: 0});
|
||||
expect(status).toBe('connected');
|
||||
mockConnection.emitStatus('connected');
|
||||
mockConnection.emitRevision({ account: 3, profile: 3, article: 3, group: 3, channel: 3, card: 3});
|
||||
await waitFor(() => (status === 'connected'));
|
||||
await waitFor(() => (mockAccount.revision == 3));
|
||||
await waitFor(() => (mockIdentity.revision == 3));
|
||||
await waitFor(() => (mockContent.revision == 3));
|
||||
await waitFor(() => (mockContact.revision == 3));
|
||||
await waitFor(() => (mockAttribute.revision == 3));
|
||||
await waitFor(() => (mockAlias.revision == 3));
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user