Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 41062cb0 rédigé par Sorunome's avatar Sorunome
Parcourir les fichiers

add tests

parent 89904ad0
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -210,7 +210,7 @@ export class MatrixEventProcessor { ...@@ -210,7 +210,7 @@ export class MatrixEventProcessor {
replyEmbed.addField("ping", `<@${uid}>`); replyEmbed.addField("ping", `<@${uid}>`);
} }
replyEmbed.timestamp = new Date(sourceEvent.origin_server_ts); replyEmbed.setTimestamp(new Date(sourceEvent.origin_server_ts));
return replyEmbed; return replyEmbed;
} catch (ex) { } catch (ex) {
log.warn("Failed to handle reply, showing a unknown embed:", ex); log.warn("Failed to handle reply, showing a unknown embed:", ex);
......
...@@ -16,6 +16,8 @@ import { IMatrixEvent } from "../src/matrixtypes"; ...@@ -16,6 +16,8 @@ import { IMatrixEvent } from "../src/matrixtypes";
// we are a test file and thus need those // we are a test file and thus need those
/* tslint:disable:no-unused-expression max-file-line-count no-any */ /* tslint:disable:no-unused-expression max-file-line-count no-any */
const TEST_TIMESTAMP = 1337;
const expect = Chai.expect; const expect = Chai.expect;
// const assert = Chai.assert; // const assert = Chai.assert;
const bot = { const bot = {
...@@ -63,7 +65,7 @@ function createMatrixEventProcessor(): MatrixEventProcessor { ...@@ -63,7 +65,7 @@ function createMatrixEventProcessor(): MatrixEventProcessor {
const bridge = { const bridge = {
getBot: () => { getBot: () => {
return { return {
isRemoteUser: () => false, isRemoteUser: (s) => s.includes("@_discord_"),
}; };
}, },
getClientFactory: () => { getClientFactory: () => {
...@@ -88,6 +90,7 @@ function createMatrixEventProcessor(): MatrixEventProcessor { ...@@ -88,6 +90,7 @@ function createMatrixEventProcessor(): MatrixEventProcessor {
content: { content: {
body: "Hello!", body: "Hello!",
}, },
origin_server_ts: TEST_TIMESTAMP,
sender: "@doggo:localhost", sender: "@doggo:localhost",
}; };
} else if (eventId === "$reply:localhost") { } else if (eventId === "$reply:localhost") {
...@@ -114,6 +117,13 @@ function createMatrixEventProcessor(): MatrixEventProcessor { ...@@ -114,6 +117,13 @@ function createMatrixEventProcessor(): MatrixEventProcessor {
}, },
sender: "@doggo:localhost", sender: "@doggo:localhost",
}; };
} else if (eventId === "$discord:localhost") {
return {
content: {
body: "Foxies",
},
sender: "@_discord_1234:localhost",
};
} }
return null; return null;
}, },
...@@ -137,6 +147,11 @@ function createMatrixEventProcessor(): MatrixEventProcessor { ...@@ -137,6 +147,11 @@ function createMatrixEventProcessor(): MatrixEventProcessor {
return Buffer.alloc(size); return Buffer.alloc(size);
}, },
}); });
const discordbot = {
GetDiscordUserOrMember: async (s) => {
return new Discord.User({ } as any, { username: "Someuser" });
},
};
return new (Proxyquire("../src/matrixeventprocessor", { return new (Proxyquire("../src/matrixeventprocessor", {
"./util": { "./util": {
...@@ -146,7 +161,7 @@ function createMatrixEventProcessor(): MatrixEventProcessor { ...@@ -146,7 +161,7 @@ function createMatrixEventProcessor(): MatrixEventProcessor {
new MatrixEventProcessorOpts( new MatrixEventProcessorOpts(
config, config,
bridge, bridge,
{} as any, discordbot as any,
)); ));
} }
const mockChannel = new MockChannel(); const mockChannel = new MockChannel();
...@@ -416,6 +431,23 @@ describe("MatrixEventProcessor", () => { ...@@ -416,6 +431,23 @@ describe("MatrixEventProcessor", () => {
} as IMatrixEvent, mockChannel as any); } as IMatrixEvent, mockChannel as any);
Chai.assert.equal(embeds.messageEmbed.description, ""); Chai.assert.equal(embeds.messageEmbed.description, "");
}); });
it("Should ping the user on discord replies", async () => {
const processor = createMatrixEventProcessor();
const embeds = await processor.EventToEmbed({
content: {
"body": "Bunnies",
"m.relates_to": {
"m.in_reply_to": {
event_id: "$discord:localhost",
},
},
"url": "mxc://bunny",
},
sender: "@test:localhost",
type: "m.room.member",
} as IMatrixEvent, mockChannel as any);
Chai.assert.equal(embeds.messageEmbed.description, "Bunnies\n(<@1234>)");
});
}); });
describe("HandleAttachment", () => { describe("HandleAttachment", () => {
const SMALL_FILE = 200; const SMALL_FILE = 200;
...@@ -627,5 +659,47 @@ This is the reply`, ...@@ -627,5 +659,47 @@ This is the reply`,
expect(result!.author!.icon_url).to.be.equal("https://fakeurl.com"); expect(result!.author!.icon_url).to.be.equal("https://fakeurl.com");
expect(result!.author!.url).to.be.equal("https://matrix.to/#/@doggo:localhost"); expect(result!.author!.url).to.be.equal("https://matrix.to/#/@doggo:localhost");
}); });
it("should add the reply time", async () => {
const processor = createMatrixEventProcessor();
const result = await processor.GetEmbedForReply({
content: {
"body": "Test",
"m.relates_to": {
"m.in_reply_to": {
event_id: "$goodEvent:localhost",
},
},
},
sender: "@test:localhost",
type: "m.room.message",
} as IMatrixEvent, mockChannel as any);
expect(result!.timestamp!.getTime()).to.be.equal(TEST_TIMESTAMP);
});
it("should add field for discord replies", async () => {
const processor = createMatrixEventProcessor();
const result = await processor.GetEmbedForReply({
content: {
"body": "foxfoxfox",
"m.relates_to": {
"m.in_reply_to": {
event_id: "$discord:localhost",
},
},
},
sender: "@test:localhost",
type: "m.room.message",
} as IMatrixEvent, mockChannel as any);
let foundField = false;
// tslint:disable-next-line prefer-for-of
for (let i = 0; i < result!.fields!.length; i++) {
const f = result!.fields![i];
if (f.name === "ping") {
foundField = true;
expect(f.value).to.be.equal("<@1234>");
break;
}
}
expect(foundField).to.be.true;
});
}); });
}); });
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter