Skip to content
196 changes: 53 additions & 143 deletions __tests__/routes_mounted.test.js
Original file line number Diff line number Diff line change
@@ -1,201 +1,108 @@
/**
* Express Route Detection
*
* This approach checks routes without making HTTP requests by
* directly inspecting the Express app's routing table.
*/

import request from "supertest"
import { jest } from "@jest/globals"
import api_routes from "../routes/api-routes.js"
import app from "../app.js"
import fs from 'fs'
import fs from "fs"

let app_stack = app._router.stack
let app_stack = app.router.stack
Copy link

Copilot AI Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Express exposes the router stack on app._router, not app.router. app.router is undefined in Express 4 and 5, which will cause a TypeError when accessing .stack. Use app._router.stack instead.

Suggested change
let app_stack = app.router.stack
let app_stack = app._router.stack

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOT ANYMORE BECAUSE EXPRESS 5

let api_stack = api_routes.stack

/**
* Check if a route exists in the Express app
* @param {Array} stack - The router stack to search
* @param {string} testPath - The path to test for
* @returns {boolean} - True if the route exists
*/
function routeExists(stack, testPath) {
for (const layer of stack) {
// Check if layer has matchers (Express 5)
if (layer.matchers && layer.matchers.length > 0) {
const matcher = layer.matchers[0]
const match = matcher(testPath)
if (match && match.path) return true
}
// Also check route.path directly if it exists
if (layer.route && layer.route.path) {
if (layer.route.path === testPath || layer.route.path.includes(testPath)) return true
}
}
return false
}
Comment on lines +22 to +36
Copy link

Copilot AI Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mount layers like '/v1' and '/client' are represented as Layer objects without layer.route, so this helper will miss mounted prefixes. Additionally, route.path can be an array. Add a fallback using layer.regexp.test(testPath) and handle array route paths to correctly detect mounts and multi-path routes.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOT ANYMORE BECAUSE EXPRESS 5


describe('Check to see that all expected top level route patterns exist.', () => {

it('/v1 -- mounted ', () => {
let exists = false
for (const middleware of app_stack) {
if (middleware.regexp && middleware.regexp.toString().includes("/v1")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(app_stack, '/v1')).toBe(true)
})

it('/client -- mounted ', () => {
let exists = false
for (const middleware of app_stack) {
if (middleware.regexp && middleware.regexp.toString().includes("/client")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(app_stack, '/client')).toBe(true)
})

it('/v1/id/{_id} -- mounted', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp && middleware.regexp.toString().includes("/id")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/id')).toBe(true)
})

it('/v1/since/{_id} -- mounted', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp && middleware.regexp.toString().includes("/since")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/since')).toBe(true)
})

it('/v1/history/{_id} -- mounted', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp && middleware.regexp.toString().includes("/history")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/history')).toBe(true)
})

})

describe('Check to see that all /v1/api/ route patterns exist.', () => {

it('/v1/api/query -- mounted ', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp
&& middleware.regexp.toString().includes("/api")
&& middleware.regexp.toString().includes("/query")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/api/query')).toBe(true)
})

it('/v1/api/create -- mounted ', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp
&& middleware.regexp.toString().includes("/api")
&& middleware.regexp.toString().includes("/create")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/api/create')).toBe(true)
})

it('/v1/api/bulkCreate -- mounted ', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp
&& middleware.regexp.toString().includes("/api")
&& middleware.regexp.toString().includes("/bulkCreate")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/api/bulkCreate')).toBe(true)
})

it('/v1/api/update -- mounted ', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp
&& middleware.regexp.toString().includes("/api")
&& middleware.regexp.toString().includes("/update")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/api/update')).toBe(true)
})

it('/v1/api/bulkUpdate -- mounted ', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp
&& middleware.regexp.toString().includes("/api")
&& middleware.regexp.toString().includes("/bulkUpdate")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/api/bulkUpdate')).toBe(true)
})

it('/v1/api/overwrite -- mounted ', () => {
expect(routeExists(api_stack, '/api/overwrite')).toBe(true)
})

it('/v1/api/patch -- mounted ', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp
&& middleware.regexp.toString().includes("/api")
&& middleware.regexp.toString().includes("/patch")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/api/patch')).toBe(true)
})

it('/v1/api/set -- mounted ', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp
&& middleware.regexp.toString().includes("/api")
&& middleware.regexp.toString().includes("/set")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/api/set')).toBe(true)
})

it('/v1/api/unset -- mounted ', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp
&& middleware.regexp.toString().includes("/api")
&& middleware.regexp.toString().includes("/unset")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/api/unset')).toBe(true)
})

it('/v1/api/delete/{id} -- mounted ', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp
&& middleware.regexp.toString().includes("/api")
&& middleware.regexp.toString().includes("/delete")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/api/delete')).toBe(true)
})

it('/v1/api/release/{id} -- mounted ', () => {
let exists = false
for (const middleware of api_stack) {
if (middleware.regexp
&& middleware.regexp.toString().includes("/api")
&& middleware.regexp.toString().includes("/release")){
exists = true
break
}
}
expect(exists).toBe(true)
expect(routeExists(api_stack, '/api/release')).toBe(true)
})

})
Expand Down Expand Up @@ -223,5 +130,8 @@ describe('Check to see that critical repo files are present', () => {
expect(fs.existsSync(filePath+"CONTRIBUTING.md")).toBeTruthy()
expect(fs.existsSync(filePath+"README.md")).toBeTruthy()
expect(fs.existsSync(filePath+"LICENSE")).toBeTruthy()
expect(fs.existsSync(filePath+".gitignore")).toBeTruthy()
expect(fs.existsSync(filePath+"jest.config.js")).toBeTruthy()
expect(fs.existsSync(filePath+"package.json")).toBeTruthy()
})
})
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ app.use(express.static(path.join(__dirname, 'public')))
* If we are, then show the sad puppy. Otherwise, continue on.
* This is without middleware
*/
app.all('*', (req, res, next) => {
app.all('*_', (req, res, next) => {
Copy link

Copilot AI Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The route path '_' will not match all requests; it will only match a literal path ending with an underscore. To preserve the intended catch-all behavior for maintenance gating, restore the path to '' so all routes are intercepted.

Suggested change
app.all('*_', (req, res, next) => {
app.all('*', (req, res, next) => {

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOT ANYMORE BECAUSE EXPRESS 5

if(process.env.DOWN === "true"){
res.status(503).json({"message":"RERUM v1 is down for updates or maintenance at this time. We apologize for the inconvenience. Try again later."})
}
Expand Down
Loading