Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/mock reset #434

Merged
merged 7 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@ module.exports = {
"^@src/(.*)$": "<rootDir>/src/$1",
"^@utils/(.*)$": "<rootDir>/src/utils/$1",
"^@test/(.*)$": "<rootDir>/test/$1"
}
},
clearMocks: true,
resetMocks: true,
testPathIgnorePatterns: [
"/node_modules/",
"/dist/",
"/build/"
]
};
1 change: 0 additions & 1 deletion src/utils/nestedCounters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as utils from '../utils'
import Crypto from '../crypto'
import { isDebugModeMiddleware, isDebugModeMiddlewareLow } from '../network/debugMiddleware'
import { getNetworkTimeOffset, shardusGetTime } from '../network'
import { Utils } from '@shardeum-foundation/lib-types'

type CounterMap = Map<string, CounterNode>
interface CounterNode {
Expand Down
1 change: 0 additions & 1 deletion test/unit/src/types/ApoptosisProposalReq.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import { initAjvSchemas } from '../../../../src/types/ajv/Helpers'
import { TypeIdentifierEnum } from '../../../../src/types/enum/TypeIdentifierEnum'
import { VectorBufferStream } from '../../../../src/utils/serialization/VectorBufferStream'
import { Utils } from '@shardeum-foundation/lib-types'

describe('ApoptosisProposalReq Serialization', () => {
test('should serialize with root true', () => {
Expand Down
312 changes: 155 additions & 157 deletions test/unit/src/types/BroadcastFinalStateReq.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,182 +9,180 @@ import {
deserializeBroadcastFinalStateReq,
serializeBroadcastFinalStateReq,
} from '../../../../src/types/BroadcastFinalStateReq'
import { stateManager } from '@src/p2p/Context'
import { beforeEachHandler } from './stateManagerSerializeMocks'

// Mock the Context module and its nested structure
jest.mock('../../../../src/p2p/Context', () => ({
setDefaultConfigs: jest.fn(),
stateManager: {
app: {
binarySerializeObject: jest.fn((enumType, data) => Buffer.from(Utils.safeStringify(data), 'utf8')),
binaryDeserializeObject: jest.fn((enumType, buffer) => Utils.safeJsonParse(buffer.toString('utf8'))),
},
app: {
binarySerializeObject: jest.fn(),
binaryDeserializeObject: jest.fn(),
}
},
setDefaultConfigs: jest.fn(),
}))

describe('BroadcastFinalStateReq Serialization Tests', () => {
describe('BroadcastFinalStateReq', () => {
beforeAll(() => {
initAjvSchemas()
})
beforeEach(() => {
jest.clearAllMocks()
beforeEachHandler()
})
test('Serialize BroadcastFinalStateReq with Valid Input Correctly, root true', () => {
const stream = new VectorBufferStream(0)
const obj: BroadcastFinalStateReq = {
txid: 'testTxid',
stateList: [
{
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
},
],
}
serializeBroadcastFinalStateReq(stream, obj, true)

const expectedStream = new VectorBufferStream(0)
expectedStream.writeUInt16(TypeIdentifierEnum.cBroadcastFinalStateReq)
expectedStream.writeUInt8(cBroadcastFinalStateReqVersion)
expectedStream.writeString(obj.txid)
expectedStream.writeUInt16(obj.stateList.length)
obj.stateList.forEach((item) => serializeWrappedDataResponse(expectedStream, item))

expect(stream.getBuffer()).toEqual(expectedStream.getBuffer())
})

test('Handle Empty StateList Correctly', () => {
const stream = new VectorBufferStream(0)
const obj: BroadcastFinalStateReq = { txid: 'testTxid', stateList: [] }
serializeBroadcastFinalStateReq(stream, obj, true)

const expectedStream = new VectorBufferStream(0)
expectedStream.writeUInt16(TypeIdentifierEnum.cBroadcastFinalStateReq)
expectedStream.writeUInt8(cBroadcastFinalStateReqVersion)
expectedStream.writeString(obj.txid)
expectedStream.writeUInt16(obj.stateList.length)
obj.stateList.forEach((item) => serializeWrappedDataResponse(expectedStream, item))
describe('BroadcastFinalStateReq Serialization Tests', () => {
test('Serialize BroadcastFinalStateReq with Valid Input Correctly, root true', () => {
const stream = new VectorBufferStream(0)
const obj: BroadcastFinalStateReq = {
txid: 'testTxid',
stateList: [
{
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
},
],
}
serializeBroadcastFinalStateReq(stream, obj, true)

const expectedStream = new VectorBufferStream(0)
expectedStream.writeUInt16(TypeIdentifierEnum.cBroadcastFinalStateReq)
expectedStream.writeUInt8(cBroadcastFinalStateReqVersion)
expectedStream.writeString(obj.txid)
expectedStream.writeUInt16(obj.stateList.length)
obj.stateList.forEach((item) => serializeWrappedDataResponse(expectedStream, item))

expect(stream.getBuffer()).toEqual(expectedStream.getBuffer())
})

expect(stream.getBuffer()).toEqual(expectedStream.getBuffer())
})
test('Handle Empty StateList Correctly', () => {
const stream = new VectorBufferStream(0)
const obj: BroadcastFinalStateReq = { txid: 'testTxid', stateList: [] }
serializeBroadcastFinalStateReq(stream, obj, true)

test('Serialize with Root Flag Set to False', () => {
const stream = new VectorBufferStream(0)
const obj: BroadcastFinalStateReq = {
txid: 'testTxid',
stateList: [
{
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
},
],
}
serializeBroadcastFinalStateReq(stream, obj, false)

const expectedStream = new VectorBufferStream(0)
expectedStream.writeUInt8(cBroadcastFinalStateReqVersion)
expectedStream.writeString(obj.txid)
expectedStream.writeUInt16(obj.stateList.length)
obj.stateList.forEach((item) => serializeWrappedDataResponse(expectedStream, item))

expect(stream.getBuffer()).toEqual(expectedStream.getBuffer())
})
})
const expectedStream = new VectorBufferStream(0)
expectedStream.writeUInt16(TypeIdentifierEnum.cBroadcastFinalStateReq)
expectedStream.writeUInt8(cBroadcastFinalStateReqVersion)
expectedStream.writeString(obj.txid)
expectedStream.writeUInt16(obj.stateList.length)
obj.stateList.forEach((item) => serializeWrappedDataResponse(expectedStream, item))

describe('BroadcastFinalStateReq Deserialization Tests', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('Deserialize Data Correctly', () => {
const stream = new VectorBufferStream(0)
stream.writeUInt8(cBroadcastFinalStateReqVersion)
stream.writeString('testTxid')
stream.writeUInt16(1)
serializeWrappedDataResponse(stream, {
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
expect(stream.getBuffer()).toEqual(expectedStream.getBuffer())
})

stream.position = 0
const result = deserializeBroadcastFinalStateReq(stream)

const expected: BroadcastFinalStateReq = {
txid: 'testTxid',
stateList: [
{
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
},
],
}
expect(result).toEqual(expected)
test('Serialize with Root Flag Set to False', () => {
const stream = new VectorBufferStream(0)
const obj: BroadcastFinalStateReq = {
txid: 'testTxid',
stateList: [
{
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
},
],
}
serializeBroadcastFinalStateReq(stream, obj, false)

const expectedStream = new VectorBufferStream(0)
expectedStream.writeUInt8(cBroadcastFinalStateReqVersion)
expectedStream.writeString(obj.txid)
expectedStream.writeUInt16(obj.stateList.length)
obj.stateList.forEach((item) => serializeWrappedDataResponse(expectedStream, item))

expect(stream.getBuffer()).toEqual(expectedStream.getBuffer())
})
})

test('Throw Error on Version Mismatch', () => {
const obj: BroadcastFinalStateReq = {
txid: 'testTxid',
stateList: [
{
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
},
],
}
const stream = new VectorBufferStream(0)
serializeBroadcastFinalStateReq(stream, obj, false)
// Manually increase the version number in the buffer to simulate a mismatch
const buffer = stream.getBuffer()
buffer[0] = cBroadcastFinalStateReqVersion + 1
const alteredStream = VectorBufferStream.fromBuffer(buffer)

expect(() => deserializeBroadcastFinalStateReq(alteredStream)).toThrow(
'BroadcastFinalStateReq version mismatch'
)
})
})
describe('BroadcastFinalStateReq Deserialization Tests', () => {
test('Deserialize Data Correctly', () => {
const stream = new VectorBufferStream(0)
stream.writeUInt8(cBroadcastFinalStateReqVersion)
stream.writeString('testTxid')
stream.writeUInt16(1)
serializeWrappedDataResponse(stream, {
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
})

stream.position = 0
const result = deserializeBroadcastFinalStateReq(stream)

const expected: BroadcastFinalStateReq = {
txid: 'testTxid',
stateList: [
{
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
},
],
}
expect(result).toEqual(expected)
})

describe('BroadcastFinalStateReq Round-trip Tests', () => {
beforeEach(() => {
jest.clearAllMocks()
test('Throw Error on Version Mismatch', () => {
const obj: BroadcastFinalStateReq = {
txid: 'testTxid',
stateList: [
{
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
},
],
}
const stream = new VectorBufferStream(0)
serializeBroadcastFinalStateReq(stream, obj, false)
// Manually increase the version number in the buffer to simulate a mismatch
const buffer = stream.getBuffer()
buffer[0] = cBroadcastFinalStateReqVersion + 1
const alteredStream = VectorBufferStream.fromBuffer(buffer)

expect(() => deserializeBroadcastFinalStateReq(alteredStream)).toThrow(
'BroadcastFinalStateReq version mismatch'
)
})
})
test('Maintain Data Integrity Through Serialization and Deserialization', () => {
const stream = new VectorBufferStream(0)
const obj: BroadcastFinalStateReq = {
txid: 'testTxid',
stateList: [
{
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
},
],
}
serializeBroadcastFinalStateReq(stream, obj, false)
stream.position = 0

const result = deserializeBroadcastFinalStateReq(stream)

expect(result).toEqual(obj)

describe('BroadcastFinalStateReq Round-trip Tests', () => {
test('Maintain Data Integrity Through Serialization and Deserialization', () => {
const stream = new VectorBufferStream(0)
const obj: BroadcastFinalStateReq = {
txid: 'testTxid',
stateList: [
{
accountCreated: true,
isPartial: true,
accountId: 'id1',
stateId: 'stateid2',
data: {},
timestamp: 12345678,
},
],
}
serializeBroadcastFinalStateReq(stream, obj, false)
stream.position = 0

const result = deserializeBroadcastFinalStateReq(stream)

expect(result).toEqual(obj)
})
})
})
})
Loading
Loading