This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
/
CompileControllerTests.coffee
217 lines (192 loc) · 6.06 KB
/
CompileControllerTests.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
SandboxedModule = require('sandboxed-module')
sinon = require('sinon')
require('chai').should()
modulePath = require('path').join __dirname, '../../../app/js/CompileController'
tk = require("timekeeper")
describe "CompileController", ->
beforeEach ->
@CompileController = SandboxedModule.require modulePath, requires:
"./CompileManager": @CompileManager = {}
"./RequestParser": @RequestParser = {}
"settings-sharelatex": @Settings =
apis:
clsi:
url: "http://clsi.example.com"
"./ProjectPersistenceManager": @ProjectPersistenceManager = {}
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), err:sinon.stub(), warn: sinon.stub()}
@Settings.externalUrl = "http://www.example.com"
@req = {}
@res = {}
@next = sinon.stub()
describe "compile", ->
beforeEach ->
@req.body = {
compile: "mock-body"
}
@req.params =
project_id: @project_id = "project-id-123"
@request = {
compile: "mock-parsed-request"
}
@request_with_project_id =
compile: @request.compile
project_id: @project_id
@output_files = [{
path: "output.pdf"
type: "pdf"
build: 1234
}, {
path: "output.log"
type: "log"
build: 1234
}]
@RequestParser.parse = sinon.stub().callsArgWith(1, null, @request)
@ProjectPersistenceManager.markProjectAsJustAccessed = sinon.stub().callsArg(1)
@res.status = sinon.stub().returnsThis()
@res.send = sinon.stub()
describe "successfully", ->
beforeEach ->
@CompileManager.doCompileWithLock = sinon.stub().callsArgWith(1, null, @output_files)
@CompileController.compile @req, @res
it "should parse the request", ->
@RequestParser.parse
.calledWith(@req.body)
.should.equal true
it "should run the compile for the specified project", ->
@CompileManager.doCompileWithLock
.calledWith(@request_with_project_id)
.should.equal true
it "should mark the project as accessed", ->
@ProjectPersistenceManager.markProjectAsJustAccessed
.calledWith(@project_id)
.should.equal true
it "should return the JSON response", ->
@res.status.calledWith(200).should.equal true
@res.send
.calledWith(
compile:
status: "success"
error: null
outputFiles: @output_files.map (file) =>
url: "#{@Settings.apis.clsi.url}/project/#{@project_id}/build/#{file.build}/output/#{file.path}"
path: file.path
type: file.type
build: file.build
)
.should.equal true
describe "with an error", ->
beforeEach ->
@CompileManager.doCompileWithLock = sinon.stub().callsArgWith(1, new Error(@message = "error message"), null)
@CompileController.compile @req, @res
it "should return the JSON response with the error", ->
@res.status.calledWith(500).should.equal true
@res.send
.calledWith(
compile:
status: "error"
error: @message
outputFiles: []
)
.should.equal true
describe "when the request times out", ->
beforeEach ->
@error = new Error(@message = "container timed out")
@error.timedout = true
@CompileManager.doCompileWithLock = sinon.stub().callsArgWith(1, @error, null)
@CompileController.compile @req, @res
it "should return the JSON response with the timeout status", ->
@res.status.calledWith(200).should.equal true
@res.send
.calledWith(
compile:
status: "timedout"
error: @message
outputFiles: []
)
.should.equal true
describe "when the request returns no output files", ->
beforeEach ->
@CompileManager.doCompileWithLock = sinon.stub().callsArgWith(1, null, [])
@CompileController.compile @req, @res
it "should return the JSON response with the failure status", ->
@res.status.calledWith(200).should.equal true
@res.send
.calledWith(
compile:
error: null
status: "failure"
outputFiles: []
)
.should.equal true
describe "syncFromCode", ->
beforeEach ->
@file = "main.tex"
@line = 42
@column = 5
@project_id = "mock-project-id"
@req.params =
project_id: @project_id
@req.query =
file: @file
line: @line.toString()
column: @column.toString()
@res.send = sinon.stub()
@CompileManager.syncFromCode = sinon.stub().callsArgWith(5, null, @pdfPositions = ["mock-positions"])
@CompileController.syncFromCode @req, @res, @next
it "should find the corresponding location in the PDF", ->
@CompileManager.syncFromCode
.calledWith(@project_id, undefined, @file, @line, @column)
.should.equal true
it "should return the positions", ->
@res.send
.calledWith(JSON.stringify
pdf: @pdfPositions
)
.should.equal true
describe "syncFromPdf", ->
beforeEach ->
@page = 5
@h = 100.23
@v = 45.67
@project_id = "mock-project-id"
@req.params =
project_id: @project_id
@req.query =
page: @page.toString()
h: @h.toString()
v: @v.toString()
@res.send = sinon.stub()
@CompileManager.syncFromPdf = sinon.stub().callsArgWith(5, null, @codePositions = ["mock-positions"])
@CompileController.syncFromPdf @req, @res, @next
it "should find the corresponding location in the code", ->
@CompileManager.syncFromPdf
.calledWith(@project_id, undefined, @page, @h, @v)
.should.equal true
it "should return the positions", ->
@res.send
.calledWith(JSON.stringify
code: @codePositions
)
.should.equal true
describe "wordcount", ->
beforeEach ->
@file = "main.tex"
@project_id = "mock-project-id"
@req.params =
project_id: @project_id
@req.query =
file: @file
image: @image = "example.com/image"
@res.send = sinon.stub()
@CompileManager.wordcount = sinon.stub().callsArgWith(4, null, @texcount = ["mock-texcount"])
@CompileController.wordcount @req, @res, @next
it "should return the word count of a file", ->
@CompileManager.wordcount
.calledWith(@project_id, undefined, @file, @image)
.should.equal true
it "should return the texcount info", ->
@res.send
.calledWith(JSON.stringify
texcount: @texcount
)
.should.equal true