-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(create-car): create car file test
- Loading branch information
Showing
3 changed files
with
63 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |