tests in typescript

This commit is contained in:
grimhilt
2023-04-01 22:36:51 +02:00
parent 9fbf5e5cf3
commit a5d325818b
22 changed files with 1258 additions and 412 deletions

View File

@@ -1,90 +0,0 @@
const { getAddresseId, getUserIdOfMailbox } from ";
const { generateAttrs, generateUsers } = require("../test-utils/test-attrsUtils");
const { registerMessageInApp, roomType } from ";
jest.mock("../../db/mail");
// todo mock db
// new message from us
// to multiple people -> room
// if response has same member => group
// if response is dm => channel
// to one person => dm
// new message from other
// to only me -> room
// if no reply to multiple message => channel
// else => dm
// to multiple people -> room
// // make it better
// if multiple members reply -> group
// if only me reply -> channel
describe("saveMessage", () => {
describe("new first message", () => {
const users = generateUsers(5);
const ownUser = users[0];
const messageId = 1;
const boxId = 1;
getUserIdOfMailbox.mockReturnValue([{ user_id: ownUser.id }]);
getAddresseId.mockImplementation((email) => {
const match = users.find((user) => user.user.mailbox + "@" + user.user.host == email);
return match.id;
});
describe("from us", () => {
it("new first message from us to one recipient should create a DM", async () => {
const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user] });
const register = new registerMessageInApp(messageId, attrs, boxId);
const createOrRegisterOnExistence = jest
.spyOn(register, "createOrRegisterOnExistence")
.mockImplementation(() => undefined);
await register.save();
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.DM);
});
it("new first message from us to multiple recipients should create a room", async () => {
const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user, users[2].user] });
const register = new registerMessageInApp(messageId, attrs, boxId);
const initiateRoom = jest
.spyOn(register, "initiateRoom")
.mockImplementation(() => undefined);
await register.save();
expect(initiateRoom).toHaveBeenCalledWith(ownUser.id, roomType.ROOM);
});
it("response to new first message to multiple recipients with same members should change room type to GROUP", () => {
});
it("response to new first message to multiple recipients with different members should change room type to CHANNEL", () => {
});
});
describe("from other", () => {
it("new first message from other to me only should create a room", async () => {
const attrs = generateAttrs({ from: [users[1].user], to: [ownUser.user] });
const register = new registerMessageInApp(messageId, attrs, boxId);
const createOrRegisterOnExistence = jest
.spyOn(register, "createOrRegisterOnExistence")
.mockImplementation(() => undefined);
await register.save();
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.ROOM);
});
});
});
describe("replies", () => {
it("", () => {});
});
describe("", () => {});
});

View File

@@ -0,0 +1,112 @@
import { generateAttrs, generateUsers } from "../test-utils/test-attrsUtils";
import registerMessageInApp, { roomType } from "../../mails/saveMessage";
import { jest, describe, it, expect } from "@jest/globals";
import { getAddresseId, getUserIdOfMailbox } from "../../db/mail";
// todo esbuild
// todo mock db
// new message from us
// to multiple people -> room
// if response has same member => group
// if response is dm => channel
// to one person => dm
// new message from other
// to only me -> room
// if no reply to multiple message => channel
// else => dm
// to multiple people -> room
// // make it better
// if multiple members reply -> group
// if only me reply -> channel
const users = generateUsers(5);
const ownUser = users[0];
const messageId = 1;
const boxId = 1;
jest.mock("../../db/mail", () => ({
getAddresseId: jest.fn().mockImplementation((email) => {
const match = users.find((user) => user.user.mailbox + "@" + user.user.host == email);
return new Promise((resolve, reject) => resolve(match?.id));
}),
getUserIdOfMailbox: jest.fn().mockImplementation((boxId) => {
return new Promise((resolve, reject) => resolve([{ user_id: ownUser.id }]));
}),
}));
describe("saveMessage", () => {
describe("functions", () => {
it("isFromUs", async () => {
const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user] });
const register = new registerMessageInApp(messageId, attrs, boxId);
await register.init();
const res = await register.isFromUs();
expect(res).toBe(true);
const attrs2 = generateAttrs({ from: [users[2].user], to: [users[1].user] });
const register2 = new registerMessageInApp(messageId, attrs2, boxId);
await register2.init();
const res2 = await register2.isFromUs();
expect(res2).toBe(false);
});
});
describe("implementation", () => {
describe("new first message from us", () => {
it("new first message from us to one recipient should create a DM", async () => {
const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user] });
const register = new registerMessageInApp(messageId, attrs, boxId);
const createOrRegisterOnExistence = jest
.spyOn(register, "createOrRegisterOnExistence")
.mockImplementation(
(owner: number, roomType: number) => new Promise((resolve, reject) => resolve()),
);
await register.save();
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.DM);
});
it("new first message from us to multiple recipients should create a ROOM", async () => {
const attrs = generateAttrs({ from: [ownUser.user], to: [users[1].user, users[2].user] });
const register = new registerMessageInApp(messageId, attrs, boxId);
const initiateRoom = jest
.spyOn(register, "initiateRoom")
.mockImplementation((owner: number, roomType: number) => Promise.resolve(1));
await register.save();
expect(initiateRoom).toHaveBeenCalledWith(ownUser.id, roomType.ROOM);
});
// it("response to new first message to multiple recipients with same members should change room type to GROUP", () => {});
// it("response to new first message to multiple recipients with different members should change room type to CHANNEL", () => {});
});
describe("new first message from other", () => {
it("new first message from other to me only should create a room", async () => {
const attrs = generateAttrs({ from: [users[1].user], to: [ownUser.user] });
const register = new registerMessageInApp(messageId, attrs, boxId);
const createOrRegisterOnExistence = jest
.spyOn(register, "createOrRegisterOnExistence")
.mockImplementation((owner: number, roomType: number) => {
return new Promise((resolve, reject) => resolve());
});
await register.save();
expect(createOrRegisterOnExistence).toHaveBeenCalledWith(users[1].id, roomType.ROOM);
});
});
// describe("replies", () => {
// it("", () => {});
// });
// describe("", () => {});
});
});

