@@ -13,21 +13,32 @@ import * as Cause from "effect/Cause";
1313import { act , type ComponentProps } from "react" ;
1414import { createRoot , type Root } from "react-dom/client" ;
1515import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
16- import { page } from "vite-plus/test/browser" ;
16+ import { page , userEvent } from "vite-plus/test/browser" ;
1717import { DraftId , useComposerDraftStore } from "~/composerDraftStore" ;
1818
1919import "~/index.css" ;
2020
21- const { newThread, prepareThread, refresh, update, addToast, updateToast, checkoutState } =
22- vi . hoisted ( ( ) => ( {
23- newThread : vi . fn ( ) ,
24- prepareThread : vi . fn ( ) ,
25- refresh : vi . fn ( ) ,
26- update : vi . fn ( ) ,
27- addToast : vi . fn ( ) ,
28- updateToast : vi . fn ( ) ,
29- checkoutState : { error : null as Error | null } ,
30- } ) ) ;
21+ const {
22+ newThread,
23+ prepareThread,
24+ refresh,
25+ update,
26+ addToast,
27+ updateToast,
28+ checkoutState,
29+ hostCommand,
30+ queries,
31+ } = vi . hoisted ( ( ) => ( {
32+ hostCommand : vi . fn ( ) ,
33+ queries : { diff : undefined as unknown } ,
34+ newThread : vi . fn ( ) ,
35+ prepareThread : vi . fn ( ) ,
36+ refresh : vi . fn ( ) ,
37+ update : vi . fn ( ) ,
38+ addToast : vi . fn ( ) ,
39+ updateToast : vi . fn ( ) ,
40+ checkoutState : { error : null as Error | null } ,
41+ } ) ) ;
3142
3243vi . mock ( import ( "~/state/session" ) , async ( original ) => ( {
3344 ...( await original ( ) ) ,
@@ -52,17 +63,20 @@ vi.mock(import("~/lib/sourceControlActions"), async (original) => ({
5263} ) ) ;
5364vi . mock ( import ( "~/state/use-atom-command" ) , async ( original ) => ( {
5465 ...( await original ( ) ) ,
55- useAtomCommand : ( ( command : unknown ) => ( command === "update" ? update : vi . fn ( ) ) ) as never ,
66+ useAtomCommand : ( ( command : unknown ) => ( command === "update" ? update : hostCommand ) ) as never ,
5667} ) ) ;
5768vi . mock ( import ( "~/state/pullRequests" ) , async ( original ) => {
5869 const actual = await original ( ) ;
5970 const { Atom } = await import ( "effect/reactivity" ) ;
6071 const cannotWriteGuides = Atom . make ( false ) ;
72+ const diff = Atom . make ( null ) ;
73+ queries . diff = diff ;
6174 return {
6275 ...actual ,
6376 pullRequestEnvironment : {
6477 ...actual . pullRequestEnvironment ,
6578 detail : ( ( ) => "detail" ) as never ,
79+ diff : ( ( ) => diff ) as never ,
6680 activity : ( ( ) => "activity" ) as never ,
6781 update : "update" as never ,
6882 reviewGuide : ( ( ) => "review-guide" ) as never ,
@@ -76,7 +90,7 @@ vi.mock(import("~/state/pullRequests"), async (original) => {
7690vi . mock ( import ( "~/state/query" ) , async ( original ) => ( {
7791 ...( await original ( ) ) ,
7892 useEnvironmentQuery : ( ( query : unknown ) => ( {
79- data : query === "detail" ? detail : null ,
93+ data : query === "detail" ? currentDetail : query === queries . diff ? diff : null ,
8094 dataUpdatedAt : 0 ,
8195 failure : null ,
8296 isPending : false ,
@@ -95,8 +109,7 @@ vi.mock(import("~/state/usePullRequestStack"), async (original) => ({
95109 refresh,
96110 } ) ) as never ,
97111} ) ) ;
98- // The real tabs load diffs, markdown and checks from the environment; these stand-ins
99- // expose the actions they hand back to the panel and a tall scroller.
112+ // The summary stand-in exposes its panel actions and nested scrolling.
100113vi . mock ( import ( "./PullRequestSummaryTab" ) , async ( original ) => ( {
101114 ...( await original ( ) ) ,
102115 PullRequestSummaryTab : ( ( {
@@ -122,38 +135,6 @@ vi.mock(import("./PullRequestSummaryTab"), async (original) => ({
122135 </ div >
123136 ) ) as never ,
124137} ) ) ;
125- vi . mock ( import ( "./PullRequestCodeTab" ) , async ( original ) => ( {
126- ...( await original ( ) ) ,
127- default : ( ( {
128- onAddToAgentSelection,
129- scrollerRef,
130- } : ComponentProps < typeof import ( "./PullRequestCodeTab" ) . default > ) => (
131- < div ref = { scrollerRef } data-testid = "code-scroller" className = "h-full overflow-y-auto" >
132- < button
133- type = "button"
134- onClick = { ( ) =>
135- onAddToAgentSelection ?.( {
136- request : "Fix this line" ,
137- comment : {
138- id : "note-1" ,
139- sectionId : "file:a.ts" ,
140- sectionTitle : "a.ts" ,
141- filePath : "a.ts" ,
142- startIndex : 0 ,
143- endIndex : 0 ,
144- rangeLabel : "L1" ,
145- text : "Please fix" ,
146- diff : "+broken()" ,
147- } ,
148- } )
149- }
150- >
151- Add to agent
152- </ button >
153- < div className = "h-[3000px]" />
154- </ div >
155- ) ) as never ,
156- } ) ) ;
157138
158139vi . mock ( import ( "../ui/toast" ) , async ( original ) => {
159140 const actual = await original ( ) ;
@@ -165,6 +146,7 @@ vi.mock(import("../ui/toast"), async (original) => {
165146
166147import { PullRequestDetailPanel } from "./PullRequestDetailPanel" ;
167148import { pullRequestPanelContext } from "./pullRequestDetail.logic" ;
149+ import { usePullRequestReviewStore } from "./pullRequestReviewStore" ;
168150
169151const detail : PullRequestDetailView = {
170152 provider : SourceControlProviderKind . make ( "github" ) ,
@@ -218,6 +200,24 @@ const detail: PullRequestDetailView = {
218200 } ,
219201} ;
220202
203+ let currentDetail = detail ;
204+
205+ // Enough lines to exercise the panel's title folding with the real virtualized Code tab.
206+ const diff = {
207+ patch : [
208+ "diff --git a/a.ts b/a.ts" ,
209+ "new file mode 100644" ,
210+ "--- /dev/null" ,
211+ "+++ b/a.ts" ,
212+ "@@ -0,0 +1,100 @@" ,
213+ "+broken()" ,
214+ ...Array . from ( { length : 99 } , ( _ , index ) => `+// line ${ index + 2 } ` ) ,
215+ "" ,
216+ ] . join ( "\n" ) ,
217+ truncated : false ,
218+ nextCursor : null ,
219+ } ;
220+
221221const threadRef : ScopedThreadRef = {
222222 environmentId : EnvironmentId . make ( "env-1" ) ,
223223 threadId : ThreadId . make ( "thread-1" ) ,
@@ -257,7 +257,10 @@ function render(props: Partial<ComponentProps<typeof PullRequestDetailPanel>> =
257257
258258beforeEach ( ( ) => {
259259 vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
260+ currentDetail = detail ;
260261 useComposerDraftStore . setState ( { draftsByThreadKey : { } } ) ;
262+ usePullRequestReviewStore . setState ( { drafts : { } , summaries : { } } ) ;
263+ hostCommand . mockReset ( ) ;
261264 newThread
262265 . mockReset ( )
263266 . mockResolvedValue ( { draftId : newDraftId , threadId : ThreadId . make ( "new-thread" ) } ) ;
@@ -300,6 +303,16 @@ const unrelatedThread = {
300303
301304const tab = ( name : string ) => page . getByRole ( "button" , { name, exact : true } ) ;
302305
306+ const selectionDraft = ( ) => page . getByRole ( "textbox" , { name : "Comment on lines a.ts:1" } ) ;
307+
308+ async function selectFirstLine ( ) {
309+ const line = page . getByText ( "broken()" , { exact : true } ) ;
310+ await expect . element ( line ) . toBeVisible ( ) ;
311+ const host = line . element ( ) . getRootNode ( ) as ShadowRoot ;
312+ const gutter = host . querySelector < HTMLElement > ( '[data-column-number="1"]' ) ! ;
313+ await userEvent . click ( gutter ) ;
314+ }
315+
303316it . each ( [
304317 { isComposing : true , keyCode : 13 } ,
305318 { isComposing : false , keyCode : 229 } ,
@@ -333,6 +346,14 @@ it.each([
333346async function runAction ( action : string ) {
334347 if ( action === "Add to agent" ) {
335348 await tab ( "Code" ) . click ( ) ;
349+ await page . getByRole ( "button" , { name : "Expand all files" , exact : true } ) . click ( ) ;
350+ await selectFirstLine ( ) ;
351+ const draft = page . getByRole ( "textbox" , { name : "Comment on lines a.ts:1" } ) ;
352+ await expect . element ( draft ) . toBeVisible ( ) ;
353+ expect (
354+ page . getByRole ( "button" , { name : "Add to review" , exact : true } ) . elements ( ) ,
355+ ) . toHaveLength ( 0 ) ;
356+ await draft . fill ( "Fix this line" ) ;
336357 }
337358 if ( [ "Ask a question" , "Explain this PR" ] . includes ( action ) || action . startsWith ( "Fix findings" ) ) {
338359 await page . getByRole ( "button" , { name : "More pull request actions" } ) . click ( ) ;
@@ -369,6 +390,17 @@ describe.each([
369390 "Fix check" ,
370391 "Add to agent" ,
371392 ] ) ( "%s writes to the correct composer" , async ( action ) => {
393+ // Cover both a host without inline comments and an account that cannot write them.
394+ currentDetail =
395+ thread === stackThread
396+ ? {
397+ ...detail ,
398+ capabilities : {
399+ ...detail . capabilities ,
400+ review : { ...detail . capabilities . review , inlineComment : true } ,
401+ } ,
402+ }
403+ : { ...detail , viewerPermissions : { ...detail . viewerPermissions , comment : true } } ;
372404 if ( target ) useComposerDraftStore . getState ( ) . setPrompt ( target , "Keep my draft" ) ;
373405 await renderPanel ( ) ;
374406 await runAction ( target ? action : action . replace ( "in this thread" , "in a thread" ) ) ;
@@ -384,8 +416,20 @@ describe.each([
384416 if ( action === "Add to agent" ) {
385417 expect ( draft ( ) ?. prompt ) . toContain ( "Fix this line" ) ;
386418 expect ( draft ( ) ?. reviewComments ) . toEqual (
387- expect . arrayContaining ( [ expect . objectContaining ( { id : "note-1" , diff : "+broken()" } ) ] ) ,
419+ expect . arrayContaining ( [
420+ expect . objectContaining ( {
421+ filePath : "a.ts" ,
422+ diff : expect . stringContaining ( "+broken()" ) ,
423+ startIndex : 0 ,
424+ endIndex : 0 ,
425+ } ) ,
426+ ] ) ,
388427 ) ;
428+ expect ( usePullRequestReviewStore . getState ( ) . drafts ) . toEqual ( { } ) ;
429+ expect ( hostCommand ) . not . toHaveBeenCalled ( ) ;
430+ await expect
431+ . element ( page . getByRole ( "textbox" , { name : "Comment on lines a.ts:1" } ) )
432+ . not . toBeInTheDocument ( ) ;
389433 }
390434 if ( target ) {
391435 expect ( draft ( ) ?. prompt ) . toContain ( "Keep my draft" ) ;
@@ -397,13 +441,72 @@ describe.each([
397441 } ) ;
398442} ) ;
399443
444+ it ( "offers both selection actions and queues a permitted host review locally" , async ( ) => {
445+ currentDetail = {
446+ ...detail ,
447+ capabilities : {
448+ ...detail . capabilities ,
449+ review : { ...detail . capabilities . review , inlineComment : true } ,
450+ } ,
451+ viewerPermissions : { ...detail . viewerPermissions , comment : true } ,
452+ } ;
453+ await render ( { composerDraftTarget : draftId , threadRef } ) ;
454+ await tab ( "Code" ) . click ( ) ;
455+ await page . getByRole ( "button" , { name : "Expand all files" , exact : true } ) . click ( ) ;
456+ await selectFirstLine ( ) ;
457+ const addToReview = page . getByRole ( "button" , { name : "Add to review" , exact : true } ) ;
458+ await expect . element ( addToReview ) . toBeVisible ( ) ;
459+ await expect
460+ . element ( page . getByRole ( "button" , { name : "Add to agent" , exact : true } ) )
461+ . toBeVisible ( ) ;
462+ await selectionDraft ( ) . fill ( "Fix this line before merging" ) ;
463+ await addToReview . click ( ) ;
464+ await expect . element ( selectionDraft ( ) ) . not . toBeInTheDocument ( ) ;
465+ expect ( Object . values ( usePullRequestReviewStore . getState ( ) . drafts ) . flat ( ) ) . toEqual ( [
466+ expect . objectContaining ( { path : "a.ts" , body : "Fix this line before merging" } ) ,
467+ ] ) ;
468+ expect ( useComposerDraftStore . getState ( ) . getComposerDraft ( draftId ) ?. reviewComments ?? [ ] ) . toEqual (
469+ [ ] ,
470+ ) ;
471+ expect ( hostCommand ) . not . toHaveBeenCalled ( ) ;
472+ } ) ;
473+
474+ it ( "cancels an agent-only selection and submits a fresh one with the keyboard without a host review" , async ( ) => {
475+ await render ( { composerDraftTarget : draftId , threadRef } ) ;
476+ await tab ( "Code" ) . click ( ) ;
477+ await page . getByRole ( "button" , { name : "Expand all files" , exact : true } ) . click ( ) ;
478+ await selectFirstLine ( ) ;
479+ await selectionDraft ( ) . fill ( "Discard this request" ) ;
480+ await page . getByRole ( "button" , { name : "Cancel" , exact : true } ) . click ( ) ;
481+ await expect . element ( selectionDraft ( ) ) . not . toBeInTheDocument ( ) ;
482+ expect ( useComposerDraftStore . getState ( ) . getComposerDraft ( draftId ) ?. reviewComments ?? [ ] ) . toEqual (
483+ [ ] ,
484+ ) ;
485+ expect ( usePullRequestReviewStore . getState ( ) . drafts ) . toEqual ( { } ) ;
486+ expect ( hostCommand ) . not . toHaveBeenCalled ( ) ;
487+
488+ await selectFirstLine ( ) ;
489+ await expect . element ( selectionDraft ( ) ) . toHaveValue ( "" ) ;
490+ await selectionDraft ( ) . fill ( "Fix this line" ) ;
491+ await userEvent . keyboard ( "{Control>}{Enter}{/Control}" ) ;
492+ await expect . element ( selectionDraft ( ) ) . not . toBeInTheDocument ( ) ;
493+ expect ( useComposerDraftStore . getState ( ) . getComposerDraft ( draftId ) ?. prompt ) . toContain (
494+ "Fix this line" ,
495+ ) ;
496+ expect ( usePullRequestReviewStore . getState ( ) . drafts ) . toEqual ( { } ) ;
497+ expect ( hostCommand ) . not . toHaveBeenCalled ( ) ;
498+ } ) ;
499+
400500it ( "folds the title only for the active tab's own scroller" , async ( ) => {
401501 await render ( ) ;
402502 const title = page . getByRole ( "heading" , { level : 1 , name : "Test pull request" } ) ;
403503 await expect . element ( title ) . toBeVisible ( ) ;
404504 const titleFold = ( ) => title . element ( ) . closest ( "[inert]" ) !== null ;
405505 const scrollTo = async ( testId : string , top : number ) => {
406- const scroller = page . getByTestId ( testId ) . element ( ) as HTMLElement ;
506+ const scroller =
507+ testId === "code-scroller"
508+ ? document . querySelector < HTMLElement > ( ".\\@container.overflow-auto" ) !
509+ : ( page . getByTestId ( testId ) . element ( ) as HTMLElement ) ;
407510 await act ( async ( ) => {
408511 scroller . scrollTop = top ;
409512 scroller . dispatchEvent ( new Event ( "scroll" ) ) ;
@@ -425,7 +528,8 @@ it("folds the title only for the active tab's own scroller", async () => {
425528 await vi . waitFor ( ( ) => expect ( titleFold ( ) ) . toBe ( true ) ) ;
426529
427530 await tab ( "Code" ) . click ( ) ;
428- await expect . element ( page . getByTestId ( "code-scroller" ) ) . toBeInTheDocument ( ) ;
531+ await page . getByRole ( "button" , { name : "Expand all files" , exact : true } ) . click ( ) ;
532+ await expect . element ( page . getByText ( "broken()" , { exact : true } ) ) . toBeVisible ( ) ;
429533 await vi . waitFor ( ( ) => expect ( titleFold ( ) ) . toBe ( false ) ) ;
430534 await scrollTo ( "code-scroller" , 600 ) ;
431535 await vi . waitFor ( ( ) => expect ( titleFold ( ) ) . toBe ( true ) ) ;
0 commit comments