Skip to content

Commit

Permalink
fix: add js tests to this repo
Browse files Browse the repository at this point in the history
Trying to debug these tests in CI is incredibly difficult and since
they depend on older versions of previously published modules, if
something breaks it's very hard to fix it.

Instead follow the pattern in the perf tests and include the full
test implementations here.
  • Loading branch information
achingbrain committed May 22, 2024
1 parent 7ce1a02 commit 07d6ef6
Show file tree
Hide file tree
Showing 30 changed files with 31,050 additions and 6,385 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ __pycache__/
### NodeJS

node_modules
dist

# ignore system files
.DS_Store
117 changes: 117 additions & 0 deletions transport-interop/impl/js/v0.45/.aegir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* eslint-disable no-console */
import http from 'http'
import { pEvent } from 'p-event'
import { createClient } from 'redis'

const redisAddr = process.env.redis_addr || 'redis:6379'
const transport = process.env.transport
const isDialer = process.env.is_dialer === 'true'

/** @type {import('aegir/types').PartialOptions} */
export default {
test: {
browser: {
config: {
// Ignore self signed certificates
browserContextOptions: { ignoreHTTPSErrors: true }
}
},
async before () {
// import after build is complete
const { createRelay } = await import('./dist/test/fixtures/relay.js')

let relayNode
let relayAddr
if (transport === 'webrtc' && !isDialer) {
relayNode = await createRelay()

const sortByNonLocalIp = (a, b) => {
if (a.toString().includes('127.0.0.1')) {
return 1
}
return -1
}

relayAddr = relayNode.getMultiaddrs().sort(sortByNonLocalIp)[0].toString()
}

const redisClient = createClient({
url: `redis://${redisAddr}`
})
redisClient.on('error', (err) => {
console.error('Redis client error:', err)
})
await redisClient.connect()

const requestListener = async function (req, res) {
const requestJSON = await new Promise(resolve => {
let body = ''
req.on('data', function (data) {
body += data
})

req.on('end', function () {
resolve(JSON.parse(body))
})
})

try {
const redisRes = await redisClient.sendCommand(requestJSON)

if (redisRes == null) {
console.error('Redis failure - sent', requestJSON, 'received', redisRes)

res.writeHead(500, {
'Access-Control-Allow-Origin': '*'
})
res.end(JSON.stringify({
message: 'Redis sent back null'
}))

return
}

res.writeHead(200, {
'Access-Control-Allow-Origin': '*'
})
res.end(JSON.stringify(redisRes))
} catch (err) {
console.error('Error in redis command:', err)
res.writeHead(500, {
'Access-Control-Allow-Origin': '*'
})
res.end(err.toString())
}
}

const proxyServer = http.createServer(requestListener)
proxyServer.listen(0)

await pEvent(proxyServer, 'listening', {
signal: AbortSignal.timeout(5000)
})

return {
redisClient,
relayNode,
proxyServer,
env: {
...process.env,
RELAY_ADDR: relayAddr,
REDIS_PROXY_PORT: proxyServer.address().port
}
}
},
async after (_, { proxyServer, redisClient, relayNode }) {
await new Promise(resolve => {
proxyServer?.close(() => resolve())
})

try {
// We don't care if this fails
await redisClient?.disconnect()
await relayNode?.stop()
} catch { }
}
}
}
13 changes: 8 additions & 5 deletions transport-interop/impl/js/v0.45/BrowserDockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ FROM ${BASE_IMAGE} as js-libp2p-base

FROM mcr.microsoft.com/playwright


COPY --from=js-libp2p-base /app/ /app/
WORKDIR /app/interop
WORKDIR /app

# We install browsers here instead of the cached version so that we use the latest browsers at run time.
# Ideally this would also be pinned, but playwright controls this, so there isn't much we can do about it.
# By installing here, we avoid installing it at test time.
RUN npx playwright install-deps
RUN npx playwright install
ARG BROWSER=chromium # Options: chromium, firefox, webkit
ENV BROWSER=$BROWSER

ENTRYPOINT npm test -- --build false --types false -t browser -- --browser $BROWSER
# Options: chromium, firefox, webkit
ARG BROWSER=chromium
ENV BROWSER=${BROWSER}

ENTRYPOINT npm test -- -t browser -- --browser $BROWSER
18 changes: 12 additions & 6 deletions transport-interop/impl/js/v0.45/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Here because we want to fetch the node_modules within docker so that it's
# installed on the same platform the test is run. Otherwise tools like `esbuild` will fail to run
FROM node:18
FROM node:22-bullseye-slim

WORKDIR /app
COPY . .
RUN npm i && npm run build

WORKDIR /app/interop
RUN npm i && npm run build
COPY package*.json .aegir.js tsconfig.json ./
COPY src ./src
COPY test ./test

# disable colored output and CLI animation from test runners
ENV CI true

RUN npm ci
RUN npm run build

ENTRYPOINT [ "npm", "test", "--", "--build", "false", "--types", "false", "-t", "node" ]
ENTRYPOINT npm test -- -t node
21 changes: 11 additions & 10 deletions transport-interop/impl/js/v0.45/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
image_name := js-v0.45
commitSha := 47a96706527fd82cd9daf3cc298f56866a5d027a

# TODO Enable webkit once https://github.com/libp2p/js-libp2p/pull/1627 is in
all: image.json chromium-image.json firefox-image.json
all: image.json chromium-image.json firefox-image.json update-lock-file

# Necessary because multistage builds require a docker image name rather than a digest to be used
load-image-json: image.json
Expand All @@ -18,17 +17,19 @@ firefox-image.json: load-image-json BrowserDockerfile
docker image inspect firefox-${image_name} -f "{{.Id}}" | \
xargs -I {} echo "{\"imageID\": \"{}\"}" > $@

image.json: js-libp2p-${commitSha}
cd js-libp2p-${commitSha} && docker build -t ${image_name} -f ../Dockerfile .
# We update the lock file here so that we make sure we are always using the correct lock file.
# If this changes, CI will fail since there are unstaged changes.
update-lock-file: image.json
CONTAINER_ID=$$(docker create $$(jq -r .imageID image.json)); \
docker cp $$CONTAINER_ID:/app/package-lock.json ./package-lock.json; \
docker rm $$CONTAINER_ID

image.json:
docker build -t ${image_name} -f ./Dockerfile .
docker image inspect ${image_name} -f "{{.Id}}" | \
xargs -I {} echo "{\"imageID\": \"{}\"}" > $@

js-libp2p-${commitSha}:
wget -O js-libp2p-${commitSha}.zip "https://github.com/libp2p/js-libp2p/archive/${commitSha}.zip"
unzip -o js-libp2p-${commitSha}.zip
unzip -o js-libp2p-${commitSha}.zip

clean:
rm -rf image.json js-libp2p-*.zip js-libp2p-* *-image.json
rm -rf *-image.json

.PHONY: all clean browser-images load-image-json
Loading

0 comments on commit 07d6ef6

Please sign in to comment.