OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git-v3 / test / org-issues-linked-core.test.js

81 lines · javascript ref:
// Linked-core org tracker at the DAEMON layer (#42).
//
// Org issues live in the org's own Room (the daemon builds HyperOrgs with
// extraSchemas:[issuesSchema], and issueRegistry.getForOrg wraps the org
// Room). So a peer that joins ONLY the org (peer mode) replicates the
// org's issues too — across daemons on DIFFERENT identity topics, purely
// via the org room topic. No separate issues:org:<name> autobase, no
// issueBootstrapKey conveyance.

import test from 'brittle'
import process from '#node:process'
import { mkdtempSync } from '#node:fs'
import { join } from '#node:path'
import { tmpdir } from '#node:os'
import b4a from 'b4a'
import createTestnet from '@hyperswarm/testnet'

import { startPearGit } from '../index.js'

let n = 0
const tmpStorage = () => mkdtempSync(join(tmpdir(), 'pgv3-oilc-' + (n++) + '-'))
const tmpSock = () => join(mkdtempSync(join(tmpdir(), 'pgv3-oilc-sock-' + (n++) + '-')), 'svc.sock')
async function waitFor (pred, ms = 15000) {
  const t0 = Date.now()
  while (Date.now() - t0 < ms) {
    try { if (await pred()) return true } catch {}
    await new Promise((r) => setTimeout(r, 50))
  }
  return false
}

test('org issue replicates to a peer that joined only the org (linked-core #42)', async (t) => {
  process.env.PGV3_USE_RAW_IDENTITY = '1'
  const testnet = await createTestnet(3, t.teardown)

  const A = await startPearGit({
    storage: tmpStorage(), uds: tmpSock(), http: { port: 0 },
    swarm: { swarm: { bootstrap: testnet.bootstrap } }, deploy: false, sync: false
  })
  t.teardown(async () => { try { await A.close() } catch {} })
  const B = await startPearGit({
    storage: tmpStorage(), uds: tmpSock(), http: { port: 0 },
    swarm: { swarm: { bootstrap: testnet.bootstrap } }, deploy: false, sync: false
  })
  t.teardown(async () => { try { await B.close() } catch {} })

  t.not(
    b4a.toString(A.svc.swarm.topic, 'hex'),
    b4a.toString(B.svc.swarm.topic, 'hex'),
    'daemons are on DIFFERENT identity topics'
  )

  // A: create org, announce its room, create an ORG ISSUE in it.
  await A.orgs.create('alpha', 'group', { displayName: 'Alpha' })
  A.svc.activateRoom(A.orgs.getOrgRoom('alpha').discoveryKey)

  const aTracker = await A.issueRegistry.getForOrg('alpha')
  // The org issue tracker IS the org record autobase (linked-core).
  t.is(
    aTracker.keyHex,
    A.orgs.getOrgRoom('alpha').keyHex,
    'org issue tracker == org record autobase (no separate tracker)'
  )
  const id = await aTracker.create({ title: 'org issue', body: 'b', author: 'owner' })

  const { invitation } = await A.orgs.createOrgInvite('alpha', { role: 'reader', swarm: A.svc.swarm.swarm })

  // B: pair into ONLY alpha (peer mode) over the swarm, join alpha's room topic.
  await B.orgs.joinOrg(invitation, { swarm: B.svc.swarm.swarm })
  B.svc.activateRoom(B.orgs.getOrgRoom('alpha').discoveryKey)

  // B sees the org issue purely via the org room replication.
  const saw = await waitFor(async () => {
    await B.orgs.sync()
    const tr = await B.issueRegistry.getForOrg('alpha')
    const iss = await tr.get(id)
    return !!(iss && iss.title === 'org issue')
  })
  t.ok(saw, 'org issue replicated to the peer via the org room (no key conveyed)')
})