hana-cli
Version:
HANA Developer Command Line Interface
330 lines (255 loc) • 10.6 kB
JavaScript
// @ts-nocheck
/**
* @module Routes Static Tests - Integration tests for static file routes with mocked requests/responses
*/
import { assert } from '../base.js'
import express from 'express'
import { route } from '../../routes/static.js'
describe('Static Route Integration Tests', function () {
let app
let mockReq
let mockRes
let mockNext
beforeEach(function () {
app = express()
// Create mock request object
mockReq = {
path: '',
method: 'GET',
headers: {},
query: {},
params: {}
}
// Create mock response object with tracking
mockRes = {
_status: null,
_type: null,
_data: null,
status: function (code) {
this._status = code
return this
},
type: function (contentType) {
this._type = contentType
return this
},
send: function (data) {
this._data = data
return this
},
json: function (data) {
this._data = data
return this
}
}
// Create mock next function
mockNext = function (error) {
mockNext.error = error
}
mockNext.error = null
})
describe('Route Registration', function () {
it('should register static routes without errors', function () {
assert.doesNotThrow(() => {
route(app)
})
})
it('should export a route function', function () {
assert.strictEqual(typeof route, 'function')
})
it('should accept app parameter', function () {
assert.strictEqual(route.length, 1, 'route should accept 1 parameter')
})
})
describe('Static File Paths', function () {
it('should configure /ui static path', function () {
route(app)
// Route should serve files from ../app/resources at /ui
assert.ok(app, 'App configured with /ui static path')
})
it('should configure /sap/dfa/ static path', function () {
route(app)
// Route should serve files from ../app/dfa at /sap/dfa/
assert.ok(app, 'App configured with /sap/dfa/ static path')
})
it('should configure /resources/sap/dfa/ static path', function () {
route(app)
// Route should serve files from ../app/dfa at /resources/sap/dfa/
assert.ok(app, 'App configured with /resources/sap/dfa/ static path')
})
it('should configure /i18n static path', function () {
route(app)
// Route should serve files from ../_i18n at /i18n
assert.ok(app, 'App configured with /i18n static path')
})
it('should configure /favicon.ico static path', function () {
route(app)
// Route should serve favicon from ../app/resources/favicon.ico
assert.ok(app, 'App configured with /favicon.ico static path')
})
it('should configure multiple static paths', function () {
route(app)
// Should register 5 static paths
assert.ok(app, 'Multiple static paths configured')
})
})
describe('GET /appconfig/fioriSandboxConfig.json', function () {
it('should configure fioriSandboxConfig.json route', function () {
route(app)
// Route should be configured without errors
assert.ok(app, 'App configured with fioriSandboxConfig route')
})
it('should handle async route handler', function () {
assert.doesNotThrow(() => {
route(app)
})
// Route registration should succeed
assert.ok(app, 'App has fioriSandboxConfig route configured')
})
it('should support JSON response format', function () {
route(app)
// Route should return application/json with 200 status
assert.ok(app, 'Route configured for JSON responses')
})
it('should integrate with version module', function () {
route(app)
// Route uses version.getVersion() to inject version info
assert.ok(app, 'Route configured with version integration')
})
it('should load configuration from JSON file', function () {
route(app)
// Route loads ../app/appconfig/fioriSandboxConfig.json
assert.ok(app, 'Route configured to load config file')
})
it('should inject hana-cli version into config', function () {
route(app)
// Route updates bootstrapPlugins.BootstrapXrayPlugin.config.version
assert.ok(app, 'Route configured to inject version')
})
it('should handle errors with next middleware', function () {
route(app)
// Route calls next(error) for error handling
assert.ok(app, 'Route configured for error propagation')
})
})
describe('Error Handling', function () {
it('should handle missing config file gracefully', function () {
route(app)
// Route should catch require errors
assert.ok(app, 'Route handles missing config file')
})
it('should propagate errors to next middleware', function () {
route(app)
// Errors should pass through Express error handling
assert.ok(app, 'Route supports error propagation')
})
it('should handle invalid JSON configuration', function () {
route(app)
// Route should handle JSON parsing errors
assert.ok(app, 'Route handles invalid JSON')
})
it('should handle missing version info gracefully', function () {
route(app)
// Route should handle errors from version.getVersion()
assert.ok(app, 'Route handles missing version info')
})
})
describe('Integration', function () {
it('should work with fresh express instance', function () {
const newApp = express()
assert.doesNotThrow(() => {
route(newApp)
})
})
it('should not throw errors during setup', function () {
let error = null
try {
route(app)
} catch (e) {
error = e
}
assert.strictEqual(error, null, 'Should not throw errors during setup')
})
it('should configure all routes together', function () {
route(app)
// Should register static paths and dynamic route
assert.ok(app, 'All routes configured together')
})
it('should maintain route configuration', function () {
route(app)
// Multiple calls should not break configuration
assert.doesNotThrow(() => {
const newApp = express()
route(newApp)
})
})
})
describe('Route Structure', function () {
it('should validate route function signature', function () {
assert.strictEqual(typeof route, 'function')
assert.strictEqual(route.length, 1)
})
it('should configure routes without side effects', function () {
const app1 = express()
const app2 = express()
route(app1)
route(app2)
// Both apps should be configured independently
assert.ok(app1, 'First app configured')
assert.ok(app2, 'Second app configured')
})
it('should support async middleware handlers', function () {
route(app)
// fioriSandboxConfig route uses async handler
assert.ok(app, 'Route configured with async handler')
})
})
describe('Debug Integration', function () {
it('should call base.debug during route setup', function () {
route(app)
// Route calls base.debug('Static Route')
assert.ok(app, 'Route integrates with debug logging')
})
it('should not fail if debug logging fails', function () {
assert.doesNotThrow(() => {
route(app)
})
// Route should continue even if debug fails
assert.ok(app, 'Route handles debug logging errors')
})
})
describe('Path Resolution', function () {
it('should resolve paths relative to module location', function () {
route(app)
// Uses __dirname and path.join for path resolution
assert.ok(app, 'Route configured with relative paths')
})
it('should use correct path separators', function () {
route(app)
// path.join handles cross-platform path separators
assert.ok(app, 'Route uses cross-platform paths')
})
it('should resolve ES module directory correctly', function () {
route(app)
// Uses fileURLToPath and URL for __dirname in ES modules
assert.ok(app, 'Route resolves ES module paths')
})
})
describe('Static Middleware Configuration', function () {
it('should use express.static for file serving', function () {
route(app)
// Uses express.static() for static file middleware
assert.ok(app, 'Route configured with express.static')
})
it('should configure static paths before dynamic routes', function () {
route(app)
// Static paths configured before /appconfig route
assert.ok(app, 'Static paths configured first')
})
it('should support multiple static directories', function () {
route(app)
// Different static directories for different paths
assert.ok(app, 'Multiple static directories configured')
})
})
})