Files
metrics/tests/metrics.mjs
2020-10-26 13:22:51 +01:00

98 lines
3.4 KiB
JavaScript

//Imports
import path from "path"
import fs from "fs"
import metrics from "../src/metrics.mjs"
import setup from "../src/setup.mjs"
import build from "../utils/build.mjs"
import octokit from "@octokit/graphql"
import OctokitRest from "@octokit/rest"
import libxmljs from "libxmljs"
import url from "url"
//Initialization
const __dirname = path.join(path.dirname(url.fileURLToPath(import.meta.url)), "..", "action")
process.on("unhandledRejection", error => { throw error })
const templates = (await fs.promises.readdir(path.join(__dirname, "..", "src/templates"))).filter(name => !/.*[.]mjs$/.test(name))
/** Test function */
export default async function test() {
//Load GitHub handlers
const token = process.argv.slice(2)[0]
const graphql = octokit.graphql.defaults({headers:{authorization: `token ${token}`}})
const rest = new OctokitRest.Octokit({auth:token})
//Perform tests
await test.build()
for (const template of templates) {
for (const q of [
{},
{"base.header":0},
{"base.activity":0},
{"base.community":0},
{"base.repositories":0},
{"base.metadata":0},
{followup:1},
{languages:1},
{followup:1, languages:1},
{habits:1, "habits.events":1},
{lines:1},
{traffic:1},
{selfskip:1},
{pagespeed:1},
{music:1},
{music:1, "music.mode":"recent"},
{music:1, "music.mode":"recent", "music.provider":"apple"},
{music:1, "music.mode":"recent", "music.provider":"spotify"},
{music:1, "music.mode":"playlist"},
{followup:1, languages:1, habits:1, "habits.events":1, lines:1, traffic:1, selfskip:1, pagespeed:1, music:1},
]) {
await test.metrics({graphql, rest, q:{template, repositories:1, ...q}})
}
}
}
/** Metrics tests */
test.metrics = async function ({graphql, rest, q}) {
//Preparation
console.log(`### Checking metrics with [${Object.entries(q).map(([key, value]) => `${key}=${value}`).join(", ")}]`)
const plugins = {
lines:{enabled:true},
traffic:{enabled:true},
pagespeed:{enabled:true},
habits:{enabled:true},
selfskip:{enabled:true},
languages:{enabled:true},
followup:{enabled:true},
musit:{enabled:true},
}
//Compute render
console.log("#### Checking that SVG can be generated")
const conf = await setup({log:false})
const rendered = await metrics({login:"lowlighter", q}, {graphql, rest, plugins, conf})
//Ensure it's a well-formed SVG image
console.log("#### Checking that generated SVG can be parsed")
const parsed = libxmljs.parseXml(rendered)
if (parsed.errors.length)
throw new Error(`Malformed SVG : \n${parsed.errors.join("\n")}`)
}
/** Build test */
test.build = async function () {
//Ensure that action has been rebuild
console.log("### Checking that code has been rebuild")
const action = `${await fs.promises.readFile(`${__dirname}/dist/index.js`)}`
const code = await build()
if (action !== code)
throw new Error(`GitHub Action has not been rebuild. Run "npm run build" to solve this issue`)
}
//Main
if (/metrics.mjs/.test(process.argv[1])) {
//Test
await test()
console.log("Test success !")
}