OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-issues / test / room-wrap.test.js

44 lines · javascript ref:
import test from 'brittle'
import Corestore from 'corestore'
import { mkdtempSync } from 'fs'
import { join } from 'path'
import { tmpdir } from 'os'
import { Room } from 'hyper-rooms'
import { HyperIssues } from '../index.js'
import { issuesSchema } from '../lib/schema.js'

function tmp () { return mkdtempSync(join(tmpdir(), 'issues-wrap-')) }

test('HyperIssues wraps an external Room and does NOT own it', async (t) => {
  const store = new Corestore(tmp())
  const room = new Room(store, { schema: issuesSchema, namespace: 'shared' })
  await room.ready()

  const issues = new HyperIssues(null, { room, ownsRoom: false })
  await issues.ready()

  const id = await issues.create({ title: 'wrapped', author: 'alice' })
  const got = await issues.get(id)
  t.is(got && got.title, 'wrapped', 'issue created/read through the wrapper')

  await issues.close() // must NOT close the borrowed room
  t.absent(room.base.closed, 'borrowed room still open after wrapper close')

  // room remains usable directly after the wrapper closed
  await room.append({ type: 'create-issue', id: 999, title: 'direct', userId: 'bob', timestamp: Date.now() })
  await room.base.update()
  const direct = await room.view.get('issue:999')
  t.is(direct && direct.value.title, 'direct', 'room still usable after wrapper close')

  await room.close()
})

test('HyperIssues owns the Room it creates (close tears it down)', async (t) => {
  const issues = new HyperIssues(new Corestore(tmp()), { namespace: 'owned' })
  await issues.ready()
  await issues.create({ title: 'x', author: 'a' })
  const base = issues.base
  await issues.close()
  t.ok(base.closed, 'self-created room closed by wrapper close')
})