browser_aiwindow_options.js (6787B)
1 const lazy = {}; 2 3 ChromeUtils.defineESModuleGetters(lazy, { 4 sinon: "resource://testing-common/Sinon.sys.mjs", 5 }); 6 7 const { AIWindowTestUtils } = ChromeUtils.importESModule( 8 "resource://testing-common/AIWindowTestUtils.sys.mjs" 9 ); 10 11 function makeMenuItem(id) { 12 const menuItem = document.createXULElement("menuitem"); 13 menuItem.setAttribute("id", id); 14 15 const node = document.childNodes[0]; 16 document.documentElement.appendChild(menuItem, node); 17 18 return menuItem; 19 } 20 21 let gSandbox = null, 22 gAiWindow = null; 23 24 async function test_AiWindowMenu_setup( 25 returnRecentConversations = true, 26 openAiWindow = true 27 ) { 28 gSandbox = lazy.sinon.createSandbox(); 29 30 const testMenuItem = makeMenuItem("historyMenuItem"); 31 const testEvent = new Event("popupShowing"); 32 testMenuItem.dispatchEvent(testEvent); 33 34 // add mock menuitem that the Chats option is added before 35 const sanitizeItem = makeMenuItem("sanitizeItem"); 36 testMenuItem.appendChild(sanitizeItem); 37 38 const startChatHistorySeparator = makeMenuItem("startChatHistorySeparator"); 39 testMenuItem.appendChild(startChatHistorySeparator); 40 41 const chatsHistoryMenu = makeMenuItem("chatsHistoryMenu"); 42 chatsHistoryMenu.hidden = true; 43 testMenuItem.appendChild(chatsHistoryMenu); 44 45 const recentChatsHeader = makeMenuItem("recentChatsHeader"); 46 testMenuItem.appendChild(recentChatsHeader); 47 48 gSandbox 49 .stub(AIWindow.chatStore, "findRecentConversations") 50 .callsFake(amount => { 51 const conversations = []; 52 53 if (!returnRecentConversations) { 54 return conversations; 55 } 56 57 for (let i = 0; i < amount; i++) { 58 conversations.push({ title: `Conversation ${i}`, id: i.toString() }); 59 } 60 61 return conversations; 62 }); 63 64 gAiWindow = await AIWindowTestUtils.openAIWindow(openAiWindow); 65 if (openAiWindow) { 66 gAiWindow.document.documentElement.setAttribute("ai-window", true); 67 } 68 69 return [testEvent, testMenuItem]; 70 } 71 72 async function test_AiWindowMenu_cleanup() { 73 await BrowserTestUtils.closeWindow(gAiWindow); 74 gSandbox.restore(); 75 } 76 77 describe("AiWindow Menu options", () => { 78 let testEvent, testMenuItem; 79 describe("AIWindow enabled and active and there are chats", () => { 80 beforeEach(async () => { 81 await AIWindowTestUtils.toggleAIWindowPref(SpecialPowers, true); 82 const testObjects = await test_AiWindowMenu_setup(true); 83 84 testEvent = testObjects[0]; 85 testMenuItem = testObjects[1]; 86 }); 87 88 afterEach(async () => { 89 await test_AiWindowMenu_cleanup(); 90 }); 91 92 it("adds the Chats option to History menu", () => { 93 AIWindow.appMenu(testEvent, gAiWindow); 94 95 const chatsOption = testMenuItem.querySelector("#chatsHistoryMenu"); 96 97 Assert.ok(chatsOption, "Could not find the Chats option"); 98 Assert.ok(!chatsOption.hidden, "The Chats option should not be hidden"); 99 }); 100 101 it("enables the Recent Chats label", async () => { 102 AIWindow.appMenu(testEvent, gAiWindow); 103 104 await waitForElement(gAiWindow, "#recentChatsHeader"); 105 106 const recentChatsHeader = 107 testMenuItem.querySelector("#recentChatsHeader"); 108 109 Assert.ok(recentChatsHeader); 110 Assert.equal(recentChatsHeader.hasAttribute("hidden"), false); 111 }); 112 113 it("adds recent chats in the history menu", async () => { 114 await AIWindow.appMenu(testEvent, gAiWindow); 115 116 await waitForElement(gAiWindow, "#recentChatsHeader"); 117 118 const conversationItems = 119 gAiWindow.document.documentElement.querySelectorAll( 120 ".recent-chat-item" 121 ); 122 Assert.equal(conversationItems.length, 4); 123 }); 124 }); 125 126 describe("AIWindow enabled and active and no chats", () => { 127 beforeEach(async () => { 128 await AIWindowTestUtils.toggleAIWindowPref(SpecialPowers, true); 129 const testObjects = await test_AiWindowMenu_setup(false); 130 131 testEvent = testObjects[0]; 132 testMenuItem = testObjects[1]; 133 }); 134 135 afterEach(async () => { 136 await test_AiWindowMenu_cleanup(); 137 }); 138 139 it("doesnt add chats options when there arent any", async () => { 140 await AIWindow.appMenu(testEvent, gAiWindow); 141 142 const conversationItems = 143 gAiWindow.document.documentElement.querySelectorAll( 144 ".recent-chat-item" 145 ); 146 Assert.equal( 147 conversationItems.length, 148 0, 149 "Found recent chat items but should be none" 150 ); 151 152 const recentChatsHeader = 153 gAiWindow.document.documentElement.querySelector("#recentChatsHeader"); 154 Assert.ok( 155 recentChatsHeader.hasAttribute("hidden"), 156 "Found chats header but should be hidden" 157 ); 158 }); 159 }); 160 161 describe("AIWindow enabled but not active", () => { 162 beforeEach(async () => { 163 await AIWindowTestUtils.toggleAIWindowPref(SpecialPowers, true); 164 const testObjects = await test_AiWindowMenu_setup(false, false); 165 166 testEvent = testObjects[0]; 167 testMenuItem = testObjects[1]; 168 }); 169 170 afterEach(async () => { 171 await test_AiWindowMenu_cleanup(); 172 }); 173 174 it("does not add chats options", async () => { 175 await AIWindow.appMenu(testEvent, gAiWindow); 176 177 const recentChatsHeader = 178 gAiWindow.document.documentElement.querySelector("#recentChatsHeader"); 179 Assert.ok( 180 recentChatsHeader.hasAttribute("hidden"), 181 "Found chats header visible but should be hidden" 182 ); 183 184 const chatsOption = 185 gAiWindow.document.documentElement.querySelector("#menu_chats"); 186 187 Assert.ok(!chatsOption, "Found the Chats option"); 188 }); 189 }); 190 191 describe("AIWindow enabled but not active", () => { 192 beforeEach(async () => { 193 await AIWindowTestUtils.toggleAIWindowPref(SpecialPowers, false); 194 const testObjects = await test_AiWindowMenu_setup(false, false); 195 196 testEvent = testObjects[0]; 197 testMenuItem = testObjects[1]; 198 }); 199 200 afterEach(async () => { 201 await test_AiWindowMenu_cleanup(); 202 }); 203 204 it("does not add chats options", async () => { 205 await AIWindow.appMenu(testEvent, gAiWindow); 206 207 const recentChatsHeader = 208 gAiWindow.document.documentElement.querySelector("#recentChatsHeader"); 209 Assert.ok( 210 recentChatsHeader.hasAttribute("hidden"), 211 "Found chats header visible but should be hidden" 212 ); 213 214 const chatsOption = 215 gAiWindow.document.documentElement.querySelector("#menu_chats"); 216 217 Assert.ok(!chatsOption, "Found the Chats option"); 218 }); 219 }); 220 221 // @todo Bug 2007583 222 // Add test for clicking Recent Chat item 223 }); 224 225 async function waitForElement(win, elementSelector) { 226 await BrowserTestUtils.waitForMutationCondition( 227 win, 228 { childList: true }, 229 () => { 230 const elements = 231 win.document.documentElement.querySelectorAll(elementSelector); 232 return elements.length; 233 } 234 ); 235 }