11const assert = require ( 'assert' )
22const fs = require ( 'fs' )
3+ const path = require ( 'path' )
34const temp = require ( 'temp' )
4- const { TextBuffer, Point } = require ( 'atom' )
5+ const { TextBuffer} = require ( 'atom' )
56const BufferBinding = require ( '../lib/buffer-binding' )
7+ const FakeBufferProxy = require ( './helpers/fake-buffer-proxy' )
68
79suite ( 'BufferBinding' , function ( ) {
810 if ( process . env . CI ) this . timeout ( process . env . TEST_TIMEOUT_IN_MS )
911
1012 test ( 'relays changes to and from the shared buffer' , ( ) => {
1113 const buffer = new TextBuffer ( 'hello\nworld' )
1214 const binding = new BufferBinding ( { buffer} )
13- const bufferProxy = new FakeBufferProxy ( binding , buffer . getText ( ) )
15+ const bufferProxy = new FakeBufferProxy ( { delegate : binding , text : buffer . getText ( ) } )
1416 binding . setBufferProxy ( bufferProxy )
1517
1618 bufferProxy . simulateRemoteTextUpdate ( [
@@ -36,7 +38,7 @@ suite('BufferBinding', function () {
3638 test ( 'does not relay empty changes to the shared buffer' , ( ) => {
3739 const buffer = new TextBuffer ( 'hello\nworld' )
3840 const binding = new BufferBinding ( { buffer} )
39- const bufferProxy = new FakeBufferProxy ( binding , buffer . getText ( ) )
41+ const bufferProxy = new FakeBufferProxy ( { delegate : binding , text : buffer . getText ( ) } )
4042 binding . setBufferProxy ( bufferProxy )
4143
4244 buffer . setTextInRange ( [ [ 0 , 0 ] , [ 0 , 0 ] ] , '' )
@@ -49,8 +51,8 @@ suite('BufferBinding', function () {
4951 // This line ensures saving works correctly even if the save function has been monkey-patched.
5052 buffer . save = ( ) => { }
5153
52- const binding = new BufferBinding ( { buffer} )
53- const bufferProxy = new FakeBufferProxy ( binding , buffer . getText ( ) )
54+ const binding = new BufferBinding ( { buffer, isHost : true } )
55+ const bufferProxy = new FakeBufferProxy ( { delegate : binding , text : buffer . getText ( ) } )
5456 binding . setBufferProxy ( bufferProxy )
5557
5658 // Calling binding.save with an in-memory buffer is ignored.
@@ -69,11 +71,34 @@ suite('BufferBinding', function () {
6971 assert . equal ( fs . readFileSync ( filePath , 'utf8' ) , 'changed' )
7072 } )
7173
74+ test ( 'relays path changes from host to guest' , async ( ) => {
75+ {
76+ const hostBuffer = new TextBuffer ( '' )
77+ const hostBinding = new BufferBinding ( { buffer : hostBuffer , isHost : true } )
78+ const hostBufferProxy = new FakeBufferProxy ( { delegate : hostBinding , text : hostBuffer . getText ( ) } )
79+ hostBinding . setBufferProxy ( hostBufferProxy )
80+
81+ await hostBuffer . saveAs ( path . join ( temp . path ( ) , 'new-filename' ) )
82+ assert ( hostBufferProxy . uri . includes ( 'new-filename' ) )
83+ }
84+
85+ {
86+ const guestBuffer = new TextBuffer ( '' )
87+ const guestBinding = new BufferBinding ( { buffer : guestBuffer , isHost : false } )
88+ const guestBufferProxy = new FakeBufferProxy ( { delegate : guestBinding , text : guestBuffer . getText ( ) } )
89+ guestBinding . setBufferProxy ( guestBufferProxy )
90+
91+ guestBufferProxy . simulateRemoteURIChange ( 'some/uri/new-filename' )
92+ assert ( guestBuffer . getPath ( ) . includes ( 'new-filename' ) )
93+ assert . equal ( guestBufferProxy . uri , 'some/uri/new-filename' )
94+ }
95+ } )
96+
7297 suite ( 'destroying the buffer' , ( ) => {
7398 test ( 'on the host, disposes the underlying buffer proxy' , ( ) => {
7499 const buffer = new TextBuffer ( '' )
75100 const binding = new BufferBinding ( { buffer, isHost : true } )
76- const bufferProxy = new FakeBufferProxy ( binding , buffer . getText ( ) )
101+ const bufferProxy = new FakeBufferProxy ( { delegate : binding , text : buffer . getText ( ) } )
77102 binding . setBufferProxy ( bufferProxy )
78103
79104 buffer . destroy ( )
@@ -83,79 +108,12 @@ suite('BufferBinding', function () {
83108 test ( 'on guests, disposes the buffer binding' , ( ) => {
84109 const buffer = new TextBuffer ( '' )
85110 const binding = new BufferBinding ( { buffer, isHost : false } )
86- const bufferProxy = new FakeBufferProxy ( binding , buffer . getText ( ) )
111+ const bufferProxy = new FakeBufferProxy ( { delegate : binding , text : buffer . getText ( ) } )
87112 binding . setBufferProxy ( bufferProxy )
88113
89114 buffer . destroy ( )
90115 assert ( binding . disposed )
91116 assert ( ! bufferProxy . disposed )
92117 } )
93118 } )
94-
95- class FakeBufferProxy {
96- constructor ( delegate , text ) {
97- this . delegate = delegate
98- this . text = text
99- this . disposed = false
100- }
101-
102- dispose ( ) {
103- this . disposed = true
104- }
105-
106- getHistory ( ) {
107- return { undoStack : [ ] , redoStack : [ ] , nextCheckpointId : 1 }
108- }
109-
110- setTextInRange ( oldStart , oldEnd , newText ) {
111- const oldStartIndex = characterIndexForPosition ( this . text , oldStart )
112- const oldEndIndex = characterIndexForPosition ( this . text , oldEnd )
113- this . text = this . text . slice ( 0 , oldStartIndex ) + newText + this . text . slice ( oldEndIndex )
114- }
115-
116- simulateRemoteTextUpdate ( changes ) {
117- assert ( changes . length > 0 , 'Must update text with at least one change' )
118-
119- for ( let i = changes . length - 1 ; i >= 0 ; i -- ) {
120- const { oldStart, oldEnd, newText} = changes [ i ]
121- this . setTextInRange ( oldStart , oldEnd , newText )
122- }
123-
124- this . delegate . updateText ( changes )
125- }
126-
127- createCheckpoint ( ) {
128- return 1
129- }
130-
131- groupChangesSinceCheckpoint ( ) {
132- return [ ]
133- }
134-
135- groupLastChanges ( ) {
136- return true
137- }
138-
139- applyGroupingInterval ( ) {
140-
141- }
142- }
143-
144- function characterIndexForPosition ( text , target ) {
145- target = Point . fromObject ( target )
146- const position = Point ( 0 , 0 )
147- let index = 0
148- while ( position . compare ( target ) < 0 && index < text . length ) {
149- if ( text [ index ] === '\n' ) {
150- position . row ++
151- position . column = 0
152- } else {
153- position . column ++
154- }
155-
156- index ++
157- }
158-
159- return index
160- }
161119} )
0 commit comments