@@ -4,7 +4,11 @@ import { networkInterfaces } from 'node:os'
44
55import { afterEach , describe , expect , it , vi } from 'vitest'
66
7- import { getNetworkAddresses , listen , resolveOpenCommand } from '../../src/dev/listen'
7+ import { copyURL , getNetworkAddresses , listen , resolveOpenCommand } from '../../src/dev/listen'
8+
9+ const writeText = vi . hoisted ( ( ) => vi . fn ( ) )
10+
11+ vi . mock ( 'tinyclip' , ( ) => ( { writeText } ) )
812
913const spawn = vi . hoisted ( ( ) => vi . fn ( ( _command : string , _args : string [ ] ) => ( { on : ( ) => ( { unref : ( ) => { } } ) } ) ) )
1014
@@ -118,3 +122,50 @@ describe('listen', () => {
118122 expect ( listener . address . port ) . toBe ( port )
119123 } )
120124} )
125+
126+ describe ( 'copyURL' , ( ) => {
127+ const platform = process . platform
128+ const env = { ...process . env }
129+
130+ afterEach ( ( ) => {
131+ Object . defineProperty ( process , 'platform' , { value : platform , configurable : true } )
132+ process . env = { ...env }
133+ vi . clearAllMocks ( )
134+ } )
135+
136+ function stubPlatform ( value : NodeJS . Platform , overrides : NodeJS . ProcessEnv = { } ) {
137+ Object . defineProperty ( process , 'platform' , { value, configurable : true } )
138+ process . env = { ...env , DISPLAY : undefined , WAYLAND_DISPLAY : undefined , WSL_DISTRO_NAME : undefined , ...overrides }
139+ }
140+
141+ it ( 'should skip copying without a display server on linux' , async ( ) => {
142+ stubPlatform ( 'linux' )
143+
144+ await copyURL ( 'http://localhost:3000/' )
145+
146+ expect ( writeText ) . not . toHaveBeenCalled ( )
147+ } )
148+
149+ it ( 'should copy when a display server is available' , async ( ) => {
150+ stubPlatform ( 'linux' , { DISPLAY : ':0' } )
151+
152+ await copyURL ( 'http://localhost:3000/' )
153+
154+ expect ( writeText ) . toHaveBeenCalledWith ( 'http://localhost:3000/' )
155+ } )
156+
157+ it ( 'should copy on platforms that do not need a display server' , async ( ) => {
158+ stubPlatform ( 'darwin' )
159+
160+ await copyURL ( 'http://localhost:3000/' )
161+
162+ expect ( writeText ) . toHaveBeenCalledWith ( 'http://localhost:3000/' )
163+ } )
164+
165+ it ( 'should warn rather than throw when copying fails' , async ( ) => {
166+ stubPlatform ( 'darwin' )
167+ writeText . mockRejectedValueOnce ( new Error ( 'no clipboard tool found' ) )
168+
169+ await expect ( copyURL ( 'http://localhost:3000/' ) ) . resolves . toBeUndefined ( )
170+ } )
171+ } )
0 commit comments