Skip to content

Commit

Permalink
test(create-car): create car file test
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Jul 28, 2023
1 parent 91183ef commit 455ea2d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
4 changes: 2 additions & 2 deletions examples/helia-create-car/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
},
"dependencies": {
"@helia/car": "^1.0.0",
"@helia/unixfs": "^1.2.1",
"@helia/unixfs": "^1.4.1",
"@ipld/car": "^5.1.1",
"helia": "^1.0.0",
"helia": "^1.3.8",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/helia-create-car/src/components/CarCreator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ export default function CarCreator () {
<div style={{ borderRadius: '3px', padding: '1rem' }}>
<div>
<b>Car file CID: </b>
<span>{rootCID.toString()}</span>
<span id="carFileCID">{rootCID.toString()}</span>
</div>
<button onClick={downloadCarFile}>Download Car file</button>
<button id="downloadCarFile" onClick={downloadCarFile}>Download Car file</button>
</div>
)
}
76 changes: 59 additions & 17 deletions examples/helia-create-car/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,86 @@
import { createReadStream } from 'node:fs'
import { unixfs } from '@helia/unixfs'
import { CarReader } from '@ipld/car'
import { createHelia } from 'helia'
import { setup, expect } from 'test-ipfs-example/browser'

// Setup
const test = setup()

const filesToUpload = [
'./public/helia-create-car-demo.gif',
'./public/vite.svg'
]

const expectedCarCID = 'bafybeifsknwjwoby7gmnqlzj236rcq47q5pskkum7svsevsod5a74caxry'

test.describe('Use Helia With react and vite', () => {
// DOM
const heliaStatus = '#heliaStatus'
const textInput = '#textInput'
const commitTextButton = '#commitTextButton'
const cidOutput = '#cidOutput'
const fetchCommittedTextButton = '#fetchCommittedTextButton'
const committedTextOutput = '#committedTextOutput'
const fileInput = '#FileUploaderInput'
const downloadCarFileButton = '#downloadCarFile'
const cidOutput = '#carFileCID'

// async dependent variables
let expectedCIDs
let helia
let heliaFs

test.beforeEach(async ({ servers, page }) => {
await page.goto(servers[0].url)

helia = await createHelia({
start: false
})
heliaFs = unixfs(helia)

expectedCIDs = await Promise.all(filesToUpload.map(async (file) => {
const cid = await heliaFs.addByteStream(createReadStream(file))
return cid.toString()
}))
})

test('should properly initialize a Helia node and add/get a file', async ({ page }) => {
test('files can be converted to a valid car file', async ({ page }) => {
// wait for helia node to be online
const text = 'Hello Helia'
const status = await page.locator(heliaStatus)
await expect(status).toHaveCSS(
'border-color',
'rgb(0, 128, 0)', // green
{ timeout: 7000 }
)

// commit text to the blockstore
await page.fill(textInput, text)
await page.click(commitTextButton)
// select the files to upload
await page.setInputFiles(fileInput, filesToUpload)

await page.waitForSelector(`${cidOutput}:has-text("bafkreig7i5kbqdnoooievvfextf27eoherluxe2pi3j26hu6zpiauydydy")`)
// make sure the output CID matchs
await page.waitForSelector(cidOutput)
const cidOutputContent = await page.textContent(cidOutput)

expect(cidOutputContent).toContain('bafkreig7i5kbqdnoooievvfextf27eoherluxe2pi3j26hu6zpiauydydy')
expect(cidOutputContent).toContain(expectedCarCID)

// download the car file
const [download] = await Promise.all([
page.waitForEvent('download'), // wait for download to start
page.click(downloadCarFileButton)
])

// car available for debugging
await download.saveAs('./test-results/helia-create-car-demo.car')

const reader = await CarReader.fromIterable(await download.createReadStream())
// ensure the CID root matches
const roots = await reader.getRoots()
expect(roots.toString()).toContain(expectedCarCID)

// get all the CIDs in the car file
const carFileCids = []
for await (const cid of await reader.cids()) {
carFileCids.push(cid.toString())
}

// retrieve text from blockstore
await page.click(fetchCommittedTextButton)
await page.waitForSelector(`${committedTextOutput}:has-text("${text}")`)
const committedTextOutputLocator = await page.locator(committedTextOutput)
await expect(committedTextOutputLocator).toHaveText(`Committed Text: ${text}`, { timeout: 2000 })
// ensure the file CIDs are included in the car file
expectedCIDs.forEach((cid) => {
expect(carFileCids).toContain(cid)
})
})
})

0 comments on commit 455ea2d

Please sign in to comment.