<commit_before>sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/UpdatesManager.js"
SandboxedModule = require('sandboxed-module')

describe "UpdatesManager", ->
	beforeEach ->
		@UpdatesManager = SandboxedModule.require modulePath, requires:
			"./UpdateCompressor": @UpdateCompressor = {}
			"./MongoManager" : @MongoManager = {}
			"./PackManager" : @PackManager = {}
			"./RedisManager" : @RedisManager = {}
			"./LockManager"  : @LockManager = {}
			"./WebApiManager": @WebApiManager = {}
			"./UpdateTrimmer": @UpdateTrimmer = {}
			"./DocArchiveManager": @DocArchiveManager = {}
			"logger-sharelatex": { log: sinon.stub(), error: sinon.stub() }
			"settings-sharelatex":
				redis: lock: key_schema:
					historyLock: ({doc_id}) -> "HistoryLock:#{doc_id}"
		@doc_id = "doc-id-123"
		@project_id = "project-id-123"
		@callback = sinon.stub()
		@temporary = "temp-mock"

	describe "compressAndSaveRawUpdates", ->
		describe "when there are no raw ops", ->
			beforeEach ->
				@MongoManager.peekLastCompressedUpdate = sinon.stub()
				@UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, [], @temporary, @callback

			it "should not need to access the database", ->
				@MongoManager.peekLastCompressedUpdate.called.should.equal false

			it "should call the callback", ->
				@callback.called.should.equal true

		describe "when there is no compressed history to begin with", ->
			beforeEach ->
				@rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
				@compressedUpdates = [ { v: 13, op: "compressed-op-12" } ]

				@MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null)
				@PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
				@UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)
				@UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback

			it "should look at the last compressed op", ->
				@MongoManager.peekLastCompressedUpdate
					.calledWith(@doc_id)
					.should.equal true
			
			it "should save the compressed ops as a pack", ->
				@PackManager.insertCompressedUpdates
					.calledWith(@project_id, @doc_id, null, @compressedUpdates, @temporary)
					.should.equal true

			it "should call the callback", ->
				@callback.called.should.equal true

		describe "when the raw ops need appending to existing history", ->
			beforeEach ->
				@lastCompressedUpdate = { v: 11, op: "compressed-op-11" }
				@compressedUpdates = [ { v: 12, op: "compressed-op-11+12" }, { v: 13, op: "compressed-op-12" } ]

				@MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @lastCompressedUpdate, @lastCompressedUpdate.v)
				@PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
				@UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)

			describe "when the raw ops start where the existing history ends", ->
				beforeEach ->
					@rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
					@UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback

				it "should look at the last compressed op", ->
					@MongoManager.peekLastCompressedUpdate
						.calledWith(@doc_id)
						.should.equal true
				
				it "should compress the raw ops", ->
					@UpdateCompressor.compressRawUpdates
						.calledWith(null, @rawUpdates)
						.should.equal true

				it "should save the new compressed ops into a pack", ->
					@PackManager.insertCompressedUpdates
						.calledWith(@project_id, @doc_id, @lastCompressedUpdate, @compressedUpdates, @temporary)
						.should.equal true

				it "should call the callback", ->
					@callback.called.should.equal true

			describe "when the raw ops start where the existing history ends and the history is in a pack", ->
				beforeEach ->
					@lastCompressedUpdate = {pack: [{ v: 11, op: "compressed-op-11" }], v:11}
					@rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
					@MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @lastCompressedUpdate, @lastCompressedUpdate.v)
					@UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback

				it "should look at the last compressed op", ->
					@MongoManager.peekLastCompressedUpdate
						.calledWith(@doc_id)
						.should.equal true

				# FIXME: Broken test, was hidden by an api mistake

				# it "should defer the compression of raw ops until they are written in a new pack", ->
				# 	console.log @UpdateCompressor.compressRawUpdates.called
				# 	@UpdateCompressor.compressRawUpdates
				# 		.called.should.not.equal true

				it "should save the new compressed ops into a pack", ->
					@PackManager.insertCompressedUpdates
						.calledWith(@project_id, @doc_id, @lastCompressedUpdate, @compressedUpdates, @temporary)
						.should.equal true

				it "should call the callback", ->
					@callback.called.should.equal true

			describe "when some raw ops are passed that have already been compressed", ->
				beforeEach ->
					@rawUpdates = [{ v: 10, op: "mock-op-10" }, { v: 11, op: "mock-op-11"}, { v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
<commit_msg>Restore a broken test that was commented out temporarily<commit_after>sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/UpdatesManager.js"
SandboxedModule = require('sandboxed-module')

describe "UpdatesManager", ->
	beforeEach ->
		@UpdatesManager = SandboxedModule.require modulePath, requires:
			"./UpdateCompressor": @UpdateCompressor = {}
			"./MongoManager" : @MongoManager = {}
			"./PackManager" : @PackManager = {}
			"./RedisManager" : @RedisManager = {}
			"./LockManager"  : @LockManager = {}
			"./WebApiManager": @WebApiManager = {}
			"./UpdateTrimmer": @UpdateTrimmer = {}
			"./DocArchiveManager": @DocArchiveManager = {}
			"logger-sharelatex": { log: sinon.stub(), error: sinon.stub() }
			"settings-sharelatex":
				redis: lock: key_schema:
					historyLock: ({doc_id}) -> "HistoryLock:#{doc_id}"
		@doc_id = "doc-id-123"
		@project_id = "project-id-123"
		@callback = sinon.stub()
		@temporary = "temp-mock"

	describe "compressAndSaveRawUpdates", ->
		describe "when there are no raw ops", ->
			beforeEach ->
				@MongoManager.peekLastCompressedUpdate = sinon.stub()
				@UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, [], @temporary, @callback

			it "should not need to access the database", ->
				@MongoManager.peekLastCompressedUpdate.called.should.equal false

			it "should call the callback", ->
				@callback.called.should.equal true

		describe "when there is no compressed history to begin with", ->
			beforeEach ->
				@rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
				@compressedUpdates = [ { v: 13, op: "compressed-op-12" } ]

				@MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null)
				@PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
				@UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)
				@UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback

			it "should look at the last compressed op", ->
				@MongoManager.peekLastCompressedUpdate
					.calledWith(@doc_id)
					.should.equal true
			
			it "should save the compressed ops as a pack", ->
				@PackManager.insertCompressedUpdates
					.calledWith(@project_id, @doc_id, null, @compressedUpdates, @temporary)
					.should.equal true

			it "should call the callback", ->
				@callback.called.should.equal true

		describe "when the raw ops need appending to existing history", ->
			beforeEach ->
				@lastCompressedUpdate = { v: 11, op: "compressed-op-11" }
				@compressedUpdates = [ { v: 12, op: "compressed-op-11+12" }, { v: 13, op: "compressed-op-12" } ]

				@MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @lastCompressedUpdate, @lastCompressedUpdate.v)
				@PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
				@UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)

			describe "when the raw ops start where the existing history ends", ->
				beforeEach ->
					@rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
					@UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback

				it "should look at the last compressed op", ->
					@MongoManager.peekLastCompressedUpdate
						.calledWith(@doc_id)
						.should.equal true
				
				it "should compress the raw ops", ->
					@UpdateCompressor.compressRawUpdates
						.calledWith(null, @rawUpdates)
						.should.equal true

				it "should save the new compressed ops into a pack", ->
					@PackManager.insertCompressedUpdates
						.calledWith(@project_id, @doc_id, @lastCompressedUpdate, @compressedUpdates, @temporary)
						.should.equal true

				it "should call the callback", ->
					@callback.called.should.equal true

			describe "when the raw ops start where the existing history ends and the history is in a pack", ->
				beforeEach ->
					@lastCompressedUpdate = {pack: [{ v: 11, op: "compressed-op-11" }], v:11}
					@rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
					@MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @lastCompressedUpdate, @lastCompressedUpdate.v)
					@UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback

				it "should look at the last compressed op", ->
					@MongoManager.peekLastCompressedUpdate
						.calledWith(@doc_id)
						.should.equal true

				# FIXME: Broken test, was hidden by an api mistake
				it "should defer the compression of raw ops until they are written in a new pack", ->
					console.log @UpdateCompressor.compressRawUpdates.called
					@UpdateCompressor.compressRawUpdates
						.called.should.not.equal true

				it "should save the new compressed ops into a pack", ->
					@PackManager.insertCompressedUpdates
						.calledWith(@project_id, @doc_id, @lastCompressedUpdate, @compressedUpdates, @temporary)
						.should.equal true

				it "should call the callback", ->
					@callback.called.should.equal true

			describe "when some raw ops are passed that have already been compressed", ->
				beforeEach ->
					@rawUpdates = [{ v: 10, op: "mock-op-10" }, { v: 11, op: "mock-op-11"}, { v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]

{'fraction_non_alphanumeric': 0.13549115543846443, 'fraction_numerical': 0.014113662024840046, 'mean_word_length': 3.7156166814551908, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2657210', 'n_tokens_mistral': 4158, 'n_tokens_neox': 3421, 'n_words': 840}