This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add playwright tests and add to CI (#87)
* Add playwright tests and add to CI * Install deps * Replace direct html injection in the example to actually creating dom elements * Small refactors suggested by ckousik * Implement Glen's suggestions * Add version to @libp2p/webrtc * Update example import to use @libp2p/webrtc
- Loading branch information
Showing
4 changed files
with
159 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: CI | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
concurrency: | ||
group: ${{ github.head_ref || github.ref_name }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
examples: | ||
runs-on: ubuntu-latest | ||
name: Test example ${{ matrix.project }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
project: | ||
- browser-to-server | ||
defaults: | ||
run: | ||
working-directory: examples/${{ matrix.project }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: lts/* | ||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: '>=1.19.0' | ||
- name: Install dependencies | ||
run: npm install | ||
working-directory: . | ||
- name: Build example | ||
run: npm run build | ||
working-directory: . | ||
- name: Install dependencies | ||
run: npm install | ||
- name: Install Playwright | ||
run: npx -y playwright install --with-deps | ||
- name: Run tests | ||
run: npm run test | ||
env: | ||
CI: true |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* eslint-disable no-console */ | ||
import { test, expect } from '@playwright/test' | ||
import { playwright } from 'test-util-ipfs-example' | ||
import { spawn, exec } from 'child_process' | ||
import { existsSync } from 'fs' | ||
|
||
// Setup | ||
const play = test.extend({ | ||
...playwright.servers() | ||
}) | ||
|
||
async function spawnGoLibp2p() { | ||
if (!existsSync('../../examples/go-libp2p-server/go-libp2p-server')) { | ||
await new Promise((resolve, reject) => { | ||
exec('go build', | ||
{ cwd: '../../examples/go-libp2p-server' }, | ||
(error, stdout, stderr) => { | ||
if (error) { | ||
throw (`exec error: ${error}`) | ||
} | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
const server = spawn('./go-libp2p-server', [], { cwd: '../../examples/go-libp2p-server', killSignal: 'SIGINT' }) | ||
server.stderr.on('data', (data) => { | ||
console.log(`stderr: ${data}`, typeof data) | ||
}) | ||
const serverAddr = await (new Promise(resolve => { | ||
server.stdout.on('data', (data) => { | ||
console.log(`stdout: ${data}`, typeof data) | ||
const addr = String(data).match(/p2p addr: ([^\s]*)/) | ||
if (addr !== null && addr.length > 0) { | ||
resolve(addr[1]) | ||
} | ||
}) | ||
})) | ||
return { server, serverAddr } | ||
} | ||
|
||
play.describe('bundle ipfs with parceljs:', () => { | ||
// DOM | ||
const connectBtn = '#connect' | ||
const connectAddr = '#peer' | ||
const messageInput = '#message' | ||
const sendBtn = '#send' | ||
const output = '#output' | ||
|
||
let server | ||
let serverAddr | ||
|
||
// eslint-disable-next-line no-empty-pattern | ||
play.beforeAll(async ({ }, testInfo) => { | ||
testInfo.setTimeout(5 * 60_000) | ||
const s = await spawnGoLibp2p() | ||
server = s.server | ||
serverAddr = s.serverAddr | ||
console.log('Server addr:', serverAddr) | ||
}, {}) | ||
|
||
play.afterAll(() => { | ||
server.kill('SIGINT') | ||
}) | ||
|
||
play.beforeEach(async ({ servers, page }) => { | ||
const url = `http://localhost:${servers[0].port}/` | ||
console.log(url) | ||
await page.goto(url) | ||
}) | ||
|
||
play('should connect to a go-libp2p node over webtransport', async ({ page }) => { | ||
const message = 'hello' | ||
|
||
// add the go libp2p multiaddress to the input field and submit | ||
await page.fill(connectAddr, serverAddr) | ||
await page.click(connectBtn) | ||
|
||
// send the relay message to the go libp2p server | ||
await page.fill(messageInput, message) | ||
await page.click(sendBtn) | ||
|
||
await page.waitForSelector('#output:has(div)') | ||
|
||
// Expected output: | ||
// | ||
// Dialing '${serverAddr}' | ||
// Peer connected '${serverAddr}' | ||
// Sending message '${message}' | ||
// Received message '${message}' | ||
const connections = await page.textContent(output) | ||
expect(connections).toContain(`Dialing '${serverAddr}'`) | ||
expect(connections).toContain(`Peer connected '${serverAddr}'`) | ||
expect(connections).toContain(`Sending message '${message}'`) | ||
expect(connections).toContain(`Received message '${message}'`) | ||
}) | ||
}) |