Files
modules/state-crdts/hyper-p2p-crdt-grow-only-set/test/test.js
T
2026-05-21 02:00:00 -04:00

52 lines
1.2 KiB
JavaScript

require('bare-process/global')
const test = require('brittle')
const { HyperP2PCrdtGrowOnlySet, PROTOCOL } = require('../index.js')
test('exports', (t) => {
t.ok(HyperP2PCrdtGrowOnlySet)
t.is(PROTOCOL, 'crdt-grow-only-set/v1')
})
test('add values has', async (t) => {
const m = new HyperP2PCrdtGrowOnlySet()
m.add('a')
m.add('b')
t.ok(m.has('a'))
t.is(m.values().sort().join(','), 'a,b')
await m.close()
})
test('add is idempotent', async (t) => {
const m = new HyperP2PCrdtGrowOnlySet()
m.add('x')
t.is(m.add('x'), false)
t.is(m.values().length, 1)
await m.close()
})
test('validation', async (t) => {
const m = new HyperP2PCrdtGrowOnlySet()
try { m.add(null); t.fail('expected throw') } catch (e) { t.ok(e) }
await m.close()
})
test('bulkAdd and merge', async (t) => {
const m = new HyperP2PCrdtGrowOnlySet()
t.is(m.bulkAdd(['a', 'b', 'a']), 2)
const other = new HyperP2PCrdtGrowOnlySet()
other.add('c')
t.is(m.merge(other.values()), 1)
t.is(m.size(), 3)
await m.close()
await other.close()
})
test('getStats', async (t) => {
const m = new HyperP2PCrdtGrowOnlySet()
m.add('z')
t.is(m.getStats().protocol, PROTOCOL)
t.is(m.getStats().mode, 'crdt')
t.is(m.getStats().size, 1)
await m.close()
})