"use strict"; const test = require("ava"); const path = require("path"); const request = require("supertest"); const cheerio = require("cheerio"); const mf = require("microformat-node"); process.chdir(path.resolve(__dirname, "./testsite/")); const config = require(path.resolve(__dirname, "../src/modules/config.js")); const app = require("../src/app.js"); const parseResponse = res => cheerio.load(res.text, { normalizeWhitespace: true, useHtmlParser2: true }); const toMicroformatsOptions = node => ({ node, textFormat: "normalised" }); async function notFound(t, url, bodyRegex) { const res = await request(app.listen()).get(url); const body = res.text; t.is(res.statusCode, 404); t.regex(body, bodyRegex); } notFound.title = (providedTitle, input, expected) => `${input} = 404 ${expected.toString()}`; test("homepage", async function(t) { const res = await request(app.listen()).get("/"); t.is(res.statusCode, 200); t.is(res.type, "text/html"); t.is(res.charset, "utf-8"); t.regex(res.text, /^/); const $ = parseResponse(res); t.is($("head > title").text(), "John Doe", "head title is site author"); t.is($("main").length, 1, "only one
tag"); const options = toMicroformatsOptions($); const count = await mf.countAsync(options); t.deepEqual(count, { "h-card": 1, "h-feed": 1, "h-entry": 1 }); const data = await mf.getAsync(options); t.snapshot(data, "should contain relevant microformats data"); }); test("post", async function(t) { const res = await request(app.listen()).get("/post/testfile"); t.is(res.statusCode, 200); t.is(res.type, "text/html"); t.is(res.charset, "utf-8"); t.regex(res.text, /^/); const $ = parseResponse(res); t.is( $("head > title").text(), "This is a test · " + "John Doe", "head title contains post and site author" ); const options = toMicroformatsOptions($); const count = await mf.countAsync(options); t.deepEqual(count, { "h-card": 1, "h-entry": 1 }); const data = await mf.getAsync(options); t.snapshot(data, "should contain relevant microformats data"); }); test("tags", async function(t) { const res = await request(app.listen()).get("/tag/a"); t.is(res.statusCode, 200); t.is(res.type, "text/html"); t.is(res.charset, "utf-8"); t.regex(res.text, /^/); const $ = parseResponse(res); t.is( $("head > title").text(), "A · John Doe", "head title contains title-cased tag and site name" ); const options = toMicroformatsOptions($); const count = await mf.countAsync(options); t.deepEqual(count, { "h-card": 1, "h-feed": 1, "h-entry": 1 }); const data = await mf.getAsync(options); t.snapshot(data, "should contain relevant microformats data"); }); test(notFound, "/post/non-existent", /Post not found/); test(notFound, "/tag/non-existent", /tag non-existent not found/);