Skip to content

Commit

Permalink
feat: add OrbitDB readiness checks to ensure nameOps are indexed befo…
Browse files Browse the repository at this point in the history
…re tests

Co-Authored-By: Nico Krause <[email protected]>
  • Loading branch information
devin-ai-integration[bot] and silkroadnomad committed Dec 23, 2024
1 parent 9ca1431 commit d1aa409
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions relay/tests/relay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { gossipsub } from "@chainsafe/libp2p-gossipsub";
import { bootstrap } from "@libp2p/bootstrap"
import { multiaddr } from '@multiformats/multiaddr'
import { mdns } from '@libp2p/mdns'
import { getOrCreateDB } from '../src/pinner/nameOpsFileManager.js'

const pubsubPeerDiscoveryTopics = process.env.RELAY_PUBSUB_PEER_DISCOVERY_TOPICS?.split(',')
const CONTENT_TOPIC = '/doichain-nfc/1/message/proto';
Expand All @@ -24,6 +25,22 @@ describe('Doichain Relay Pinning Service Test', function() {
const messages = [];
const TIMEOUT = 5000;

// Helper function to check if OrbitDB has nameOps
async function waitForNameOps(orbitdb, maxAttempts = 10) {
for (let attempt = 0; attempt < maxAttempts; attempt++) {
const db = await getOrCreateDB(orbitdb);
const allDocs = await db.all();
if (allDocs.length > 0) {
console.log(`Found ${allDocs.length} nameOps in OrbitDB`);
return true;
}
console.log(`Attempt ${attempt + 1}/${maxAttempts}: Waiting for nameOps to be indexed...`);
await new Promise(resolve => setTimeout(resolve, 2000)); // Wait 2 seconds between attempts
}
console.log('Timed out waiting for nameOps');
return false;
}

before(async function() {
this.timeout(100000);

Expand Down Expand Up @@ -68,6 +85,13 @@ describe('Doichain Relay Pinning Service Test', function() {
});

await new Promise(resolve => setTimeout(resolve, TIMEOUT));

// Wait for nameOps to be indexed
console.log('Waiting for nameOps to be indexed in OrbitDB...');
const hasNameOps = await waitForNameOps(helia.orbitdb);
if (!hasNameOps) {
console.warn('No nameOps found in OrbitDB after timeout');
}
});

after(async () => {
Expand Down Expand Up @@ -136,12 +160,16 @@ describe('Doichain Relay Pinning Service Test', function() {
it.only('should receive CIDs response when requesting LIST_TODAY', async function() {
this.timeout(20000);
messages.length = 0;

// Wait for nameOps to be indexed before starting test
console.log('Waiting for nameOps before LIST_TODAY test...');
const hasNameOps = await waitForNameOps(helia.orbitdb);
expect(hasNameOps, 'Expected nameOps to be indexed before running test').to.be.true;

// Use ISO format for consistency
const today = "2024-12-08";
// Use "TODAY" for consistent date handling
const messageObject = {
type: "LIST",
dateString: today, // Send actual date instead of "TODAY"
dateString: "TODAY", // Use TODAY instead of hardcoded date
pageSize: 10,
from: 0,
filter: ""
Expand Down Expand Up @@ -204,10 +232,16 @@ describe('Doichain Relay Pinning Service Test', function() {

it.only('should receive last 100 NameOps when requesting LIST_LAST_100', async function() {
this.timeout(20000);
messages.length = 0;
messages.length = 0;

// Wait for nameOps to be indexed before starting test
console.log('Waiting for nameOps before LIST_LAST_100 test...');
const hasNameOps = await waitForNameOps(helia.orbitdb);
expect(hasNameOps, 'Expected nameOps to be indexed before running test').to.be.true;

const messageObject = {
type: "LIST",
dateString: "LAST", // Add dateString parameter to avoid INVALID_DATE_FORMAT
pageSize: 100,
from: 0,
filter: "" // empty string for no filter
Expand Down

0 comments on commit d1aa409

Please sign in to comment.