From ce003a810a23d05bb2997421da0440b2a5300bab Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 9 Feb 2024 15:45:32 +0100 Subject: [PATCH 1/3] chore: add test to verified fetch example Adds a test for JSON-codec data --- .../helia-browser-verified-fetch/package.json | 2 +- .../test/blockstore.js | 54 +++++++++++++++++++ .../test/index.spec.js | 20 ++++--- .../test/vite.config.js | 11 ++++ 4 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 examples/helia-browser-verified-fetch/test/blockstore.js create mode 100644 examples/helia-browser-verified-fetch/test/vite.config.js diff --git a/examples/helia-browser-verified-fetch/package.json b/examples/helia-browser-verified-fetch/package.json index 74bc55ad..c239f092 100644 --- a/examples/helia-browser-verified-fetch/package.json +++ b/examples/helia-browser-verified-fetch/package.json @@ -8,7 +8,7 @@ "dev": "vite", "build": "tsc && vite build", "preview": "vite preview", - "test": "npm run build && test-browser-example test" + "test": "vite build -c ./test/vite.config.js && test-browser-example test" }, "dependencies": { "@helia/verified-fetch": "next", diff --git a/examples/helia-browser-verified-fetch/test/blockstore.js b/examples/helia-browser-verified-fetch/test/blockstore.js new file mode 100644 index 00000000..e8d94b65 --- /dev/null +++ b/examples/helia-browser-verified-fetch/test/blockstore.js @@ -0,0 +1,54 @@ +import { CodeError } from '@libp2p/interface' +import { base32 } from 'multiformats/bases/base32' +import { CID } from 'multiformats/cid' +import * as raw from 'multiformats/codecs/raw' +import * as Digest from 'multiformats/hashes/digest' + +export class MemoryBlockstore { + data + + constructor () { + this.data = new Map() + + // prefill blockstore with test fixtures + + // JSON codec, contents are `{ "hello": "world" }` + this.put( + CID.parse('bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea'), + Uint8Array.from([123, 34, 104, 101, 108, 108, 111, 34, 58, 34, 119, 111, 114, 108, 100, 34, 125]) + ) + } + + put (key, val) { // eslint-disable-line require-await + this.data.set(base32.encode(key.multihash.bytes), val) + + return key + } + + get (key) { + const buf = this.data.get(base32.encode(key.multihash.bytes)) + + if (buf == null) { + throw new CodeError('Not found', 'ERR_NOT_FOUND') + } + + return buf + } + + has (key) { + return this.data.has(base32.encode(key.multihash.bytes)) + } + + async delete (key) { + this.data.delete(base32.encode(key.multihash.bytes)) + } + + async * getAll () { + for (const [key, value] of this.data.entries()) { + yield { + cid: CID.createV1(raw.code, Digest.decode(base32.decode(key))), + block: value + } + } + } +} diff --git a/examples/helia-browser-verified-fetch/test/index.spec.js b/examples/helia-browser-verified-fetch/test/index.spec.js index 5196899c..45afd44f 100644 --- a/examples/helia-browser-verified-fetch/test/index.spec.js +++ b/examples/helia-browser-verified-fetch/test/index.spec.js @@ -3,25 +3,29 @@ import { setup, expect } from 'test-ipfs-example/browser' // Setup const test = setup() -const testCidPath = 'ipfs://bagaaieracglt4ey6qsxtvzqsgwnsw3b6p2tb7nmx5wdgxur2zia7q6nnzh7q' - test.describe('Use @helia/verified-fetch With react and vite', () => { // DOM const ipfsPathInput = '#ipfs-path' - // const fetchOutput = '#output' - // const fetchAutoBtn = '#button-fetch-auto' + const fetchOutput = '#output' + const fetchAutoBtn = '#button-fetch-auto' test.beforeEach(async ({ servers, page }) => { await page.goto(servers[0].url) }) - test('should properly render ui with the ipfs path input', async ({ page }) => { + test('should properly render ui with the ipfs path input and display JSON', async ({ page }) => { // wait for helia node to be online const ipfsPath = await page.locator(ipfsPathInput) await expect(ipfsPath).toHaveClass(/bg-gray-50/) - await page.fill(ipfsPathInput, testCidPath) - // await page.click(fetchAutoBtn) - // await page.locator(fetchOutput).waitFor('visible') + await page.fill(ipfsPathInput, 'ipfs://bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea') + await page.click(fetchAutoBtn) + await page.locator(fetchOutput).waitFor('visible') + + const output = await page.locator(fetchOutput) + await expect(output).toContainText( + '{ "hello": "world" }', + { timeout: 2000 } + ) }) }) diff --git a/examples/helia-browser-verified-fetch/test/vite.config.js b/examples/helia-browser-verified-fetch/test/vite.config.js new file mode 100644 index 00000000..e2d8d001 --- /dev/null +++ b/examples/helia-browser-verified-fetch/test/vite.config.js @@ -0,0 +1,11 @@ +import { resolve } from 'path' +import defaultConfig from '../vite.config.js' + +// override resolution of `blockstore-core` module so we can pre-fill a memory +// blockstore with test data +defaultConfig.resolve ??= {} +defaultConfig.resolve.alias ??= {} +defaultConfig.resolve.alias['blockstore-core'] = resolve(process.cwd(), 'test/blockstore.js') + +// https://vitejs.dev/config/ +export default defaultConfig From 05e88cfd72caffcbccd5defe54571376200739b4 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 9 Feb 2024 15:49:42 +0100 Subject: [PATCH 2/3] chore: test verified fetch during ci --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dc63a6a2..c3000c36 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,7 @@ jobs: matrix: project: - helia-101 + - helia-browser-verified-fetch - helia-cjs - helia-electron - helia-esbuild @@ -86,6 +87,7 @@ jobs: project: - helia-101 - helia-cjs + - helia-browser-verified-fetch - helia-electron - helia-esbuild - helia-jest From 151981ea634987cb77de992ba537ce2b5c264f40 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 9 Feb 2024 17:07:01 +0100 Subject: [PATCH 3/3] chore: add tests for other types --- .../test/blockstore.js | 10 +++--- .../test/fixtures.js | 16 +++++++++ .../test/index.spec.js | 36 ++++++++++++++++++- 3 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 examples/helia-browser-verified-fetch/test/fixtures.js diff --git a/examples/helia-browser-verified-fetch/test/blockstore.js b/examples/helia-browser-verified-fetch/test/blockstore.js index e8d94b65..b8b48452 100644 --- a/examples/helia-browser-verified-fetch/test/blockstore.js +++ b/examples/helia-browser-verified-fetch/test/blockstore.js @@ -3,6 +3,7 @@ import { base32 } from 'multiformats/bases/base32' import { CID } from 'multiformats/cid' import * as raw from 'multiformats/codecs/raw' import * as Digest from 'multiformats/hashes/digest' +import fixtures from './fixtures.js' export class MemoryBlockstore { data @@ -11,12 +12,9 @@ export class MemoryBlockstore { this.data = new Map() // prefill blockstore with test fixtures - - // JSON codec, contents are `{ "hello": "world" }` - this.put( - CID.parse('bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea'), - Uint8Array.from([123, 34, 104, 101, 108, 108, 111, 34, 58, 34, 119, 111, 114, 108, 100, 34, 125]) - ) + Object.values(fixtures).forEach((fixture) => { + this.put(fixture.cid, fixture.data) + }) } put (key, val) { // eslint-disable-line require-await diff --git a/examples/helia-browser-verified-fetch/test/fixtures.js b/examples/helia-browser-verified-fetch/test/fixtures.js new file mode 100644 index 00000000..987afbe3 --- /dev/null +++ b/examples/helia-browser-verified-fetch/test/fixtures.js @@ -0,0 +1,16 @@ +import { CID } from 'multiformats/cid' + +export default { + json: { + cid: CID.parse('bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea'), + data: Uint8Array.from([123, 34, 104, 101, 108, 108, 111, 34, 58, 34, 119, 111, 114, 108, 100, 34, 125]) + }, + dagJson: { + cid: CID.parse('baguqeerasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea'), + data: Uint8Array.from([123, 34, 104, 101, 108, 108, 111, 34, 58, 34, 119, 111, 114, 108, 100, 34, 125]) + }, + dagCbor: { + cid: CID.parse('bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae'), + data: Uint8Array.from([161, 101, 104, 101, 108, 108, 111, 101, 119, 111, 114, 108, 100]) + } +} diff --git a/examples/helia-browser-verified-fetch/test/index.spec.js b/examples/helia-browser-verified-fetch/test/index.spec.js index 45afd44f..e78637f6 100644 --- a/examples/helia-browser-verified-fetch/test/index.spec.js +++ b/examples/helia-browser-verified-fetch/test/index.spec.js @@ -1,4 +1,5 @@ import { setup, expect } from 'test-ipfs-example/browser' +import fixtures from './fixtures.js' // Setup const test = setup() @@ -18,7 +19,40 @@ test.describe('Use @helia/verified-fetch With react and vite', () => { const ipfsPath = await page.locator(ipfsPathInput) await expect(ipfsPath).toHaveClass(/bg-gray-50/) - await page.fill(ipfsPathInput, 'ipfs://bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea') + await page.fill(ipfsPathInput, `ipfs://${fixtures.json.cid.toString()}`) + await page.click(fetchAutoBtn) + await page.locator(fetchOutput).waitFor('visible') + + const output = await page.locator(fetchOutput) + await expect(output).toContainText( + '{ "hello": "world" }', + { timeout: 2000 } + ) + }) + + test('should properly render ui with the ipfs path input and display DAG-JSON', async ({ page }) => { + // wait for helia node to be online + const ipfsPath = await page.locator(ipfsPathInput) + await expect(ipfsPath).toHaveClass(/bg-gray-50/) + + await page.fill(ipfsPathInput, `ipfs://${fixtures.dagJson.cid.toString()}`) + await page.click(fetchAutoBtn) + await page.locator(fetchOutput).waitFor('visible') + + const output = await page.locator(fetchOutput) + await expect(output).toContainText( + '{ "hello": "world" }', + { timeout: 2000 } + ) + }) + + // TODO: DAG-CBOR is broken - https://github.com/ipfs/helia/pull/426 + test.skip('should properly render ui with the ipfs path input and display DAG-CBOR', async ({ page }) => { + // wait for helia node to be online + const ipfsPath = await page.locator(ipfsPathInput) + await expect(ipfsPath).toHaveClass(/bg-gray-50/) + + await page.fill(ipfsPathInput, `ipfs://${fixtures.dagCbor.cid.toString()}`) await page.click(fetchAutoBtn) await page.locator(fetchOutput).waitFor('visible')