Skip to content

Commit

Permalink
Tests for expected sequence outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Dec 16, 2024
1 parent ef001d6 commit f870ffb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"--run",
"--inspect-brk",
"--no-file-parallelism",
"packages/modules/packages/diviner/packages/indexing/packages/temporal/packages/memory/src/StateToIndexCandidateDiviner/spec/Diviner.spec.ts"
"packages/protocol/packages/payload/packages/model/src/StorageMeta/spec/SequenceParser.spec.ts"
],
"sourceMaps": true,
"resolveSourceMapLocations": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,32 @@ export class SequenceParser {
}

get address(): Address {
const start = SequenceConstants.localSequenceBytes * 2
const end = SequenceConstants.qualifiedSequenceBytes * 2
const start = SequenceConstants.localSequenceBytes
const end = SequenceConstants.qualifiedSequenceBytes
return toHex(this.data.slice(start, end).buffer, { prefix: false })
}

get epoch(): Epoch {
const start = 0
const end = SequenceConstants.epochBytes * 2
const end = SequenceConstants.epochBytes
return toHex(this.data.slice(start, end).buffer, { prefix: false })
}

get localSequence(): LocalSequence {
const start = 0
const end = SequenceConstants.localSequenceBytes * 2
const end = SequenceConstants.localSequenceBytes
return toHex(this.data.slice(start, end).buffer, { prefix: false })
}

get nonce(): Nonce {
const start = SequenceConstants.epochBytes * 2
const end = SequenceConstants.localSequenceBytes * 2
const start = SequenceConstants.epochBytes
const end = SequenceConstants.localSequenceBytes
return toHex(this.data.slice(start, end).buffer, { prefix: false })
}

get qualifiedSequence(): QualifiedSequence {
const start = 0
const end = SequenceConstants.qualifiedSequenceBytes * 2
const end = SequenceConstants.qualifiedSequenceBytes
return toHex(this.data.slice(start, end).buffer, { prefix: false })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,78 @@ import {
describe, expect, it,
} from 'vitest'

import { SequenceConstants } from '../Sequence.ts'
import { SequenceParser } from '../SequenceParser.ts'

describe('SequenceParser', () => {
const hash: Hash = toHex('1269b95d3ebf1b1258a82ccca0b365fabf4b8c99bf8fc852e5045e30ad20fbb1')
const address: Address = 'b36d327210f67ad98be881ddf6ad1f1b3e2c5137'
const timestamp = 1_234_567_890_123
const wrapper: SequenceParser = SequenceParser.from(timestamp, hash, address)
const parsed: SequenceParser = SequenceParser.from(timestamp, hash, address)

describe('epoch', () => {
it('converts timestamp to epoch correctly', () => {
expect(wrapper.epoch).toBe(SequenceParser.toEpoch(timestamp))
expect(wrapper.epoch).toBe('0000011f71fb04cb')
expect(parsed.epoch).toBe(SequenceParser.toEpoch(timestamp))
expect(parsed.epoch).toBe('0000011f71fb04cb')
})
})

describe('from', () => {
it('parses sequence from timestamp and hash correctly', () => {
const parsed = SequenceParser.from(timestamp, hash)
const timestampHex = SequenceParser.toEpoch(timestamp)
const nonceHex = SequenceParser.toNonce(hash)
expect(parsed.localSequence).toBe(`${timestampHex}${nonceHex}`)
expect(parsed.localSequence).toBe('0000011f71fb04cbe5045e30ad20fbb1')
})
it('parses sequence from timestamp, hash, and address correctly', () => {
const parsed = SequenceParser.from(timestamp, hash, address)
const timestampHex = SequenceParser.toEpoch(timestamp)
const nonceHex = SequenceParser.toNonce(hash)
expect(parsed.localSequence).toBe(`${timestampHex}${nonceHex}`)
expect(parsed.localSequence).toBe('0000011f71fb04cbe5045e30ad20fbb1')
expect(parsed.qualifiedSequence).toBe(`${timestampHex}${nonceHex}${address}`)
expect(parsed.qualifiedSequence).toBe('0000011f71fb04cbe5045e30ad20fbb1b36d327210f67ad98be881ddf6ad1f1b3e2c5137')
})
it('equivalently parses two local sequences with and without address', () => {
const withAddress = SequenceParser.from(timestamp, hash, address)
const withoutAddress = SequenceParser.from(timestamp, hash)
expect(withAddress.localSequence).toEqual(withoutAddress.localSequence)
})
it('equivalently parses two qualified sequences with and without address', () => {
const address = '0'.repeat(SequenceConstants.addressBytes * 2) as Address
const withAddress = SequenceParser.from(timestamp, hash, address)
const withoutAddress = SequenceParser.from(timestamp, hash)
expect(withAddress.qualifiedSequence).toEqual(withoutAddress.qualifiedSequence)
})
})

describe('nonce', () => {
it('derives nonce from hash correctly', () => {
expect(wrapper.nonce).toBe(SequenceParser.toNonce(hash))
expect(wrapper.nonce).toBe(hash.slice(-8 * 2))
expect(wrapper.nonce).toBe('e5045e30ad20fbb1')
expect(parsed.nonce).toBe(SequenceParser.toNonce(hash))
expect(parsed.nonce).toBe(hash.slice(-8 * 2))
expect(parsed.nonce).toBe('e5045e30ad20fbb1')
})
})

describe('address', () => {
it('stores the address correctly', () => {
expect(wrapper.address).toBe(address)
expect(parsed.address).toBe(address)
})
})

describe('localSequence', () => {
it('concatenates epoch and nonce correctly', () => {
expect(wrapper.localSequence).toBe(`${wrapper.epoch}${wrapper.nonce}`)
expect(wrapper.localSequence).toBe('0000011f71fb04cbe5045e30ad20fbb1')
expect(parsed.localSequence).toBe(`${parsed.epoch}${parsed.nonce}`)
expect(parsed.localSequence).toBe('0000011f71fb04cbe5045e30ad20fbb1')
})
})

describe('qualifiedSequence', () => {
it('concatenates localSequence and address correctly', () => {
expect(wrapper.qualifiedSequence).toBe(`${wrapper.localSequence}${wrapper.address}`)
expect(wrapper.qualifiedSequence).toBe(
expect(parsed.localSequence).toBe(`${parsed.epoch}${parsed.nonce}${parsed.address}`)
expect(parsed.qualifiedSequence).toBe(`${parsed.localSequence}${parsed.address}`)
expect(parsed.qualifiedSequence).toBe(
'0000011f71fb04cbe5045e30ad20fbb1b36d327210f67ad98be881ddf6ad1f1b3e2c5137',
)
})
Expand Down

0 comments on commit f870ffb

Please sign in to comment.