diff --git a/src/util.ts b/src/util.ts index 548b61231bde464ba906cb29b46b6ace6a65c313..82fceb6992e8c4825d08ae5279ed11253f4341ae 100644 --- a/src/util.ts +++ b/src/util.ts @@ -240,7 +240,7 @@ export class Util { public static GetReplyFromReplyBody(body: string) { const lines = body.split("\n"); - while(lines[0].startsWith("> ") || lines[0].trim().length > 0) { + while(lines[0].startsWith("> ") || lines[0].trim().length === 0) { lines.splice(0,1); if (lines.length === 0) { return ""; diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index 63e62d59ad7b7dc1f465b6c301d0b2b3f72e84d2..6505924ab5363a1ecbe2b5dc0a83a421b4bff7c2 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -102,14 +102,17 @@ This is the first reply`, config.bridge.disableDiscordMentions = disableMentions; config.bridge.disableEveryoneMention = disableEveryone; config.bridge.disableHereMention = disableHere; + + const Util = Object.assign(require("../src/util").Util, { + DownloadFile: (name: string) => { + const size = parseInt(name.substring(name.lastIndexOf("/") + 1), undefined); + return Buffer.alloc(size); + }, + }) + return new (Proxyquire("../src/matrixeventprocessor", { "./util": { - Util: { - DownloadFile: (name: string) => { - const size = parseInt(name.substring(name.lastIndexOf("/") + 1), undefined); - return Buffer.alloc(size); - }, - }, + Util }, })).MatrixEventProcessor( new MatrixEventProcessorOpts( @@ -672,7 +675,7 @@ describe("MatrixEventProcessor", () => { }); expect(result).to.be.undefined; }); - it("should handle replies with a wrongly formatted body", async () => { + it("should handle replies without a fallback", async () => { const processor = createMatrixEventProcessor(); const result = await processor.GetEmbedForReply({ sender: "@test:localhost", @@ -681,12 +684,16 @@ describe("MatrixEventProcessor", () => { "body": "Test", "m.relates_to": { "m.in_reply_to": { - event_id: "!event:thing", + event_id: "$goodEvent:localhost", }, }, }, }); - expect(result).to.be.undefined; + expect(result[0].description).to.be.equal("Hello!"); + expect(result[0].author.name).to.be.equal("Doggo!"); + expect(result[0].author.icon_url).to.be.equal("https://fakeurl.com"); + expect(result[0].author.url).to.be.equal("https://matrix.to/#/@doggo:localhost"); + expect(result[1]).to.be.equal("Test"); }); it("should handle replies with a missing event", async () => { const processor = createMatrixEventProcessor(); @@ -699,15 +706,15 @@ describe("MatrixEventProcessor", () => { This is where the reply goes`, "m.relates_to": { "m.in_reply_to": { - event_id: "!event:thing", + event_id: "$event:thing", }, }, }, }); - expect(result[0].description).to.be.equal("This is the fake body"); - expect(result[0].author.name).to.be.equal("Doggo!"); - expect(result[0].author.icon_url).to.be.equal("https://fakeurl.com"); - expect(result[0].author.url).to.be.equal("https://matrix.to/#/@doggo:localhost"); + expect(result[0].description).to.be.equal("Reply with unknown content"); + expect(result[0].author.name).to.be.equal("Unknown"); + expect(result[0].author.icon_url).to.be.undefined; + expect(result[0].author.url).to.be.undefined; expect(result[1]).to.be.equal("This is where the reply goes"); }); it("should handle replies with a valid reply event", async () => { diff --git a/test/test_util.ts b/test/test_util.ts index 6dc150e572228d7e17d0aa674752cfd86168ea9f..dc48346bf48a8c85bbeaf673c49eb804f24590ec 100644 --- a/test/test_util.ts +++ b/test/test_util.ts @@ -135,5 +135,9 @@ there are even more lines here.`); `); return expect(reply).to.equal(""); }); + it("Should return body if no reply found", () => { + const reply = Util.GetReplyFromReplyBody("Test\nwith\nhalfy"); + return expect(reply).to.equal("Test\nwith\nhalfy"); + }); }); });