1namespace ts.projectSystem { 2 describe("unittests:: tsserver:: applyChangesToOpenFiles", () => { 3 const configFile: File = { 4 path: "/a/b/tsconfig.json", 5 content: "{}" 6 }; 7 const file3: File = { 8 path: "/a/b/file3.ts", 9 content: "let xyz = 1;" 10 }; 11 const app: File = { 12 path: "/a/b/app.ts", 13 content: "let z = 1;" 14 }; 15 16 function fileContentWithComment(file: File) { 17 return `// some copy right notice 18${file.content}`; 19 } 20 21 function verifyText(service: server.ProjectService, file: string, expected: string) { 22 const info = service.getScriptInfo(file)!; 23 const snap = info.getSnapshot(); 24 // Verified applied in reverse order 25 assert.equal(snap.getText(0, snap.getLength()), expected, `Text of changed file: ${file}`); 26 } 27 28 function verifyProjectVersion(project: server.Project, expected: number) { 29 assert.equal(Number(project.getProjectVersion()), expected); 30 } 31 32 interface Verify { 33 applyChangesToOpen: (session: TestSession) => void; 34 openFile1Again: (session: TestSession) => void; 35 } 36 function verify({ applyChangesToOpen, openFile1Again }: Verify) { 37 const host = createServerHost([app, file3, commonFile1, commonFile2, libFile, configFile]); 38 const session = createSession(host); 39 session.executeCommandSeq<protocol.OpenRequest>({ 40 command: protocol.CommandTypes.Open, 41 arguments: { file: app.path } 42 }); 43 const service = session.getProjectService(); 44 const project = service.configuredProjects.get(configFile.path)!; 45 assert.isDefined(project); 46 verifyProjectVersion(project, 1); 47 session.executeCommandSeq<protocol.OpenRequest>({ 48 command: protocol.CommandTypes.Open, 49 arguments: { 50 file: file3.path, 51 fileContent: fileContentWithComment(file3) 52 } 53 }); 54 verifyProjectVersion(project, 2); 55 56 // Verify Texts 57 verifyText(service, commonFile1.path, commonFile1.content); 58 verifyText(service, commonFile2.path, commonFile2.content); 59 verifyText(service, app.path, app.content); 60 verifyText(service, file3.path, fileContentWithComment(file3)); 61 62 // Apply changes 63 applyChangesToOpen(session); 64 65 // Verify again 66 verifyProjectVersion(project, 3); 67 // Open file contents 68 verifyText(service, commonFile1.path, fileContentWithComment(commonFile1)); 69 verifyText(service, commonFile2.path, fileContentWithComment(commonFile2)); 70 verifyText(service, app.path, "let zzz = 10;let zz = 10;let z = 1;"); 71 verifyText(service, file3.path, file3.content); 72 73 // Open file1 again 74 openFile1Again(session); 75 assert.isTrue(service.getScriptInfo(commonFile1.path)!.isScriptOpen()); 76 77 // Verify that file1 contents are changed 78 verifyProjectVersion(project, 4); 79 verifyText(service, commonFile1.path, commonFile1.content); 80 verifyText(service, commonFile2.path, fileContentWithComment(commonFile2)); 81 verifyText(service, app.path, "let zzz = 10;let zz = 10;let z = 1;"); 82 verifyText(service, file3.path, file3.content); 83 } 84 85 it("with applyChangedToOpenFiles request", () => { 86 verify({ 87 applyChangesToOpen: session => session.executeCommandSeq<protocol.ApplyChangedToOpenFilesRequest>({ 88 command: protocol.CommandTypes.ApplyChangedToOpenFiles, 89 arguments: { 90 openFiles: [ 91 { 92 fileName: commonFile1.path, 93 content: fileContentWithComment(commonFile1) 94 }, 95 { 96 fileName: commonFile2.path, 97 content: fileContentWithComment(commonFile2) 98 } 99 ], 100 changedFiles: [ 101 { 102 fileName: app.path, 103 changes: [ 104 { 105 span: { start: 0, length: 0 }, 106 newText: "let zzz = 10;" 107 }, 108 { 109 span: { start: 0, length: 0 }, 110 newText: "let zz = 10;" 111 } 112 ] 113 } 114 ], 115 closedFiles: [ 116 file3.path 117 ] 118 } 119 }), 120 openFile1Again: session => session.executeCommandSeq<protocol.ApplyChangedToOpenFilesRequest>({ 121 command: protocol.CommandTypes.ApplyChangedToOpenFiles, 122 arguments: { 123 openFiles: [{ 124 fileName: commonFile1.path, 125 content: commonFile1.content 126 }] 127 } 128 }), 129 }); 130 }); 131 132 it("with updateOpen request", () => { 133 verify({ 134 applyChangesToOpen: session => session.executeCommandSeq<protocol.UpdateOpenRequest>({ 135 command: protocol.CommandTypes.UpdateOpen, 136 arguments: { 137 openFiles: [ 138 { 139 file: commonFile1.path, 140 fileContent: fileContentWithComment(commonFile1) 141 }, 142 { 143 file: commonFile2.path, 144 fileContent: fileContentWithComment(commonFile2) 145 } 146 ], 147 changedFiles: [ 148 { 149 fileName: app.path, 150 textChanges: [ 151 { 152 start: { line: 1, offset: 1 }, 153 end: { line: 1, offset: 1 }, 154 newText: "let zzz = 10;", 155 }, 156 { 157 start: { line: 1, offset: 1 }, 158 end: { line: 1, offset: 1 }, 159 newText: "let zz = 10;", 160 } 161 ] 162 } 163 ], 164 closedFiles: [ 165 file3.path 166 ] 167 } 168 }), 169 openFile1Again: session => session.executeCommandSeq<protocol.UpdateOpenRequest>({ 170 command: protocol.CommandTypes.UpdateOpen, 171 arguments: { 172 openFiles: [{ 173 file: commonFile1.path, 174 fileContent: commonFile1.content 175 }] 176 } 177 }), 178 }); 179 }); 180 }); 181} 182