added service test, fixed others

This commit is contained in:
Roland Osborne 2025-03-07 16:38:35 -08:00
parent 5f57f4b0f6
commit 732553c3ee
9 changed files with 81 additions and 21 deletions

View File

@ -152,7 +152,7 @@ const crypto = new TestCrypto();
test('received contact updates', async () => {
let testCards: Card[] = [];
const update = (cards: Card[]) => { testCards = cards }
const contact = new ContactModule(log, store, crypto, null, 'test_guid', 'test_token', 'test_url', false, [], []);
const contact = new ContactModule(log, store, crypto, null, 'test_guid', 'test_token', 'test_url', false, []);
contact.addCardListener(update);
contact.setRevision(1)
await waitFor(() => testCards.length === 1);
@ -163,7 +163,7 @@ test('received contact updates', async () => {
test('adds new contact', async () => {
let testCards: Card[] = [];
const update = (cards: Card[]) => { testCards = cards }
const contact = new ContactModule(log, store, crypto, null, 'test_guid', 'test_token', 'test_url', false, [], []);
const contact = new ContactModule(log, store, crypto, null, 'test_guid', 'test_token', 'test_url', false, []);
contact.addCardListener(update);
contact.setRevision(3)
await waitFor(() => testCards.length === 1);
@ -176,7 +176,7 @@ test('adds new contact', async () => {
test('removes contact', async () => {
let testCards: Card[] = [];
const update = (cards: Card[]) => { testCards = cards }
const contact = new ContactModule(log, store, crypto, null, 'test_guid', 'test_token', 'test_url', false, [], []);
const contact = new ContactModule(log, store, crypto, null, 'test_guid', 'test_token', 'test_url', false, []);
contact.addCardListener(update);
contact.setRevision(8)
await waitFor(() => testCards.length === 1);
@ -189,7 +189,7 @@ test('removes contact', async () => {
test('connects and disconnects with known contact', async () => {
let testCards: Card[] = [];
const update = (cards: Card[]) => { testCards = cards }
const contact = new ContactModule(log, store, crypto, null, 'test_guid', 'test_token', 'test_url', false, [], []);
const contact = new ContactModule(log, store, crypto, null, 'test_guid', 'test_token', 'test_url', false, []);
contact.addCardListener(update);
contact.setRevision(11)
await waitFor(() => testCards.length === 1);

View File

@ -160,7 +160,7 @@ const store = new TestStore();
test('received contact updates', async () => {
const cardChannels = new Map<string | null, Channel[]>();
const stream = new StreamModule(log, store, null, null, 'test_guid', 'test_token', 'test_url', false, []);
const contact = new ContactModule(log, store, null, null, 'test_guid', 'test_token', 'test_url', false, [], []);
const contact = new ContactModule(log, store, null, null, 'test_guid', 'test_token', 'test_url', false, []);
const content = new ContentModule(log, null, contact, stream);
const channelUpdate = ({ channels, cardId }: { channels: Channel[]; cardId: string | null }) => {
@ -188,7 +188,7 @@ test('received contact updates', async () => {
test('received stream updates', async () => {
const streamChannels = new Map<string | null, Channel[]>();
const stream = new StreamModule(log, store, null, null, 'test_guid', 'test_token', 'test_url', false, []);
const contact = new ContactModule(log, store, null, null, 'test_guid', 'test_token', 'test_url', false, [], []);
const contact = new ContactModule(log, store, null, null, 'test_guid', 'test_token', 'test_url', false, []);
const content = new ContentModule(log, null, contact, stream);
const channelUpdate = ({ channels, cardId }: { channels: Channel[]; cardId: string | null }) => {

View File

@ -0,0 +1,60 @@
import { DatabagSDK } from '../src/index';
import { Member } from '../src/types';
export type AccountEntity = {
accountId: number;
guid: string;
handle: string;
name: string;
description: string;
location: string;
imageSet: boolean;
revision: number;
seal?: string;
version: string;
node: string;
storageUsed: number;
disabled: boolean;
};
function getAccount(id: number) {
return {
accountId: id,
guid: `account.guid.${id}`,
handle: `account.handle.${id}`,
name: 'test.name',
description: 'test.description',
location: 'test.location',
imageSet: false,
revision: 1,
version: 'test.version',
node: 'test.node',
storageUsed: 1,
disabled: false,
}
}
jest.mock('../src/net/fetchUtil', () => {
const fn = jest.fn().mockImplementation((url: string, options: { method: string, body: string }) => {
if (url === 'http://test.node/admin/access?token=test.token') {
return Promise.resolve({ state: 200, json: () => ('admin.token') });
} else if (url === 'http://test.node/admin/accounts?token=admin.token') {
const accounts = [ getAccount(1), getAccount(2), getAccount(3) ];
return Promise.resolve({ state: 200, json: () => (accounts) });
}
});
return {
fetchWithTimeout: fn,
checkResponse: () => {},
}
});
test('allocates service correctly', async () => {
let status: string = '';
const sdk = new DatabagSDK({ channelTypes: []});
const service = await sdk.configure('test.node', false, 'test.token', null);
const members = await service.getMembers();
expect(members.length === 3).toBe(true);
});

View File

@ -96,7 +96,7 @@ jest.mock('../src/ring', () => {
test('allocates session correctly', async () => {
let status: string = '';
const sdk = new DatabagSDK({ topicBatch: 3, tagBatch: 3, articleTypes: [], channelTypes: []});
const sdk = new DatabagSDK({ channelTypes: []});
const params: SessionParams = { pushType: '', deviceToken: '', notifications: [], deviceId: '', version: '', appName: '' };
const session = await sdk.login('handle', 'password', 'jest.test', true, null, params);
session.addStatusListener((ev: string) => { status = ev; });

View File

@ -1,5 +1,6 @@
import { EventEmitter } from 'eventemitter3';
import type { Alias, Settings, Logging } from './api';
import type { Alias, Settings } from './api';
import { Logging } from './logging';
import type { Group } from './types';
import { Store } from './store';
@ -30,7 +31,7 @@ export class AliasModule implements Alias {
this.emitter.off('group', ev);
}
public async close(): void {}
public async close(): Promise<void> {}
public async setRevision(rev: number): Promise<void> {
console.log('set alias revision:', rev);

View File

@ -1,5 +1,6 @@
import { EventEmitter } from 'eventemitter3';
import type { Attribute, Settings, Logging } from './api';
import type { Attribute, Settings } from './api';
import { Logging } from './logging';
import type { Article } from './types';
import { Store } from './store';
@ -30,7 +31,7 @@ export class AttributeModule implements Attribute {
this.emitter.off('article', ev);
}
public async close(): void {}
public async close(): Promise<void> {}
public async setRevision(rev: number): Promise<void> {
console.log('set attribute revision:', rev);

View File

@ -1,5 +1,5 @@
import { EventEmitter } from 'eventemitter3';
import { Logging } from './api';
import { Logging } from './logging';
import { Revision } from './entities';
import { Call } from './types';
@ -10,7 +10,7 @@ export class Connection {
private closed: boolean;
private emitter: EventEmitter;
private websocket: WebSocket;
private stale: number;
private stale: ReturnType<typeof setTimeout>;
constructor(log: Logging, token: string, node: string, secure: boolean) {
this.closed = false;
@ -20,7 +20,8 @@ export class Connection {
this.stale = setInterval(() => {
if (this.websocket?.readyState == 1) {
this.websocket.ping?.(); // not defined in browser
const ws: any = this.websocket;
ws.ping?.(); // not defined in browser
}
}, PING_INTERVAL);
}

View File

@ -898,7 +898,7 @@ export class ContactModule implements Contact {
const contact = await setCardOpenMessage(server, !insecure, message);
if (contact.status === 'connected') {
const { token: contactToken, articleRevision, channelRevision, profileRevision } = contact;
await setCardConnected(node, secure, token, cardId, contactToken, articleRevision, channelRevision, profileRevision);
await setCardConnected(node, secure, token, added.id, contactToken, articleRevision, channelRevision, profileRevision);
}
} catch (err) {
this.log.error('failed to deliver open message');

View File

@ -1,6 +1,7 @@
import { EventEmitter } from 'eventemitter3';
import { LinkModule } from './link';
import type { Ring, Link, Logging } from './api';
import type { Ring, Link } from './api';
import { Logging } from './logging';
import type { Call } from './types';
import { removeContactCall } from './net/removeContactCall';
@ -9,9 +10,6 @@ const EXPIRES = 6000;
export class RingModule implements Ring {
private log: Logging;
private emitter: EventEmitter;
private token: string;
private node: string;
private secure: boolean;
private calls: Map<string, { call: Call, expires: number, status: string }>
private closed: boolean;
private endContactCall: (cardId: string, callId: string) => Promise<void>;
@ -20,8 +18,7 @@ export class RingModule implements Ring {
this.log = log;
this.endContactCall = endContactCall;
this.emitter = new EventEmitter();
this.calls = new Map<string, { call: Call, expires: number }>();
this.expire = null;
this.calls = new Map<string, { call: Call, expires: number, status: string }>();
this.closed = false;
}