Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quality control improvements #28

Merged
merged 13 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- Add any related issues and a description of the changes proposed in the pull request. -->
Resolves \#1

Description of the changes...

---
<!-- List the pages that should be automatically tested as part of your custom metric changes. -->
**Test websites**:

- https://example.com/
95 changes: 95 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Tests

on:
pull_request_target:
branches:
- main
paths:
- "src/technologies/*.json"
- "src/categories.json"
- "src/groups.json"
- "tests/*.js"
workflow_dispatch:

jobs:
test:
name: WebPageTest Test Cases
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0

- name: Install dependencies
run: yarn install

- name: Validate
run: yarn run validate

- name: Run WebPageTest with unit tests
env:
WPT_SERVER: "webpagetest.httparchive.org"
WPT_API_KEY: ${{ secrets.HA_API_KEY }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
echo "::group::Unit tests"
yarn test
echo "::endgroup::"

- name: Run WebPageTest for more websites
env:
WPT_SERVER: "webpagetest.httparchive.org"
WPT_API_KEY: ${{ secrets.HA_API_KEY }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
# Get the PR body
PR_BODY="$(cat <<'EOF'
${{ github.event.pull_request.body }}
EOF
)"

# Read PR body into an array, removing line breaks and carriage returns
declare -a lines
while IFS= read -r line; do
lines+=("${line//[$'\r\n']}")
done <<< "$PR_BODY"

# Find the index of the line after "**Test websites**:"
start_index=-1
for ((i=0; i<${#lines[@]}; i++)); do
if [[ "${lines[$i]}" == *"**Test websites**:"* ]]; then
start_index=$((i + 1))
break
fi
done

# If the index is valid, then parse the URLs
if [ $start_index -gt -1 ]; then
# Initialize an array for URLs
declare -a URLS
url_pattern="((http|https|ftp):\/\/[a-zA-Z0-9.-]+(\.[a-zA-Z]{2,4})(\/[a-zA-Z0-9_.-]+)*(\/?)(\?[a-zA-Z0-9_.-]+=[a-zA-Z0-9%_.-]+)*(\#?)([a-zA-Z0-9%_.-=]+)*)"

for ((i=start_index; i<${#lines[@]}; i++)); do
if [[ ${lines[$i]} =~ $url_pattern ]]; then
URLS+=("${BASH_REMATCH[1]}")
fi
done

# Run WebPageTest for each URL
for TEST_WEBSITE in "${URLS[@]}"; do
echo "::group::Detecting technologies for $TEST_WEBSITE"
node tests/wpt.js "$TEST_WEBSITE"
echo "::endgroup::"
done
else
echo "No websites found."
fi

- name: Add comment with results
uses: mshick/add-pr-comment@v2
if: always()
with:
refresh-message-position: true
message-path: test-results.md
30 changes: 0 additions & 30 deletions .github/workflows/validate.yml

This file was deleted.

30 changes: 21 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,33 @@
"@nuxtjs/eslint-config": "^3.1.0",
"@nuxtjs/eslint-module": "^2.0.0",
"babel-eslint": "^10.1.0",
"eslint": "^7.13.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-json": "^2.1.2",
"eslint-plugin-nuxt": "^1.0.0",
"eslint-plugin-prettier": "^3.1.4",
"prettier": "^2.1.2",
"terminal-overwrite": "^2.0.1"
"eslint-plugin-prettier": "^3.4.1",
"jest": "^29.7.0",
"prettier": "^2.8.8",
"terminal-overwrite": "^2.0.1",
"webpagetest": "github:HTTPArchive/WebPageTest.api-nodejs"
},
"scripts": {
"lint": "eslint src/**/*.{js,json} && jsonlint -jsV ./schema.json --trim-trailing-commas --enforce-double-quotes ./src/technologies/ && jsonlint -js --trim-trailing-commas --enforce-double-quotes ./src/categories.json",
"lint:fix": "eslint --fix src/**/*.{js,json} && jsonlint -isV ./schema.json --trim-trailing-commas --enforce-double-quotes ./src/technologies/ && jsonlint -is --trim-trailing-commas --enforce-double-quotes ./src/categories.json",
"lint": "eslint src/**/*.{js,json} tests/**/*.js bin/**/*.js && jsonlint -jsV ./schema.json --trim-trailing-commas --enforce-double-quotes ./src/technologies/ && jsonlint -js --trim-trailing-commas --enforce-double-quotes ./src/categories.json",
"lint:fix": "eslint --fix src/**/*.{js,json} tests/**/*.js bin/**/*.js && jsonlint -isV ./schema.json --trim-trailing-commas --enforce-double-quotes ./src/technologies/ && jsonlint -is --trim-trailing-commas --enforce-double-quotes ./src/categories.json",
"validate": "yarn run lint && node ./bin/validate.js",
"test": "jest",
"convert": "node --no-warnings ./bin/convert.js",
"build": "yarn run validate && yarn run prettify && yarn run convert && node ./bin/build.js",
"build:safari": "xcrun safari-web-extension-converter --swift --project-location build --force src",
"manifest": "node ./bin/manifest.js"
"build": "yarn run validate && yarn run convert && node ./bin/build.js"
},
"jest": {
"reporters": [
"default",
[
"github-actions",
{
"silent": false
}
]
]
}
}
12 changes: 12 additions & 0 deletions tests/unit-tests.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const assert = require('assert')
const { runWPTTest } = require('./wpt.js')
const testWebsite = 'https://almanac.httparchive.org/en/2022/'

let responseData
beforeAll(async () => {
responseData = await runWPTTest(testWebsite)
}, 400000)

test('wappalyzer successful', () => {
assert.ok(responseData.runs['1'].firstView.wappalyzer_failed === 0)
})
58 changes: 58 additions & 0 deletions tests/wpt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const fs = require('fs')
const { argv } = require('node:process')
const WebPageTest = require('webpagetest')

const isDirectRun = require.main === module

const wptServer = process.env.WPT_SERVER
const wptApiKey = process.env.WPT_API_KEY
const PRnumber = parseInt(process.env.PR_NUMBER)
const wpt = new WebPageTest(wptServer, wptApiKey)

/**
* Runs a WebPageTest (WPT) test for a given URL.
*
* @param {string} url - The URL to run the test on.
* @returns {Promise<object>} A promise that resolves with an object containing the custom metrics.
* @throws {Error} If the test run fails or the response status code is not 200.
*/
function runWPTTest(url) {
const options = { key: wptApiKey, wappalyzerpr: PRnumber }

return new Promise((resolve, reject) => {
wpt.runTestAndWait(url, options, (error, response) => {
if (error || response.statusCode !== 200) {
reject(error || response)
} else {
const technologies = {
detected: response.data.runs['1'].firstView.detected,
detected_apps: response.data.runs['1'].firstView.detected_apps,
detected_technologies:
response.data.runs['1'].firstView.detected_technologies,
detected_raw: response.data.runs['1'].firstView.detected_raw,
}

fs.appendFileSync(
'test-results.md',
'<details>\n' +
`<summary><strong>Custom metrics for ${url}</strong></summary>\n\n` +
`WPT test run results: ${response.data.summary}\n` +
(isDirectRun
? 'Changed custom metrics values:\n' +
`\`\`\`json\n${JSON.stringify(technologies, null, 4)}\n\`\`\`\n`
: '') +
'</details>\n'
)

resolve(response.data)
}
})
})
}

if (isDirectRun) {
const url = argv[2]
runWPTTest(url)
}

module.exports = { runWPTTest }
Loading