Repository navigation
fix(web): seek the first transcript search result #2884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
richiemcilroy
merged 3 commits into
CapSoftware:main
from
yashranaway:fix/web-transcript
Oct 23, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
198 changes: 198 additions & 0 deletions
198
apps/web/__tests__/unit/transcript-sidebar-search.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| // @vitest-environment jsdom | ||
|
|
||
| import type { Video } from "@cap/web-domain"; | ||
| import { act, createElement, type RefObject } from "react"; | ||
| import { createRoot, type Root } from "react-dom/client"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| getEditTranscript: vi.fn(), | ||
| scrollToIndex: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/actions/videos/get-edit-transcript", () => ({ | ||
| getEditTranscript: mocks.getEditTranscript, | ||
| requestEditTranscript: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/app/s/[videoId]/edit/use-active-transcript-word-index", () => ({ | ||
| useActiveTranscriptWordIndex: () => -1, | ||
| })); | ||
|
|
||
| vi.mock("@virtual-grid/react", () => ({ | ||
| useVirtualizer: ({ count }: { count: number }) => ({ | ||
| getTotalSize: () => count * 100, | ||
| getVirtualItems: () => | ||
| Array.from({ length: count }, (_, index) => ({ | ||
| index, | ||
| key: index, | ||
| start: index * 100, | ||
| })), | ||
| measureElement: vi.fn(), | ||
| scrollToIndex: mocks.scrollToIndex, | ||
| }), | ||
| })); | ||
|
|
||
| import { TranscriptSidebar } from "@/app/s/[videoId]/edit/TranscriptSidebar"; | ||
|
|
||
| const videoId = "video-id" as Video.VideoId; | ||
| const transcript = { | ||
| version: 3 as const, | ||
| speechModelUsed: "fixture", | ||
| durationMs: 5_000, | ||
| languageCode: "en", | ||
| words: [ | ||
| { | ||
| id: "word-0", | ||
| text: "Match", | ||
| startMs: 1_000, | ||
| endMs: 1_400, | ||
| confidence: 1, | ||
| speaker: null, | ||
| channel: null, | ||
| }, | ||
| { | ||
| id: "word-1", | ||
| text: "between", | ||
| startMs: 2_000, | ||
| endMs: 2_400, | ||
| confidence: 1, | ||
| speaker: null, | ||
| channel: null, | ||
| }, | ||
| { | ||
| id: "word-2", | ||
| text: "matching", | ||
| startMs: 3_000, | ||
| endMs: 3_400, | ||
| confidence: 1, | ||
| speaker: null, | ||
| channel: null, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| let container: HTMLDivElement; | ||
| let root: Root; | ||
| let video: HTMLVideoElement; | ||
|
|
||
| async function waitFor(assertion: () => void) { | ||
| let error: unknown; | ||
| for (let attempt = 0; attempt < 50; attempt++) { | ||
| try { | ||
| assertion(); | ||
| return; | ||
| } catch (cause) { | ||
| error = cause; | ||
| } | ||
| await act(async () => { | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
| }); | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| async function enterSearch(query: string, expectedCounter = "1/2") { | ||
| const input = container.querySelector<HTMLInputElement>( | ||
| 'input[placeholder="Search transcript"]', | ||
| ); | ||
| if (!input) throw new Error("Search input missing"); | ||
|
|
||
| await act(async () => { | ||
| const setter = Object.getOwnPropertyDescriptor( | ||
| HTMLInputElement.prototype, | ||
| "value", | ||
| )?.set; | ||
| setter?.call(input, query); | ||
| input.dispatchEvent(new Event("input", { bubbles: true })); | ||
| }); | ||
| await waitFor(() => expect(container.textContent).toContain(expectedCounter)); | ||
| return input; | ||
| } | ||
|
|
||
| async function pressEnter(input: HTMLInputElement) { | ||
| await act(async () => { | ||
| input.dispatchEvent( | ||
| new KeyboardEvent("keydown", { bubbles: true, key: "Enter" }), | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| beforeEach(async () => { | ||
| vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); | ||
| vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => { | ||
| callback(0); | ||
| return 1; | ||
| }); | ||
| HTMLElement.prototype.scrollIntoView = vi.fn(); | ||
| mocks.getEditTranscript.mockResolvedValue({ status: "ready", transcript }); | ||
| container = document.createElement("div"); | ||
| document.body.append(container); | ||
| root = createRoot(container); | ||
| video = document.createElement("video"); | ||
| const videoRef: RefObject<HTMLVideoElement | null> = { current: video }; | ||
|
|
||
| await act(async () => { | ||
| root.render( | ||
| createElement(TranscriptSidebar, { | ||
| videoId, | ||
| videoRef, | ||
| keepRanges: [{ start: 0, end: 5 }], | ||
| onDeleteRanges: vi.fn(), | ||
| }), | ||
| ); | ||
| }); | ||
| await waitFor(() => | ||
| expect( | ||
| container.querySelector('input[placeholder="Search transcript"]'), | ||
| ).not.toBeNull(), | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await act(async () => root.unmount()); | ||
| container.remove(); | ||
| vi.unstubAllGlobals(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("transcript search navigation", () => { | ||
| it("seeks to the first match before advancing through later matches", async () => { | ||
| const input = await enterSearch("match"); | ||
|
|
||
| await pressEnter(input); | ||
| expect(video.currentTime).toBe(1); | ||
| expect(container.textContent).toContain("1/2"); | ||
|
|
||
| await pressEnter(input); | ||
| expect(video.currentTime).toBe(3); | ||
| expect(container.textContent).toContain("2/2"); | ||
|
|
||
| await pressEnter(input); | ||
| expect(video.currentTime).toBe(1); | ||
| expect(container.textContent).toContain("1/2"); | ||
| }); | ||
|
|
||
| it("seeks to the last match when Previous is pressed first", async () => { | ||
| await enterSearch("match"); | ||
| const previous = container.querySelector<HTMLButtonElement>( | ||
| 'button[aria-label="Previous search result"]', | ||
| ); | ||
| if (!previous) throw new Error("Previous search button missing"); | ||
|
|
||
| await act(async () => previous.click()); | ||
| expect(video.currentTime).toBe(3); | ||
| expect(container.textContent).toContain("2/2"); | ||
| }); | ||
|
|
||
| it("seeks to the new first match after changing the query", async () => { | ||
| const input = await enterSearch("between", "1/1"); | ||
| await pressEnter(input); | ||
| expect(video.currentTime).toBe(2); | ||
|
|
||
| await enterSearch("match"); | ||
| await pressEnter(input); | ||
| expect(video.currentTime).toBe(1); | ||
| expect(container.textContent).toContain("1/2"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new test only presses Enter on one unchanged query. It does not cover starting with Previous or changing the query after selecting a match, both of which depend on the new
-1state. Those paths could break while this test still passes. Add checks that fresh Previous seeks the last match and that the first Next after changing the query seeks the new first match.Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added both, Previous on a fresh search seeks the last match and the first Next after changing the query seeks the new first match. Both pass with the current logic so no component change was needed (4fe95e0).