|
| 1 | +/* |
| 2 | + * Copyright (c) Jupyter Development Team. |
| 3 | + * Distributed under the terms of the Modified BSD License. |
| 4 | + */ |
| 5 | + |
| 6 | +import { |
| 7 | + IJupyterLabPageFixture, |
| 8 | + expect, |
| 9 | + galata, |
| 10 | + test |
| 11 | +} from '@jupyterlab/galata'; |
| 12 | + |
| 13 | +import { User } from '@jupyterlab/services'; |
| 14 | +import { UUID } from '@lumino/coreutils'; |
| 15 | + |
| 16 | +import { openChat, openSettings, sendMessage, USER } from './test-utils'; |
| 17 | + |
| 18 | +const FILENAME = 'my-chat.chat'; |
| 19 | +const MSG_CONTENT = 'Hello World!'; |
| 20 | +const USERNAME = USER.identity.username; |
| 21 | + |
| 22 | +test.describe('#notifications', () => { |
| 23 | + const baseTime = 1714116341; |
| 24 | + const messagesCount = 15; |
| 25 | + const messagesList: any[] = []; |
| 26 | + for (let i = 0; i < messagesCount; i++) { |
| 27 | + messagesList.push({ |
| 28 | + type: 'msg', |
| 29 | + id: UUID.uuid4(), |
| 30 | + sender: USERNAME, |
| 31 | + body: `Message ${i}`, |
| 32 | + time: baseTime + i * 60 |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + const chatContent = { |
| 37 | + messages: messagesList, |
| 38 | + users: {} |
| 39 | + }; |
| 40 | + chatContent.users[USERNAME] = USER.identity; |
| 41 | + |
| 42 | + let guestPage: IJupyterLabPageFixture; |
| 43 | + |
| 44 | + test.beforeEach( |
| 45 | + async ({ baseURL, browser, page, tmpPath, waitForApplication }) => { |
| 46 | + // Create a chat file with content |
| 47 | + await page.filebrowser.contents.uploadContent( |
| 48 | + JSON.stringify(chatContent), |
| 49 | + 'text', |
| 50 | + FILENAME |
| 51 | + ); |
| 52 | + |
| 53 | + // Create a new user. |
| 54 | + const user2: Partial<User.IUser> = { |
| 55 | + identity: { |
| 56 | + username: 'jovyan_2', |
| 57 | + name: 'jovyan_2', |
| 58 | + display_name: 'jovyan_2', |
| 59 | + initials: 'JP', |
| 60 | + color: 'var(--jp-collaborator-color2)' |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + // Create a new page for guest. |
| 65 | + const { page: newPage } = await galata.newPage({ |
| 66 | + baseURL: baseURL!, |
| 67 | + browser, |
| 68 | + mockUser: user2, |
| 69 | + tmpPath, |
| 70 | + waitForApplication |
| 71 | + }); |
| 72 | + await newPage.evaluate(() => { |
| 73 | + // Acknowledge any dialog |
| 74 | + window.galataip.on('dialog', d => { |
| 75 | + d?.resolve(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + guestPage = newPage; |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + test.afterEach(async ({ page }) => { |
| 83 | + guestPage.close(); |
| 84 | + if (await page.filebrowser.contents.fileExists(FILENAME)) { |
| 85 | + await page.filebrowser.contents.deleteFile(FILENAME); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + test('should receive notification on unread message', async ({ page }) => { |
| 90 | + const chatPanel = await openChat(page, FILENAME); |
| 91 | + const messages = chatPanel.locator('.jp-chat-message'); |
| 92 | + await messages.first().scrollIntoViewIfNeeded(); |
| 93 | + |
| 94 | + await sendMessage(guestPage, FILENAME, MSG_CONTENT); |
| 95 | + await page.waitForCondition( |
| 96 | + async () => (await page.notifications).length > 0 |
| 97 | + ); |
| 98 | + const notifications = await page.notifications; |
| 99 | + expect(notifications).toHaveLength(1); |
| 100 | + |
| 101 | + // TODO: fix it, the notification should be info but is 'default' |
| 102 | + // expect(notifications[0].type).toBe('info'); |
| 103 | + expect(notifications[0].message).toBe( |
| 104 | + '1 incoming message(s) in my-chat.chat' |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + test('should remove notification when the message is read', async ({ |
| 109 | + page |
| 110 | + }) => { |
| 111 | + const chatPanel = await openChat(page, FILENAME); |
| 112 | + const messages = chatPanel.locator('.jp-chat-message'); |
| 113 | + await messages.first().scrollIntoViewIfNeeded(); |
| 114 | + |
| 115 | + await sendMessage(guestPage, FILENAME, MSG_CONTENT); |
| 116 | + await page.waitForCondition( |
| 117 | + async () => (await page.notifications).length > 0 |
| 118 | + ); |
| 119 | + let notifications = await page.notifications; |
| 120 | + expect(notifications).toHaveLength(1); |
| 121 | + |
| 122 | + await messages.last().scrollIntoViewIfNeeded(); |
| 123 | + await page.waitForCondition( |
| 124 | + async () => (await page.notifications).length === 0 |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + test('should update existing notification on new message', async ({ |
| 129 | + page |
| 130 | + }) => { |
| 131 | + const chatPanel = await openChat(page, FILENAME); |
| 132 | + const messages = chatPanel.locator('.jp-chat-message'); |
| 133 | + await messages.first().scrollIntoViewIfNeeded(); |
| 134 | + |
| 135 | + await sendMessage(guestPage, FILENAME, MSG_CONTENT); |
| 136 | + await page.waitForCondition( |
| 137 | + async () => (await page.notifications).length > 0 |
| 138 | + ); |
| 139 | + let notifications = await page.notifications; |
| 140 | + expect(notifications).toHaveLength(1); |
| 141 | + |
| 142 | + expect(notifications[0].message).toBe( |
| 143 | + '1 incoming message(s) in my-chat.chat' |
| 144 | + ); |
| 145 | + |
| 146 | + await sendMessage(guestPage, FILENAME, MSG_CONTENT); |
| 147 | + notifications = await page.notifications; |
| 148 | + expect(notifications[0].message).toBe( |
| 149 | + '2 incoming message(s) in my-chat.chat' |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + test('should remove notifications from settings', async ({ page }) => { |
| 154 | + const chatPanel = await openChat(page, FILENAME); |
| 155 | + const messages = chatPanel.locator('.jp-chat-message'); |
| 156 | + await messages.first().scrollIntoViewIfNeeded(); |
| 157 | + |
| 158 | + await sendMessage(guestPage, FILENAME, MSG_CONTENT); |
| 159 | + await page.waitForCondition( |
| 160 | + async () => (await page.notifications).length > 0 |
| 161 | + ); |
| 162 | + let notifications = await page.notifications; |
| 163 | + expect(notifications).toHaveLength(1); |
| 164 | + |
| 165 | + const settings = await openSettings(page); |
| 166 | + const unreadNotifications = settings?.getByRole('checkbox', { |
| 167 | + name: 'unreadNotifications' |
| 168 | + }); |
| 169 | + await unreadNotifications?.uncheck(); |
| 170 | + |
| 171 | + // wait for the settings to be saved |
| 172 | + await expect(page.activity.getTabLocator('Settings')).toHaveAttribute( |
| 173 | + 'class', |
| 174 | + /jp-mod-dirty/ |
| 175 | + ); |
| 176 | + await expect(page.activity.getTabLocator('Settings')).not.toHaveAttribute( |
| 177 | + 'class', |
| 178 | + /jp-mod-dirty/ |
| 179 | + ); |
| 180 | + |
| 181 | + // Activate the chat panel |
| 182 | + await page.activity.activateTab(FILENAME); |
| 183 | + |
| 184 | + await page.waitForCondition( |
| 185 | + async () => (await page.notifications).length === 0 |
| 186 | + ); |
| 187 | + |
| 188 | + await sendMessage(guestPage, FILENAME, MSG_CONTENT); |
| 189 | + await expect(messages).toHaveCount(messagesCount + 2); |
| 190 | + |
| 191 | + notifications = await page.notifications; |
| 192 | + expect(notifications).toHaveLength(0); |
| 193 | + }); |
| 194 | + |
| 195 | + test('should add unread symbol in tab label', async ({ page }) => { |
| 196 | + const chatPanel = await openChat(page, FILENAME); |
| 197 | + const messages = chatPanel.locator('.jp-chat-message'); |
| 198 | + await messages.first().scrollIntoViewIfNeeded(); |
| 199 | + |
| 200 | + const tab = page.activity.getTabLocator(FILENAME); |
| 201 | + const tabLabel = tab.locator('.lm-TabBar-tabLabel'); |
| 202 | + await expect(tabLabel).toHaveText(FILENAME); |
| 203 | + |
| 204 | + await sendMessage(guestPage, FILENAME, MSG_CONTENT); |
| 205 | + const beforePseudo = tabLabel.evaluate(elem => { |
| 206 | + return window.getComputedStyle(elem, ':before'); |
| 207 | + }); |
| 208 | + expect(await beforePseudo).toHaveProperty('content', '"* "'); |
| 209 | + expect(await tab.screenshot()).toMatchSnapshot('tab-with-unread.png'); |
| 210 | + |
| 211 | + await messages.last().scrollIntoViewIfNeeded(); |
| 212 | + expect(await tab.screenshot()).toMatchSnapshot('tab-without-unread.png'); |
| 213 | + }); |
| 214 | +}); |
0 commit comments