|
| 1 | +import { extractIntroduction } from "../../../../../app/js/core/chatbot/parsers/extractIntroduction.mjs"; |
| 2 | + |
| 3 | +describe("extractIntroduction", () => { |
| 4 | + it("extracts the title, the initial message and the end of the introduction correctly", () => { |
| 5 | + const input = `# Title\nThis is an intro.\nMultiple lines\n\n are authorized !\n## Section`; |
| 6 | + const result = extractIntroduction(input); |
| 7 | + |
| 8 | + expect(result.chatbotTitle).toEqual(["Title"]); |
| 9 | + expect(result.chatbotInitialMessage.trim()).toEqual( |
| 10 | + "This is an intro.\nMultiple lines\n\n are authorized !", |
| 11 | + ); |
| 12 | + expect(result.indexEnd).toBe(input.trim().indexOf("## Section")); |
| 13 | + }); |
| 14 | + |
| 15 | + it("extracts the title, the initial message and the end of the introduction correctly, even if the beginning of the main content is a heading in Markdown which is not a level 2 heading", () => { |
| 16 | + const input = `# Title\nThis is an intro.\nMultiple lines\n\n are authorized !\n### Section`; |
| 17 | + const result = extractIntroduction(input); |
| 18 | + |
| 19 | + expect(result.chatbotTitle).toEqual(["Title"]); |
| 20 | + expect(result.chatbotInitialMessage.trim()).toEqual( |
| 21 | + "This is an intro.\nMultiple lines\n\n are authorized !", |
| 22 | + ); |
| 23 | + expect(result.indexEnd).toBe(input.trim().indexOf("### Section")); |
| 24 | + }); |
| 25 | + |
| 26 | + it("extracts the title, the initial message and the end of the introduction correctly, even if the beginning of the main content is the second level 1 heading", () => { |
| 27 | + const input = `# Title\nThis is an intro.\nMultiple lines\n\n are authorized !\n# Section`; |
| 28 | + const result = extractIntroduction(input); |
| 29 | + |
| 30 | + expect(result.chatbotTitle).toEqual(["Title"]); |
| 31 | + expect(result.chatbotInitialMessage.trim()).toEqual( |
| 32 | + "This is an intro.\nMultiple lines\n\n are authorized !", |
| 33 | + ); |
| 34 | + expect(result.indexEnd).toBe(input.trim().indexOf("# Section")); |
| 35 | + }); |
| 36 | + |
| 37 | + it("extracts the title, the message and the end of the introduction correctly, even if there are return or whitespaces characters before or after the title", () => { |
| 38 | + const input = ` \n # Title \n\nThis is an intro.\n## Section`; |
| 39 | + const result = extractIntroduction(input); |
| 40 | + |
| 41 | + expect(result.chatbotTitle).toEqual(["Title"]); |
| 42 | + expect(result.chatbotInitialMessage.trim()).toEqual("This is an intro."); |
| 43 | + expect(result.indexEnd).toBe(input.trim().indexOf("## Section")); |
| 44 | + }); |
| 45 | + |
| 46 | + it("defaults to 'Chatbot' if no title is found", () => { |
| 47 | + const input = `Intro text\n## Next section`; |
| 48 | + const result = extractIntroduction(input); |
| 49 | + |
| 50 | + expect(result.chatbotTitle).toEqual(["Chatbot"]); |
| 51 | + expect(result.chatbotInitialMessage.trim()).toEqual("Intro text"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("considers the whole text as the introduction if there is no other markdown titles in the content", () => { |
| 55 | + const input = ` # Title \n\nThis is an intro.\nAnd there is no other content.`; |
| 56 | + const result = extractIntroduction(input); |
| 57 | + |
| 58 | + expect(result.chatbotTitle).toEqual(["Title"]); |
| 59 | + expect(result.chatbotInitialMessage.trim()).toEqual( |
| 60 | + "This is an intro.\nAnd there is no other content.", |
| 61 | + ); |
| 62 | + expect(result.indexEnd).toBe(input.trim().length); |
| 63 | + }); |
| 64 | +}); |
0 commit comments