Files
shumai/apps/web/e2e/tests/file/view-transcript.spec.ts
T

357 lines
13 KiB
TypeScript
Raw Normal View History

import type { Locator } from '@playwright/test'
import { expect, test } from '../../fixtures'
test.use({ fileOptions: { mediaType: 'video' } })
test('reads, searches and follows a saved transcript in the video player', async ({
file,
prisma,
}) => {
const { page, projectId, fileId } = file
await page.setViewportSize({ width: 1280, height: 720 })
await page.emulateMedia({ colorScheme: 'dark' })
await expect
.poll(() =>
prisma.asset.findUnique({
where: { id: fileId },
select: { status: true, media: true },
}),
)
.toMatchObject({ status: 'processed', media: { proxyType: 'video' } })
await prisma.assetTranscript.create({
data: {
assetId: fileId,
language: 'en',
model: 'whisper-tiny',
workflowTaskId: 'english-test',
segments: {
create: [
{ position: 0, startMs: 0, endMs: 180, text: 'Signal. signal.' },
...Array.from({ length: 28 }, (_, index) => ({
position: index + 1,
startMs: 300 + index * 12,
endMs: 312 + index * 12,
text: `Intervening segment ${index + 1}`,
})),
{ position: 29, startMs: 720, endMs: 980, text: 'Final signal.' },
{ position: 30, startMs: 995, endMs: 1000, text: 'Closing word.' },
],
},
},
})
await prisma.assetTranscript.create({
data: {
assetId: fileId,
language: 'zh',
model: 'whisper-tiny',
workflowTaskId: 'chinese-test',
segments: { create: { position: 0, startMs: 0, endMs: 700, text: '你好。' } },
},
})
await page.goto(`/projects/${projectId}/files/${fileId}`)
const toggle = page.getByRole('button', { name: 'Show transcript' })
const navigator = page.getByRole('link', { name: 'Preview', exact: true })
await expect(navigator).toBeVisible()
await toggle.click()
const panel = page.getByRole('complementary', { name: 'Transcript' })
const divider = page.getByRole('separator', { name: 'Resize transcript' })
const first = panel.getByRole('button', { name: /Signal\. signal\./ })
const last = panel.getByRole('button', { name: /Final signal\./ })
const video = page.locator('video').first()
const sync = panel.getByRole('button', { name: 'Sync to playback' })
await expect(navigator).toBeHidden()
await expect(panel).toHaveCSS('width', '384px')
await expect(toggle).toHaveAttribute('aria-expanded', 'true')
await expect(toggle).toHaveCSS('cursor', 'pointer')
await expect(first).toHaveAttribute('aria-current', 'true')
await expect(first.getByText('Signal. signal.', { exact: true })).toHaveCSS('font-weight', '700')
await expect(first).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)')
await expect(first).toContainText('00:00.000 – 00:00.180')
const dragDivider = async (handle: Locator, delta: number) => {
const box = await handle.boundingBox()
if (!box) throw new Error('Expected the divider to be visible')
const x = box.x + box.width / 2
const y = box.y + box.height / 2
await page.mouse.move(x, y)
await page.mouse.down()
await page.mouse.move(x + delta, y, { steps: 8 })
await page.mouse.up()
}
await dragDivider(divider, 256)
await expect(panel).toHaveCSS('width', '576px')
await dragDivider(divider, -384)
await expect(panel).toHaveCSS('width', '256px')
await divider.press('ArrowRight')
await expect(divider).toHaveAttribute('aria-valuenow', '21')
await divider.press('Home')
await expect(panel).toHaveCSS('width', '256px')
await dragDivider(divider, 128)
await expect(panel).toHaveCSS('width', '384px')
const viewport = panel.locator('[data-slot=scroll-area-viewport]')
const nearBottomId = await viewport.evaluate((element) => {
const view = element.getBoundingClientRect()
const nearBottom = Array.from(element.querySelectorAll('li[data-segment-id]')).find((row) => {
const { top, bottom } = row.getBoundingClientRect()
return top > view.top + view.height * 0.75 && bottom < view.bottom
})
return nearBottom?.getAttribute('data-segment-id')
})
if (!nearBottomId) throw new Error('Expected a visible segment in the bottom quarter')
const nearBottom = panel.locator(`[data-segment-id="${nearBottomId}"]`).getByRole('button')
await viewport.evaluate((element) => {
let scrollSteps = 0
const record = () => {
element.setAttribute('data-test-scroll-steps', String(++scrollSteps))
if (scrollSteps === 2) element.removeEventListener('scroll', record)
}
element.addEventListener('scroll', record)
})
await nearBottom.click()
await expect(nearBottom).toHaveAttribute('aria-current', 'true')
await expect
.poll(() =>
nearBottom.evaluate((element) => {
const view = element.closest('[data-slot=scroll-area-viewport]')!.getBoundingClientRect()
const row = element.getBoundingClientRect()
return (row.top + row.height / 2 - view.top) / view.height
}),
)
.toBeCloseTo(0.5, 1)
await expect(viewport).toHaveAttribute('data-test-scroll-steps', '2')
await expect(sync).toBeHidden()
await page.emulateMedia({ reducedMotion: 'reduce' })
await viewport.evaluate((element) => {
let previousTop = element.scrollTop
const record = () => {
const top = element.scrollTop
if (top < previousTop) {
element.setAttribute('data-test-first-reverse-scroll-top', String(top))
element.removeEventListener('scroll', record)
}
previousTop = top
}
element.addEventListener('scroll', record)
})
await video.evaluate((element) => {
const media = element as HTMLVideoElement
media.currentTime = 0
})
await expect(first).toHaveAttribute('aria-current', 'true')
await expect(first).toBeInViewport()
await expect(viewport).toHaveAttribute('data-test-first-reverse-scroll-top', '0')
await page.emulateMedia({ reducedMotion: 'no-preference' })
await panel.getByRole('button', { name: 'Search transcript' }).click()
const search = panel.getByRole('searchbox', { name: 'Search transcript' })
await expect(search).toBeFocused()
await search.fill('SIGNAL.')
await expect(panel.getByText('1 / 3')).toBeVisible()
const match = panel.locator('[data-current-match]')
await expect(match).toHaveCSS('padding-left', '1px')
await expect(match).toHaveCSS('padding-right', '1px')
await panel.getByRole('button', { name: 'Next match' }).click()
await expect(panel.getByText('2 / 3')).toBeVisible()
await search.press('Enter')
await expect(panel.getByText('3 / 3')).toBeVisible()
await expect(last).toBeInViewport()
await expect(first).toHaveAttribute('aria-current', 'true')
await last.click()
await expect(last).toHaveAttribute('aria-current', 'true')
await expect
.poll(() => video.evaluate((element) => (element as HTMLVideoElement).currentTime))
.toBeCloseTo(0.72, 1)
await expect(video).toHaveJSProperty('paused', true)
await panel.getByRole('button', { name: 'Next match' }).click()
await expect(panel.getByText('1 / 3')).toBeVisible()
await panel.getByRole('button', { name: 'Previous match' }).click()
await expect(panel.getByText('3 / 3')).toBeVisible()
await search.fill('unmatched')
await expect(panel.getByText('0 / 0')).toBeVisible()
await search.press('Escape')
await expect(search).toBeHidden()
await sync.click()
await expect(sync).toBeHidden()
await viewport.hover()
await page.mouse.wheel(0, -2500)
await expect(sync).toBeVisible()
await expect(page.locator('html')).toHaveClass('dark')
const background = await panel.evaluate((element) => getComputedStyle(element).backgroundColor)
await expect(sync).toHaveCSS('background-color', background)
await expect(sync.locator('svg')).toHaveCount(0)
expect(
await sync.evaluate(
(element) =>
parseFloat(getComputedStyle(element).borderTopLeftRadius) >= element.clientHeight / 2,
),
).toBe(true)
await sync.hover()
await expect(sync).not.toHaveCSS('background-color', background)
await expect(sync).toHaveCSS('background-color', /^oklch\([^/]+\)$/)
await expect(last).not.toBeInViewport()
await expect(last).toHaveAttribute('aria-current', 'true')
await viewport.evaluate((element) => {
element.addEventListener(
'scroll',
() => {
element.dispatchEvent(new WheelEvent('wheel', { bubbles: true, deltaY: -120 }))
element.setAttribute('data-test-follow-interrupted', 'true')
},
{ once: true },
)
})
await sync.click()
await expect(viewport).toHaveAttribute('data-test-follow-interrupted', 'true')
await expect(sync).toBeVisible()
await expect(last).not.toBeInViewport()
await sync.click()
await expect(last).toBeInViewport()
await expect(sync).toBeHidden()
await video.evaluate((element) => {
const media = element as HTMLVideoElement
media.currentTime = 0.22
})
await expect(panel.locator('[aria-current=true]')).toHaveCount(0)
await video.evaluate((element) => {
const media = element as HTMLVideoElement
media.currentTime = 0
})
await expect(first).toHaveAttribute('aria-current', 'true')
await expect(first).toBeInViewport()
await video.evaluate(async (element) => {
const media = element as HTMLVideoElement
media.playbackRate = 0.5
await media.play()
})
await expect(last).toBeInViewport()
await expect(sync).toBeHidden()
await expect(video).toHaveJSProperty('ended', true)
await last.press('Enter')
await expect(last).toHaveAttribute('aria-current', 'true')
await expect(video).toHaveJSProperty('paused', true)
const closingWord = panel.getByRole('button', { name: /Closing word\./ })
await closingWord.click()
await expect
.poll(() =>
video.evaluate((element) => {
const media = element as HTMLVideoElement
return { seeking: media.seeking, timeMs: Math.round(media.currentTime * 1000) }
}),
)
.toEqual({ seeking: false, timeMs: 995 })
await expect(closingWord).toHaveAttribute('aria-current', 'true')
await expect(video).toHaveJSProperty('paused', true)
await panel.getByRole('combobox', { name: 'Language' }).selectOption('zh')
await expect(panel.getByText('你好。')).toBeVisible()
await expect(last).toHaveCount(0)
await dragDivider(divider, 64)
await expect(panel).toHaveCSS('width', '448px')
await panel.getByRole('button', { name: 'Close transcript' }).click()
await expect(panel).toBeHidden()
await expect(navigator).toBeVisible()
await expect(toggle).toBeFocused()
await toggle.click()
await expect(panel).toHaveCSS('width', '448px')
const commentsDivider = page.locator('main [data-slot="resize-handle"]:not([role="separator"])')
await dragDivider(commentsDivider, -200)
await page.setViewportSize({ width: 840, height: 720 })
await expect
.poll(async () => Number(await divider.getAttribute('aria-valuemax')))
.toBeLessThan(20)
await divider.press('End')
await page.setViewportSize({ width: 1280, height: 720 })
await dragDivider(commentsDivider, 200)
await expect(panel).toHaveCSS('width', '448px')
await panel.getByRole('button', { name: 'Close transcript' }).click()
await page.setViewportSize({ width: 390, height: 844 })
const comments = page.getByTestId('mobile-bottom-sheet')
await expect(comments).toBeVisible()
await toggle.click()
await expect(panel).toBeVisible()
await expect(video).toBeVisible()
await expect(comments).toBeHidden()
await panel.getByRole('button', { name: 'Close transcript' }).click()
await expect(comments).toBeVisible()
})
test('uses the displayed video version and hides the toggle for versions without a transcript', async ({
file,
prisma,
}) => {
const { page, projectId, fileId, rootFolderId } = file
await expect
.poll(() =>
prisma.asset.findUnique({
where: { id: fileId },
select: { status: true, media: true },
}),
)
.toMatchObject({ status: 'processed', media: { proxyType: 'video' } })
const latest = await prisma.asset.findUniqueOrThrow({ where: { id: fileId } })
if (!latest.media) throw new Error('Expected processed video media')
const stack = await prisma.asset.create({
data: {
projectId,
parentId: rootFolderId,
name: latest.name,
type: 'version_stack',
status: 'processed',
fileCount: 2,
},
})
await prisma.asset.update({
where: { id: fileId },
data: { parentId: stack.id, sortIndex: 'a0' },
})
const old = await prisma.asset.create({
data: {
projectId,
parentId: stack.id,
name: 'Earlier video.mp4',
type: 'file',
status: 'processed',
mediaType: latest.mediaType,
storageKeyId: latest.storageKeyId,
media: latest.media,
sizeByte: latest.sizeByte,
sortIndex: 'a1',
},
})
await prisma.assetTranscript.create({
data: {
assetId: fileId,
language: 'en',
model: 'whisper-tiny',
workflowTaskId: 'latest-version-test',
segments: {
create: { position: 0, startMs: 0, endMs: 950, text: 'Latest version text' },
},
},
})
const stackUrl = `/projects/${projectId}/files/${stack.id}`
await page.goto(stackUrl)
const toggle = page.getByRole('button', { name: 'Show transcript' })
await toggle.click()
const panel = page.getByRole('complementary', { name: 'Transcript' })
await expect(panel.getByText('Latest version text')).toBeVisible()
const [languages] = await Promise.all([
page.waitForResponse(`**/api/files/${old.id}/transcripts`),
page.goto(`${stackUrl}?version=${old.id}`),
])
expect(await languages.json()).toEqual({ languages: [] })
await expect(panel).toBeHidden()
await expect(toggle).toBeHidden()
})