import test from 'brittle'
import Corestore from 'corestore'
import { mkdtempSync } from 'fs'
import { join } from 'path'
import { tmpdir } from 'os'
import { replicateAndSync } from 'autobase-test-helpers'
import { HyperOrgs } from '../index.js'
import { HyperIssues, issuesSchema } from 'hyper-issues'
function tmp () { return mkdtempSync(join(tmpdir(), 'orgs-issues-')) }
// #42 fix, proven at the library layer.
//
// An org Room built with composeSchemas([orgsSchema, issuesSchema])
// carries its issues IN the org's own autobase. So a peer that joins
// ONLY the org's record autobase (peer mode — mountOrg) also gets its
// issues, with NO separate tracker autobase and NO issueBootstrapKey
// conveyance. This is the linked-core (Keet db+blobs) shape.
test('org issues live in the org room and replicate WITH the org', async (t) => {
const owner = new HyperOrgs(new Corestore(tmp()), {
namespace: 'owner', extraSchemas: [issuesSchema]
})
await owner.ready()
await owner.create('alpha', 'group', { displayName: 'Alpha' })
const ownerRoom = owner.getOrgRoom('alpha')
const ownerIssues = new HyperIssues(null, { room: ownerRoom, ownsRoom: false })
await ownerIssues.ready()
const id = await ownerIssues.create({ title: 'org issue', body: 'b', author: 'owner' })
// Peer mounts ONLY alpha's record autobase (peer mode).
const peer = new HyperOrgs(new Corestore(tmp()), {
namespace: 'peer', extraSchemas: [issuesSchema]
})
await peer.ready()
await peer.mountOrg('alpha', ownerRoom.keyHex)
await replicateAndSync([owner.getOrgBase('alpha'), peer.getOrgBase('alpha')])
// Org record replicated (sanity).
const org = await peer.get('alpha')
t.is(org && org.displayName, 'Alpha', 'org record replicated to peer')
// The org issue replicated WITH the org — no separate tracker.
const peerIssues = new HyperIssues(null, { room: peer.getOrgRoom('alpha'), ownsRoom: false })
await peerIssues.ready()
const got = await peerIssues.get(id)
t.is(got && got.title, 'org issue', 'org issue replicated to peer via the org autobase')
await ownerIssues.close()
await peerIssues.close()
await owner.close()
await peer.close()
})
// Control: WITHOUT extraSchemas the org room ignores issue ops, so the
// issue never materialises (proves the replication above is due to the
// composed schema, not some other path).
test('control: org room without issuesSchema does not store issues', async (t) => {
const owner = new HyperOrgs(new Corestore(tmp()), { namespace: 'owner' }) // no extraSchemas
await owner.ready()
await owner.create('beta', 'group', { displayName: 'Beta' })
const room = owner.getOrgRoom('beta')
const issues = new HyperIssues(null, { room, ownsRoom: false })
await issues.ready()
const id = await issues.create({ title: 'ignored', author: 'owner' })
const got = await issues.get(id)
t.absent(got, 'issue op ignored when org room has no issuesSchema composed')
await issues.close()
await owner.close()
})