Skip to content

Commit

Permalink
test: enhance test mocks and configurations
Browse files Browse the repository at this point in the history
- Update PinningService mocks with proper Helia and OrbitDB interfaces
- Update PubsubHandler mock node with complete libp2p structure
- Fix test config with proper Ed25519 key handling

Co-Authored-By: Nico Krause <[email protected]>
  • Loading branch information
devin-ai-integration[bot] and silkroadnomad committed Dec 22, 2024
1 parent 167b6c8 commit 861a03e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
8 changes: 7 additions & 1 deletion restructure/relay/tests/test-config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// Test configuration for libp2p-relay
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
import { createEd25519PeerId } from '@libp2p/peer-id-factory';

// Create a consistent test PeerId for all tests
const testPeerId = await createEd25519PeerId();

export const testConfig = {
relayDevMode: true,
serverName: 'test-server',
relayPubsubPeerDiscoveryTopics: 'test-topic',
relayPrivateKey: '1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
relayPrivateKey: testPeerId.privateKey,
bootstrapList: ['/ip4/127.0.0.1/tcp/63785/p2p/12D3KooWRBxYrPDRsLS9PSK3H8YQpKYHTrzK7J2kgjJwGp8JmpHR'],
pubsubPeerDiscoveryTopics: ['test-topic'],
scoreThresholds: {
Expand Down
30 changes: 25 additions & 5 deletions restructure/relay/tests/unit/testPinningService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,31 @@ describe('PinningService', () => {
sandbox = sinon.createSandbox();
mockHelia = {
blockstore: {
put: sandbox.stub().resolves(),
get: sandbox.stub().resolves()
put: sandbox.stub().resolves(Buffer.from('test')),
get: sandbox.stub().resolves(Buffer.from('test')),
has: sandbox.stub().resolves(true)
},
dag: {
get: sandbox.stub().resolves({ value: Buffer.from('test') }),
put: sandbox.stub().resolves()
}
};
mockOrbitdb = {
open: sandbox.stub().resolves({
add: sandbox.stub().resolves('hash'),
get: sandbox.stub().resolves({ value: 'test' }),
close: sandbox.stub().resolves()
}),
close: sandbox.stub().resolves(),
id: 'test-id',
identity: { id: 'test-identity' }
};
mockElectrumClient = {
request: sandbox.stub().resolves()
request: sandbox.stub().resolves(),
connect: sandbox.stub().resolves(),
close: sandbox.stub().resolves(),
blockchain_scripthash_subscribe: sandbox.stub().resolves(),
blockchain_scripthash_get_history: sandbox.stub().resolves([])
};

pinningService = new PinningService(mockHelia, mockOrbitdb, mockElectrumClient);
Expand All @@ -46,10 +61,15 @@ describe('PinningService', () => {

describe('pinContent', () => {
it('should pin content successfully', async () => {
const cid = 'QmPZv7P8nQUSh6E3dGXhE3k8SqF6kY4GKH5bmFtX9DVQeH';
const mockCid = {
toString: () => 'QmPZv7P8nQUSh6E3dGXhE3k8SqF6kY4GKH5bmFtX9DVQeH',
toV1: () => ({
toString: () => 'QmPZv7P8nQUSh6E3dGXhE3k8SqF6kY4GKH5bmFtX9DVQeH'
})
};
const duration = 30;

await pinningService.pinContent(cid, duration);
await pinningService.pinContent(mockCid, duration);

expect(mockHelia.blockstore.put.called).to.be.true;
});
Expand Down
42 changes: 28 additions & 14 deletions restructure/relay/tests/unit/testPubsubHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,36 @@ describe('PubsubHandler', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
mockNode = {
services: {
pubsub: {
subscribe: sandbox.stub().resolves(),
publish: sandbox.stub().resolves(),
libp2p: {
services: {
pubsub: {
subscribe: sandbox.stub().resolves(),
publish: sandbox.stub().resolves(),
addEventListener: sandbox.stub(),
removeEventListener: sandbox.stub(),
topics: new Set(),
start: sandbox.stub().resolves(),
stop: sandbox.stub().resolves()
},
identify: {
multicodecs: ['/noise'],
start: sandbox.stub().resolves(),
stop: sandbox.stub().resolves()
}
},
connectionManager: {
getConnections: sandbox.stub().returns([]),
addEventListener: sandbox.stub(),
topics: new Set()
removeEventListener: sandbox.stub()
},
peerId: {
toString: () => 'QmTestPeerId',
toBytes: () => new Uint8Array([1, 2, 3]),
type: 'Ed25519'
},
identify: {
multicodecs: ['/noise']
}
},
connectionManager: {
getConnections: sandbox.stub().returns([])
},
peerId: {
toString: () => 'QmTestPeerId'
start: sandbox.stub().resolves(),
stop: sandbox.stub().resolves(),
getMultiaddrs: () => []
}
};
mockPinningService = {
Expand Down

0 comments on commit 861a03e

Please sign in to comment.