Convert tests to ESM

GitOrigin-RevId: 20585e01dee90e691476a0d47fd5c63b0412e4a6
This commit is contained in:
Andrew Rumble
2025-10-17 10:50:36 +01:00
committed by Copybot
parent f02f6475ac
commit b7c883ac38
37 changed files with 9076 additions and 8299 deletions
@@ -1,19 +1,22 @@
const { expect } = require('chai')
const sinon = require('sinon')
import { vi, expect } from 'vitest'
import sinon from 'sinon'
import Errors from '../../../../app/src/Features/Errors/Errors.js'
const modulePath = '../../../../app/src/Features/Project/ProjectEntityHandler'
const SandboxedModule = require('sandboxed-module')
const Errors = require('../../../../app/src/Features/Errors/Errors')
vi.mock('../../../../app/src/Features/Errors/Errors.js', () =>
vi.importActual('../../../../app/src/Features/Errors/Errors.js')
)
describe('ProjectEntityHandler', function () {
const projectId = '4eecb1c1bffa66588e0000a1'
const docId = '4eecb1c1bffa66588e0000a2'
beforeEach(function () {
this.TpdsUpdateSender = {
beforeEach(async function (ctx) {
ctx.TpdsUpdateSender = {
addDoc: sinon.stub().callsArg(1),
addFile: sinon.stub().callsArg(1),
}
this.ProjectModel = class Project {
ctx.ProjectModel = class Project {
constructor(options) {
this._id = projectId
this.name = 'project_name_here'
@@ -21,59 +24,77 @@ describe('ProjectEntityHandler', function () {
this.rootFolder = [this.rootFolder]
}
}
this.project = new this.ProjectModel()
ctx.project = new ctx.ProjectModel()
this.ProjectLocator = { findElement: sinon.stub() }
this.DocumentUpdaterHandler = {
ctx.ProjectLocator = { findElement: sinon.stub() }
ctx.DocumentUpdaterHandler = {
updateProjectStructure: sinon.stub().yields(),
}
this.callback = sinon.stub()
ctx.callback = sinon.stub()
this.ProjectEntityHandler = SandboxedModule.require(modulePath, {
requires: {
'../Docstore/DocstoreManager': (this.DocstoreManager = {
promises: {},
}),
'../../Features/DocumentUpdater/DocumentUpdaterHandler':
this.DocumentUpdaterHandler,
'../../models/Project': {
Project: this.ProjectModel,
},
'./ProjectLocator': this.ProjectLocator,
'./ProjectGetter': (this.ProjectGetter = { promises: {} }),
'../ThirdPartyDataStore/TpdsUpdateSender': this.TpdsUpdateSender,
},
})
vi.doMock('../../../../app/src/Features/Docstore/DocstoreManager', () => ({
default: (ctx.DocstoreManager = {
promises: {},
}),
}))
vi.doMock(
'../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler',
() => ({
default: ctx.DocumentUpdaterHandler,
})
)
vi.doMock('../../../../app/src/models/Project', () => ({
Project: ctx.ProjectModel,
}))
vi.doMock('../../../../app/src/Features/Project/ProjectLocator', () => ({
default: ctx.ProjectLocator,
}))
vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
default: (ctx.ProjectGetter = { promises: {} }),
}))
vi.doMock(
'../../../../app/src/Features/ThirdPartyDataStore/TpdsUpdateSender',
() => ({
default: ctx.TpdsUpdateSender,
})
)
ctx.ProjectEntityHandler = (await import(modulePath)).default
})
describe('getting folders, docs and files', function () {
beforeEach(function () {
this.project.rootFolder = [
beforeEach(function (ctx) {
ctx.project.rootFolder = [
{
docs: [
(this.doc1 = {
(ctx.doc1 = {
name: 'doc1',
_id: 'doc1_id',
}),
],
fileRefs: [
(this.file1 = {
(ctx.file1 = {
rev: 1,
_id: 'file1_id',
name: 'file1',
}),
],
folders: [
(this.folder1 = {
(ctx.folder1 = {
name: 'folder1',
docs: [
(this.doc2 = {
(ctx.doc2 = {
name: 'doc2',
_id: 'doc2_id',
}),
],
fileRefs: [
(this.file2 = {
(ctx.file2 = {
rev: 2,
name: 'file2',
_id: 'file2_id',
@@ -84,54 +105,54 @@ describe('ProjectEntityHandler', function () {
],
},
]
this.ProjectGetter.promises.getProjectWithoutDocLines = sinon
ctx.ProjectGetter.promises.getProjectWithoutDocLines = sinon
.stub()
.resolves(this.project)
.resolves(ctx.project)
})
describe('getAllDocs', function () {
let fetchedDocs
beforeEach(async function () {
this.docs = [
beforeEach(async function (ctx) {
ctx.docs = [
{
_id: this.doc1._id,
lines: (this.lines1 = ['one']),
rev: (this.rev1 = 1),
_id: ctx.doc1._id,
lines: (ctx.lines1 = ['one']),
rev: (ctx.rev1 = 1),
},
{
_id: this.doc2._id,
lines: (this.lines2 = ['two']),
rev: (this.rev2 = 2),
_id: ctx.doc2._id,
lines: (ctx.lines2 = ['two']),
rev: (ctx.rev2 = 2),
},
]
this.DocstoreManager.promises.getAllDocs = sinon
ctx.DocstoreManager.promises.getAllDocs = sinon
.stub()
.resolves(this.docs)
.resolves(ctx.docs)
fetchedDocs =
await this.ProjectEntityHandler.promises.getAllDocs(projectId)
await ctx.ProjectEntityHandler.promises.getAllDocs(projectId)
})
it('should get the doc lines and rev from the docstore', function () {
this.DocstoreManager.promises.getAllDocs
it('should get the doc lines and rev from the docstore', function (ctx) {
ctx.DocstoreManager.promises.getAllDocs
.calledWith(projectId)
.should.equal(true)
})
it('should call the callback with the docs with the lines and rev included', function () {
it('should call the callback with the docs with the lines and rev included', function (ctx) {
expect(fetchedDocs).to.deep.equal({
'/doc1': {
_id: this.doc1._id,
lines: this.lines1,
name: this.doc1.name,
rev: this.rev1,
folder: this.project.rootFolder[0],
_id: ctx.doc1._id,
lines: ctx.lines1,
name: ctx.doc1.name,
rev: ctx.rev1,
folder: ctx.project.rootFolder[0],
},
'/folder1/doc2': {
_id: this.doc2._id,
lines: this.lines2,
name: this.doc2.name,
rev: this.rev2,
folder: this.folder1,
_id: ctx.doc2._id,
lines: ctx.lines2,
name: ctx.doc2.name,
rev: ctx.rev2,
folder: ctx.folder1,
},
})
})
@@ -139,85 +160,85 @@ describe('ProjectEntityHandler', function () {
describe('getAllFiles', function () {
let allFiles
beforeEach(async function () {
this.callback = sinon.stub()
allFiles = await this.ProjectEntityHandler.promises.getAllFiles(
beforeEach(async function (ctx) {
ctx.callback = sinon.stub()
allFiles = await ctx.ProjectEntityHandler.promises.getAllFiles(
projectId,
this.callback
ctx.callback
)
})
it('should call the callback with the files', function () {
it('should call the callback with the files', function (ctx) {
expect(allFiles).to.deep.equal({
'/file1': { ...this.file1, folder: this.project.rootFolder[0] },
'/folder1/file2': { ...this.file2, folder: this.folder1 },
'/file1': { ...ctx.file1, folder: ctx.project.rootFolder[0] },
'/folder1/file2': { ...ctx.file2, folder: ctx.folder1 },
})
})
})
describe('getAllDocPathsFromProject', function () {
beforeEach(function () {
this.docs = [
beforeEach(function (ctx) {
ctx.docs = [
{
_id: this.doc1._id,
lines: (this.lines1 = ['one']),
rev: (this.rev1 = 1),
_id: ctx.doc1._id,
lines: (ctx.lines1 = ['one']),
rev: (ctx.rev1 = 1),
},
{
_id: this.doc2._id,
lines: (this.lines2 = ['two']),
rev: (this.rev2 = 2),
_id: ctx.doc2._id,
lines: (ctx.lines2 = ['two']),
rev: (ctx.rev2 = 2),
},
]
})
it('should call the callback with the path for each docId', function () {
it('should call the callback with the path for each docId', function (ctx) {
const expected = {
[this.doc1._id]: `/${this.doc1.name}`,
[this.doc2._id]: `/folder1/${this.doc2.name}`,
[ctx.doc1._id]: `/${ctx.doc1.name}`,
[ctx.doc2._id]: `/folder1/${ctx.doc2.name}`,
}
expect(
this.ProjectEntityHandler.getAllDocPathsFromProject(
this.project,
this.callback
ctx.ProjectEntityHandler.getAllDocPathsFromProject(
ctx.project,
ctx.callback
)
).to.deep.equal(expected)
})
})
describe('getDocPathByProjectIdAndDocId', function () {
it('should call the callback with the path for an existing doc id at the root level', async function () {
it('should call the callback with the path for an existing doc id at the root level', async function (ctx) {
const path =
await this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
await ctx.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
projectId,
this.doc1._id
ctx.doc1._id
)
expect(path).to.deep.equal(`/${this.doc1.name}`)
expect(path).to.deep.equal(`/${ctx.doc1.name}`)
})
it('should call the callback with the path for an existing doc id nested within a folder', async function () {
it('should call the callback with the path for an existing doc id nested within a folder', async function (ctx) {
const path =
await this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
await ctx.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
projectId,
this.doc2._id
ctx.doc2._id
)
expect(path).to.deep.equal(`/folder1/${this.doc2.name}`)
expect(path).to.deep.equal(`/folder1/${ctx.doc2.name}`)
})
it('should call the callback with a NotFoundError for a non-existing doc', async function () {
it('should call the callback with a NotFoundError for a non-existing doc', async function (ctx) {
await expect(
this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
ctx.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
projectId,
'non-existing-id'
)
).to.be.rejectedWith(Errors.NotFoundError)
})
it('should call the callback with a NotFoundError for an existing file', async function () {
it('should call the callback with a NotFoundError for an existing file', async function (ctx) {
await expect(
this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
ctx.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
projectId,
this.file1._id
ctx.file1._id
)
).to.be.rejectedWith(Errors.NotFoundError)
})
@@ -225,66 +246,66 @@ describe('ProjectEntityHandler', function () {
describe('_getAllFolders', async function () {
let folders
beforeEach(async function () {
this.callback = sinon.stub()
beforeEach(async function (ctx) {
ctx.callback = sinon.stub()
folders =
await this.ProjectEntityHandler.promises._getAllFolders(projectId)
await ctx.ProjectEntityHandler.promises._getAllFolders(projectId)
})
it('should get the project without the docs lines', function () {
this.ProjectGetter.promises.getProjectWithoutDocLines
it('should get the project without the docs lines', function (ctx) {
ctx.ProjectGetter.promises.getProjectWithoutDocLines
.calledWith(projectId)
.should.equal(true)
})
it('should call the callback with the folders', function () {
it('should call the callback with the folders', function (ctx) {
expect(folders).to.deep.equal([
{ path: '/', folder: this.project.rootFolder[0] },
{ path: '/folder1', folder: this.folder1 },
{ path: '/', folder: ctx.project.rootFolder[0] },
{ path: '/folder1', folder: ctx.folder1 },
])
})
})
describe('_getAllFoldersFromProject', function () {
it('should return the folders', function () {
it('should return the folders', function (ctx) {
expect(
this.ProjectEntityHandler._getAllFoldersFromProject(this.project)
ctx.ProjectEntityHandler._getAllFoldersFromProject(ctx.project)
).to.deep.equal([
{ path: '/', folder: this.project.rootFolder[0] },
{ path: '/folder1', folder: this.folder1 },
{ path: '/', folder: ctx.project.rootFolder[0] },
{ path: '/folder1', folder: ctx.folder1 },
])
})
})
})
describe('with an invalid file tree', function () {
beforeEach(function () {
this.project.rootFolder = [
beforeEach(function (ctx) {
ctx.project.rootFolder = [
{
docs: [
(this.doc1 = {
(ctx.doc1 = {
name: null, // invalid doc name
_id: 'doc1_id',
}),
],
fileRefs: [
(this.file1 = {
(ctx.file1 = {
rev: 1,
_id: 'file1_id',
name: null, // invalid file name
}),
],
folders: [
(this.folder1 = {
(ctx.folder1 = {
name: null, // invalid folder name
docs: [
(this.doc2 = {
(ctx.doc2 = {
name: 'doc2',
_id: 'doc2_id',
}),
],
fileRefs: [
(this.file2 = {
(ctx.file2 = {
rev: 2,
name: 'file2',
_id: 'file2_id',
@@ -296,107 +317,107 @@ describe('ProjectEntityHandler', function () {
],
},
]
this.ProjectGetter.promises.getProjectWithoutDocLines = sinon
ctx.ProjectGetter.promises.getProjectWithoutDocLines = sinon
.stub()
.resolves(this.project)
.resolves(ctx.project)
})
describe('getAllDocs', function () {
beforeEach(async function () {
this.docs = [
beforeEach(async function (ctx) {
ctx.docs = [
{
_id: this.doc1._id,
lines: (this.lines1 = ['one']),
rev: (this.rev1 = 1),
_id: ctx.doc1._id,
lines: (ctx.lines1 = ['one']),
rev: (ctx.rev1 = 1),
},
{
_id: this.doc2._id,
lines: (this.lines2 = ['two']),
rev: (this.rev2 = 2),
_id: ctx.doc2._id,
lines: (ctx.lines2 = ['two']),
rev: (ctx.rev2 = 2),
},
]
this.DocstoreManager.promises.getAllDocs = sinon
ctx.DocstoreManager.promises.getAllDocs = sinon
.stub()
.resolves(this.docs)
.resolves(ctx.docs)
})
it('should call the callback with an error', async function () {
await expect(this.ProjectEntityHandler.promises.getAllDocs(projectId))
.to.be.rejected
it('should call the callback with an error', async function (ctx) {
await expect(ctx.ProjectEntityHandler.promises.getAllDocs(projectId)).to
.be.rejected
})
})
describe('getAllFiles', function () {
it('should call the callback with and error', async function () {
await expect(this.ProjectEntityHandler.promises.getAllFiles(projectId))
it('should call the callback with and error', async function (ctx) {
await expect(ctx.ProjectEntityHandler.promises.getAllFiles(projectId))
.to.be.rejected
})
})
describe('getDocPathByProjectIdAndDocId', function () {
it('should call the callback with an error for an existing doc id at the root level', async function () {
it('should call the callback with an error for an existing doc id at the root level', async function (ctx) {
await expect(
this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
ctx.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
projectId,
this.doc1._id
ctx.doc1._id
)
).to.be.rejectedWith(Error)
})
it('should call the callback with an error for an existing doc id nested within a folder', async function () {
it('should call the callback with an error for an existing doc id nested within a folder', async function (ctx) {
await expect(
this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
ctx.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
projectId,
this.doc2._id
ctx.doc2._id
)
).to.be.rejectedWith(Error)
})
it('should call the callback with an error for a non-existing doc', async function () {
it('should call the callback with an error for a non-existing doc', async function (ctx) {
await expect(
this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
ctx.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
projectId,
'non-existing-id'
)
).to.be.rejectedWith(Error)
})
it('should call the callback with an error for an existing file', async function () {
it('should call the callback with an error for an existing file', async function (ctx) {
await expect(
this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
ctx.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
projectId,
this.file1._id
ctx.file1._id
)
).to.be.rejectedWith(Error)
})
})
describe('_getAllFolders', function () {
it('should call the callback with an error', async function () {
it('should call the callback with an error', async function (ctx) {
await expect(
this.ProjectEntityHandler.promises._getAllFolders(projectId)
ctx.ProjectEntityHandler.promises._getAllFolders(projectId)
).to.be.rejected
})
})
describe('getAllEntities', function () {
beforeEach(function () {
this.ProjectGetter.promises.getProject = sinon
beforeEach(function (ctx) {
ctx.ProjectGetter.promises.getProject = sinon
.stub()
.resolves(this.project)
.resolves(ctx.project)
})
it('should call the callback with an error', async function () {
it('should call the callback with an error', async function (ctx) {
await expect(
this.ProjectEntityHandler.promises.getAllEntities(projectId)
ctx.ProjectEntityHandler.promises.getAllEntities(projectId)
).to.be.rejected
})
})
describe('getAllDocPathsFromProjectById', function () {
it('should call the callback with an error', async function () {
it('should call the callback with an error', async function (ctx) {
await expect(
this.ProjectEntityHandler.promises.getAllDocPathsFromProjectById(
ctx.ProjectEntityHandler.promises.getAllDocPathsFromProjectById(
projectId
)
).to.be.rejected
@@ -404,11 +425,11 @@ describe('ProjectEntityHandler', function () {
})
describe('getDocPathFromProjectByDocId', function () {
it('should call the callback with an error', async function () {
it('should call the callback with an error', async function (ctx) {
await expect(
this.ProjectEntityHandler.promises.getDocPathFromProjectByDocId(
ctx.ProjectEntityHandler.promises.getDocPathFromProjectByDocId(
projectId,
this.doc1._id
ctx.doc1._id
)
).to.be.rejected
})
@@ -416,26 +437,26 @@ describe('ProjectEntityHandler', function () {
})
describe('getDoc', function () {
beforeEach(function () {
this.lines = ['mock', 'doc', 'lines']
this.rev = 5
this.version = 42
this.ranges = { mock: 'ranges' }
this.callback = sinon.stub()
this.DocstoreManager.promises.getDoc = sinon.stub().resolves({
lines: this.lines,
rev: this.rev,
version: this.version,
ranges: this.ranges,
beforeEach(function (ctx) {
ctx.lines = ['mock', 'doc', 'lines']
ctx.rev = 5
ctx.version = 42
ctx.ranges = { mock: 'ranges' }
ctx.callback = sinon.stub()
ctx.DocstoreManager.promises.getDoc = sinon.stub().resolves({
lines: ctx.lines,
rev: ctx.rev,
version: ctx.version,
ranges: ctx.ranges,
})
})
it('should call the callback with the lines, version and rev', async function () {
const doc = await this.ProjectEntityHandler.promises.getDoc(
it('should call the callback with the lines, version and rev', async function (ctx) {
const doc = await ctx.ProjectEntityHandler.promises.getDoc(
projectId,
docId
)
this.DocstoreManager.promises.getDoc
ctx.DocstoreManager.promises.getDoc
.calledWith(projectId, docId)
.should.equal(true)
expect(doc).to.exist
@@ -445,32 +466,32 @@ describe('ProjectEntityHandler', function () {
describe('promises.getDoc', function () {
let result
beforeEach(async function () {
this.lines = ['mock', 'doc', 'lines']
this.rev = 5
this.version = 42
this.ranges = { mock: 'ranges' }
beforeEach(async function (ctx) {
ctx.lines = ['mock', 'doc', 'lines']
ctx.rev = 5
ctx.version = 42
ctx.ranges = { mock: 'ranges' }
this.DocstoreManager.promises.getDoc = sinon.stub().resolves({
lines: this.lines,
rev: this.rev,
version: this.version,
ranges: this.ranges,
ctx.DocstoreManager.promises.getDoc = sinon.stub().resolves({
lines: ctx.lines,
rev: ctx.rev,
version: ctx.version,
ranges: ctx.ranges,
})
result = await this.ProjectEntityHandler.promises.getDoc(projectId, docId)
result = await ctx.ProjectEntityHandler.promises.getDoc(projectId, docId)
})
it('should call the docstore', function () {
this.DocstoreManager.promises.getDoc
it('should call the docstore', function (ctx) {
ctx.DocstoreManager.promises.getDoc
.calledWith(projectId, docId)
.should.equal(true)
})
it('should return the lines, rev, version and ranges', function () {
expect(result.lines).to.equal(this.lines)
expect(result.rev).to.equal(this.rev)
expect(result.version).to.equal(this.version)
expect(result.ranges).to.equal(this.ranges)
it('should return the lines, rev, version and ranges', function (ctx) {
expect(result.lines).to.equal(ctx.lines)
expect(result.rev).to.equal(ctx.rev)
expect(result.version).to.equal(ctx.version)
expect(result.ranges).to.equal(ctx.ranges)
})
})
})