OverviewFilesCommitsIssuesDocsReleasesPeers

pgh / scripts / proof-real-network.mjs

60 lines · javascript ref:
/**
 * REAL-NETWORK proof: the batteries-included createRuntime() — resolve via
 * pgh-sidecar, fetch a real published closure over Hyperswarm with the REAL
 * NetworkFetcher, materialize atomically, and finalize (launcher + manifest)
 * — into an isolated temp prefix. Proves the complete self-contained updater
 * path end-to-end from { name } alone. Run on tabertron (sidecar + published
 * hyperident required). Read-only outside the temp dirs.
 */
import fs from '#node:fs'
import os from '#node:os'
import path from '#node:path'
import { TreeStore } from '../lib/store.js'
import { createRuntime } from '../lib/runtime-factory.js'

const APP = 'hyperident'
const HOST = 'linux-x64'

const storeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'pgh-netproof-store-'))
const binDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pgh-netproof-bin-'))
const store = new TreeStore({ root: storeRoot })

console.log('[proof] createRuntime(' + APP + ') — resolve + join deploy topic...')
const t0 = Date.now()
const rt = await createRuntime({ name: APP, store, binDir, host: HOST, interval: 0, runtimePath: 'bare' })
console.log('[proof] runtime ready — starting (fetch closure + materialize + finalize)...')
await rt.start()
const ms = Date.now() - t0

const installPath = store.appPath(APP)
const ready = rt.readyToApply()
const treeHash = ready ? ready.newTreeHash : null
const rootOk = treeHash && store.hasTree(treeHash)
const entryOk = fs.existsSync(path.join(installPath, 'package.json'))
const nm = path.join(installPath, 'node_modules')
const nmCount = fs.existsSync(nm) ? fs.readdirSync(nm).filter(n => n !== '.pgh').length : 0
const launcher = path.join(binDir, APP)
const launcherOk = fs.existsSync(launcher)
const launcherBody = launcherOk ? fs.readFileSync(launcher, 'utf8') : ''
const launcherPointsIn = launcherBody.includes(installPath)
const manifestPath = path.join(installPath, '.pgh-install.json')
const manifestOk = fs.existsSync(manifestPath)
let manName = null
try { manName = JSON.parse(fs.readFileSync(manifestPath, 'utf8')).name } catch {}

console.log('[proof] ── RESULT ─────────────────────────────')
console.log('[proof] createRuntime + start in ' + ms + 'ms')
console.log('[proof] install tree present:      ' + rootOk + '  (tree ' + String(treeHash).slice(0, 12) + ')')
console.log('[proof] install dir + pkg.json:    ' + entryOk + '  (' + installPath + ')')
console.log('[proof] node_modules wired:        ' + nmCount + ' packages')
console.log('[proof] launcher written:          ' + launcherOk + '  -> points into store: ' + launcherPointsIn)
console.log('[proof] install manifest written:  ' + manifestOk + '  (name=' + manName + ')')
const pass = rootOk && entryOk && nmCount > 0 && launcherOk && launcherPointsIn && manifestOk && manName === APP
console.log('[proof] ' + (pass ? 'PASS ✅ — createRuntime produced a COMPLETE install (tree + node_modules + launcher + manifest) over the real network from { name } alone' : 'FAIL ❌'))

try { await rt.stop() } catch {}
try { if (rt._fetcher) await rt._fetcher.close() } catch {}
try { fs.rmSync(storeRoot, { recursive: true, force: true }) } catch {}
try { fs.rmSync(binDir, { recursive: true, force: true }) } catch {}
process.exit(pass ? 0 : 1)