Skip to content

Commit be53ccb

Browse files
committed
Add student test suite
1 parent 9d59ddd commit be53ccb

File tree

14 files changed

+115
-32
lines changed

14 files changed

+115
-32
lines changed

e2e/chat.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test.describe('Chat v2 Conversation tests', () => {
1212
await page.getByTestId('model-selector').first().click()
1313
await page.getByRole('option', { name: 'mock' }).click()
1414

15-
const chatInput = page.locator('#chat-input').first()
15+
const chatInput = page.getByTestId('chat-input').first()
1616
await chatInput.fill('testinen morjens')
1717
await chatInput.press('Shift+Enter')
1818

@@ -27,7 +27,7 @@ test.describe('Chat v2 Conversation tests', () => {
2727
await page.getByTestId('model-selector').first().click()
2828
await page.getByRole('option', { name: 'mock' }).click()
2929

30-
const chatInput = page.locator('#chat-input').first()
30+
const chatInput = page.getByTestId('chat-input').first()
3131
await chatInput.fill('tää tyhjennetään')
3232
await chatInput.press('Shift+Enter')
3333

e2e/chatFeatures.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ test.describe('Chat v2 UI Features Tests', () => {
88
})
99

1010
test('Disclaimer is visible', async ({ page }) => {
11-
await expect(page.locator('#submit-accept-disclaimer')).toBeVisible()
11+
await expect(page.getByTestId('submit-accept-disclaimer')).toBeVisible()
1212
})
1313

1414
test('Disclaimer is not visible after accepting and reloading', async ({ page }) => {
1515
await acceptDisclaimer(page)
1616
await page.reload()
17-
await expect(page.locator('#submit-accept-disclaimer')).not.toBeVisible()
17+
await expect(page.getByTestId('submit-accept-disclaimer')).not.toBeVisible()
1818
})
1919

2020
test('Disclaimer (help) can be opened manually', async ({ page }) => {
2121
await acceptDisclaimer(page)
2222
await page.getByTestId('help-button').click()
23-
await expect(page.locator('#submit-accept-disclaimer')).toBeVisible()
23+
await expect(page.getByTestId('submit-accept-disclaimer')).toBeVisible()
2424
})
2525

2626
test('Settings can be opened and closed', async ({ page }) => {

e2e/courseChatRag.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test.describe('Course Chat v2', () => {
1212
await page.getByTestId('model-selector').first().click()
1313
await page.getByRole('option', { name: 'mock' }).click()
1414

15-
const chatInput = page.locator('#chat-input').first()
15+
const chatInput = page.getByTestId('chat-input').first()
1616
await chatInput.fill('testinen morjens')
1717
await chatInput.press('Shift+Enter')
1818

@@ -25,7 +25,7 @@ test.describe('Course Chat v2', () => {
2525
await page.locator('#rag-index-selector').first().click()
2626
await page.getByRole('menuitem', { name: ragName }).click()
2727
28-
const chatInput = page.locator('#chat-input').first()
28+
const chatInput = page.getByTestId('chat-input').first()
2929
await chatInput.fill('rag')
3030
await chatInput.press('Shift+Enter')
3131

e2e/fixtures.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,46 @@ export const test = base.extend<{ forEachTest: void }>({
1212
* Parallel worker isolation: each worker has its own test user identified by the worker index.
1313
*/
1414

15+
const testUserIdx = studentTest.info().workerIndex
16+
const testUserRole = 'admin'
17+
18+
page.context().setExtraHTTPHeaders({
19+
'x-test-user-index': String(testUserIdx),
20+
'x-test-user-role': testUserRole,
21+
})
22+
23+
await request.post('/api/test/reset-test-data', { data: { testUserIdx, testUserRole } })
24+
25+
// Run the test
26+
await use()
27+
28+
// This code runs after every test.
29+
// console.log('Last URL:', page.url());
30+
},
31+
{ auto: true },
32+
], // automatically starts for every test.
33+
})
34+
35+
export const studentTest = base.extend<{ forEachTest: void }>({
36+
forEachTest: [
37+
async ({ page, request }, use) => {
38+
// This code runs before every test.
39+
40+
// setupLogging(page)
41+
42+
/*
43+
* Parallel worker isolation: each worker has its own test user identified by the worker index.
44+
*/
45+
46+
const testUserIdx = studentTest.info().workerIndex
47+
const testUserRole = 'student'
48+
1549
page.context().setExtraHTTPHeaders({
16-
'x-test-user-index': String(test.info().workerIndex),
50+
'x-test-user-index': String(testUserIdx),
51+
'x-test-user-role': testUserRole,
1752
})
1853

19-
await request.post('/api/test/reset-test-data', { data: { testUserIdx: test.info().workerIndex } })
54+
await request.post('/api/test/reset-test-data', { data: { testUserIdx, testUserRole } })
2055

2156
// Run the test
2257
await use()

e2e/prompts.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test.describe('Prompts', () => {
2222
await page.keyboard.press('Escape')
2323

2424
// Send something
25-
let chatInput = page.locator('#chat-input').first()
25+
let chatInput = page.getByTestId('chat-input').first()
2626
await chatInput.fill('testinen morjens')
2727
await chatInput.press('Shift+Enter')
2828

@@ -39,7 +39,7 @@ test.describe('Prompts', () => {
3939
// Reload page to ensure prompt is saved
4040
await page.reload()
4141

42-
chatInput = page.locator('#chat-input').first()
42+
chatInput = page.getByTestId('chat-input').first()
4343
await chatInput.fill('testinen morjens')
4444
await chatInput.press('Shift+Enter')
4545

@@ -81,7 +81,7 @@ test.describe('Prompts', () => {
8181
await page.keyboard.press('Escape')
8282

8383
// Send something
84-
const chatInput = page.locator('#chat-input').first()
84+
const chatInput = page.getByTestId('chat-input').first()
8585
await chatInput.fill('testinen morjens')
8686
await chatInput.press('Shift+Enter')
8787

@@ -147,7 +147,7 @@ test.describe('Prompts', () => {
147147
await page.keyboard.press('Escape')
148148

149149
// Send message, response should echo the prompt
150-
const chatInput = page.locator('#chat-input').first()
150+
const chatInput = page.getByTestId('chat-input').first()
151151
await chatInput.fill('testinen morjens')
152152
await chatInput.press('Shift+Enter')
153153

e2e/student.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { expect } from '@playwright/test'
2+
import { acceptDisclaimer } from './utils/test-helpers'
3+
import { studentTest as test } from './fixtures'
4+
5+
test.describe('Student', () => {
6+
test('Enrolled chat works', async ({ page }) => {
7+
await page.goto('/v2/test-course')
8+
await acceptDisclaimer(page)
9+
10+
await page.getByTestId('model-selector').first().click()
11+
await page.getByRole('option', { name: 'mock' }).click()
12+
13+
const chatInput = page.getByTestId('chat-input').first()
14+
await chatInput.fill('testinen morjens')
15+
await chatInput.press('Shift+Enter')
16+
17+
await expect(page.getByTestId('user-message')).toContainText('testinen morjens')
18+
await expect(page.getByTestId('assistant-message')).toContainText('You are calling mock endpoint for streaming mock data')
19+
})
20+
21+
test('is sent to chats page from general chat', async ({ page }) => {
22+
// Tries to access general chat
23+
await page.goto('/v2')
24+
// Student is sent to chats page
25+
await expect(page).not.toHaveURL(/v2\/sandbox/)
26+
await expect(page).toHaveURL(/chats/)
27+
})
28+
29+
test('is sent to chats page from non-enrolled course', async ({ page }) => {
30+
// Tries to access sandbox course
31+
await page.goto('/v2/sandbox')
32+
// Student is sent to chats page
33+
await expect(page).not.toHaveURL(/v2\/sandbox/)
34+
await expect(page).toHaveURL(/chats/)
35+
})
36+
})

e2e/utils/test-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Page } from '@playwright/test'
22

33
export const acceptDisclaimer = async (page: Page) => {
4-
await page.locator('#accept-disclaimer').click()
5-
await page.locator('#submit-accept-disclaimer').click()
4+
await page.getByTestId('accept-disclaimer').click()
5+
await page.getByTestId('submit-accept-disclaimer').click()
66
}

src/client/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { useUpdateUrlLang } from './hooks/useUpdateUrlLang'
2020
import Styles from './GlobalStyles'
2121

2222
const hasAccess = (user: User | null | undefined, courseId?: string) => {
23+
console.log(user, courseId)
2324
if (!user) return false
2425
if (user.isAdmin) return true
2526
if (courseId && !user.activeCourseIds.includes(courseId)) return false
@@ -112,7 +113,7 @@ const Layout = () => {
112113
ref={appRef}
113114
>
114115
{!isEmbedded && <NavBar />}
115-
<Box sx={{ flex: 1 }}>
116+
<Box sx={{ flex: 1, mt: '4rem' }}>
116117
<Content />
117118
</Box>
118119
<Feedback />

src/client/components/ChatV2/ChatBox.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,11 @@ export const ChatBox = ({
210210
sx={{ padding: '0.5rem' }}
211211
variant="standard"
212212
slotProps={{
213+
htmlInput: {
214+
'data-testid': 'chat-input',
215+
},
213216
input: {
214217
disableUnderline: true,
215-
id: 'chat-input',
216218
},
217219
}}
218220
/>

src/client/components/ChatV2/Disclaimer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ export const DisclaimerModal = ({
9292
<FormControlLabel
9393
disabled={termsAccepted}
9494
sx={{ display: termsAccepted ? 'none' : '' }}
95-
control={<Checkbox required checked={hasRead} onChange={handleToggle} id="accept-disclaimer" />}
95+
control={<Checkbox required checked={hasRead} onChange={handleToggle} data-testid="accept-disclaimer" />}
9696
label={t('info:acceptDisclaimer')}
9797
/>
9898
<Box sx={{ display: 'flex', justifyContent: 'flex-end' }}>
99-
<BlueButton disabled={!(hasRead || termsAccepted) || acceptTermsMutation.isPending} type="submit" id="submit-accept-disclaimer">
99+
<BlueButton disabled={!(hasRead || termsAccepted) || acceptTermsMutation.isPending} type="submit" data-testid="submit-accept-disclaimer">
100100
OK
101101
</BlueButton>
102102
</Box>

0 commit comments

Comments
 (0)