Files
gold_land_user/e2e/portfolio.spec.ts
T
safavimohadeseh78andCursor c53a67831d
Frontend CI / unit (test, type-check, build) (push) Successful in 7m21s
Frontend CI / browser E2E (frontend + pinned backend) (push) Successful in 11m38s
feat: complete the thirteen-stage learning experience and E2E journey
Play levels 7-13 in the app and pin E2E to the local thirteen-stage backend commit.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-22 19:42:04 +03:30

153 lines
7.3 KiB
TypeScript

import { test, expect } from '@playwright/test'
import { fetchMe, registerViaUi, uniqueUsername } from './helpers/auth'
const lessons: Record<number, Array<Record<string, unknown>>> = {
1: [
{ questionId: 'money-q1', answerId: 'option-a' },
{ questionId: 'money-q2', answerId: 'true' },
{ questionId: 'money-q3', answerId: 'option-a' },
{ questionId: 'money-q4', answerId: 'true' },
{ questionId: 'money-q5', answerId: 'option-b' },
],
2: [
{ questionId: 'saving-q1', answerId: 'option-b' },
{ questionId: 'saving-q2', answerId: 'true' },
{ questionId: 'saving-q3', answerId: 'option-a' },
{ questionId: 'saving-q4', answerId: 'true' },
{ questionId: 'saving-q5', answerId: 'option-a' },
],
3: [
{ questionId: 'needs-q1', answerId: 'option-a' },
{ questionId: 'needs-q2', answerId: 'true' },
{ questionId: 'needs-q3', orderedItemIds: ['water', 'food', 'medicine', 'shoes', 'toy', 'crown'] },
{ questionId: 'needs-q4', answerId: 'option-a' },
{ questionId: 'needs-q5', answerId: 'true' },
],
4: [
{ questionId: 'gold-q1', answerId: 'option-a' },
{ questionId: 'gold-q2', answerId: 'true' },
{ questionId: 'gold-q3', answerId: 'option-a' },
{ questionId: 'gold-q4', answerId: 'false' },
{ questionId: 'gold-q5', answerId: 'option-b' },
],
5: [
{ questionId: 'market-q1', answerId: 'option-a' },
{ questionId: 'market-q2', answerId: 'false' },
{ questionId: 'market-q3', answerId: 'option-a' },
{ questionId: 'market-q4', answerId: 'true' },
{ questionId: 'market-q5', answerId: 'option-b' },
],
6: [
{ questionId: 'inflation-q1', answerId: 'option-b' },
{ questionId: 'inflation-q2', answerId: 'true' },
{ questionId: 'inflation-q3', answerId: 'option-a' },
{ questionId: 'inflation-q4', answerId: 'false' },
{ questionId: 'inflation-q5', answerId: 'option-b' },
],
}
async function answerPortfolio(page: import('@playwright/test').Page) {
const choices = [
'نتیجه بیشتر به همان گزینه وابسته می‌شود',
'خیر؛ افت همگانی می‌تواند چند گزینه را هم‌زمان کم کند',
'خیر؛ رفتار و وابستگی آن‌ها مهم است',
'خیر؛ یک سناریو آینده را تضمین نمی‌کند',
'توجه به هدف، زمان نیاز به پول و ریسک؛ در مسائل واقعی کمک از بزرگسال قابل اعتماد',
]
for (let index = 0; index < choices.length; index += 1) {
await page.getByText(choices[index]!, { exact: true }).click()
if (index < choices.length - 1) await page.getByRole('button', { name: 'بعدی' }).click()
else await page.getByRole('button', { name: 'مرور پاسخ‌ها' }).click()
}
}
test('portfolio game from the map', async ({ page }) => {
const user = uniqueUsername('port')
await registerViaUi(page, user, 'pass1234', 'سبد')
const token = await page.evaluate(() => localStorage.getItem('gold-land-auth-token'))
const headers = { authorization: `Bearer ${token}` }
for (let levelId = 1; levelId <= 6; levelId += 1) {
expect((await page.request.post(`/api/gold-land/levels/${levelId}/start`, { headers })).ok()).toBeTruthy()
expect((await page.request.post(`/api/gold-land/levels/${levelId}/complete`, {
headers,
data: { answers: lessons[levelId] },
})).ok()).toBeTruthy()
}
expect((await page.request.post('/api/gold-land/levels/7/start', { headers })).ok()).toBeTruthy()
expect((await page.request.post('/api/gold-land/levels/7/complete', {
headers,
data: {
answers: [
{ questionId: 'initial', allocations: { transport: 20, food: 25, savings: 20, fun: 0 } },
{ questionId: 'final', allocations: { transport: 30, food: 25, savings: 20, fun: 0 } },
],
},
})).ok()).toBeTruthy()
expect((await page.request.post('/api/gold-land/levels/8/start', { headers })).ok()).toBeTruthy()
expect((await page.request.post('/api/gold-land/levels/8/complete', {
headers,
data: {
answers: [
{ questionId: 'q-loss', answerId: 'b' },
{ questionId: 'q-certain', answerId: 'no' },
{ questionId: 'q-need', answerId: 'keep' },
{ questionId: 'q-compare', answerId: 'range' },
{ questionId: 'q-promise', answerId: 'check' },
],
},
})).ok()).toBeTruthy()
await page.goto('/map')
await page.getByRole('button', { name: 'مرحله 9: سبد سرمایه‌گذاری' }).click()
await page.getByRole('button', { name: 'شروع بازی' }).click()
await page.getByRole('button', { name: 'ساخت سبد' }).click()
await expect(page.getByRole('button', { name: 'دیدن سه سناریو' })).toBeDisabled()
const sunUp = page.getByRole('button', { name: 'افزایش آفتاب' })
const rainUp = page.getByRole('button', { name: 'افزایش باران' })
const windUp = page.getByRole('button', { name: 'افزایش باد' })
for (let index = 0; index < 4; index += 1) await sunUp.click()
for (let index = 0; index < 3; index += 1) await rainUp.click()
for (let index = 0; index < 3; index += 1) await windUp.click()
await page.getByRole('button', { name: 'دیدن سه سناریو' }).click()
await expect(page.getByText('سود 5 سکه')).toBeVisible()
await expect(page.getByText('زیان 5 سکه')).toBeVisible()
await expect(page.getByText('زیان 10 سکه').first()).toBeVisible()
await page.getByRole('button', { name: 'تغییر ترکیب' }).click()
await page.getByRole('button', { name: 'کاهش آفتاب' }).click()
await page.getByRole('button', { name: 'افزایش باد' }).click()
await page.getByRole('button', { name: 'دیدن سه سناریو' }).click()
await page.getByRole('button', { name: 'رفتن به سؤال‌ها' }).click()
await answerPortfolio(page)
const before = await fetchMe(page)
await page.getByRole('button', { name: 'ثبت نتیجه' }).click()
await expect(page.getByText('پاداش:')).toBeVisible()
await expect(page.getByText('زیان را حذف یا سود را تضمین نمی‌کند')).toBeVisible()
await page.getByRole('button', { name: 'بازگشت به نقشه' }).click()
await page.reload()
const after = await fetchMe(page)
expect(after.progress.mapLevels.find((level) => level.id === 9)?.stars).toBe(3)
expect(after.user.coins).toBe(before.user.coins + 20 + 9)
await page.getByRole('button', { name: 'مرحله 9: سبد سرمایه‌گذاری' }).click()
await page.getByRole('button', { name: 'مرور مجدد' }).click()
await page.getByRole('button', { name: 'ساخت سبد' }).click()
for (let index = 0; index < 10; index += 1) await page.getByRole('button', { name: 'افزایش آفتاب' }).click()
await page.getByRole('button', { name: 'دیدن سه سناریو' }).click()
await page.getByRole('button', { name: 'رفتن به سؤال‌ها' }).click()
await answerPortfolio(page)
const replayBefore = await fetchMe(page)
await page.getByRole('button', { name: 'ثبت نتیجه' }).click()
await expect(page.getByText('پاداش تازه‌ای اضافه نشد')).toBeVisible()
const replayAfter = await fetchMe(page)
expect(replayAfter.user.coins).toBe(replayBefore.user.coins)
await page.goto('/map')
await expect(page.getByRole('button', { name: 'مرحله 11: خرید هوشمند' })).toBeDisabled()
})