Skip to content

Commit

Permalink
feat: [#38] Add JobId wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
yeraydavidrodriguez committed May 16, 2022
1 parent 9ccfeb8 commit 2f22e0a
Show file tree
Hide file tree
Showing 17 changed files with 376 additions and 212 deletions.
3 changes: 2 additions & 1 deletion __tests__/unit/commit-body.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {CommitBody} from '../../src/commit-body'
import {JobId} from '../../src/job-id'
import {NewJobMessage} from '../../src/message'
import {dummyCommitBodyText} from '../../src/__tests__/helpers'
import {nullCommitHash} from '../../src/commit-hash'
Expand Down Expand Up @@ -81,7 +82,7 @@ describe('CommitBody', () => {

it('could be built from a Message', () => {
const commitBody = CommitBody.fromMessage(
new NewJobMessage('message-payload', 0)
new NewJobMessage('message-payload', new JobId(0))
)

const builtBody = JSON.stringify({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/unit/commit-subject-parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {CommitHash} from '../../src/commit-hash'
import {CommitSubjectParser} from '../../src/commit-subject-parser'
import {JobId} from '../../src/job-id'

describe('CommitSubjectParser', () => {
it('should parse the message key from a commit subject', () => {
Expand Down Expand Up @@ -88,7 +89,7 @@ describe('CommitSubjectParser', () => {
})

it('should fail when the job Id does not exist', () => {
const fn = (): number => {
const fn = (): JobId => {
const parser = new CommitSubjectParser(
'📝🈺: queue-name: job.ref.f1a69d48a01cc130a64aeac5eaf762e4ba685de7'
)
Expand Down
40 changes: 40 additions & 0 deletions __tests__/unit/job-id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {JobId, nullJobId} from '../../src/job-id'

describe('JobId', () => {
it('should contain the Id of a Job', () => {
const jobId = new JobId(42)

expect(jobId.getId()).toBe(42)
})

it('should fail when using invalid job value', () => {
const negativeJobId = (): JobId => {
return new JobId(-2)
}
const NaNJobId = (): JobId => {
return new JobId(NaN)
}
const zeroJobId = (): JobId => {
return new JobId(0)
}

expect(negativeJobId).toThrowError()
expect(NaNJobId).toThrowError()
expect(zeroJobId).not.toThrowError()
})

it('should compare two JobIds', () => {
const jobId1 = new JobId(42)
const jobId2 = new JobId(43)
const jobId3 = new JobId(42)

expect(jobId1.equalsTo(jobId2)).toBe(false)
expect(jobId1.equalsTo(jobId3)).toBe(true)
})

it('should be nullable', () => {
const nullObject = nullJobId()

expect(nullObject.isNull()).toBe(true)
})
})
17 changes: 10 additions & 7 deletions __tests__/unit/job.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {CommitHash} from '../../src/commit-hash'
import {CommitInfo} from '../../src/commit-info'
import {Job, nullJob} from '../../src/job'
import {JobId} from '../../src/job-id'
import {NewJobCommittedMessage} from '../../src/committed-message'
import {dummyCommitBodyText} from '../../src/__tests__/helpers'

Expand All @@ -9,7 +10,7 @@ describe('Job', () => {
const job = new Job(
'payload',
new CommitHash('a362802b98c78df052a78796a1a7cde60a5c1faf'),
1
new JobId(1)
)

expect(job.getPayload()).toBe('payload')
Expand All @@ -19,7 +20,7 @@ describe('Job', () => {
const commitHash = new CommitHash(
'a362802b98c78df052a78796a1a7cde60a5c1faf'
)
const job = new Job('payload', commitHash, 1)
const job = new Job('payload', commitHash, new JobId(1))

expect(job.getCommitHash()).toBe(commitHash)
})
Expand All @@ -28,10 +29,12 @@ describe('Job', () => {
const job = new Job(
'payload',
new CommitHash('a362802b98c78df052a78796a1a7cde60a5c1faf'),
42
new JobId(42)
)

expect(job.getId()).toBe(42)
const expectedJobId = new JobId(42)

expect(job.getJobId().equalsTo(expectedJobId)).toBe(true)
})

it('should be nullable', () => {
Expand All @@ -44,19 +47,19 @@ describe('Job', () => {
const job1 = new Job(
'payload',
new CommitHash('a362802b98c78df052a78796a1a7cde60a5c1faf'),
0
new JobId(0)
)

const job2 = new Job(
'payload',
new CommitHash('8f51fa0a019b277103acc5ef75c52dfb2a9bcce3'),
0
new JobId(0)
)

const job3 = new Job(
'payload3',
new CommitHash('a362802b98c78df052a78796a1a7cde60a5c1faf'),
0
new JobId(0)
)

expect(job1.equalsTo(job1)).toBe(true)
Expand Down
15 changes: 10 additions & 5 deletions __tests__/unit/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import {JobFinishedMessage, NewJobMessage} from '../../src/message'
import {CommitHash} from '../../src/commit-hash'
import {JobId} from '../../src/job-id'

describe('Message', () => {
it('should have a payload', () => {
const message = new NewJobMessage('payload', 0)
const message = new NewJobMessage('payload', new JobId(0))
expect(message.getPayload()).toBe('payload')
})

it('could have an optional reference (commit hash) to another job', () => {
const hash = new CommitHash('f1a69d48a01cc130a64aeac5eaf762e4ba685de7')

const message = new JobFinishedMessage('payload', 0, hash)
const message = new JobFinishedMessage('payload', new JobId(0), hash)

expect(message.getJobRef().equalsTo(hash)).toBe(true)
})

it('should allow to omit the job reference', () => {
const message = new NewJobMessage('payload', 0)
const message = new NewJobMessage('payload', new JobId(0))

expect(message.getJobRef().isNull()).toBe(true)
})

it('should indicate if it has a job ref', () => {
const messageWithoutJobRef = new NewJobMessage('payload', 0)
const messageWithoutJobRef = new NewJobMessage('payload', new JobId(0))
expect(messageWithoutJobRef.hasJobRef()).toBe(false)

const hash = new CommitHash('f1a69d48a01cc130a64aeac5eaf762e4ba685de7')
const messageWithJobRef = new JobFinishedMessage('payload', 0, hash)
const messageWithJobRef = new JobFinishedMessage(
'payload',
new JobId(0),
hash
)
expect(messageWithJobRef.hasJobRef()).toBe(true)
})
})
23 changes: 16 additions & 7 deletions __tests__/unit/queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {QueueName} from '../../src/queue-name'
import {SigningKeyId} from '../../src/signing-key-id'

import {testConfiguration} from '../../src/__tests__/config'
import {JobId} from '../../src/job-id'

function commitOptionsForTests(): CommitOptions {
const author = CommitAuthor.fromNameAndEmail(
Expand Down Expand Up @@ -91,7 +92,9 @@ describe('Queue', () => {
const job = await queue.createJob(dummyPayload())

const commitHash = getLatestCommitHash(queue.getGitRepoDir())
expect(job.equalsTo(new Job(dummyPayload(), commitHash, 0))).toBe(true)
expect(
job.equalsTo(new Job(dummyPayload(), commitHash, new JobId(0)))
).toBe(true)
})

it('should fail when trying to create a job if the previous job has not finished yet', async () => {
Expand Down Expand Up @@ -274,9 +277,9 @@ describe('Queue', () => {
const nextJob = queue.getNextJob()

const latestCommit = getLatestCommitHash(queue.getGitRepoDir())
expect(nextJob.equalsTo(new Job(dummyPayload(), latestCommit, 1))).toBe(
true
)
expect(
nextJob.equalsTo(new Job(dummyPayload(), latestCommit, new JobId(1)))
).toBe(true)
})
})

Expand All @@ -299,16 +302,22 @@ describe('Queue', () => {
const nextJob2 = queue2.getNextJob()

const newJob1Commit = getSecondToLatestCommitHash(queue1.getGitRepoDir())
expect(nextJob1.equalsTo(new Job(payload1, newJob1Commit, 0))).toBe(true)
expect(
nextJob1.equalsTo(new Job(payload1, newJob1Commit, new JobId(0)))
).toBe(true)

let latestCommit = getLatestCommitHash(queue2.getGitRepoDir())
expect(nextJob2.equalsTo(new Job(payload2, latestCommit, 0))).toBe(true)
expect(
nextJob2.equalsTo(new Job(payload2, latestCommit, new JobId(0)))
).toBe(true)

await queue1.markJobAsStarted(payload1)
await queue1.markJobAsFinished(payload1)
await queue1.createJob(payload2)
const nextJob3 = queue1.getNextJob()
latestCommit = getLatestCommitHash(queue1.getGitRepoDir())
expect(nextJob3.equalsTo(new Job(payload2, latestCommit, 1))).toBe(true)
expect(
nextJob3.equalsTo(new Job(payload2, latestCommit, new JobId(1)))
).toBe(true)
})
})
Loading

0 comments on commit 2f22e0a

Please sign in to comment.