summary refs log tree commit diff
path: root/test/web.js
blob: 250694aabaaaf497cc5f2d5227545b1179cb6002 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const passthrough = require("../src/passthrough")
const h3 = require("h3")
const http = require("http")
const {SnowTransfer} = require("snowtransfer")
const assert = require("assert").strict
const domino = require("domino")
const {extend} = require("supertape")
const {reg} = require("../src/matrix/read-registration")

const {AppService} = require("@cloudrac3r/in-your-element")
const defaultAs = new AppService(reg)

/**
 * @param {string} html
 */
function getContent(html) {
	const doc = domino.createDocument(html)
	doc.querySelectorAll("svg").cache.forEach(e => e.remove())
	const content = doc.getElementById("content")
	assert(content)
	return content.innerHTML.trim()
}

const test = extend({
	has: operator => /** @param {string | RegExp} expected */ (html, expected, message = "should have substring in html content") => {
		const content = getContent(html)
		const is = expected instanceof RegExp ? content.match(expected) : content.includes(expected)
		const {output, result} = operator.equal(content, expected.toString())
		return {
			expected: expected.toString(),
			message,
			is,
			result: result,
			output: output
		}
	}
})

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, getOauth2Token?: any, getClient?: (string) => {user: {getGuilds: () => Promise<DiscordTypes.RESTGetAPICurrentUserGuildsResult>}}, api?: Partial<import("../src/matrix/api")>, snow?: {[k in keyof SnowTransfer]?: Partial<SnowTransfer[k]>}, createRoom?: Partial<import("../src/d2m/actions/create-room")>, createSpace?: Partial<import("../src/d2m/actions/create-space")>, mxcDownloader?: import("../src/m2d/actions/emoji-sheet")["getAndConvertEmoji"], headers?: any}} [options]
	 */
	async 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"
		}

		try {
			return await this.routes.get(key)(Object.assign(event, {
				__is_event__: true,
				method: method.toUpperCase(),
				path: `${url.pathname}${url.search}`,
				_requestBody: options.body,
				node: {
					req,
					res: new http.ServerResponse(req)
				},
				context: {
					api: options.api,
					mxcDownloader: options.mxcDownloader,
					params: options.params,
					snow: options.snow,
					createRoom: options.createRoom,
					createSpace: options.createSpace,
					getOauth2Token: options.getOauth2Token,
					getClient: options.getClient,
					sessions: {
						h3: {
							id: "h3",
							createdAt: 0,
							data: options.sessionData || {}
						}
					}
				}
			}))
		} catch (error) {
			// Post-process error data
			defaultAs.app.options.onError(error)
			throw error
		}
	}
}

const router = new Router()

passthrough.as = {router, on() {}, options: defaultAs.app.options}

module.exports.router = router
module.exports.test = test