Skip to content

Commit eadebad

Browse files
committed
fix: vis output
1 parent 2e76d18 commit eadebad

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

test/clock.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// eslint-disable-next-line no-unused-vars
22
import * as API from '../src/clock/api.js'
33
import { advance, EventBlock, vis } from '../src/clock/index.js'
4-
import { Blockstore, randomCID } from './helpers.js'
4+
import { Blockstore, randomCID, clockVis } from './helpers.js'
55

66
async function randomEventData () {
77
return {
@@ -19,7 +19,7 @@ describe('clock', () => {
1919
await blocks.put(event.cid, event.bytes)
2020
const head = await advance(blocks, [], event.cid)
2121

22-
for await (const line of vis(blocks, head)) console.log(line)
22+
await clockVis(blocks, head)
2323
assert.equal(head.length, 1)
2424
assert.equal(head[0].toString(), event.cid.toString())
2525
})
@@ -37,7 +37,7 @@ describe('clock', () => {
3737

3838
head = await advance(blocks, head, event.cid)
3939

40-
for await (const line of vis(blocks, head)) console.log(line)
40+
await clockVis(blocks, head)
4141
assert.equal(head.length, 1)
4242
assert.equal(head[0].toString(), event.cid.toString())
4343
})
@@ -59,7 +59,7 @@ describe('clock', () => {
5959
await blocks.put(event1.cid, event1.bytes)
6060
head = await advance(blocks, head, event1.cid)
6161

62-
for await (const line of vis(blocks, head)) console.log(line)
62+
await clockVis(blocks, head)
6363
assert.equal(head.length, 2)
6464
assert.equal(head[0].toString(), event0.cid.toString())
6565
assert.equal(head[1].toString(), event1.cid.toString())
@@ -94,7 +94,7 @@ describe('clock', () => {
9494
await blocks.put(event4.cid, event4.bytes)
9595
head = await advance(blocks, head, event4.cid)
9696

97-
for await (const line of vis(blocks, head)) console.log(line)
97+
await clockVis(blocks, head)
9898
assert.equal(head.length, 2)
9999
assert.equal(head[0].toString(), event3.cid.toString())
100100
assert.equal(head[1].toString(), event4.cid.toString())
@@ -137,7 +137,7 @@ describe('clock', () => {
137137
await blocks.put(event5.cid, event5.bytes)
138138
head = await advance(blocks, head, event5.cid)
139139

140-
for await (const line of vis(blocks, head)) console.log(line)
140+
await clockVis(blocks, head)
141141
assert.equal(head.length, 1)
142142
assert.equal(head[0].toString(), event5.cid.toString())
143143
})
@@ -192,7 +192,7 @@ describe('clock', () => {
192192
head = await advance(blocks, head, event6.cid)
193193
assert.equal(count - before, 10)
194194

195-
for await (const line of vis(blocks, head)) console.log(line)
195+
await clockVis(blocks, head)
196196
assert.equal(head.length, 2)
197197
assert.equal(head[0].toString(), event5.cid.toString())
198198
assert.equal(head[1].toString(), event6.cid.toString())
@@ -214,7 +214,7 @@ describe('clock', () => {
214214

215215
head = await advance(blocks, head, event1.cid)
216216

217-
for await (const line of vis(blocks, head)) console.log(line)
217+
await clockVis(blocks, head)
218218
assert.equal(head.length, 1)
219219
assert.equal(head[0].toString(), event1.cid.toString())
220220
})

test/crdt.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as API from '../src/crdt/api.js'
44
import { advance, vis } from '../src/clock/index.js'
55
import { put, get, root, entries } from '../src/crdt/index.js'
66
import * as Batch from '../src/crdt/batch/index.js'
7-
import { Blockstore, randomCID, randomString } from './helpers.js'
7+
import { Blockstore, clockVis, randomCID, randomString } from './helpers.js'
88

99
describe('CRDT', () => {
1010
it('put a value to a new clock', async () => {
@@ -315,9 +315,7 @@ class TestPail {
315315
: ''})`
316316
/** @type {(e: API.EventBlockView<API.Operation>) => string} */
317317
const renderNodeLabel = event => `${shortLink(event.cid)}\\n${renderOp(event.value.data)}`
318-
for await (const line of vis(this.blocks, this.head, { renderNodeLabel })) {
319-
console.log(line)
320-
}
318+
await clockVis(this.blocks, this.head, { renderNodeLabel })
321319
}
322320

323321
/** @param {string} key */

test/helpers.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import clc from 'cli-color'
55
import archy from 'archy'
66
// eslint-disable-next-line no-unused-vars
77
import * as API from '../src/api.js'
8+
// eslint-disable-next-line no-unused-vars
9+
import * as ClockAPI from '../src/clock/api.js'
810
import { ShardFetcher, decodeBlock } from '../src/shard.js'
911
import { MemoryBlockstore } from '../src/block.js'
1012
import { entries, get, put } from '../src/index.js'
13+
import * as Clock from '../src/clock/index.js'
1114

1215
/**
1316
* @param {number} min
@@ -156,3 +159,18 @@ export async function randomBytes (size) {
156159
myCrypto.getRandomValues(bytes)
157160
return bytes
158161
}
162+
163+
/**
164+
* @template T
165+
* @param {API.BlockFetcher} blocks Block storage.
166+
* @param {ClockAPI.EventLink<T>[]} head
167+
* @param {object} [options]
168+
* @param {(b: ClockAPI.EventBlockView<T>) => string} [options.renderNodeLabel]
169+
*/
170+
export const clockVis = async (blocks, head, options) => {
171+
const lines = []
172+
for await (const line of Clock.vis(blocks, head, options)) {
173+
lines.push(line)
174+
}
175+
console.log(lines.join('\n'))
176+
}

0 commit comments

Comments
 (0)