Files
speckle-server/packages/server/modules/core/tests/commits.spec.ts
T
Daniel Gak Anagrov 1f5003bd32 feat(multiregion): cron job to detect regions out of sync (#5392)
* feat: basic structure

* fix: detect regions out of sync

* feat: added sync logic

* fix: tests

* feat: merge all strategy

* fix: mr comments

* fix: updatedAt in updates

* feat: project updatedAt no longer is a hidden operation

* feat: project region sync

* feat: delete project on cross-region-move

* removal: unnecesary permisions

* fix: caching issue

* fix: saved views on project cross region move
2025-09-10 16:35:48 +01:00

483 lines
14 KiB
TypeScript

/* istanbul ignore file */
import { expect } from 'chai'
import { beforeEachContext } from '@/test/hooks'
import { createBranchAndNotifyFactory } from '@/modules/core/services/branch/management'
import cryptoRandomString from 'crypto-random-string'
import {
createBranchFactory,
getStreamBranchByNameFactory,
markCommitBranchUpdatedFactory,
getBranchByIdFactory
} from '@/modules/core/repositories/branches'
import { db } from '@/db/knex'
import {
getCommitFactory,
deleteCommitFactory,
createCommitFactory,
insertStreamCommitsFactory,
insertBranchCommitsFactory,
getCommitBranchFactory,
switchCommitBranchFactory,
updateCommitFactory,
getStreamCommitCountFactory,
legacyGetPaginatedUserCommitsPage,
legacyGetPaginatedStreamCommitsPageFactory,
getBranchCommitsTotalCountFactory,
getPaginatedBranchCommitsItemsFactory
} from '@/modules/core/repositories/commits'
import {
deleteCommitAndNotifyFactory,
createCommitByBranchIdFactory,
createCommitByBranchNameFactory,
updateCommitAndNotifyFactory
} from '@/modules/core/services/commit/management'
import {
getStreamFactory,
getCommitStreamFactory
} from '@/modules/core/repositories/streams'
import {
getObjectFactory,
storeSingleObjectIfNotFoundFactory
} from '@/modules/core/repositories/objects'
import { getEventBus } from '@/modules/shared/services/eventBus'
import {
getBranchCommitsTotalCountByNameFactory,
getPaginatedBranchCommitsItemsByNameFactory
} from '@/modules/core/services/commit/retrieval'
import { createObjectFactory } from '@/modules/core/services/objects/management'
import { ensureError } from '@speckle/shared'
import { VersionEvents } from '@/modules/core/domain/commits/events'
import type { BasicTestUser } from '@/test/authHelper'
import { createTestUser } from '@/test/authHelper'
import {
createTestStream,
type BasicTestStream
} from '@/test/speckle-helpers/streamHelper'
import { buildBasicTestProject } from '@/modules/core/tests/helpers/creation'
const getCommitStream = getCommitStreamFactory({ db })
const getStream = getStreamFactory({ db })
const createBranch = createBranchFactory({ db })
const createBranchAndNotify = createBranchAndNotifyFactory({
createBranch,
getStreamBranchByName: getStreamBranchByNameFactory({ db }),
eventEmit: getEventBus().emit
})
const getCommit = getCommitFactory({ db })
const deleteCommitAndNotify = deleteCommitAndNotifyFactory({
getCommit,
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db }),
deleteCommit: deleteCommitFactory({ db }),
emitEvent: getEventBus().emit
})
const getObject = getObjectFactory({ db })
const createCommitByBranchId = createCommitByBranchIdFactory({
createCommit: createCommitFactory({ db }),
getObject,
getBranchById: getBranchByIdFactory({ db }),
insertStreamCommits: insertStreamCommitsFactory({ db }),
insertBranchCommits: insertBranchCommitsFactory({ db }),
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db }),
emitEvent: getEventBus().emit
})
const createCommitByBranchName = createCommitByBranchNameFactory({
createCommitByBranchId,
getStreamBranchByName: getStreamBranchByNameFactory({ db }),
getBranchById: getBranchByIdFactory({ db })
})
const updateCommitAndNotify = updateCommitAndNotifyFactory({
getCommit: getCommitFactory({ db }),
getStream,
getCommitStream,
getStreamBranchByName: getStreamBranchByNameFactory({ db }),
getCommitBranch: getCommitBranchFactory({ db }),
switchCommitBranch: switchCommitBranchFactory({ db }),
updateCommit: updateCommitFactory({ db }),
emitEvent: getEventBus().emit,
markCommitBranchUpdated: markCommitBranchUpdatedFactory({ db })
})
const getStreamCommitCount = getStreamCommitCountFactory({ db })
const getCommitsByUserId = legacyGetPaginatedUserCommitsPage({ db })
const getCommitsByStreamId = legacyGetPaginatedStreamCommitsPageFactory({ db })
const getCommitsTotalCountByBranchName = getBranchCommitsTotalCountByNameFactory({
getStreamBranchByName: getStreamBranchByNameFactory({ db }),
getBranchCommitsTotalCount: getBranchCommitsTotalCountFactory({ db })
})
const getCommitsByBranchName = getPaginatedBranchCommitsItemsByNameFactory({
getStreamBranchByName: getStreamBranchByNameFactory({ db }),
getPaginatedBranchCommitsItems: getPaginatedBranchCommitsItemsFactory({ db })
})
const createObject = createObjectFactory({
storeSingleObjectIfNotFoundFactory: storeSingleObjectIfNotFoundFactory({ db })
})
describe('Commits @core-commits', () => {
let user: BasicTestUser
let stream: BasicTestStream
const testObject = {
foo: 'bar',
baz: 'qux'
}
const testObject2 = {
foo: 'bar3',
baz: 'qux3'
}
const testObject3 = {
foo: 'bar4',
baz: 'qux5'
}
let quitters: (() => void)[] = []
const generateObject = async (streamId = stream.id, object = testObject) =>
await createObject({ streamId, object })
let commitId1: string, commitId2: string, commitId3: string
before(async () => {
await beforeEachContext()
user = await createTestUser({
name: 'Dimitrie Stefanescu',
email: 'didimitrie4342@example.org',
password: 'sn3aky-1337-b1m',
id: ''
})
stream = await createTestStream(
{
name: 'Test Stream References',
description: 'Whatever goes in here usually...'
},
user
)
const testObjectId = await createObject({ streamId: stream.id, object: testObject })
const testObject2Id = await createObject({
streamId: stream.id,
object: testObject2
})
const testObject3Id = await createObject({
streamId: stream.id,
object: testObject3
})
commitId1 = (
await createCommitByBranchName({
streamId: stream.id,
branchName: 'main',
message: 'first commit',
sourceApplication: 'tests',
objectId: testObjectId,
authorId: user.id
})
).id
commitId2 = (
await createCommitByBranchName({
streamId: stream.id,
branchName: 'main',
message: 'second commit',
sourceApplication: 'tests',
objectId: testObject2Id,
authorId: user.id,
parents: [commitId1]
})
).id
commitId3 = (
await createCommitByBranchName({
streamId: stream.id,
branchName: 'main',
message: 'third commit',
sourceApplication: 'tests',
objectId: testObject3Id,
authorId: user.id,
parents: [commitId1, commitId2]
})
).id
})
afterEach(() => {
quitters.forEach((quit) => quit())
quitters = []
})
it('Should create a commit by branch name', async () => {
const msg = 'first commit ive ever done, yes sir'
let eventFired = false
quitters.push(
getEventBus().listen(VersionEvents.Created, ({ payload }) => {
expect(payload.version.message).to.equal(msg)
eventFired = true
})
)
const objectId = await generateObject()
const id = (
await createCommitByBranchName({
streamId: stream.id,
branchName: 'main',
message: msg,
sourceApplication: 'tests',
objectId,
authorId: user.id
})
).id
expect(id).to.be.a.string
expect(eventFired).to.be.true
})
// support SKDs not being able to handle the fe1 - fe2 link transition
it('Should create a commit by branch id', async () => {
const objectId = await generateObject()
const branch = await createBranchAndNotify(
{ name: 'foobar', projectId: stream.id },
user.id
)
const id = (
await createCommitByBranchName({
streamId: stream.id,
branchName: branch.id,
message: 'first commit',
sourceApplication: 'tests',
objectId,
authorId: user.id
})
).id
expect(id).to.be.a.string
})
it('Should fail to create a commit if the branch is not a valid name or id', async () => {
const objectId = await generateObject()
try {
const id = (
await createCommitByBranchName({
streamId: stream.id,
branchName: cryptoRandomString({ length: 10 }),
message: 'first commit',
sourceApplication: 'tests',
objectId,
authorId: user.id
})
).id
expect(id).null
} catch (error) {
expect(ensureError(error).message).contains(
'Failed to find branch with name or id'
)
}
})
it('Should create a commit with a previous commit id', async () => {
const objectId = await generateObject()
const objectId2 = await generateObject()
const id = (
await createCommitByBranchName({
streamId: stream.id,
branchName: 'main',
message: 'second commit',
sourceApplication: 'tests',
objectId,
authorId: user.id,
parents: [commitId1]
})
).id
expect(id).to.be.a.string
const id2 = (
await createCommitByBranchName({
streamId: stream.id,
branchName: 'main',
message: 'third commit',
sourceApplication: 'tests',
objectId: objectId2,
authorId: user.id,
parents: [commitId1, commitId2]
})
).id
expect(id2).to.be.a.string
})
it('Should update a commit', async () => {
const res = await updateCommitAndNotify(
{
id: commitId1,
message: 'FIRST COMMIT YOOOOOO',
streamId: stream.id
},
user.id
)
expect(res).to.be.ok
})
it('Should delete a commit', async () => {
const objectId = await generateObject()
const tempCommitId = (
await createCommitByBranchName({
streamId: stream.id,
branchName: 'main',
message: 'temp commit',
sourceApplication: 'tests',
objectId,
authorId: user.id
})
).id
const res = await deleteCommitAndNotify(tempCommitId, stream.id, user.id)
expect(res).to.be.ok
})
it('Should get a commit by id', async () => {
const cm = await getCommit(commitId1, { streamId: stream.id })
expect(cm!.message).to.equal('FIRST COMMIT YOOOOOO')
expect(cm!.author).to.equal(user.id)
})
it('Should get the commits and their total count from a branch', async () => {
const { id: streamId } = await createTestStream(buildBasicTestProject(), user)
for (let i = 0; i < 10; i++) {
const t = { qux: i, id: '' }
t.id = await createObject({ streamId, object: t })
await createCommitByBranchName({
streamId,
branchName: 'main',
message: `commit # ${i + 3}`,
sourceApplication: 'tests',
objectId: t.id,
authorId: user.id
})
}
const { commits, cursor } = await getCommitsByBranchName({
streamId,
branchName: 'main',
limit: 2
})
expect(commits).to.be.an('array')
expect(commits.length).to.equal(2)
const { commits: commits2 } = await getCommitsByBranchName({
streamId,
branchName: 'main',
limit: 5,
cursor
})
expect(commits2.length).to.equal(5)
const c = await getCommitsTotalCountByBranchName({
streamId,
branchName: 'main'
})
expect(c).to.equal(10)
})
it('Should get the commits and their total count from a stream', async () => {
const { id: streamId } = await createTestStream(buildBasicTestProject(), user)
await createBranch({
name: 'dim/dev',
streamId,
authorId: user.id,
description: null
})
for (let i = 0; i < 15; i++) {
const t = { thud: i, id: '' }
t.id = await createObject({ streamId, object: t })
await createCommitByBranchName({
streamId,
branchName: 'dim/dev',
message: `pushed something # ${i + 3}`,
sourceApplication: 'tests',
objectId: t.id,
authorId: user.id
})
}
const { commits, cursor } = await getCommitsByStreamId({
streamId,
limit: 10
})
const { commits: commits2 } = await getCommitsByStreamId({
streamId,
limit: 20,
cursor
})
expect(commits.length).to.equal(10)
expect(commits2.length).to.equal(5)
const c = await getStreamCommitCount(streamId)
expect(c).to.equal(15)
})
it('Commits should have source, total count, branch name and parents fields', async () => {
const { commits: userCommits } = await getCommitsByUserId({
userId: user.id,
limit: 1000
})
const userCommit = userCommits[0]
const { commits: streamCommits } = await getCommitsByStreamId({
streamId: stream.id,
limit: 10
})
const serverCommit = streamCommits[0]
const { commits: branchCommits } = await getCommitsByBranchName({
streamId: stream.id,
branchName: 'main',
limit: 2
})
const branchCommit = branchCommits[0]
const idCommit = await getCommit(commitId3, { streamId: stream.id })
expect(userCommit).to.have.property('sourceApplication')
expect(userCommit.sourceApplication).to.be.a('string')
expect(userCommit).to.have.property('totalChildrenCount')
expect(userCommit.totalChildrenCount).to.be.a('number')
expect(userCommit).to.have.property('parents')
for (const commit of [serverCommit, branchCommit, idCommit]) {
expect(commit).to.have.property('sourceApplication')
expect(commit!.sourceApplication).to.be.a('string')
expect(commit).to.have.property('totalChildrenCount')
expect(commit!.totalChildrenCount).to.be.a('number')
expect(commit).to.have.property('parents')
expect(commit!.streamId).to.equal(stream.id)
}
expect(idCommit!.parents).to.be.a('array')
expect(idCommit!.parents!.length).to.equal(2)
expect(idCommit!.streamId).to.equal(stream.id)
})
it('Should have an array of parents', async () => {
const commits = [
await getCommit(commitId3, { streamId: stream.id }),
await getCommit(commitId2, { streamId: stream.id })
]
for (const commit of commits) {
expect(commit).to.have.property('parents')
expect(commit!.parents).to.be.a('array')
expect(commit!.parents!.length).to.greaterThan(0)
}
})
})