test: add e2e tests (#645)

pull/649/head
Amio Jin 2023-08-21 11:00:43 -05:00 zatwierdzone przez GitHub
rodzic 0ca768cf1f
commit b44717c473
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
3 zmienionych plików z 69 dodań i 2 usunięć

16
.github/workflows/e2e.yml vendored 100644
Wyświetl plik

@ -0,0 +1,16 @@
name: E2E Test
on:
deployment_status:
jobs:
run-e2es:
if: github.event_name == 'deployment_status' && github.event.deployment_status.state == 'success'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run e2e tests
run: node test/e2e.test.mjs
env:
BASE_URL: ${{ github.event.deployment_status.target_url }}
MEMO_BADGE_TOKEN: ${{ secrets.MEMO_BADGE_TOKEN }}

Wyświetl plik

@ -1,10 +1,9 @@
name: lint and build
name: Lint and Build
on: pull_request
jobs:
build:
runs-on: ubuntu-latest
steps:

52
test/e2e.test.mjs 100644
Wyświetl plik

@ -0,0 +1,52 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { execSync } from 'node:child_process'
const BASE_URL = process.env.BASE_URL || 'https://badgen.net'
test('/static: simple static badge', async (t) => {
const badgeURL = `${BASE_URL}/static/v1.2.3/blue`
const response = await fetch(badgeURL)
assert.strictEqual(response.status, 200)
assert.strictEqual(response.headers.get('content-type'), 'image/svg+xml;charset=utf-8')
});
test('/memo: update "depoyed" badge', async (t) => {
if (!process.env.MEMO_BADGE_TOKEN) {
throw new Error('MEMO_BADGE_TOKEN is required to run this test')
}
const status = getGitLastCommitDate()
const label = 'Deployed'
const color = getGitCurrentBranch() === 'main' ? 'green' : 'cyan'
const badgeURL = `${BASE_URL}/memo/deployed/${label}/${status}/${color}`
const fetchOptions = {
method: 'PUT',
headers: {
authorization: `Bearer ${process.env.MEMO_BADGE_TOKEN}`
}
}
const response = await fetch(badgeURL, fetchOptions)
assert.strictEqual(response.status, 200)
const result = await response.json()
assert.deepEqual(result, { status, label, color })
})
function getGitLastCommitDate () {
const lastCommitDateString = execSync('git log -1 --format=%cI').toString().trim()
return extractBareDate(lastCommitDateString)
}
function extractBareDate(dateString) {
return new Date(dateString).toISOString().substring(0, 10)
}
function getGitCurrentBranch () {
return execSync('git rev-parse --abbrev-ref HEAD').toString().trim()
}