Tests and coverage for web

This commit is contained in:
Cadence Ember 2024-12-24 01:06:19 +13:00
parent 53379a962d
commit c599dff590
17 changed files with 593 additions and 171 deletions

View file

@ -163,9 +163,13 @@ INSERT INTO member_power (mxid, room_id, power_level) VALUES
INSERT INTO lottie (sticker_id, mxc_url) VALUES
('860171525772279849', 'mxc://cadence.moe/ZtvvVbwMIdUZeovWVyGVFCeR');
INSERT INTO "auto_emoji" ("name","emoji_id","guild_id") VALUES
('L1','1144820033948762203','529176156398682115'),
('L2','1144820084079087647','529176156398682115'),
('_','_','529176156398682115');
INSERT INTO auto_emoji (name, emoji_id, guild_id) VALUES
('L1', '1144820033948762203', '529176156398682115'),
('L2', '1144820084079087647', '529176156398682115'),
('_', '_', '529176156398682115');
INSERT INTO media_proxy (permitted_hash) VALUES
(-429802515645771439),
(4558604729745184757);
COMMIT;

View file

@ -20,9 +20,9 @@ const {reg} = require("../src/matrix/read-registration")
reg.ooye.discord_token = "Njg0MjgwMTkyNTUzODQ0NzQ3.Xl3zlw.baby"
reg.ooye.server_origin = "https://matrix.cadence.moe" // so that tests will pass even when hard-coded
reg.ooye.server_name = "cadence.moe"
reg.id = "baby" // don't actually take authenticated actions on the server
reg.as_token = "baby"
reg.hs_token = "baby"
reg.id = "baby"
reg.as_token = "don't actually take authenticated actions on the server"
reg.hs_token = "don't actually take authenticated actions on the server"
reg.ooye.bridge_origin = "https://bridge.example.org"
const sync = new HeatSync({watchFS: false})
@ -31,6 +31,9 @@ const discord = {
guilds: new Map([
[data.guild.general.id, data.guild.general]
]),
guildChannelMap: new Map([
[data.guild.general.id, [data.channel.general.id]],
]),
application: {
id: "684280192553844747"
},
@ -97,7 +100,7 @@ file._actuallyUploadDiscordFileToMxc = function(url, res) { throw new Error(`Not
])
}, {timeout: 60000})
}
/* c8 ignore end */
/* c8 ignore stop */
const p = migrate.migrate(db)
test("migrate: migration works", async t => {
@ -142,4 +145,9 @@ file._actuallyUploadDiscordFileToMxc = function(url, res) { throw new Error(`Not
require("../src/discord/interactions/permissions.test")
require("../src/discord/interactions/privacy.test")
require("../src/discord/interactions/reactions.test")
require("../src/web/server.test")
require("../src/web/routes/download-discord.test")
require("../src/web/routes/download-matrix.test")
require("../src/web/routes/guild.test")
require("../src/web/routes/qr.test")
})()

69
test/web.js Normal file
View file

@ -0,0 +1,69 @@
const passthrough = require("../src/passthrough")
const h3 = require("h3")
const http = require("http")
const {SnowTransfer} = require("snowtransfer")
class Router {
constructor() {
/** @type {Map<string, h3.EventHandler>} */
this.routes = new Map()
for (const method of ["get", "post", "put", "patch", "delete"]) {
this[method] = function(url, handler) {
const key = `${method} ${url}`
this.routes.set(`${key}`, handler)
}
}
}
/**
* @param {string} method
* @param {string} inputUrl
* @param {{event?: any, params?: any, body?: any, sessionData?: any, api?: Partial<import("../src/matrix/api")>, snow?: {[k in keyof SnowTransfer]?: Partial<SnowTransfer[k]>}, headers?: any}} [options]
*/
test(method, inputUrl, options = {}) {
const url = new URL(inputUrl, "http://a")
const key = `${method} ${options.route || url.pathname}`
/* c8 ignore next */
if (!this.routes.has(key)) throw new Error(`Route not found: "${key}"`)
const req = {
method: method.toUpperCase(),
headers: options.headers || {},
url
}
const event = options.event || {}
if (typeof options.body === "object" && options.body.constructor === Object) {
options.body = JSON.stringify(options.body)
req.headers["content-type"] = "application/json"
}
return this.routes.get(key)(Object.assign(event, {
method: method.toUpperCase(),
path: `${url.pathname}${url.search}`,
_requestBody: options.body,
node: {
req,
res: new http.ServerResponse(req)
},
context: {
api: options.api,
params: options.params,
snow: options.snow,
sessions: {
h3: {
id: "h3",
createdAt: 0,
data: options.sessionData || {}
}
}
}
}))
}
}
const router = new Router()
passthrough.as = {router}
module.exports.router = router