diff --git a/packages/restapi/tests/lib/pushapi/base.test.ts b/packages/restapi/tests/lib/chat/base.test.ts similarity index 100% rename from packages/restapi/tests/lib/pushapi/base.test.ts rename to packages/restapi/tests/lib/chat/base.test.ts diff --git a/packages/restapi/tests/lib/chat/chat.test.ts b/packages/restapi/tests/lib/chat/chat.test.ts index 05b56d0ef..c98e3e0ca 100644 --- a/packages/restapi/tests/lib/chat/chat.test.ts +++ b/packages/restapi/tests/lib/chat/chat.test.ts @@ -1,33 +1,66 @@ import * as path from 'path'; import * as dotenv from 'dotenv'; dotenv.config({ path: path.resolve(__dirname, '../../.env') }); -import { chat } from '../../../src/lib/chat/chat'; -import { ethers } from 'ethers'; + +import { PushAPI } from '../../../src/lib/pushapi/PushAPI'; // Ensure correct import path import { expect } from 'chai'; -import Constants from '../../../src/lib/constants'; +import { ethers } from 'ethers'; +import { MessageType } from '../../../src/lib/constants'; + +describe('PushAPI.chat functionality', () => { + let userAlice: PushAPI; + let userBob: PushAPI; + let signer1: any; + let account1: string; + let signer2: any; + let account2: string; + const MESSAGE = 'Hey There!!!'; + + beforeEach(async () => { + const WALLET1 = ethers.Wallet.createRandom(); + signer1 = new ethers.Wallet(WALLET1.privateKey); + account1 = WALLET1.address; -const WALLET_PRIVATE_KEY = process.env['WALLET_PRIVATE_KEY']; -const _env = Constants.ENV.DEV; + const WALLET2 = ethers.Wallet.createRandom(); + signer2 = new ethers.Wallet(WALLET2.privateKey); + account2 = WALLET2.address; -describe('Get chat', () => { - it('Should return {} when not chat between users', async () => { - try { - const provider = ethers.getDefaultProvider(5); - const Pkey = `0x${WALLET_PRIVATE_KEY}`; - const _signer = new ethers.Wallet(Pkey, provider); - const walletAddress = _signer.address; - const account = `eip155:${walletAddress}`; - const inbox = await chat({ - account: account, - env: _env, - toDecrypt: true, - recipient: '0xDAFEA492D9c6733ae3d56b7Ed1ADB60692c98Bc5', - }); - expect(inbox).not.to.be.null; - expect(inbox).not.to.be.undefined; - expect(inbox).not.to.be.equal({}); - } catch (error) { - console.log(error); - } + userAlice = await PushAPI.initialize(signer1); + userBob = await PushAPI.initialize(signer2); + }); + + it('Should list request ', async () => { + await userAlice.chat.send(account2, { content: MESSAGE }); + const response = await userBob.chat.list('REQUESTS', { + page: 1, + limit: 10, + }); + expect(response).to.be.an('array'); + expect(response.length).to.equal(1); + }); + it('Should list chats ', async () => { + const response = await userAlice.chat.list('CHATS', { + page: 1, + limit: 10, + }); + expect(response).to.be.an('array'); + }); + it('Should send message ', async () => { + const response = await userAlice.chat.send(account2, { + content: 'Hello', + type: MessageType.TEXT, + }); + expect(response).to.be.an('object'); + }); + it('Should decrypt message ', async () => { + await userAlice.chat.send(account2, { + content: 'Hello', + type: MessageType.TEXT, + }); + const messagePayloads = await userAlice.chat.history(account2); + const decryptedMessagePayloads = await userBob.chat.decrypt( + messagePayloads + ); + expect(decryptedMessagePayloads).to.be.an('array'); }); }); diff --git a/packages/restapi/tests/lib/pushapi/group.test.ts b/packages/restapi/tests/lib/chat/group.test.ts similarity index 100% rename from packages/restapi/tests/lib/pushapi/group.test.ts rename to packages/restapi/tests/lib/chat/group.test.ts diff --git a/packages/restapi/tests/lib/pushapi/initialize.test.ts b/packages/restapi/tests/lib/chat/initialize.test.ts similarity index 100% rename from packages/restapi/tests/lib/pushapi/initialize.test.ts rename to packages/restapi/tests/lib/chat/initialize.test.ts diff --git a/packages/restapi/tests/lib/pushapi/profile.test.ts b/packages/restapi/tests/lib/chat/profile.test.ts similarity index 100% rename from packages/restapi/tests/lib/pushapi/profile.test.ts rename to packages/restapi/tests/lib/chat/profile.test.ts diff --git a/packages/restapi/tests/lib/chatLowLevel/chat.test.ts b/packages/restapi/tests/lib/chatLowLevel/chat.test.ts new file mode 100644 index 000000000..05b56d0ef --- /dev/null +++ b/packages/restapi/tests/lib/chatLowLevel/chat.test.ts @@ -0,0 +1,33 @@ +import * as path from 'path'; +import * as dotenv from 'dotenv'; +dotenv.config({ path: path.resolve(__dirname, '../../.env') }); +import { chat } from '../../../src/lib/chat/chat'; +import { ethers } from 'ethers'; +import { expect } from 'chai'; +import Constants from '../../../src/lib/constants'; + +const WALLET_PRIVATE_KEY = process.env['WALLET_PRIVATE_KEY']; +const _env = Constants.ENV.DEV; + +describe('Get chat', () => { + it('Should return {} when not chat between users', async () => { + try { + const provider = ethers.getDefaultProvider(5); + const Pkey = `0x${WALLET_PRIVATE_KEY}`; + const _signer = new ethers.Wallet(Pkey, provider); + const walletAddress = _signer.address; + const account = `eip155:${walletAddress}`; + const inbox = await chat({ + account: account, + env: _env, + toDecrypt: true, + recipient: '0xDAFEA492D9c6733ae3d56b7Ed1ADB60692c98Bc5', + }); + expect(inbox).not.to.be.null; + expect(inbox).not.to.be.undefined; + expect(inbox).not.to.be.equal({}); + } catch (error) { + console.log(error); + } + }); +}); diff --git a/packages/restapi/tests/lib/chat/createGroup.test.ts b/packages/restapi/tests/lib/chatLowLevel/createGroup.test.ts similarity index 100% rename from packages/restapi/tests/lib/chat/createGroup.test.ts rename to packages/restapi/tests/lib/chatLowLevel/createGroup.test.ts diff --git a/packages/restapi/tests/lib/chat/send.test.ts b/packages/restapi/tests/lib/chatLowLevel/send.test.ts similarity index 100% rename from packages/restapi/tests/lib/chat/send.test.ts rename to packages/restapi/tests/lib/chatLowLevel/send.test.ts diff --git a/packages/restapi/tests/lib/chat/updateGroup.test.ts b/packages/restapi/tests/lib/chatLowLevel/updateGroup.test.ts similarity index 100% rename from packages/restapi/tests/lib/chat/updateGroup.test.ts rename to packages/restapi/tests/lib/chatLowLevel/updateGroup.test.ts diff --git a/packages/restapi/tests/lib/pushNotification/alias.test.ts b/packages/restapi/tests/lib/notification/alias.test.ts similarity index 100% rename from packages/restapi/tests/lib/pushNotification/alias.test.ts rename to packages/restapi/tests/lib/notification/alias.test.ts diff --git a/packages/restapi/tests/lib/notification/channel.test.ts b/packages/restapi/tests/lib/notification/channel.test.ts new file mode 100644 index 000000000..6d06bba64 --- /dev/null +++ b/packages/restapi/tests/lib/notification/channel.test.ts @@ -0,0 +1,299 @@ +import * as path from 'path'; +import * as dotenv from 'dotenv'; +dotenv.config({ path: path.resolve(__dirname, '../../../.env') }); + +import { PushAPI } from '../../../src/lib/pushapi/PushAPI'; +import { expect } from 'chai'; +import { ethers } from 'ethers'; + +describe('PushAPI.channel functionality', () => { + let userAlice: PushAPI; + let userBob: PushAPI; + let userKate: PushAPI; + let signer1: any; + let account1: string; + let signer2: any; + let account2: string; + + beforeEach(async () => { + signer1 = new ethers.Wallet(`0x${process.env['WALLET_PRIVATE_KEY']}`); + account1 = await signer1.getAddress(); + + const provider = new ethers.providers.JsonRpcProvider( + // PUBLIC RPC + 'https://goerli.blockpi.network/v1/rpc/public' + ); + + signer2 = new ethers.Wallet( + `0x${process.env['WALLET_PRIVATE_KEY']}`, + provider + ); + account2 = await signer2.getAddress(); + enum ENV { + PROD = 'prod', + STAGING = 'staging', + DEV = 'dev', + /** + * **This is for local development only** + */ + LOCAL = 'local', + } + // initialisation with signer and provider + userKate = await PushAPI.initialize(signer2, {env: ENV.DEV}) + // initialisation with signer + userAlice = await PushAPI.initialize(signer2); + // TODO: remove signer1 after chat makes signer as optional + //initialisation without signer + userBob = await PushAPI.initialize(signer1); + }); + + describe('channel :: info', () => { + // TODO: remove skip after signer becomes optional + it.skip('Without signer and account: Should throw error', async () => { + await expect(() => userBob.channel.info()).to.Throw; + }); + + it('Without signer but with non-caip account: Should return response', async () => { + const res = await userBob.channel.info( + '0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5' + ); + // console.log(res) + expect(res).not.null; + }); + + it('Without signer and with valid caip account: Should return response', async () => { + const res = await userBob.channel.info( + 'eip155:5:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5' + ); + // console.log(res); + expect(res).not.null; + }); + }); + + describe('channel :: search', () => { + it('Without signer and account : Should return response', async () => { + const res = await userBob.channel.search(' '); + // console.log(res); + expect(res).not.null; + }); + + it('With signer: Should return response', async () => { + const res = await userBob.channel.search(' '); + // console.log(res); + expect(res).not.null; + }); + + it('Should throw error for empty query', async () => { + // const res = await userBob.channel.search('') + await expect(() => userBob.channel.search('')).to.Throw; + }); + }); + + describe('channel :: subscribers', () => { + // TODO: remove skip after signer becomes optional + it.skip('Without signer and account : Should throw error', async () => { + await expect(() => userBob.channel.subscribers()).to.Throw; + }); + + it('Without signer and account : Should return response as address is passed', async () => { + const res = await userBob.channel.subscribers({ + channel: 'eip155:5:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5', + }); + // console.log(res) + expect(res).not.null; + }); + + it('Without signer and account : Should return response for alias address', async () => { + const res = await userBob.channel.subscribers({ + channel: 'eip155:80001:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5', + }); + // console.log(res) + expect(res).not.null; + }); + + it('Without signer and account : Should return response without passing the options', async () => { + const res = await userKate.channel.subscribers(); + expect(res).not.null; + }); + + it('Without signer and account : Should throw error for invalid caip', async () => { + await expect(() => + userBob.channel.subscribers({ + channel: '0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5', + }) + ).to.Throw; + }); + }); + + describe('channel :: send', () => { + // TODO: remove skip after signer becomes optional + it.skip('Without signer and account : Should throw error', async () => { + await expect(() => { + userBob.channel.send(['*'], { + notification: { + title: 'test', + body: 'test', + }, + }); + }).to.Throw; + }); + + it('With signer : broadcast : Should send notification with title and body', async () => { + const res = await userAlice.channel.send(['*'], { + notification: { + title: 'test', + body: 'test', + }, + }); + // console.log(res) + expect(res.status).to.equal(204); + }); + + it('With signer : targeted : Should send notification with title and body', async () => { + const res = await userAlice.channel.send( + ['eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681'], + { + notification: { + title: 'hi', + body: 'test-targeted', + }, + } + ); + expect(res.status).to.equal(204); + }); + + it('With signer : targeted : Should send notification with title and body', async () => { + const res = await userAlice.channel.send( + ['eip155:5:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5'], + { + notification: { + title: 'hi', + body: 'test-targeted', + }, + } + ); + expect(res.status).to.equal(204); + }); + + it('With signer : subset : Should send notification with title and body', async () => { + const res = await userAlice.channel.send( + [ + 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681', + 'eip155:5:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5', + ], + { + notification: { + title: 'hi', + body: 'test-targeted', + }, + } + ); + expect(res.status).to.equal(204); + }); + + it('With signer : subset : Should send notification with title and body along with additional options', async () => { + const res = await userAlice.channel.send( + [ + 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681', + 'eip155:5:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5', + ], + { + notification: { + title: 'hi', + body: 'test-targeted', + }, + payload: { + title: 'testing first notification', + body: 'testing with random body', + cta: 'https://google.com/', + embed: 'https://avatars.githubusercontent.com/u/64157541?s=200&v=4', + }, + } + ); + expect(res.status).to.equal(204); + }); + + it('With signer : subset : Should send notification with title and body along with additional options', async () => { + const res = await userAlice.channel.send( + [ + 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681', + 'eip155:5:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5', + ], + { + notification: { + title: 'hi', + body: 'test-subset', + }, + payload: { + title: 'testing first subset notification', + body: 'testing with random body', + cta: 'https://google.com/', + embed: 'https://avatars.githubusercontent.com/u/64157541?s=200&v=4', + }, + } + ); + expect(res.status).to.equal(204); + }); + + it('With signer : subset : Should send notification with title and body along with additional options for alias', async () => { + const res = await userAlice.channel.send( + [ + 'eip155:97:0xD8634C39BBFd4033c0d3289C4515275102423681', + 'eip155:97:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5', + ], + { + notification: { + title: 'hi', + body: 'test-subset', + }, + payload: { + title: 'testing first subset notification', + body: 'testing with random body', + cta: 'https://google.com/', + embed: 'https://avatars.githubusercontent.com/u/64157541?s=200&v=4', + }, + channel: 'eip155:97:0xD8634C39BBFd4033c0d3289C4515275102423681', + } + ); + expect(res.status).to.equal(204); + }); + }); + + describe.skip('channel :: update', () => { + it('Should update channel meta', async () => { + const res = await userKate.channel.update({ + name: 'Updated Name', + description: 'Testing new description', + url: 'https://google.com', + icon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAz0lEQVR4AcXBsU0EQQyG0e+saWJ7oACiKYDMEZVs6GgSpC2BIhzRwAS0sgk9HKn3gpFOAv3v3V4/3+4U4Z1q5KTy42Ql940qvFONnFSGmCFmiN2+fj7uCBlihpgh1ngwcvKfwjuVIWaIGWKNB+GdauSk8uNkJfeNKryzYogZYoZY40m5b/wlQ8wQM8TayMlKeKcaOVkJ71QjJyuGmCFmiDUe+HFy4VyEd57hx0mV+0ZliBlihlgL71w4FyMnVXhnZeSkiu93qheuDDFDzBD7BcCyMAOfy204AAAAAElFTkSuQmCC', + }); + // console.log(res) + expect(res).not.null; + }, 10000000000); + }); + + describe.skip('channel :: create', () => { + it('Should create channel', async () => { + const res = await userKate.channel.create({ + name: 'SDK Test', + description: 'Testing new description', + url: 'https://google.com', + icon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAz0lEQVR4AcXBsU0EQQyG0e+saWJ7oACiKYDMEZVs6GgSpC2BIhzRwAS0sgk9HKn3gpFOAv3v3V4/3+4U4Z1q5KTy42Ql940qvFONnFSGmCFmiN2+fj7uCBlihpgh1ngwcvKfwjuVIWaIGWKNB+GdauSk8uNkJfeNKryzYogZYoZY40m5b/wlQ8wQM8TayMlKeKcaOVkJ71QjJyuGmCFmiDUe+HFy4VyEd57hx0mV+0ZliBlihlgL71w4FyMnVXhnZeSkiu93qheuDDFDzBD7BcCyMAOfy204AAAAAElFTkSuQmCC', + }); + // console.log(res) + expect(res).not.null; + }, 10000000000); + }); + + describe.skip('channel :: settings', () => { + it('Should create channel', async () => { + const res = await userKate.channel.setting([ + {type: 2, default: 5, description: "My notif setting 2", data: {upper:100, lower:5, ticker: 10, enabled: true}}, + { type: 1, default: 1, description: 'My Notif Settings' }, + + ]); + // console.log(res) + expect(res).not.null; + }, 10000000000); + }); +}); diff --git a/packages/restapi/tests/lib/pushNotification/delegate.test.ts b/packages/restapi/tests/lib/notification/delegate.test.ts similarity index 100% rename from packages/restapi/tests/lib/pushNotification/delegate.test.ts rename to packages/restapi/tests/lib/notification/delegate.test.ts diff --git a/packages/restapi/tests/lib/pushNotification/notification.test.ts b/packages/restapi/tests/lib/notification/notification.test.ts similarity index 100% rename from packages/restapi/tests/lib/pushNotification/notification.test.ts rename to packages/restapi/tests/lib/notification/notification.test.ts diff --git a/packages/restapi/tests/lib/pushNotification/onchain.test.ts b/packages/restapi/tests/lib/notification/onchain.test.ts similarity index 100% rename from packages/restapi/tests/lib/pushNotification/onchain.test.ts rename to packages/restapi/tests/lib/notification/onchain.test.ts diff --git a/packages/restapi/tests/lib/pushNotification/tokenABI.ts b/packages/restapi/tests/lib/notification/tokenABI.ts similarity index 100% rename from packages/restapi/tests/lib/pushNotification/tokenABI.ts rename to packages/restapi/tests/lib/notification/tokenABI.ts diff --git a/packages/restapi/tests/lib/pushapi/chat.test.ts b/packages/restapi/tests/lib/pushapi/chat.test.ts deleted file mode 100644 index c98e3e0ca..000000000 --- a/packages/restapi/tests/lib/pushapi/chat.test.ts +++ /dev/null @@ -1,66 +0,0 @@ -import * as path from 'path'; -import * as dotenv from 'dotenv'; -dotenv.config({ path: path.resolve(__dirname, '../../.env') }); - -import { PushAPI } from '../../../src/lib/pushapi/PushAPI'; // Ensure correct import path -import { expect } from 'chai'; -import { ethers } from 'ethers'; -import { MessageType } from '../../../src/lib/constants'; - -describe('PushAPI.chat functionality', () => { - let userAlice: PushAPI; - let userBob: PushAPI; - let signer1: any; - let account1: string; - let signer2: any; - let account2: string; - const MESSAGE = 'Hey There!!!'; - - beforeEach(async () => { - const WALLET1 = ethers.Wallet.createRandom(); - signer1 = new ethers.Wallet(WALLET1.privateKey); - account1 = WALLET1.address; - - const WALLET2 = ethers.Wallet.createRandom(); - signer2 = new ethers.Wallet(WALLET2.privateKey); - account2 = WALLET2.address; - - userAlice = await PushAPI.initialize(signer1); - userBob = await PushAPI.initialize(signer2); - }); - - it('Should list request ', async () => { - await userAlice.chat.send(account2, { content: MESSAGE }); - const response = await userBob.chat.list('REQUESTS', { - page: 1, - limit: 10, - }); - expect(response).to.be.an('array'); - expect(response.length).to.equal(1); - }); - it('Should list chats ', async () => { - const response = await userAlice.chat.list('CHATS', { - page: 1, - limit: 10, - }); - expect(response).to.be.an('array'); - }); - it('Should send message ', async () => { - const response = await userAlice.chat.send(account2, { - content: 'Hello', - type: MessageType.TEXT, - }); - expect(response).to.be.an('object'); - }); - it('Should decrypt message ', async () => { - await userAlice.chat.send(account2, { - content: 'Hello', - type: MessageType.TEXT, - }); - const messagePayloads = await userAlice.chat.history(account2); - const decryptedMessagePayloads = await userBob.chat.decrypt( - messagePayloads - ); - expect(decryptedMessagePayloads).to.be.an('array'); - }); -}); diff --git a/packages/restapi/tests/lib/pushstream/initialize.test.ts b/packages/restapi/tests/lib/stream/initialize.test.ts similarity index 100% rename from packages/restapi/tests/lib/pushstream/initialize.test.ts rename to packages/restapi/tests/lib/stream/initialize.test.ts diff --git a/packages/restapi/tests/lib/user/createUser.test.ts b/packages/restapi/tests/lib/userLowLevel/createUser.test.ts similarity index 100% rename from packages/restapi/tests/lib/user/createUser.test.ts rename to packages/restapi/tests/lib/userLowLevel/createUser.test.ts diff --git a/packages/restapi/tests/lib/user/createUserWithProfile.test.ts b/packages/restapi/tests/lib/userLowLevel/createUserWithProfile.test.ts similarity index 100% rename from packages/restapi/tests/lib/user/createUserWithProfile.test.ts rename to packages/restapi/tests/lib/userLowLevel/createUserWithProfile.test.ts diff --git a/packages/restapi/tests/lib/user/getUser.test.ts b/packages/restapi/tests/lib/userLowLevel/getUser.test.ts similarity index 100% rename from packages/restapi/tests/lib/user/getUser.test.ts rename to packages/restapi/tests/lib/userLowLevel/getUser.test.ts diff --git a/packages/restapi/tests/lib/user/upgradeUser.test.ts b/packages/restapi/tests/lib/userLowLevel/upgradeUser.test.ts similarity index 100% rename from packages/restapi/tests/lib/user/upgradeUser.test.ts rename to packages/restapi/tests/lib/userLowLevel/upgradeUser.test.ts