From b44717c473c2975275b414cfae97d1017ca4eec4 Mon Sep 17 00:00:00 2001 From: Amio Jin Date: Mon, 21 Aug 2023 11:00:43 -0500 Subject: [PATCH] test: add e2e tests (#645) --- .github/workflows/e2e.yml | 16 +++++++++++ .github/workflows/nodejs.yml | 3 +-- test/e2e.test.mjs | 52 ++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/e2e.yml create mode 100644 test/e2e.test.mjs diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml new file mode 100644 index 0000000..0a45055 --- /dev/null +++ b/.github/workflows/e2e.yml @@ -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 }} diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 835c133..f69a0f3 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -1,10 +1,9 @@ -name: lint and build +name: Lint and Build on: pull_request jobs: build: - runs-on: ubuntu-latest steps: diff --git a/test/e2e.test.mjs b/test/e2e.test.mjs new file mode 100644 index 0000000..73fae2d --- /dev/null +++ b/test/e2e.test.mjs @@ -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() +}