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:
parent
4318b9a22a
commit
3c9ec37d2f
|
@ -0,0 +1 @@
|
|||
.nyc_output
|
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
|
@ -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": {
|
||||
|
|
|
@ -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;
|
||||
|
|
20
src/index.js
20
src/index.js
|
@ -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}`);
|
||||
|
||||
|
|
10
src/ssb.js
10
src/ssb.js
|
@ -174,5 +174,15 @@ module.exports = ({ offline }) => {
|
|||
});
|
||||
});
|
||||
},
|
||||
close() {
|
||||
return new Promise((resolve) => {
|
||||
handle.then((ssb) => {
|
||||
if (ssb.closed === false) {
|
||||
ssb.close();
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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());
|
Loading…
Reference in New Issue