OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-wallet / tests / manual / test-autobase-minimal.cjs

107 lines · javascript ref:
// test-autobase-minimal.cjs - Minimal test without Hyperbee
// Test the basic Autobase functionality first

require('bare-node-runtime/global')
const path = require('bare-path')
const os = require('bare-os')
const fs = require('bare-fs')
const Corestore = require('corestore')
const Autobase = require('autobase')
const b4a = require('b4a')

// Test storage directory
const TEST_DIR = path.join(os.homedir(), '.hyper-wallet-test', 'autobase-minimal')

// Clean test directory
if (fs.existsSync(TEST_DIR)) {
  fs.rmSync(TEST_DIR, { recursive: true })
}
fs.mkdirSync(TEST_DIR, { recursive: true, mode: 0o700 })

/**
 * View class - minimal version using just Hypercore
 * Pattern from autobase-example
 */
class View {
  open(store) {
    // Return plain Hypercore, not Hyperbee
    const core = store.get('view')
    return core
  }

  async apply(nodes, view, base) {
    console.log(`📝 Applying ${nodes.length} nodes...`)

    for (const node of nodes) {
      const value = JSON.parse(node.value)

      if (value.addWriter) {
        console.log('  ➕ Adding writer:', value.addWriter)
        await base.addWriter(Buffer.from(value.addWriter, 'hex'))
        continue
      }

      // Just append the value to the view core
      await view.append(JSON.stringify({ echo: value }))
      console.log('  💾 Appended:', value)
    }

    console.log('✅ Apply complete\n')
  }

  close(view) {
    return view.close()
  }
}

async function runTest() {
  console.log('🧪 Testing Autobase Minimal (No Hyperbee)\n')
  console.log('Test directory:', TEST_DIR, '\n')

  // Initialize Corestore
  const store = new Corestore(TEST_DIR)
  await store.ready()
  console.log('✅ Corestore initialized')

  // Get namespace
  const namespace = await Autobase.getLocalKey(store)
  console.log('✅ Namespace key:', b4a.toString(namespace, 'hex').slice(0, 16) + '...')

  // Initialize Autobase
  const autobase = new Autobase(store.namespace(namespace), null, new View())
  await autobase.ready()
  console.log('✅ Autobase initialized')
  console.log('   Autobase key:', b4a.toString(autobase.key, 'hex').slice(0, 16) + '...')
  console.log('   Local writer key:', b4a.toString(autobase.local.key, 'hex').slice(0, 16) + '...')
  console.log('   View length:', autobase.view.length)
  console.log()

  // Test: Append a message
  console.log('Test: Append message')
  await autobase.append(JSON.stringify({ hello: 'world', from: 'test' }))
  console.log('📤 Appended message')

  // Wait for view to update
  await new Promise(resolve => setTimeout(resolve, 200))

  console.log('   View length after append:', autobase.view.length)

  // Read from view
  if (autobase.view.length > 0) {
    const last = await autobase.view.get(autobase.view.length - 1)
    console.log('   Last entry:', JSON.parse(last))
  }

  // Cleanup
  await autobase.close()
  await store.close()

  console.log('\n✅ Minimal test passed!')
}

// Run the test
runTest().catch(err => {
  console.error('❌ Test failed:', err)
  process.exit(1)
})