1- import { describe , expect , it } from "vite-plus/test" ;
1+ import { afterEach , describe , expect , it , vi } from "vite-plus/test" ;
2+
3+ import { writeTextToClipboard } from "../hooks/useCopyToClipboard" ;
24
35import {
6+ createTerminalSelectionAutoCopy ,
47 getCopyableDomSelectionText ,
58 isCopyOnSelectEditableTarget ,
69 normalizeTerminalSelectionText ,
@@ -9,6 +12,93 @@ import {
912 snapshotDomSelection ,
1013} from "./copyOnSelect" ;
1114
15+ describe ( "terminal copy on select" , ( ) => {
16+ afterEach ( ( ) => {
17+ vi . unstubAllGlobals ( ) ;
18+ } ) ;
19+
20+ it ( "preserves terminal input when the browser rejects the clipboard write" , async ( ) => {
21+ const terminalInput = {
22+ value : "にほんご" ,
23+ selectionStart : 4 ,
24+ selectionEnd : 4 ,
25+ select : vi . fn ( ) ,
26+ } ;
27+ vi . stubGlobal ( "window" , { } ) ;
28+ vi . stubGlobal ( "navigator" , {
29+ clipboard : { writeText : vi . fn ( ) . mockRejectedValue ( new Error ( "denied" ) ) } ,
30+ } ) ;
31+ const onCopied = vi . fn ( ) ;
32+ const autoCopy = createTerminalSelectionAutoCopy ( {
33+ readSettings : ( ) => ( { enabled : true , showToast : true } ) ,
34+ writeText : ( text ) => writeTextToClipboard ( text , "terminal selection" ) ,
35+ onCopied,
36+ } ) ;
37+
38+ await expect ( autoCopy . copy ( "git status" ) ) . resolves . toBe ( false ) ;
39+ expect ( terminalInput . value ) . toBe ( "にほんご" ) ;
40+ expect ( terminalInput . selectionStart ) . toBe ( 4 ) ;
41+ expect ( terminalInput . selectionEnd ) . toBe ( 4 ) ;
42+ expect ( terminalInput . select ) . not . toHaveBeenCalled ( ) ;
43+ expect ( onCopied ) . not . toHaveBeenCalled ( ) ;
44+ } ) ;
45+
46+ it ( "allows the same selection to retry after a failed write" , async ( ) => {
47+ const writeText = vi
48+ . fn ( )
49+ . mockRejectedValueOnce ( new Error ( "denied" ) )
50+ . mockResolvedValueOnce ( true ) ;
51+ const onCopied = vi . fn ( ) ;
52+ const autoCopy = createTerminalSelectionAutoCopy ( {
53+ readSettings : ( ) => ( { enabled : true , showToast : true } ) ,
54+ writeText,
55+ onCopied,
56+ } ) ;
57+
58+ await expect ( autoCopy . copy ( "git status" ) ) . resolves . toBe ( false ) ;
59+ await expect ( autoCopy . copy ( "git status" ) ) . resolves . toBe ( true ) ;
60+ expect ( onCopied ) . toHaveBeenCalledOnce ( ) ;
61+ } ) ;
62+
63+ it ( "coalesces a pending write and honors the current toast setting" , async ( ) => {
64+ let finishWrite : ( ( didCopy : boolean ) => void ) | undefined ;
65+ const writeText = vi . fn (
66+ ( ) =>
67+ new Promise < boolean > ( ( resolve ) => {
68+ finishWrite = resolve ;
69+ } ) ,
70+ ) ;
71+ const onCopied = vi . fn ( ) ;
72+ const autoCopy = createTerminalSelectionAutoCopy ( {
73+ readSettings : ( ) => ( { enabled : true , showToast : false } ) ,
74+ writeText,
75+ onCopied,
76+ } ) ;
77+
78+ const firstCopy = autoCopy . copy ( "git status" ) ;
79+ await expect ( autoCopy . copy ( "git status" ) ) . resolves . toBe ( false ) ;
80+ expect ( writeText ) . toHaveBeenCalledOnce ( ) ;
81+
82+ finishWrite ?.( true ) ;
83+ await expect ( firstCopy ) . resolves . toBe ( true ) ;
84+ expect ( onCopied ) . not . toHaveBeenCalled ( ) ;
85+ } ) ;
86+
87+ it ( "keeps a successful explicit copy from immediately auto-copying the same selection" , async ( ) => {
88+ const writeText = vi . fn ( ) . mockResolvedValue ( true ) ;
89+ const autoCopy = createTerminalSelectionAutoCopy ( {
90+ readSettings : ( ) => ( { enabled : true , showToast : true } ) ,
91+ writeText,
92+ onCopied : vi . fn ( ) ,
93+ } ) ;
94+
95+ autoCopy . rememberCopied ( "git status" ) ;
96+
97+ await expect ( autoCopy . copy ( "git status" ) ) . resolves . toBe ( false ) ;
98+ expect ( writeText ) . not . toHaveBeenCalled ( ) ;
99+ } ) ;
100+ } ) ;
101+
12102function mouseUp ( overrides : Partial < MouseEvent > = { } ) : MouseEvent {
13103 return { button : 0 , ctrlKey : false , metaKey : false , altKey : false , ...overrides } as MouseEvent ;
14104}
@@ -67,12 +157,23 @@ class FakeElement {
67157 }
68158}
69159
70- function selectionOf ( text : string , startContainer : unknown , endContainer : unknown ) {
160+ function selectionOf (
161+ text : string ,
162+ startContainer : unknown ,
163+ endContainer : unknown ,
164+ options : { crossesExcludedContent ?: boolean } = { } ,
165+ ) {
71166 return {
72167 isCollapsed : false ,
73168 rangeCount : 1 ,
74169 toString : ( ) => text ,
75- getRangeAt : ( ) => ( { startContainer, endContainer } ) ,
170+ getRangeAt : ( ) => ( {
171+ startContainer,
172+ endContainer,
173+ cloneContents : ( ) => ( {
174+ querySelector : ( ) => ( options . crossesExcludedContent ? new FakeElement ( ) : null ) ,
175+ } ) ,
176+ } ) ,
76177 } as unknown as Selection ;
77178}
78179
@@ -118,6 +219,10 @@ describe("normalizeTerminalSelectionText", () => {
118219 expect ( normalizeTerminalSelectionText ( "git status" ) ) . toBe ( "git status" ) ;
119220 } ) ;
120221
222+ it ( "normalizes line endings and trims blank boundary lines" , ( ) => {
223+ expect ( normalizeTerminalSelectionText ( "\r\n\r\ngit status\r\n\r\n" ) ) . toBe ( "git status" ) ;
224+ } ) ;
225+
121226 it ( "rejects empty and blank-line-only selections" , ( ) => {
122227 expect ( normalizeTerminalSelectionText ( "" ) ) . toBeNull ( ) ;
123228 expect ( normalizeTerminalSelectionText ( "\n\n" ) ) . toBeNull ( ) ;
@@ -128,19 +233,13 @@ describe("normalizeTerminalSelectionText", () => {
128233describe ( "dom selection snapshots" , ( ) => {
129234 it ( "snapshots null for missing or collapsed selections" , ( ) => {
130235 expect ( snapshotDomSelection ( null ) ) . toBeNull ( ) ;
131- expect (
132- snapshotDomSelection ( { isCollapsed : true } as unknown as Selection ) ,
133- ) . toBeNull ( ) ;
236+ expect ( snapshotDomSelection ( { isCollapsed : true } as unknown as Selection ) ) . toBeNull ( ) ;
134237 } ) ;
135238
136239 it ( "treats an unchanged selection across a click as the same" , ( ) => {
137240 const node = textNode ( "hello" ) ;
138- const before = snapshotDomSelection (
139- selectionWithAnchors ( "hello" , node , 0 , node , 5 ) ,
140- ) ;
141- const after = snapshotDomSelection (
142- selectionWithAnchors ( "hello" , node , 0 , node , 5 ) ,
143- ) ;
241+ const before = snapshotDomSelection ( selectionWithAnchors ( "hello" , node , 0 , node , 5 ) ) ;
242+ const after = snapshotDomSelection ( selectionWithAnchors ( "hello" , node , 0 , node , 5 ) ) ;
144243 expect ( sameDomSelectionSnapshot ( before , after ) ) . toBe ( true ) ;
145244 } ) ;
146245
@@ -223,6 +322,20 @@ describe("getCopyableDomSelectionText", () => {
223322 }
224323 } ) ;
225324
325+ it ( "rejects a range that crosses opted-out content between ordinary endpoints" , ( ) => {
326+ const container = new FakeElement ( ) ;
327+ const message = new FakeElement ( "DIV" , "plain" , container ) ;
328+ const start = textNode ( "before" , message ) ;
329+ const end = textNode ( "after" , message ) ;
330+
331+ expect (
332+ getCopyableDomSelectionText (
333+ selectionOf ( "before private after" , start , end , { crossesExcludedContent : true } ) ,
334+ container as unknown as HTMLElement ,
335+ ) ,
336+ ) . toBeNull ( ) ;
337+ } ) ;
338+
226339 it ( "rejects collapsed, multi-range, and whitespace-only selections" , ( ) => {
227340 const container = new FakeElement ( ) ;
228341 const node = textNode ( "hi" , new FakeElement ( "DIV" , "plain" , container ) ) ;
0 commit comments