176 lines
8.7 KiB
JavaScript
176 lines
8.7 KiB
JavaScript
import { expect, test } from '@playwright/test'
|
|
|
|
const admin = {
|
|
id: 'admin',
|
|
username: 'admin',
|
|
name: 'Fixture Admin',
|
|
role: 'admin',
|
|
must_setup: false,
|
|
}
|
|
|
|
const persona = {
|
|
id: 'p1',
|
|
name: 'Acme decision maker',
|
|
profession: 'Operations lead',
|
|
age_group: '35-44',
|
|
channel: 'facebook',
|
|
initiation_mode: 'customer',
|
|
}
|
|
|
|
async function installFixtureApi(page, { chat = false } = {}) {
|
|
await page.addInitScript(() => localStorage.setItem('st_token', 'fixture-token'))
|
|
let finishCalls = 0
|
|
|
|
await page.route('**/api/**', async (route) => {
|
|
const request = route.request()
|
|
const url = new URL(request.url())
|
|
const method = request.method()
|
|
const path = url.pathname
|
|
if (!path.startsWith('/api/')) return route.continue()
|
|
|
|
if (method === 'GET' && path === '/api/auth/me') {
|
|
return route.fulfill({ json: { user: admin } })
|
|
}
|
|
if (method === 'GET' && path === '/api/groups') {
|
|
return route.fulfill({ json: { groups: [{ id: 'g1', title: 'CRM assistant', status: 'ready', persona_count: 15, input: { product: 'CRM assistant' } }] } })
|
|
}
|
|
if (method === 'GET' && path === '/api/groups/g1/report') {
|
|
return route.fulfill({ json: { title: 'CRM assistant', markdown: '# CRM assistant\n\n## Sales Kit\n\nPractice report' } })
|
|
}
|
|
if (method === 'GET' && path === '/api/analytics') {
|
|
return route.fulfill({
|
|
json: {
|
|
overall: { total_sessions: 1, wins: 1, losses: 0, avg_score: 82, close_rate: 100 },
|
|
trainee_count: 1,
|
|
hardest_personas: [{ persona_name: 'Acme decision maker', wins: 1, losses: 0, plays: 1, avg_score: 82 }],
|
|
},
|
|
})
|
|
}
|
|
if (method === 'GET' && path === '/api/analytics/export/token') {
|
|
return route.fulfill({ json: { url: '/api/analytics/export?token=fixture' } })
|
|
}
|
|
if (method === 'GET' && path === '/api/analytics/export') {
|
|
return route.fulfill({ contentType: 'text/csv', body: 'persona,outcome\nAcme decision maker,won\n' })
|
|
}
|
|
if (method === 'GET' && path === '/api/groups/g1/personas/p1') {
|
|
return route.fulfill({ json: { persona } })
|
|
}
|
|
if (chat && method === 'GET' && path === '/api/chat/g1/personas/p1/chat/resume') {
|
|
return route.fulfill({ status: 404, json: { error: 'no active session' } })
|
|
}
|
|
if (chat && method === 'GET' && path === '/api/chat/sessions') {
|
|
return route.fulfill({ json: { sessions: [] } })
|
|
}
|
|
if (chat && method === 'POST' && path === '/api/chat/g1/personas/p1/chat/start') {
|
|
return route.fulfill({ json: { session: { id: 's1', messages: [] } } })
|
|
}
|
|
if (chat && method === 'POST' && path === '/api/chat/g1/personas/p1/chat/send') {
|
|
return route.fulfill({ json: { messages: [{ role: 'customer', text: 'Tell me more.' }], finished: false } })
|
|
}
|
|
if (chat && method === 'POST' && path === '/api/chat/g1/personas/p1/chat/finish') {
|
|
finishCalls += 1
|
|
return route.fulfill({
|
|
json: {
|
|
session: { id: 's1', messages: [{ role: 'customer', text: 'Tell me more.' }], status: 'finished' },
|
|
debrief: { outcome: 'won', score: 82, pain: 'manual work', why: 'clear value', coaching: [] },
|
|
},
|
|
})
|
|
}
|
|
|
|
return route.fulfill({ status: 404, json: { error: `unmocked fixture route: ${method} ${path}` } })
|
|
})
|
|
|
|
return () => finishCalls
|
|
}
|
|
|
|
async function installUserJourneyApi(page) {
|
|
await page.addInitScript(() => localStorage.setItem('st_token', 'fixture-user-token'))
|
|
await page.route('**/api/**', async (route) => {
|
|
const request = route.request()
|
|
const url = new URL(request.url())
|
|
const method = request.method()
|
|
const path = url.pathname
|
|
if (!path.startsWith('/api/')) return route.continue()
|
|
|
|
if (method === 'GET' && path === '/api/auth/me') {
|
|
return route.fulfill({ json: { user: { id: 'trainee', username: 'trainee', name: 'Fixture Trainee', role: 'user', must_setup: false } } })
|
|
}
|
|
if (method === 'GET' && path === '/api/me/board') {
|
|
return route.fulfill({ json: { board: [{ id: 'p1', persona_name: 'Acme decision maker', my_outcome: 'won' }] } })
|
|
}
|
|
if (method === 'GET' && path === '/api/chat/sessions') {
|
|
return route.fulfill({ json: { sessions: [{ id: 's1', persona_name: 'Acme decision maker', status: 'finished', outcome: 'won' }] } })
|
|
}
|
|
if (method === 'GET' && path === '/api/chat/sessions/s1') {
|
|
return route.fulfill({ json: { session: { id: 's1', persona_name: 'Acme decision maker', status: 'finished', outcome: 'won', messages: [{ role: 'customer', text: 'Tell me more.' }], debrief: { outcome: 'won', score: 82, pain: 'manual work', why: 'clear value', failurePoints: [], coaching: [] } } } })
|
|
}
|
|
if (method === 'GET' && path === '/api/me/weak-areas') {
|
|
return route.fulfill({ json: { insight: { total_sessions: 1, wins: 1, losses: 0, dimensions: { objection_handling: { label: 'Objection handling', evidence_count: 1, session_ids: ['s1'] } } } } })
|
|
}
|
|
if (method === 'POST' && path === '/api/me/personas/generate') {
|
|
return route.fulfill({ json: { persona: { id: 'private-p1', name: 'Locked pain persona' } } })
|
|
}
|
|
if (method === 'GET' && path === '/api/me/personas') {
|
|
return route.fulfill({ json: { group: { id: 'private-group' }, personas: [{ id: 'private-p1', name: 'Locked pain persona', tier: 'B', profession: 'Buyer', age_group: '35-44', location: 'Bangkok', product_context: 'CRM assistant' }] } })
|
|
}
|
|
|
|
return route.fulfill({ status: 404, json: { error: `unmocked user fixture route: ${method} ${path}` } })
|
|
})
|
|
}
|
|
|
|
test('admin can open the training list and human-readable group report', async ({ page }) => {
|
|
await installFixtureApi(page)
|
|
await page.goto('/training')
|
|
|
|
await expect(page.getByRole('link', { name: 'CRM assistant', exact: true })).toBeVisible()
|
|
await expect(page.getByText(/15 personas|15 บุคคลต้นแบบ/)).toBeVisible()
|
|
await page.goto('/admin/groups/g1/report')
|
|
await expect(page.getByRole('heading', { name: /รายงานกลุ่มบุคคลต้นแบบ|Group report/ })).toBeVisible()
|
|
await expect(page.getByText('Practice report')).toBeVisible()
|
|
await expect(page.getByRole('button', { name: /ดาวน์โหลดรายงาน|Download report/ })).toBeVisible()
|
|
|
|
await page.goto('/')
|
|
await expect(page.getByText(/Sessions|ครั้งที่ฝึก/, { exact: true })).toBeVisible()
|
|
const exportResponse = page.waitForResponse((response) => response.url().includes('/api/analytics/export?token=fixture'))
|
|
await page.getByRole('button', { name: 'CSV' }).click()
|
|
await expect((await exportResponse).status()).toBe(200)
|
|
|
|
const widthState = await page.evaluate(() => ({ width: window.innerWidth, scrollWidth: document.documentElement.scrollWidth }))
|
|
expect(widthState.scrollWidth).toBeLessThanOrEqual(widthState.width)
|
|
})
|
|
|
|
test('chat preview supports manual finish once and removes the finish action after completion', async ({ page }) => {
|
|
const getFinishCalls = await installFixtureApi(page, { chat: true })
|
|
await page.goto('/groups/g1/chat/p1')
|
|
|
|
await expect(page.getByText(/Preview mode|โหมด Preview/i)).toBeVisible()
|
|
await page.getByRole('button', { name: /เริ่มต้น|Start/ }).click()
|
|
await expect(page.getByText('Acme decision maker')).toBeVisible()
|
|
await page.locator('input[placeholder="ส่ง"], input[placeholder="Send"]').fill('I can help.')
|
|
await page.getByRole('button', { name: /ส่ง|Send/ }).click()
|
|
page.once('dialog', (dialog) => dialog.accept())
|
|
await page.getByRole('button', { name: /สรุปผล|Finish/ }).click()
|
|
|
|
await expect(page.getByText(/ผลลัพธ์และข้อเสนอแนะ|Result & Coaching/)).toBeVisible()
|
|
expect(getFinishCalls()).toBe(1)
|
|
await expect(page.getByRole('button', { name: /สรุปผล|Finish/ })).toHaveCount(0)
|
|
})
|
|
|
|
test('trainee can move from board to detail, weak area, and private persona', async ({ page }) => {
|
|
await installUserJourneyApi(page)
|
|
await page.goto('/my/board')
|
|
|
|
await expect(page.getByText('Acme decision maker')).toBeVisible()
|
|
await page.getByRole('link', { name: /ดูรายละเอียด|View details/ }).click()
|
|
await expect(page.getByText('Tell me more.')).toBeVisible()
|
|
|
|
await page.goto('/my/weak-areas')
|
|
await expect(page.getByText('Objection handling')).toBeVisible()
|
|
await page.getByRole('button', { name: /สร้างเพื่อฝึกจุดนี้|Create for this weakness/ }).click()
|
|
|
|
await expect(page).toHaveURL(/\/my\/personas$/)
|
|
await expect(page.getByText('Locked pain persona')).toBeVisible()
|
|
const widthState = await page.evaluate(() => ({ width: window.innerWidth, scrollWidth: document.documentElement.scrollWidth }))
|
|
expect(widthState.scrollWidth).toBeLessThanOrEqual(widthState.width)
|
|
})
|