137 lines
5.4 KiB
JavaScript
137 lines
5.4 KiB
JavaScript
/**
|
|
* @jest-environment node
|
|
*/
|
|
|
|
//Imports
|
|
const processes = require("child_process")
|
|
const yaml = require("js-yaml")
|
|
const fs = require("fs")
|
|
const path = require("path")
|
|
const url = require("url")
|
|
const axios = require("axios")
|
|
const faker = require("faker")
|
|
const ejs = require("ejs")
|
|
|
|
//Github action
|
|
const action = yaml.load(fs.readFileSync(path.join(__dirname, "../action.yml"), "utf8"))
|
|
action.defaults = Object.fromEntries(Object.entries(action.inputs).map(([key, {default:value}]) => [key, value]))
|
|
action.input = vars => Object.fromEntries([...Object.entries(action.defaults), ...Object.entries(vars)].map(([key, value]) => [`INPUT_${key.toLocaleUpperCase()}`, value]))
|
|
action.run = async (vars) => await new Promise((solve, reject) => {
|
|
let [stdout, stderr] = ["", ""]
|
|
const env = {...process.env, ...action.input(vars), GITHUB_REPOSITORY:"lowlighter/metrics"}
|
|
const child = processes.spawn("node", ["source/app/action/index.mjs"], {env})
|
|
child.stdout.on("data", data => stdout += data)
|
|
child.stderr.on("data", data => stderr += data)
|
|
child.on("close", code => {
|
|
if (code === 0)
|
|
return solve(true)
|
|
console.log(stdout, stderr)
|
|
reject(stdout)
|
|
})
|
|
})
|
|
|
|
//Web instance
|
|
const web = {}
|
|
web.run = async (vars) => (await axios(`http://localhost:3000/lowlighter?${new url.URLSearchParams(Object.fromEntries(Object.entries(vars).map(([key, value]) => [key.replace(/^plugin_/, "").replace(/_/g, "."), value])))}`)).status === 200
|
|
beforeAll(async done => {
|
|
await fs.promises.rmdir(path.join(__dirname, "../source/templates/@classic"), {recursive:true})
|
|
await new Promise((solve, reject) => {
|
|
let stdout = ""
|
|
web.instance = processes.spawn("node", ["source/app/web/index.mjs"], {env:{...process.env, USE_MOCKED_DATA:true, NO_SETTINGS:true}})
|
|
web.instance.stdout.on("data", data => (stdout += data, /Server ready !/.test(stdout) ? solve() : null))
|
|
web.instance.stderr.on("data", data => console.error(`${data}`))
|
|
})
|
|
done()
|
|
})
|
|
afterAll(async done => {
|
|
await web.instance.kill("SIGKILL")
|
|
await fs.promises.rmdir(path.join(__dirname, "../source/templates/@classic"), {recursive:true})
|
|
done()
|
|
})
|
|
|
|
//Web instance placeholder
|
|
require("./../source/app/web/statics/app.placeholder.js")
|
|
const placeholder = globalThis.placeholder
|
|
delete globalThis.placeholder
|
|
placeholder.init({faker, ejs, axios:{async get(url) { return axios(`http://localhost:3000${url}`) }}})
|
|
placeholder.run = async (vars) => {
|
|
const options = Object.fromEntries(Object.entries(vars).map(([key, value]) => [key.replace(/^plugin_/, "").replace(/_/g, "."), value]))
|
|
const enabled = Object.fromEntries(Object.entries(vars).filter(([key]) => /^plugin_[a-z]+$/.test(key)))
|
|
const config = Object.fromEntries(Object.entries(options).filter(([key]) => /^config[.]/.test(key)))
|
|
const base = Object.fromEntries(Object.entries(options).filter(([key]) => /^base[.]/.test(key)))
|
|
return typeof await placeholder({
|
|
templates:{selected:vars.template},
|
|
plugins:{enabled:{...enabled, base}, options},
|
|
config,
|
|
version:"TEST",
|
|
user:"lowlighter",
|
|
avatar:"https://github.com/lowlighter.png",
|
|
}) === "string"
|
|
}
|
|
|
|
//Test cases
|
|
const tests = require("./testscases")()
|
|
|
|
//Tests run
|
|
describe("GitHub Action", () =>
|
|
describe.each([
|
|
["classic", {}],
|
|
["terminal", {}],
|
|
["repository", {repo:"metrics"}],
|
|
])("Template : %s", (template, query) => {
|
|
for (const [name, input, {skip = [], modes = []} = {}] of tests)
|
|
if ((skip.includes(template))||((modes.length)&&(!modes.includes("action"))))
|
|
test.skip(name, () => null)
|
|
else
|
|
test(name, async () => expect(await action.run({
|
|
token:"MOCKED_TOKEN",
|
|
plugin_pagespeed_token:"MOCKED_TOKEN",
|
|
plugin_tweets_token:"MOCKED_TOKEN",
|
|
plugin_music_token:"MOCKED_CLIENT_ID, MOCKED_CLIENT_SECRET, MOCKED_REFRESH_TOKEN",
|
|
template, base:"", query:JSON.stringify(query),
|
|
config_timezone:"Europe/Paris",
|
|
plugins_errors_fatal:true, dryrun:true, use_mocked_data:true, verify:true,
|
|
...input
|
|
})).toBe(true), 60*1e3)
|
|
})
|
|
)
|
|
|
|
describe("Web instance", () =>
|
|
describe.each([
|
|
["classic", {}],
|
|
["terminal", {}],
|
|
["repository", {repo:"metrics"}],
|
|
])("Template : %s", (template, query) => {
|
|
for (const [name, input, {skip = [], modes = []} = {}] of tests)
|
|
if ((skip.includes(template))||((modes.length)&&(!modes.includes("web"))))
|
|
test.skip(name, () => null)
|
|
else
|
|
test(name, async () => expect(await web.run({
|
|
template, base:0, ...query,
|
|
config_timezone:"Europe/Paris",
|
|
plugins_errors_fatal:true, verify:true,
|
|
...input
|
|
})).toBe(true), 60*1e3)
|
|
})
|
|
)
|
|
|
|
describe("Web instance (placeholder)", () =>
|
|
describe.each([
|
|
["classic", {}],
|
|
["terminal", {}],
|
|
])("Template : %s", (template, query) => {
|
|
for (const [name, input, {skip = [], modes = []} = {}] of tests)
|
|
if ((skip.includes(template))||((modes.length)&&(!modes.includes("placeholder"))))
|
|
test.skip(name, () => null)
|
|
else
|
|
test(name, async () => expect(await placeholder.run({
|
|
template, base:0, ...query,
|
|
config_timezone:"Europe/Paris",
|
|
plugins_errors_fatal:true, verify:true,
|
|
...input
|
|
})).toBe(true))
|
|
})
|
|
)
|
|
|
|
|