hyperpubee
Version:
Self-publishing over the decentralised internet
129 lines (103 loc) • 3.9 kB
JavaScript
const { expect } = require('chai')
const { createArrayFromStream } = require('../lib/core/utils')
const { CONTENT, METADATA } = require('../lib/core/constants')
const api = require('../lib/api')
const { hyperInterfaceFactory } = require('./fixtures')
const utils = require('../lib/core/utils')
describe('Test api', function () {
let hyperInterface
this.beforeEach(async function () {
hyperInterface = await hyperInterfaceFactory()
})
this.afterEach(async function () {
await hyperInterface.close()
})
it('can build a poem from text', async function () {
const text = '[poetry]# My poem title\n\nLine 1\nLine 2\nLine 3'
const poemHyperbee = await api.buildWorkFromText(
text,
'my work',
hyperInterface
)
const bee = await poemHyperbee.getReadOnlyBee()
const actualContent = await createArrayFromStream(
bee.sub(CONTENT).createReadStream()
)
const expectedContent = ['My poem title', 'Line 1', 'Line 2', 'Line 3']
expect(actualContent).to.deep.equal(expectedContent)
})
it('can build prose from text', async function () {
const text = '[prose]# My prose title\n\nparagraph1.\n\nParagraph2.'
const pubee = await api.buildWorkFromText(
text,
'my work',
hyperInterface
)
const bee = await pubee.getReadOnlyBee()
const actualContent = await createArrayFromStream(
bee.sub(CONTENT).createReadStream()
)
const expectedContent = ['My prose title', 'paragraph1.', 'Paragraph2.']
expect(actualContent).to.deep.equal(expectedContent)
})
it('can add an author', async function () {
const text = '[prose]paragraph1.'
const pubee = await api.buildWorkFromText(
text,
'my work',
hyperInterface,
'Someone'
)
const metadata = await pubee.get(METADATA)
expect(metadata.author).to.equal('Someone')
})
describe('Test reading a pubee', function () {
let originalBee
this.beforeEach(async function () {
const text = '[poetry]# The title\n\nLine 1\nLine 2'
originalBee = await api.buildWorkFromText(
text,
'my test work',
hyperInterface
)
})
it('Can read a pubee from key', async function () {
const otherBee = await api.readWork(originalBee.key, hyperInterface)
await otherBee.ensureIsReadable()
const readContentStream = otherBee.bee.createReadStream()
const readContent = await createArrayFromStream(readContentStream)
const originalContentStream = originalBee.bee.createReadStream()
const originalContent = await createArrayFromStream(
originalContentStream
)
expect(originalContent.length).to.equal(10) // Sanity check
expect(originalContent).to.deep.equal(readContent)
})
it('Can get the root', async function () {
const root = await api.getRoot(originalBee.key, hyperInterface)
expect(root.length).to.equal(2) // Title and poem
})
it('Can get an entry', async function () {
const location = utils.getIndexedKeyFromComponents([CONTENT, 0])
const content = await api.getEntry(originalBee.key, location, hyperInterface)
expect(content).to.equal('The title')
})
it('Can get all embeddings', async function () {
const text = `[poetry]
Embed <${'a'.repeat(64)}/content/0>
More <${'b'.repeat(64)}/content/4>
`
const pubee = await api.buildWorkFromText(
text,
'test work',
hyperInterface
)
const embeddings = await api.getAllEmbeddings(pubee.key, hyperInterface)
expect(embeddings.length).to.deep.equal(2)
expect(embeddings[0].referencedHash === 'a'.repeat(64))
expect(embeddings[1].referencedHash === 'b'.repeat(64))
})
// TODO: Missings tests: what happens if key not found?
// Invalid key? Valid key but no hyperbee/hyperpubee?
})
})