View File

@@ -1,44 +1,34 @@
const { nbMembers } from ";
const { generateUsers } = require("../../test-utils/test-attrsUtils");
import { nbMembers } from "../../../mails/utils/envelopeUtils";
import { generateAttrs, generateUsers } from "../../test-utils/test-attrsUtils";
import { describe, it, expect } from '@jest/globals';
describe("envelopeUtils", () => {
const names = generateUsers(6);
describe("nbMembers", () => {
it("sender and from shouldn't be counted twice if there are the same", () => {
const envelope = {
const envelope = generateAttrs({
from: [names[0].user],
sender: [names[0].user],
replyTo: null,
to: null,
cc: null,
bcc: null,
inReplyTo: null,
};
}).envelope;
expect(nbMembers(envelope)).toBe(1);
});
it("sender and from shoud be counted twice if there are the same", () => {
const envelope = {
const envelope = generateAttrs({
from: [names[0].user],
sender: [names[1].user],
replyTo: null,
to: null,
cc: null,
bcc: null,
inReplyTo: null,
};
}).envelope;
expect(nbMembers(envelope)).toBe(2);
});
it("should count every members", () => {
// todo should merge identic members
const envelope = {
const envelope = generateAttrs({
from: [names[0].user],
sender: [names[1].user],
replyTo: [names[2].user],
to: [names[3].user],
cc: [names[4].user],
bcc: [names[5].user],
inReplyTo: null,
};
}).envelope;
expect(nbMembers(envelope)).toBe(6);
});
});

View File

@@ -201,6 +201,4 @@ const names = [
"Lori",
];
module.exports = {
names,
};
export default names;

View File

@@ -1,61 +0,0 @@
const { names } from ";
function generateAttrs(options) {
const attrs = {
"size": 42,
"envelope": {
date: "2023-03-21T15:25:42.000Z",
subject: options.subject ?? "subject" + randomString(10),
from: options.from ?? null,
sender: options.sender ?? null,
replyTo: options.replyTo ?? null,
to: options.to ?? null,
cc: options.cc ?? null,
bcc: options.bcc ?? null,
inReplyTo: options.inReplyTo ?? null,
messageId: options.messageId ?? randomString(10),
},
"date": options.date ?? new Date(),
"flags": options.flags ?? [],
"uid": options.uid ?? randomInt(3),
"modseq": options.modseq ?? randomInt(7),
"x-gm-labels": ["\\Inbox"],
"x-gm-msgid": "1760991478422670209",
"x-gm-thrid": "1760991478422670209",
};
return attrs;
}
function generateUsers(nb) {
const users = [];
for (let i = 0; i < nb; i++) {
users.push({
user: {
name: "",
mailbox: names[i],
host: "provider.com",
},
id: i,
});
}
return users;
}
function randomString(length) {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function randomInt(length) {
return (Math.random() * Math.pow(10, length)).toFixed();
}
module.exports = {
generateAttrs,
generateUsers,
};

View File

@@ -0,0 +1,73 @@
import { AttrsWithEnvelope, User } from "../../interfaces/mail/attrs.interface";
import names from "./names";
interface Options {
subject?: string;
from?: User[];
sender?: User[];
replyTo?: User[];
to?: User[];
cc?: User[];
bcc?: User[];
inReplyTo?: string;
messageId?: string;
date?: string;
flags?: string[];
uid?: number;
modseq?: number;
}
export function generateAttrs(options: Options): AttrsWithEnvelope {
const attrs = {
"size": 42,
"envelope": {
date: "2023-03-21T15:25:42.000Z",
subject: options.subject ?? "subject" + randomString(10),
from: options.from ?? undefined,
sender: options.sender ?? undefined,
replyTo: options.replyTo ?? undefined,
to: options.to ?? undefined,
cc: options.cc ?? undefined,
bcc: options.bcc ?? undefined,
inReplyTo: options.inReplyTo ?? undefined,
messageId: options.messageId ?? randomString(10),
},
"date": options.date ?? new Date().toDateString(),
"flags": options.flags ?? [],
"uid": options.uid ?? randomInt(3),
"modseq": options.modseq ?? randomInt(7),
"x-gm-labels": ["\\Inbox"],
"x-gm-msgid": "1760991478422670209",
"x-gm-thrid": "1760991478422670209",
};
return attrs;
}
export function generateUsers(nb: number) {
const users: {user: User, id: number}[] = [];
for (let i = 0; i < nb; i++) {
users.push({
user: {
name: "",
mailbox: names[i],
host: "provider.com",
},
id: i,
});
}
return users;
}
function randomString(length: number): string {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function randomInt(length: number): number {
return parseInt((Math.random() * Math.pow(10, length)).toFixed());
}