Add very basic test suite

Problem: ESLint and TypeScript help catch some types of regressions, but
they don't protect us against obvious stuff like "the server won't
start". This means that humans need to test a bunch of stuff manually,
and that can be really tedious and exhausting.

Solution: Yesterday someone invented this cool concept called "testing"
where you write automated tests for your software to ensure it actually
works the way you expect. It might have beeen invented before yesterday,
I don't know. Anyway, this solution adds a bunch of tests that send HTTP
GET requests to a bunch of endpoints to make sure the server is at least
returning HTTP 200 responses. It also fixes a race condition where HTTP
server was available before the readme / version strings were loaded.
This commit is contained in:
Christian Bundy 2020-04-03 09:07:39 -07:00
parent 4318b9a22a
commit 3c9ec37d2f
7 changed files with 2676 additions and 36 deletions

1
.prettierignore Normal file
View File

@ -0,0 +1 @@
.nyc_output

2631
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -16,7 +16,7 @@
"dev": "nodemon --inspect src/index.js --debug --no-open",
"fix": "common-good fix",
"start": "node src/index.js",
"test": "common-good check --dependency-check-suffix '-i changelog-version -i mkdirp -i nodemon -i stylelint-config-recommended'",
"test": "tap && common-good check --dependency-check-suffix '-i changelog-version -i mkdirp -i nodemon -i stylelint-config-recommended'",
"preversion": "npm test",
"version": "mv docs/CHANGELOG.md ./ && changelog-version && mv CHANGELOG.md docs/ && git add CHANGELOG.md"
},
@ -65,12 +65,15 @@
"@types/nodemon": "^1.19.0",
"@types/pull-stream": "^3.6.0",
"@types/sharp": "^0.23.1",
"@types/supertest": "^2.0.8",
"@types/yargs": "^15.0.4",
"changelog-version": "^1.0.1",
"common-good": "^2.0.3",
"mkdirp": "^1.0.0",
"nodemon": "^2.0.2",
"stylelint-config-recommended": "^3.0.0"
"stylelint-config-recommended": "^3.0.0",
"supertest": "^4.0.2",
"tap": "^14.10.7"
},
"optionalDependencies": {
"sharp": "^0.23.0"
@ -80,7 +83,8 @@
},
"homepage": "https://github.com/fraction/oasis#readme",
"directories": {
"doc": "docs"
"doc": "docs",
"test": "test"
},
"keywords": [],
"engines": {

View File

@ -8,7 +8,7 @@ const mount = require("koa-mount");
* @param {{ host: string, port: number, middleware }} input
* @return function
*/
const http = ({ host, port, middleware }) => {
module.exports = ({ host, port, middleware }) => {
const assets = new Koa();
assets.use(koaStatic(path.join(__dirname, "assets")));
@ -78,5 +78,3 @@ const http = ({ host, port, middleware }) => {
middleware.forEach((m) => app.use(m));
return app.listen({ host, port });
};
module.exports = http;

View File

@ -179,13 +179,8 @@ try {
const readmePath = path.join(__dirname, "..", "README.md");
const packagePath = path.join(__dirname, "..", "package.json");
fs.promises.readFile(readmePath, "utf8").then((text) => {
config.readme = text;
});
fs.promises.readFile(packagePath, "utf8").then((text) => {
config.version = JSON.parse(text).version;
});
const readme = fs.readFileSync(readmePath, "utf8");
const version = JSON.parse(fs.readFileSync(packagePath, "utf8")).version;
router
.param("imageSize", (imageSize, ctx, next) => {
@ -525,7 +520,7 @@ router
peers: peersWithNames,
theme,
themeNames,
version: config.version.toString(),
version: version.toString(),
});
};
ctx.body = await getMeta({ theme });
@ -547,7 +542,7 @@ router
const status = async (text) => {
return markdownView({ text });
};
ctx.body = await status(config.readme);
ctx.body = await status(readme);
})
.get("/mentions/", async (ctx) => {
const mentions = async () => {
@ -805,7 +800,12 @@ const middleware = [
routes,
];
http({ host, port, middleware });
const app = http({ host, port, middleware });
app.on("close", () => {
cooler.close();
});
module.exports = app;
log(`Listening on ${url}`);

View File

@ -174,5 +174,15 @@ module.exports = ({ offline }) => {
});
});
},
close() {
return new Promise((resolve) => {
handle.then((ssb) => {
if (ssb.closed === false) {
ssb.close();
}
resolve();
});
});
},
};
};

36
test/basic.js Normal file
View File

@ -0,0 +1,36 @@
// HACK: Prevent Oasis from opening the web browser.
process.argv.push("--no-open");
const app = require("../src");
const supertest = require("supertest");
const tap = require("tap");
// TODO: Generate programmatically?
const paths = [
"/inbox",
"/mentions",
"/profile",
"/profile/edit",
"/public/latest",
"/public/latest/extended",
"/public/latest/summaries",
"/public/latest/threads",
"/public/latest/topics",
"/public/popular/day",
"/public/popular/week",
"/publish",
"/publish/custom",
"/search",
"/search?query=foo",
"/settings",
"/settings/readme",
];
tap.plan(paths.length);
paths.map((path) => {
tap.comment(path);
supertest(app).get(path).expect(200).end(tap.error);
});
tap.teardown(() => app.close());