Skip to content

Commit

Permalink
added create room test
Browse files Browse the repository at this point in the history
added send message test
  • Loading branch information
dev2-nomo committed Nov 16, 2023
1 parent 524f4ac commit 50cfb76
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/create_room/create_room.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import axios from "axios";
const server = 'https://zeniq.chat/';

/**
* TODO: document here
*/
export async function nomoCreateRoom(args: Map<string, any>, accessToken: string): Promise<string> {
const url = server + `_matrix/client/v3/createRoom`;

try {
const response = await axios.post(url, args, {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
});
return response.data['room_id'];
} catch (error) {
console.error('Error sending message:', error);
return "";
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
export {nomoRegisterOrLogin, UserMatrix, nomoLogin, nomoRegister} from "./register_login/register_login"
export {nomoListenRoom} from "./listen_room/listen_room"
export {nomoJoinRoom} from "./join_room/join_room"
export {nomoSendMessage} from "./send_message/send_message"
export {nomoSendMessage, SendMessageArgs} from "./send_message/send_message"
export {nomoSyncUser} from "./syncing/sync_user"
export {nomoCreateFilter} from "./syncing/create_filter"
27 changes: 22 additions & 5 deletions src/send_message/send_message.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@

import axios from "axios";
const server = 'https://zeniq.chat/';
export interface SendMessageArgs {
room: string;
content: string;
roomId: string;
content: Map<string, any>;
transationID: string;
eventType: string;
}

/**
* TODO: document here
*/
export async function nomoSendMessage(args: SendMessageArgs) {
console.log("nomoSendMessage not implemented");
export async function nomoSendMessage(args: SendMessageArgs, accessToken: string): Promise<string> {
let { roomId, content, transationID, eventType } = args;
const url = server + `_matrix/client/v3/rooms/${roomId}/send/${eventType}/${transationID}`;

try {
const response = await axios.put(url, content, {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
});
return response.data['event_id'];
} catch (error) {
console.error('Error sending message:', error);
return "";
}
}
21 changes: 21 additions & 0 deletions test/create_room_test/create_room.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { nomoRegisterOrLogin, UserMatrix, } from "../../src";
import { nomoCreateRoom } from "../../src/create_room/create_room";
import { generateRandomString } from "../test_util";

test("create room", async () => {
const mnemonic = "diet say develop title sibling steel blast table chicken foster fuel giraffe";
const userMatrix: UserMatrix = await nomoRegisterOrLogin(mnemonic);
const args: Map<string, any> = new Map
{
[
['name', generateRandomString(25)],
["preset", "private_chat",],
['room_alias_name', generateRandomString(10)]
]
};
let roomId = await nomoCreateRoom(args, userMatrix.access_token);
console.log(roomId);
if (roomId.length == 0) {
fail("no roomId");
}
});
37 changes: 35 additions & 2 deletions test/send_message_test/send_message.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import {nomoSendMessage} from "../../src";
import { nomoSendMessage, nomoRegisterOrLogin, UserMatrix, SendMessageArgs } from "../../src";
import { nomoCreateRoom } from "../../src/create_room/create_room";
import { generateRandomString } from "../test_util";

test("send message", async () => {
await nomoSendMessage({content: "foo", room: "bar"});
const mnemonic = "diet say develop title sibling steel blast table chicken foster fuel giraffe";
const userMatrix: UserMatrix = await nomoRegisterOrLogin(mnemonic);
const args: Map<string, any> = new Map
{
[
['name', generateRandomString(25)],
["preset", "private_chat",],
['room_alias_name', generateRandomString(10)]
]
};
let roomId = await nomoCreateRoom(args, userMatrix.access_token);
if (roomId.length == 0) {
fail("no roomId");
}
const contentMap: Map<string, any> = new Map
{
[
['body', generateRandomString(25)],
["msgtype", "m.text"],
]
};
const testMessage: SendMessageArgs =
{
content: contentMap,
roomId: roomId,
transationID: generateRandomString(200),
eventType: "m.room.message",
};
let eventId = await nomoSendMessage(testMessage, userMatrix.access_token);
if (eventId.length == 0) {
fail("eventId empty");
}
});
2 changes: 1 addition & 1 deletion test/syncing/sync_user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ test("sync user", async () => {
const mnemonic = "diet say develop title sibling steel blast table chicken foster fuel giraffe";
const userMatrix: UserMatrix = await nomoRegisterOrLogin(mnemonic);
// const filterID: string = await nomoCreateFilter(userMatrix.access_token, userMatrix.user_id);
// await nomoSyncUser(userMatrix.access_token, "", filterID, "online", false);
// await nomoSyncUser(userMatrix.access_token, "", '', "online", false);
// expect(userMatrix.user_id).toBe("@0x3f0e8cF0c6eb9789348541D9D0Ce4ac847277e9B:zeniq.chat");
}, 10000);

0 comments on commit 50cfb76

Please sign in to comment.